Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Converted to PSR-2 #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
286 changes: 147 additions & 139 deletions code/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,147 +7,155 @@
* @package swipestripe
* @subpackage order
*/
class Address extends DataObject {

/**
* DB fields for an address
*
* @var Array
*/
private static $db = array(
'Default' => 'Boolean',
'FirstName' => 'Varchar',
'Surname' => 'Varchar',
'Company' => 'Varchar',
'Address' => 'Varchar(255)',
'AddressLine2' => 'Varchar(255)',
'City' => 'Varchar(100)',
'PostalCode' => 'Varchar(30)',
'State' => 'Varchar(100)',

//De-normalise these values in case region or country is deleted
'CountryName' => 'Varchar',
'CountryCode' => 'Varchar(2)', //ISO 3166
'RegionName' => 'Varchar',
'RegionCode' => 'Varchar(2)'
);

/**
* Relations for address
*
* @var Array
*/
private static $has_one = array(
'Member' => 'Customer',
'Country' => 'Country',
'Region' => 'Region'
);

public function onAfterWrite() {
parent::onAfterWrite();

//Make sure there is only one default address
if ($this->Default == true) {

$addrs = Address::get()
->where("\"ClassName\" = '" . get_class($this) . "' AND \"MemberID\" = '{$this->MemberID}' AND \"Default\" = 1 AND \"ID\" != {$this->ID}");

if ($addrs && $addrs->exists()) foreach ($addrs as $addr) {
$addr->Default = 0;
$addr->write();
}
}
}

class Address extends DataObject
{

/**
* DB fields for an address
*
* @var Array
*/
private static $db = array(
'Default' => 'Boolean',
'FirstName' => 'Varchar',
'Surname' => 'Varchar',
'Company' => 'Varchar',
'Address' => 'Varchar(255)',
'AddressLine2' => 'Varchar(255)',
'City' => 'Varchar(100)',
'PostalCode' => 'Varchar(30)',
'State' => 'Varchar(100)',

//De-normalise these values in case region or country is deleted
'CountryName' => 'Varchar',
'CountryCode' => 'Varchar(2)', //ISO 3166
'RegionName' => 'Varchar',
'RegionCode' => 'Varchar(2)'
);

/**
* Relations for address
*
* @var Array
*/
private static $has_one = array(
'Member' => 'Customer',
'Country' => 'Country',
'Region' => 'Region'
);

public function onAfterWrite()
{
parent::onAfterWrite();

//Make sure there is only one default address
if ($this->Default == true) {
$addrs = Address::get()
->where("\"ClassName\" = '" . get_class($this) . "' AND \"MemberID\" = '{$this->MemberID}' AND \"Default\" = 1 AND \"ID\" != {$this->ID}");

if ($addrs && $addrs->exists()) {
foreach ($addrs as $addr) {
$addr->Default = 0;
$addr->write();
}
}
}
}
}

class Address_Shipping extends Address {

public function onBeforeWrite() {
parent::onBeforeWrite();

$code = $this->CountryCode;
$country = Country_Shipping::get()
->where("\"Code\" = '$code'")
->first();

if ($country && $country->exists()) {
$this->CountryName = $country->Title;
$this->CountryID = $country->ID;
}

$code = $this->RegionCode;
$region = Region_Shipping::get()
->where("\"Code\" = '$code'")
->first();

if ($region && $region->exists()) {
$this->RegionName = $region->Title;
$this->RegionID = $region->ID;
}
}

/**
* Return data in an Array with keys formatted to match the field names
* on the checkout form so that it can be loaded into an order form.
*
* @see Form::loadDataFrom()
* @return Array Data for loading into the form
*/
public function getCheckoutFormData() {
$formattedData = array();

$formattedData['ShippingFirstName'] = $this->FirstName;
$formattedData['ShippingSurname'] = $this->Surname;
$formattedData['ShippingCompany'] = $this->Company;
$formattedData['ShippingAddress'] = $this->Address;
$formattedData['ShippingAddressLine2'] = $this->AddressLine2;
$formattedData['ShippingCity'] = $this->City;
$formattedData['ShippingPostalCode'] = $this->PostalCode;
$formattedData['ShippingState'] = $this->State;
$formattedData['ShippingCountryCode'] = $this->CountryCode;
$formattedData['ShippingRegionCode'] = $this->RegionCode;

return $formattedData;
}
class Address_Shipping extends Address
{

public function onBeforeWrite()
{
parent::onBeforeWrite();

$code = $this->CountryCode;
$country = Country_Shipping::get()
->where("\"Code\" = '$code'")
->first();

if ($country && $country->exists()) {
$this->CountryName = $country->Title;
$this->CountryID = $country->ID;
}

$code = $this->RegionCode;
$region = Region_Shipping::get()
->where("\"Code\" = '$code'")
->first();

if ($region && $region->exists()) {
$this->RegionName = $region->Title;
$this->RegionID = $region->ID;
}
}

/**
* Return data in an Array with keys formatted to match the field names
* on the checkout form so that it can be loaded into an order form.
*
* @see Form::loadDataFrom()
* @return Array Data for loading into the form
*/
public function getCheckoutFormData()
{
$formattedData = array();

$formattedData['ShippingFirstName'] = $this->FirstName;
$formattedData['ShippingSurname'] = $this->Surname;
$formattedData['ShippingCompany'] = $this->Company;
$formattedData['ShippingAddress'] = $this->Address;
$formattedData['ShippingAddressLine2'] = $this->AddressLine2;
$formattedData['ShippingCity'] = $this->City;
$formattedData['ShippingPostalCode'] = $this->PostalCode;
$formattedData['ShippingState'] = $this->State;
$formattedData['ShippingCountryCode'] = $this->CountryCode;
$formattedData['ShippingRegionCode'] = $this->RegionCode;

return $formattedData;
}
}

class Address_Billing extends Address {

public function onBeforeWrite() {
parent::onBeforeWrite();

$code = $this->CountryCode;
$country = Country_Billing::get()
->where("\"Code\" = '$code'")
->first();

if ($country && $country->exists()) {
$this->CountryName = $country->Title;
$this->CountryID = $country->ID;
}
}

/**
* Return data in an Array with keys formatted to match the field names
* on the checkout form so that it can be loaded into an order form.
*
* @see Form::loadDataFrom()
* @return Array Data for loading into the form
*/
public function getCheckoutFormData() {
$formattedData = array();

$formattedData['BillingFirstName'] = $this->FirstName;
$formattedData['BillingSurname'] = $this->Surname;
$formattedData['BillingCompany'] = $this->Company;
$formattedData['BillingAddress'] = $this->Address;
$formattedData['BillingAddressLine2'] = $this->AddressLine2;
$formattedData['BillingCity'] = $this->City;
$formattedData['BillingPostalCode'] = $this->PostalCode;
$formattedData['BillingState'] = $this->State;
$formattedData['BillingCountryCode'] = $this->CountryCode;

return $formattedData;
}
class Address_Billing extends Address
{

public function onBeforeWrite()
{
parent::onBeforeWrite();

$code = $this->CountryCode;
$country = Country_Billing::get()
->where("\"Code\" = '$code'")
->first();

if ($country && $country->exists()) {
$this->CountryName = $country->Title;
$this->CountryID = $country->ID;
}
}

/**
* Return data in an Array with keys formatted to match the field names
* on the checkout form so that it can be loaded into an order form.
*
* @see Form::loadDataFrom()
* @return Array Data for loading into the form
*/
public function getCheckoutFormData()
{
$formattedData = array();

$formattedData['BillingFirstName'] = $this->FirstName;
$formattedData['BillingSurname'] = $this->Surname;
$formattedData['BillingCompany'] = $this->Company;
$formattedData['BillingAddress'] = $this->Address;
$formattedData['BillingAddressLine2'] = $this->AddressLine2;
$formattedData['BillingCity'] = $this->City;
$formattedData['BillingPostalCode'] = $this->PostalCode;
$formattedData['BillingState'] = $this->State;
$formattedData['BillingCountryCode'] = $this->CountryCode;

return $formattedData;
}
}
Loading