diff --git a/code/Address.php b/code/Address.php index 66426e1..b689fc2 100644 --- a/code/Address.php +++ b/code/Address.php @@ -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; + } } diff --git a/code/Addresses.php b/code/Addresses.php index 2eb566f..02b7bf4 100644 --- a/code/Addresses.php +++ b/code/Addresses.php @@ -1,443 +1,468 @@ 'Varchar', - 'ShippingSurname' => 'Varchar', - 'ShippingCompany' => 'Varchar', - 'ShippingAddress' => 'Varchar(255)', - 'ShippingAddressLine2' => 'Varchar(255)', - 'ShippingCity' => 'Varchar(100)', - 'ShippingPostalCode' => 'Varchar(30)', - 'ShippingState' => 'Varchar(100)', - 'ShippingCountryName' => 'Varchar', - 'ShippingCountryCode' => 'Varchar(2)', //ISO 3166 - 'ShippingRegionName' => 'Varchar', - 'ShippingRegionCode' => 'Varchar(2)', - - 'BillingFirstName' => 'Varchar', - 'BillingSurname' => 'Varchar', - 'BillingCompany' => 'Varchar', - 'BillingAddress' => 'Varchar(255)', - 'BillingAddressLine2' => 'Varchar(255)', - 'BillingCity' => 'Varchar(100)', - 'BillingPostalCode' => 'Varchar(30)', - 'BillingState' => 'Varchar(100)', - 'BillingCountryName' => 'Varchar', - 'BillingCountryCode' => 'Varchar(2)', //ISO 3166 - 'BillingRegionName' => 'Varchar', - 'BillingRegionCode' => 'Varchar(2)' - ); - - public function onBeforeWrite() { - - //Update address names - $country = Country_Shipping::get()->where("\"Code\" = '{$this->owner->ShippingCountryCode}'")->first(); - if ($country && $country->exists()) $this->owner->ShippingCountryName = $country->Title; - - $region = Region_Shipping::get()->where("\"Code\" = '{$this->owner->ShippingRegionCode}'")->first(); - if ($region && $region->exists()) $this->owner->ShippingRegionName = $region->Title; - - $country = Country_Billing::get()->where("\"Code\" = '{$this->owner->BillingCountryCode}'")->first(); - if ($country && $country->exists()) $this->owner->BillingCountryName = $country->Title; - } - - public function onBeforePayment() { - - //Save the addresses to the Customer - $customer = $this->owner->Member(); - if ($customer && $customer->exists()) { - $customer->createAddresses($this->owner); - } - } +class Addresses_Order extends DataExtension +{ + + private static $db = array( + //Address fields + 'ShippingFirstName' => 'Varchar', + 'ShippingSurname' => 'Varchar', + 'ShippingCompany' => 'Varchar', + 'ShippingAddress' => 'Varchar(255)', + 'ShippingAddressLine2' => 'Varchar(255)', + 'ShippingCity' => 'Varchar(100)', + 'ShippingPostalCode' => 'Varchar(30)', + 'ShippingState' => 'Varchar(100)', + 'ShippingCountryName' => 'Varchar', + 'ShippingCountryCode' => 'Varchar(2)', //ISO 3166 + 'ShippingRegionName' => 'Varchar', + 'ShippingRegionCode' => 'Varchar(2)', + + 'BillingFirstName' => 'Varchar', + 'BillingSurname' => 'Varchar', + 'BillingCompany' => 'Varchar', + 'BillingAddress' => 'Varchar(255)', + 'BillingAddressLine2' => 'Varchar(255)', + 'BillingCity' => 'Varchar(100)', + 'BillingPostalCode' => 'Varchar(30)', + 'BillingState' => 'Varchar(100)', + 'BillingCountryName' => 'Varchar', + 'BillingCountryCode' => 'Varchar(2)', //ISO 3166 + 'BillingRegionName' => 'Varchar', + 'BillingRegionCode' => 'Varchar(2)' + ); + + public function onBeforeWrite() + { + + //Update address names + $country = Country_Shipping::get()->where("\"Code\" = '{$this->owner->ShippingCountryCode}'")->first(); + if ($country && $country->exists()) { + $this->owner->ShippingCountryName = $country->Title; + } + + $region = Region_Shipping::get()->where("\"Code\" = '{$this->owner->ShippingRegionCode}'")->first(); + if ($region && $region->exists()) { + $this->owner->ShippingRegionName = $region->Title; + } + + $country = Country_Billing::get()->where("\"Code\" = '{$this->owner->BillingCountryCode}'")->first(); + if ($country && $country->exists()) { + $this->owner->BillingCountryName = $country->Title; + } + } + + public function onBeforePayment() + { + + //Save the addresses to the Customer + $customer = $this->owner->Member(); + if ($customer && $customer->exists()) { + $customer->createAddresses($this->owner); + } + } } -class Addresses_Customer extends DataExtension { - - private static $has_many = array( - 'ShippingAddresses' => 'Address_Shipping', - 'BillingAddresses' => 'Address_Billing' - ); - - public function createAddresses($order) { - //Find identical addresses - //If none exist then create a new address and set it as default - //Default is not used when comparing - - $data = $order->toMap(); - - // Set Firstname/Surname Fields on Member table - if(!$this->owner->FirstName) { - $this->owner->FirstName = $data['ShippingFirstName']; - $this->owner->write(); - } - if(!$this->owner->Surname) { - $this->owner->Surname = $data['ShippingSurname']; - $this->owner->write(); - } - - $shippingAddress = Address_Shipping::create(array( - 'MemberID' => $this->owner->ID, - 'FirstName' => $data['ShippingFirstName'], - 'Surname' => $data['ShippingSurname'], - 'Company' => $data['ShippingCompany'], - 'Address' => $data['ShippingAddress'], - 'AddressLine2' => $data['ShippingAddressLine2'], - 'City' => $data['ShippingCity'], - 'PostalCode' => $data['ShippingPostalCode'], - 'State' => $data['ShippingState'], - 'CountryName' => $data['ShippingCountryName'], - 'CountryCode' => $data['ShippingCountryCode'], - 'RegionName' => (isset($data['ShippingRegionName'])) ? $data['ShippingRegionName'] : null, - 'RegionCode' => (isset($data['ShippingRegionCode'])) ? $data['ShippingRegionCode'] : null, - )); - - $billingAddress = Address_Billing::create(array( - 'MemberID' => $this->owner->ID, - 'FirstName' => $data['BillingFirstName'], - 'Surname' => $data['BillingSurname'], - 'Company' => $data['BillingCompany'], - 'Address' => $data['BillingAddress'], - 'AddressLine2' => $data['BillingAddressLine2'], - 'City' => $data['BillingCity'], - 'PostalCode' => $data['BillingPostalCode'], - 'State' => $data['BillingState'], - 'CountryName' => $data['BillingCountryName'], - 'CountryCode' => $data['BillingCountryCode'], - 'RegionName' => (isset($data['BillingRegionName'])) ? $data['ShippingRegionName'] : null, - 'RegionCode' => (isset($data['BillingRegionCode'])) ? $data['ShippingRegionCode'] : null, - )); - - //Look for identical existing addresses - - //TODO when a match is made then make that matched address the default now - - $match = false; - foreach ($this->owner->ShippingAddresses() as $address) { - - $existing = $address->toMap(); - $new = $shippingAddress->toMap(); - $result = array_intersect_assoc($existing, $new); - - //If no difference, then match is found - $diff = array_diff_assoc($new, $result); - $match = empty($diff); - } - - if (!$match) { - $shippingAddress->Default = true; - $shippingAddress->write(); - } - - $match = false; - foreach ($this->owner->BillingAddresses() as $address) { - - $existing = $address->toMap(); - $new = $billingAddress->toMap(); - $result = array_intersect_assoc($existing, $new); - - $diff = array_diff_assoc($new, $result); - $match = empty($diff); - } - - if (!$match) { - $billingAddress->Default = true; - $billingAddress->write(); - } - } - - /** - * Retrieve the last used billing address for this Member from their previous saved addresses. - * TODO make this more efficient - * - * @return Address The last billing address - */ - public function BillingAddress() { - - $addrs = $this->owner->BillingAddresses(); - if ($addrs && $addrs->exists()) { - return $addrs - ->where("\"Default\" = 1") - ->first(); - } - return null; - } - - /** - * Retrieve the last used shipping address for this Member from their previous saved addresses. - * TODO make this more efficient - * - * @return Address The last shipping address - */ - public function ShippingAddress() { - - $addrs = $this->owner->ShippingAddresses(); - if ($addrs && $addrs->exists()) { - return $addrs - ->where("\"Default\" = 1") - ->first(); - } - return null; - } +class Addresses_Customer extends DataExtension +{ + + private static $has_many = array( + 'ShippingAddresses' => 'Address_Shipping', + 'BillingAddresses' => 'Address_Billing' + ); + + public function createAddresses($order) + { + //Find identical addresses + //If none exist then create a new address and set it as default + //Default is not used when comparing + + $data = $order->toMap(); + + // Set Firstname/Surname Fields on Member table + if (!$this->owner->FirstName) { + $this->owner->FirstName = $data['ShippingFirstName']; + $this->owner->write(); + } + if (!$this->owner->Surname) { + $this->owner->Surname = $data['ShippingSurname']; + $this->owner->write(); + } + + $shippingAddress = Address_Shipping::create(array( + 'MemberID' => $this->owner->ID, + 'FirstName' => $data['ShippingFirstName'], + 'Surname' => $data['ShippingSurname'], + 'Company' => $data['ShippingCompany'], + 'Address' => $data['ShippingAddress'], + 'AddressLine2' => $data['ShippingAddressLine2'], + 'City' => $data['ShippingCity'], + 'PostalCode' => $data['ShippingPostalCode'], + 'State' => $data['ShippingState'], + 'CountryName' => $data['ShippingCountryName'], + 'CountryCode' => $data['ShippingCountryCode'], + 'RegionName' => (isset($data['ShippingRegionName'])) ? $data['ShippingRegionName'] : null, + 'RegionCode' => (isset($data['ShippingRegionCode'])) ? $data['ShippingRegionCode'] : null, + )); + + $billingAddress = Address_Billing::create(array( + 'MemberID' => $this->owner->ID, + 'FirstName' => $data['BillingFirstName'], + 'Surname' => $data['BillingSurname'], + 'Company' => $data['BillingCompany'], + 'Address' => $data['BillingAddress'], + 'AddressLine2' => $data['BillingAddressLine2'], + 'City' => $data['BillingCity'], + 'PostalCode' => $data['BillingPostalCode'], + 'State' => $data['BillingState'], + 'CountryName' => $data['BillingCountryName'], + 'CountryCode' => $data['BillingCountryCode'], + 'RegionName' => (isset($data['BillingRegionName'])) ? $data['ShippingRegionName'] : null, + 'RegionCode' => (isset($data['BillingRegionCode'])) ? $data['ShippingRegionCode'] : null, + )); + + //Look for identical existing addresses + + //TODO when a match is made then make that matched address the default now + + $match = false; + foreach ($this->owner->ShippingAddresses() as $address) { + $existing = $address->toMap(); + $new = $shippingAddress->toMap(); + $result = array_intersect_assoc($existing, $new); + + //If no difference, then match is found + $diff = array_diff_assoc($new, $result); + $match = empty($diff); + } + + if (!$match) { + $shippingAddress->Default = true; + $shippingAddress->write(); + } + + $match = false; + foreach ($this->owner->BillingAddresses() as $address) { + $existing = $address->toMap(); + $new = $billingAddress->toMap(); + $result = array_intersect_assoc($existing, $new); + + $diff = array_diff_assoc($new, $result); + $match = empty($diff); + } + + if (!$match) { + $billingAddress->Default = true; + $billingAddress->write(); + } + } + + /** + * Retrieve the last used billing address for this Member from their previous saved addresses. + * TODO make this more efficient + * + * @return Address The last billing address + */ + public function BillingAddress() + { + $addrs = $this->owner->BillingAddresses(); + if ($addrs && $addrs->exists()) { + return $addrs + ->where("\"Default\" = 1") + ->first(); + } + return null; + } + + /** + * Retrieve the last used shipping address for this Member from their previous saved addresses. + * TODO make this more efficient + * + * @return Address The last shipping address + */ + public function ShippingAddress() + { + $addrs = $this->owner->ShippingAddresses(); + if ($addrs && $addrs->exists()) { + return $addrs + ->where("\"Default\" = 1") + ->first(); + } + return null; + } } -class Addresses_OrderForm extends Extension { - - public function updateFields($fields) { - - Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.js'); - Requirements::javascript(THIRDPARTY_DIR . '/jquery-entwine/dist/jquery.entwine-dist.js'); - Requirements::javascript('swipestripe-addresses/javascript/Addresses_OrderForm.js'); - - $shippingAddressFields = CompositeField::create( - HeaderField::create(_t('CheckoutPage.SHIPPING_ADDRESS',"Shipping Address"), 3), - TextField::create('ShippingFirstName', _t('CheckoutPage.FIRSTNAME',"First Name")) - ->addExtraClass('shipping-firstname') - ->setCustomValidationMessage(_t('CheckoutPage.PLEASE_ENTER_FIRSTNAME',"Please enter a first name.")), - TextField::create('ShippingSurname', _t('CheckoutPage.SURNAME',"Surname")) - ->setCustomValidationMessage(_t('CheckoutPage.PLEASE_ENTER_SURNAME',"Please enter a surname.")), - TextField::create('ShippingCompany', _t('CheckoutPage.COMPANY',"Company")), - TextField::create('ShippingAddress', _t('CheckoutPage.ADDRESS',"Address")) - ->setCustomValidationMessage(_t('CheckoutPage.PLEASE_ENTER_ADDRESS',"Please enter an address.")) - ->addExtraClass('address-break'), - TextField::create('ShippingAddressLine2', ' '), - TextField::create('ShippingCity', _t('CheckoutPage.CITY',"City")) - ->setCustomValidationMessage(_t('CheckoutPage.PLEASE_ENTER_CITY',"Please enter a city.")), - TextField::create('ShippingPostalCode', _t('CheckoutPage.POSTAL_CODE',"Zip / Postal Code")), - TextField::create('ShippingState', _t('CheckoutPage.STATE',"State / Province")) - ->addExtraClass('address-break'), - DropdownField::create('ShippingCountryCode', - _t('CheckoutPage.COUNTRY',"Country"), - Country_Shipping::get()->map('Code', 'Title')->toArray() - ) - ->setCustomValidationMessage(_t('CheckoutPage.PLEASE_ENTER_COUNTRY',"Please enter a country.")) - ->addExtraClass('country-code') - )->setID('ShippingAddress')->setName('ShippingAddress'); - - $billingAddressFields = CompositeField::create( - HeaderField::create(_t('CheckoutPage.BILLINGADDRESS',"Billing Address"), 3), - $checkbox = CheckboxField::create('BillToShippingAddress', _t('CheckoutPage.SAME_ADDRESS',"same as shipping address?")) - ->addExtraClass('shipping-same-address'), - TextField::create('BillingFirstName', _t('CheckoutPage.FIRSTNAME',"First Name")) - ->setCustomValidationMessage(_t('CheckoutPage.PLEASEENTERYOURFIRSTNAME',"Please enter your first name.")) - ->addExtraClass('address-break'), - TextField::create('BillingSurname', _t('CheckoutPage.SURNAME',"Surname")) - ->setCustomValidationMessage(_t('CheckoutPage.PLEASEENTERYOURSURNAME',"Please enter your surname.")), - TextField::create('BillingCompany', _t('CheckoutPage.COMPANY',"Company")), - TextField::create('BillingAddress', _t('CheckoutPage.ADDRESS',"Address")) - ->setCustomValidationMessage(_t('CheckoutPage.PLEASEENTERYOURADDRESS',"Please enter your address.")) - ->addExtraClass('address-break'), - TextField::create('BillingAddressLine2', ' '), - TextField::create('BillingCity', _t('CheckoutPage.CITY',"City")) - ->setCustomValidationMessage(_t('CheckoutPage.PLEASEENTERYOURCITY',"Please enter your city")), - TextField::create('BillingPostalCode', _t('CheckoutPage.POSTALCODE',"Zip / Postal Code")), - TextField::create('BillingState', _t('CheckoutPage.STATE',"State / Province")) - ->addExtraClass('address-break'), - DropdownField::create('BillingCountryCode', - _t('CheckoutPage.COUNTRY',"Country"), - Country_Billing::get()->map('Code', 'Title')->toArray() - )->setCustomValidationMessage(_t('CheckoutPage.PLEASEENTERYOURCOUNTRY',"Please enter your country.")) - )->setID('BillingAddress')->setName('BillingAddress'); - - $fields->push($shippingAddressFields); - $fields->push($billingAddressFields); - } - - public function updateValidator($validator) { - - $validator->appendRequiredFields(RequiredFields::create( - 'ShippingFirstName', - 'ShippingSurname', - 'ShippingAddress', - 'ShippingCity', - 'ShippingCountryCode', - 'BillingFirstName', - 'BillingSurname', - 'BillingAddress', - 'BillingCity', - 'BillingCountryCode' - )); - } - - public function updatePopulateFields(&$data) { - - $member = Customer::currentUser() ? Customer::currentUser() : singleton('Customer'); - - $shippingAddress = $member->ShippingAddress(); - $shippingAddressData = ($shippingAddress && $shippingAddress->exists()) - ? $shippingAddress->getCheckoutFormData() - : array(); - unset($shippingAddressData['ShippingRegionCode']); //Not available billing address option - - $billingAddress = $member->BillingAddress(); - $billingAddressData = ($billingAddress && $billingAddress->exists()) - ? $billingAddress->getCheckoutFormData() - : array(); - - //If billing address is a subset of shipping address, consider them equal - $intersect = array_intersect(array_values($shippingAddressData), array_values($billingAddressData)); - if (array_values($intersect) == array_values($billingAddressData)) $billingAddressData['BillToShippingAddress'] = true; - - $data = array_merge( - $data, - $shippingAddressData, - $billingAddressData - ); - } - - public function getShippingAddressFields() { - return $this->owner->Fields()->fieldByName('ShippingAddress'); - } - - public function getBillingAddressFields() { - return $this->owner->Fields()->fieldByName('BillingAddress'); - } +class Addresses_OrderForm extends Extension +{ + + public function updateFields($fields) + { + Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.js'); + Requirements::javascript(THIRDPARTY_DIR . '/jquery-entwine/dist/jquery.entwine-dist.js'); + Requirements::javascript('swipestripe-addresses/javascript/Addresses_OrderForm.js'); + + $shippingAddressFields = CompositeField::create( + HeaderField::create(_t('CheckoutPage.SHIPPING_ADDRESS', "Shipping Address"), 3), + TextField::create('ShippingFirstName', _t('CheckoutPage.FIRSTNAME', "First Name")) + ->addExtraClass('shipping-firstname') + ->setCustomValidationMessage(_t('CheckoutPage.PLEASE_ENTER_FIRSTNAME', "Please enter a first name.")), + TextField::create('ShippingSurname', _t('CheckoutPage.SURNAME', "Surname")) + ->setCustomValidationMessage(_t('CheckoutPage.PLEASE_ENTER_SURNAME', "Please enter a surname.")), + TextField::create('ShippingCompany', _t('CheckoutPage.COMPANY', "Company")), + TextField::create('ShippingAddress', _t('CheckoutPage.ADDRESS', "Address")) + ->setCustomValidationMessage(_t('CheckoutPage.PLEASE_ENTER_ADDRESS', "Please enter an address.")) + ->addExtraClass('address-break'), + TextField::create('ShippingAddressLine2', ' '), + TextField::create('ShippingCity', _t('CheckoutPage.CITY', "City")) + ->setCustomValidationMessage(_t('CheckoutPage.PLEASE_ENTER_CITY', "Please enter a city.")), + TextField::create('ShippingPostalCode', _t('CheckoutPage.POSTAL_CODE', "Zip / Postal Code")), + TextField::create('ShippingState', _t('CheckoutPage.STATE', "State / Province")) + ->addExtraClass('address-break'), + DropdownField::create('ShippingCountryCode', + _t('CheckoutPage.COUNTRY', "Country"), + Country_Shipping::get()->map('Code', 'Title')->toArray() + ) + ->setCustomValidationMessage(_t('CheckoutPage.PLEASE_ENTER_COUNTRY', "Please enter a country.")) + ->addExtraClass('country-code') + )->setID('ShippingAddress')->setName('ShippingAddress'); + + $billingAddressFields = CompositeField::create( + HeaderField::create(_t('CheckoutPage.BILLINGADDRESS', "Billing Address"), 3), + $checkbox = CheckboxField::create('BillToShippingAddress', _t('CheckoutPage.SAME_ADDRESS', "same as shipping address?")) + ->addExtraClass('shipping-same-address'), + TextField::create('BillingFirstName', _t('CheckoutPage.FIRSTNAME', "First Name")) + ->setCustomValidationMessage(_t('CheckoutPage.PLEASEENTERYOURFIRSTNAME', "Please enter your first name.")) + ->addExtraClass('address-break'), + TextField::create('BillingSurname', _t('CheckoutPage.SURNAME', "Surname")) + ->setCustomValidationMessage(_t('CheckoutPage.PLEASEENTERYOURSURNAME', "Please enter your surname.")), + TextField::create('BillingCompany', _t('CheckoutPage.COMPANY', "Company")), + TextField::create('BillingAddress', _t('CheckoutPage.ADDRESS', "Address")) + ->setCustomValidationMessage(_t('CheckoutPage.PLEASEENTERYOURADDRESS', "Please enter your address.")) + ->addExtraClass('address-break'), + TextField::create('BillingAddressLine2', ' '), + TextField::create('BillingCity', _t('CheckoutPage.CITY', "City")) + ->setCustomValidationMessage(_t('CheckoutPage.PLEASEENTERYOURCITY', "Please enter your city")), + TextField::create('BillingPostalCode', _t('CheckoutPage.POSTALCODE', "Zip / Postal Code")), + TextField::create('BillingState', _t('CheckoutPage.STATE', "State / Province")) + ->addExtraClass('address-break'), + DropdownField::create('BillingCountryCode', + _t('CheckoutPage.COUNTRY', "Country"), + Country_Billing::get()->map('Code', 'Title')->toArray() + )->setCustomValidationMessage(_t('CheckoutPage.PLEASEENTERYOURCOUNTRY', "Please enter your country.")) + )->setID('BillingAddress')->setName('BillingAddress'); + + $fields->push($shippingAddressFields); + $fields->push($billingAddressFields); + } + + public function updateValidator($validator) + { + $validator->appendRequiredFields(RequiredFields::create( + 'ShippingFirstName', + 'ShippingSurname', + 'ShippingAddress', + 'ShippingCity', + 'ShippingCountryCode', + 'BillingFirstName', + 'BillingSurname', + 'BillingAddress', + 'BillingCity', + 'BillingCountryCode' + )); + } + + public function updatePopulateFields(&$data) + { + $member = Customer::currentUser() ? Customer::currentUser() : singleton('Customer'); + + $shippingAddress = $member->ShippingAddress(); + $shippingAddressData = ($shippingAddress && $shippingAddress->exists()) + ? $shippingAddress->getCheckoutFormData() + : array(); + unset($shippingAddressData['ShippingRegionCode']); //Not available billing address option + + $billingAddress = $member->BillingAddress(); + $billingAddressData = ($billingAddress && $billingAddress->exists()) + ? $billingAddress->getCheckoutFormData() + : array(); + + //If billing address is a subset of shipping address, consider them equal + $intersect = array_intersect(array_values($shippingAddressData), array_values($billingAddressData)); + if (array_values($intersect) == array_values($billingAddressData)) { + $billingAddressData['BillToShippingAddress'] = true; + } + + $data = array_merge( + $data, + $shippingAddressData, + $billingAddressData + ); + } + + public function getShippingAddressFields() + { + return $this->owner->Fields()->fieldByName('ShippingAddress'); + } + + public function getBillingAddressFields() + { + return $this->owner->Fields()->fieldByName('BillingAddress'); + } } -class Addresses_Extension extends DataExtension { +class Addresses_Extension extends DataExtension +{ - private static $has_many = array( - 'ShippingCountries' => 'Country_Shipping', - 'BillingCountries' => 'Country_Billing', - 'ShippingRegions' => 'Region_Shipping', - 'BillingRegions' => 'Region_Billing' - ); + private static $has_many = array( + 'ShippingCountries' => 'Country_Shipping', + 'BillingCountries' => 'Country_Billing', + 'ShippingRegions' => 'Region_Shipping', + 'BillingRegions' => 'Region_Billing' + ); } -class Addresses_CountriesAdmin extends ShopAdmin { - - private static $tree_class = 'ShopConfig'; - - private static $allowed_actions = array( - 'Countries', - 'CountriesForm' - ); - - private static $url_rule = 'ShopConfig/Countries'; - protected static $url_priority = 70; - private static $menu_title = 'Shop Countries'; - - private static $url_handlers = array( - 'ShopConfig/Countries/CountriesForm' => 'CountriesForm', - 'ShopConfig/Countries' => 'Countries' - ); - - public function init() { - parent::init(); - if (!in_array(get_class($this), self::$hidden_sections)) { - $this->modelClass = 'ShopConfig'; - } - } - - public function Breadcrumbs($unlinked = false) { - - $request = $this->getRequest(); - $items = parent::Breadcrumbs($unlinked); - - if ($items->count() > 1) $items->remove($items->pop()); - - $items->push(new ArrayData(array( - 'Title' => 'Countries', - 'Link' => $this->Link(Controller::join_links($this->sanitiseClassName($this->modelClass), 'Countries')) - ))); - - return $items; - } - - public function SettingsForm($request = null) { - return $this->CountriesForm(); - } - - public function Countries($request) { - - if ($request->isAjax()) { - $controller = $this; - $responseNegotiator = new PjaxResponseNegotiator( - array( - 'CurrentForm' => function() use(&$controller) { - return $controller->CountriesForm()->forTemplate(); - }, - 'Content' => function() use(&$controller) { - return $controller->renderWith('ShopAdminSettings_Content'); - }, - 'Breadcrumbs' => function() use (&$controller) { - return $controller->renderWith('CMSBreadcrumbs'); - }, - 'default' => function() use(&$controller) { - return $controller->renderWith($controller->getViewer('show')); - } - ), - $this->response - ); - return $responseNegotiator->respond($this->getRequest()); - } - - return $this->renderWith('ShopAdminSettings'); - } - - public function CountriesForm() { - - $shopConfig = ShopConfig::get()->First(); - - $fields = new FieldList( - $rootTab = new TabSet("Root", - $tabMain = new Tab('Shipping', - new HiddenField('ShopConfigSection', null, 'Countries'), - new GridField( - 'ShippingCountries', - 'Shipping Countries', - $shopConfig->ShippingCountries(), - GridFieldConfig_RecordEditor::create() - ->removeComponentsByType('GridFieldFilterHeader') - ->removeComponentsByType('GridFieldAddExistingAutocompleter') - ) - ), - new Tab('Billing', - new GridField( - 'BillingCountries', - 'Billing Countries', - $shopConfig->BillingCountries(), - GridFieldConfig_RecordEditor::create() - ->removeComponentsByType('GridFieldFilterHeader') - ->removeComponentsByType('GridFieldAddExistingAutocompleter') - ) - ) - ) - ); - - $actions = new FieldList(); - - $form = new Form( - $this, - 'EditForm', - $fields, - $actions - ); - - $form->setTemplate('ShopAdminSettings_EditForm'); - $form->setAttribute('data-pjax-fragment', 'CurrentForm'); - $form->addExtraClass('cms-content cms-edit-form center ss-tabset'); - if($form->Fields()->hasTabset()) $form->Fields()->findOrMakeTab('Root')->setTemplate('CMSTabSet'); - $form->setFormAction(Controller::join_links($this->Link($this->sanitiseClassName($this->modelClass)), 'Countries/CountriesForm')); - - $form->loadDataFrom($shopConfig); - - return $form; - } - - public function getSnippet() { - - if (!$member = Member::currentUser()) return false; - if (!Permission::check('CMS_ACCESS_' . get_class($this), 'any', $member)) return false; - - return $this->customise(array( - 'Title' => 'Countries and Regions', - 'Help' => 'Shipping and billing countries and regions.', - 'Link' => Controller::join_links($this->Link('ShopConfig'), 'Countries'), - 'LinkTitle' => 'Edit Countries and Regions' - ))->renderWith('ShopAdmin_Snippet'); - } - +class Addresses_CountriesAdmin extends ShopAdmin +{ + + private static $tree_class = 'ShopConfig'; + + private static $allowed_actions = array( + 'Countries', + 'CountriesForm' + ); + + private static $url_rule = 'ShopConfig/Countries'; + protected static $url_priority = 70; + private static $menu_title = 'Shop Countries'; + + private static $url_handlers = array( + 'ShopConfig/Countries/CountriesForm' => 'CountriesForm', + 'ShopConfig/Countries' => 'Countries' + ); + + public function init() + { + parent::init(); + if (!in_array(get_class($this), self::$hidden_sections)) { + $this->modelClass = 'ShopConfig'; + } + } + + public function Breadcrumbs($unlinked = false) + { + $request = $this->getRequest(); + $items = parent::Breadcrumbs($unlinked); + + if ($items->count() > 1) { + $items->remove($items->pop()); + } + + $items->push(new ArrayData(array( + 'Title' => 'Countries', + 'Link' => $this->Link(Controller::join_links($this->sanitiseClassName($this->modelClass), 'Countries')) + ))); + + return $items; + } + + public function SettingsForm($request = null) + { + return $this->CountriesForm(); + } + + public function Countries($request) + { + if ($request->isAjax()) { + $controller = $this; + $responseNegotiator = new PjaxResponseNegotiator( + array( + 'CurrentForm' => function () use (&$controller) { + return $controller->CountriesForm()->forTemplate(); + }, + 'Content' => function () use (&$controller) { + return $controller->renderWith('ShopAdminSettings_Content'); + }, + 'Breadcrumbs' => function () use (&$controller) { + return $controller->renderWith('CMSBreadcrumbs'); + }, + 'default' => function () use (&$controller) { + return $controller->renderWith($controller->getViewer('show')); + } + ), + $this->response + ); + return $responseNegotiator->respond($this->getRequest()); + } + + return $this->renderWith('ShopAdminSettings'); + } + + public function CountriesForm() + { + $shopConfig = ShopConfig::get()->First(); + + $fields = new FieldList( + $rootTab = new TabSet("Root", + $tabMain = new Tab('Shipping', + new HiddenField('ShopConfigSection', null, 'Countries'), + new GridField( + 'ShippingCountries', + 'Shipping Countries', + $shopConfig->ShippingCountries(), + GridFieldConfig_RecordEditor::create() + ->removeComponentsByType('GridFieldFilterHeader') + ->removeComponentsByType('GridFieldAddExistingAutocompleter') + ) + ), + new Tab('Billing', + new GridField( + 'BillingCountries', + 'Billing Countries', + $shopConfig->BillingCountries(), + GridFieldConfig_RecordEditor::create() + ->removeComponentsByType('GridFieldFilterHeader') + ->removeComponentsByType('GridFieldAddExistingAutocompleter') + ) + ) + ) + ); + + $actions = new FieldList(); + + $form = new Form( + $this, + 'EditForm', + $fields, + $actions + ); + + $form->setTemplate('ShopAdminSettings_EditForm'); + $form->setAttribute('data-pjax-fragment', 'CurrentForm'); + $form->addExtraClass('cms-content cms-edit-form center ss-tabset'); + if ($form->Fields()->hasTabset()) { + $form->Fields()->findOrMakeTab('Root')->setTemplate('CMSTabSet'); + } + $form->setFormAction(Controller::join_links($this->Link($this->sanitiseClassName($this->modelClass)), 'Countries/CountriesForm')); + + $form->loadDataFrom($shopConfig); + + return $form; + } + + public function getSnippet() + { + if (!$member = Member::currentUser()) { + return false; + } + if (!Permission::check('CMS_ACCESS_' . get_class($this), 'any', $member)) { + return false; + } + + return $this->customise(array( + 'Title' => 'Countries and Regions', + 'Help' => 'Shipping and billing countries and regions.', + 'Link' => Controller::join_links($this->Link('ShopConfig'), 'Countries'), + 'LinkTitle' => 'Edit Countries and Regions' + ))->renderWith('ShopAdmin_Snippet'); + } } diff --git a/code/Country.php b/code/Country.php index 7c34377..3cb92fd 100755 --- a/code/Country.php +++ b/code/Country.php @@ -7,318 +7,320 @@ * @package swipestripe * @subpackage order */ -class Country extends DataObject { - - /** - * Singular name - * - * @var String - */ - private static $singular_name = 'Country'; - - /** - * Plural name - * - * @var String - */ - private static $plural_name = 'Countries'; - - /** - * ISO 3166 Country Codes, used to generate inital billing countries - * - * @see Country_Billing::requireDefaultRecords() - * @var Array - */ - protected static $iso_3166_countryCodes = array( - 'AD' => "Andorra", - 'AE' => "United Arab Emirates", - 'AF' => "Afghanistan", - 'AG' => "Antigua and Barbuda", - 'AI' => "Anguilla", - 'AL' => "Albania", - 'AM' => "Armenia", - 'AN' => "Netherlands Antilles", - 'AO' => "Angola", - 'AP' => "Asia/Pacific Region", - 'AQ' => "Antarctica", - 'AR' => "Argentina", - 'AS' => "American Samoa", - 'AT' => "Austria", - 'AU' => "Australia", - 'AW' => "Aruba", - 'AZ' => "Azerbaijan", - 'BA' => "Bosnia and Herzegovina", - 'BB' => "Barbados", - 'BD' => "Bangladesh", - 'BE' => "Belgium", - 'BF' => "Burkina Faso", - 'BG' => "Bulgaria", - 'BH' => "Bahrain", - 'BI' => "Burundi", - 'BJ' => "Benin", - 'BM' => "Bermuda", - 'BN' => "Brunei Darussalam", - 'BO' => "Bolivia", - 'BR' => "Brazil", - 'BS' => "Bahamas", - 'BT' => "Bhutan", - 'BV' => "Bouvet Island", - 'BW' => "Botswana", - 'BY' => "Belarus", - 'BZ' => "Belize", - 'CA' => "Canada", - 'CC' => "Cocos (Keeling) Islands", - 'CF' => "Central African Republic", - 'CG' => "Congo", - 'CH' => "Switzerland", - 'CI' => "Cote D'Ivoire", - 'CK' => "Cook Islands", - 'CL' => "Chile", - 'CM' => "Cameroon", - 'CN' => "China", - 'CO' => "Colombia", - 'CR' => "Costa Rica", - 'CU' => "Cuba", - 'CV' => "Cape Verde", - 'CX' => "Christmas Island", - 'CY' => "Cyprus", - 'CZ' => "Czech Republic", - 'DE' => "Germany", - 'DJ' => "Djibouti", - 'DK' => "Denmark", - 'DM' => "Dominica", - 'DO' => "Dominican Republic", - 'DZ' => "Algeria", - 'EC' => "Ecuador", - 'EE' => "Estonia", - 'EG' => "Egypt", - 'EH' => "Western Sahara", - 'ER' => "Eritrea", - 'ES' => "Spain", - 'ET' => "Ethiopia", - 'EU' => "Europe", - 'FI' => "Finland", - 'FJ' => "Fiji", - 'FK' => "Falkland Islands (Malvinas)", - 'FM' => "Micronesia - Federated States of", - 'FO' => "Faroe Islands", - 'FR' => "France", - 'FX' => "France (Metropolitan)", - 'GA' => "Gabon", - 'GB' => "United Kingdom", - 'GD' => "Grenada", - 'GE' => "Georgia", - 'GF' => "French Guiana", - 'GH' => "Ghana", - 'GI' => "Gibraltar", - 'GL' => "Greenland", - 'GM' => "Gambia", - 'GN' => "Guinea", - 'GP' => "Guadeloupe", - 'GQ' => "Equatorial Guinea", - 'GR' => "Greece", - 'GS' => "South Georgia and the South Sandwich Islands", - 'GT' => "Guatemala", - 'GU' => "Guam", - 'GW' => "Guinea-Bissau", - 'GY' => "Guyana", - 'HK' => "Hong Kong", - 'HM' => "Heard Island and McDonald Islands", - 'HN' => "Honduras", - 'HR' => "Croatia", - 'HT' => "Haiti", - 'HU' => "Hungary", - 'ID' => "Indonesia", - 'IE' => "Ireland", - 'IL' => "Israel", - 'IN' => "India", - 'IO' => "British Indian Ocean Territory", - 'IQ' => "Iraq", - 'IR' => "Iran - Islamic Republic of", - 'IS' => "Iceland", - 'IT' => "Italy", - 'JM' => "Jamaica", - 'JO' => "Jordan", - 'JP' => "Japan", - 'KE' => "Kenya", - 'KG' => "Kyrgyzstan", - 'KH' => "Cambodia", - 'KI' => "Kiribati", - 'KM' => "Comoros", - 'KN' => "Saint Kitts and Nevis", - 'KP' => "Korea - Democratic People's Republic of", - 'KR' => "Korea - Republic of", - 'KW' => "Kuwait", - 'KY' => "Cayman Islands", - 'KZ' => "Kazakhstan", - 'LA' => "Lao People's Democratic Republic", - 'LB' => "Lebanon", - 'LC' => "Saint Lucia", - 'LI' => "Liechtenstein", - 'LK' => "Sri Lanka", - 'LR' => "Liberia", - 'LS' => "Lesotho", - 'LT' => "Lithuania", - 'LU' => "Luxembourg", - 'LV' => "Latvia", - 'LY' => "Libyan Arab Jamahiriya", - 'MA' => "Morocco", - 'MC' => "Monaco", - 'MD' => "Moldova - Republic of", - 'MG' => "Madagascar", - 'MH' => "Marshall Islands", - 'MK' => "Macedonia - the Former Yugoslav Republic of", - 'ML' => "Mali", - 'MM' => "Myanmar", - 'MN' => "Mongolia", - 'MO' => "Macao", - 'MP' => "Northern Mariana Islands", - 'MQ' => "Martinique", - 'MR' => "Mauritania", - 'MS' => "Montserrat", - 'MT' => "Malta", - 'MU' => "Mauritius", - 'MV' => "Maldives", - 'MW' => "Malawi", - 'MX' => "Mexico", - 'MY' => "Malaysia", - 'MZ' => "Mozambique", - 'NA' => "Namibia", - 'NC' => "New Caledonia", - 'NE' => "Niger", - 'NF' => "Norfolk Island", - 'NG' => "Nigeria", - 'NI' => "Nicaragua", - 'NL' => "Netherlands", - 'NO' => "Norway", - 'NP' => "Nepal", - 'NR' => "Nauru", - 'NU' => "Niue", - 'NZ' => "New Zealand", - 'OM' => "Oman", - 'PA' => "Panama", - 'PE' => "Peru", - 'PF' => "French Polynesia", - 'PG' => "Papua New Guinea", - 'PH' => "Philippines", - 'PK' => "Pakistan", - 'PL' => "Poland", - 'PM' => "Saint Pierre and Miquelon", - 'PN' => "Pitcairn", - 'PR' => "Puerto Rico", - 'PS' => "Palestinian Territory - Occupied", - 'PT' => "Portugal", - 'PW' => "Palau", - 'PY' => "Paraguay", - 'QA' => "Qatar", - 'RE' => "Reunion", - 'RO' => "Romania", - 'RU' => "Russian Federation", - 'RW' => "Rwanda", - 'SA' => "Saudi Arabia", - 'SB' => "Solomon Islands", - 'SC' => "Seychelles", - 'SD' => "Sudan", - 'SE' => "Sweden", - 'SG' => "Singapore", - 'SH' => "Saint Helena", - 'SI' => "Slovenia", - 'SJ' => "Svalbard and Jan Mayen", - 'SK' => "Slovakia", - 'SL' => "Sierra Leone", - 'SM' => "San Marino", - 'SN' => "Senegal", - 'SO' => "Somalia", - 'SR' => "Suriname", - 'ST' => "Sao Tome and Principe", - 'SV' => "El Salvador", - 'SY' => "Syrian Arab Republic", - 'SZ' => "Swaziland", - 'TC' => "Turks and Caicos Islands", - 'TD' => "Chad", - 'TF' => "French Southern Territories", - 'TG' => "Togo", - 'TH' => "Thailand", - 'TJ' => "Tajikistan", - 'TK' => "Tokelau", - 'TL' => "East Timor", - 'TM' => "Turkmenistan", - 'TN' => "Tunisia", - 'TO' => "Tonga", - 'TR' => "Turkey", - 'TT' => "Trinidad and Tobago", - 'TV' => "Tuvalu", - 'TW' => "Taiwan", - 'TZ' => "Tanzania (United Republic of)", - 'UA' => "Ukraine", - 'UG' => "Uganda", - 'UM' => "United States Minor Outlying Islands", - 'US' => "United States", - 'UY' => "Uruguay", - 'UZ' => "Uzbekistan", - 'VA' => "Holy See (Vatican City State)", - 'VC' => "Saint Vincent and the Grenadines", - 'VE' => "Venezuela", - 'VG' => "Virgin Islands - British", - 'VI' => "Virgin Islands - U.S.", - 'VN' => "Vietnam", - 'VU' => "Vanuatu", - 'WF' => "Wallis and Futuna", - 'WS' => "Samoa", - 'YE' => "Yemen", - 'YT' => "Mayotte", - 'YU' => "Yugoslavia", - 'ZA' => "South Africa", - 'ZM' => "Zambia", - 'ZR' => "Zaire", - 'ZW' => "Zimbabwe" - ); +class Country extends DataObject +{ + + /** + * Singular name + * + * @var String + */ + private static $singular_name = 'Country'; + + /** + * Plural name + * + * @var String + */ + private static $plural_name = 'Countries'; + + /** + * ISO 3166 Country Codes, used to generate inital billing countries + * + * @see Country_Billing::requireDefaultRecords() + * @var Array + */ + protected static $iso_3166_countryCodes = array( + 'AD' => "Andorra", + 'AE' => "United Arab Emirates", + 'AF' => "Afghanistan", + 'AG' => "Antigua and Barbuda", + 'AI' => "Anguilla", + 'AL' => "Albania", + 'AM' => "Armenia", + 'AN' => "Netherlands Antilles", + 'AO' => "Angola", + 'AP' => "Asia/Pacific Region", + 'AQ' => "Antarctica", + 'AR' => "Argentina", + 'AS' => "American Samoa", + 'AT' => "Austria", + 'AU' => "Australia", + 'AW' => "Aruba", + 'AZ' => "Azerbaijan", + 'BA' => "Bosnia and Herzegovina", + 'BB' => "Barbados", + 'BD' => "Bangladesh", + 'BE' => "Belgium", + 'BF' => "Burkina Faso", + 'BG' => "Bulgaria", + 'BH' => "Bahrain", + 'BI' => "Burundi", + 'BJ' => "Benin", + 'BM' => "Bermuda", + 'BN' => "Brunei Darussalam", + 'BO' => "Bolivia", + 'BR' => "Brazil", + 'BS' => "Bahamas", + 'BT' => "Bhutan", + 'BV' => "Bouvet Island", + 'BW' => "Botswana", + 'BY' => "Belarus", + 'BZ' => "Belize", + 'CA' => "Canada", + 'CC' => "Cocos (Keeling) Islands", + 'CF' => "Central African Republic", + 'CG' => "Congo", + 'CH' => "Switzerland", + 'CI' => "Cote D'Ivoire", + 'CK' => "Cook Islands", + 'CL' => "Chile", + 'CM' => "Cameroon", + 'CN' => "China", + 'CO' => "Colombia", + 'CR' => "Costa Rica", + 'CU' => "Cuba", + 'CV' => "Cape Verde", + 'CX' => "Christmas Island", + 'CY' => "Cyprus", + 'CZ' => "Czech Republic", + 'DE' => "Germany", + 'DJ' => "Djibouti", + 'DK' => "Denmark", + 'DM' => "Dominica", + 'DO' => "Dominican Republic", + 'DZ' => "Algeria", + 'EC' => "Ecuador", + 'EE' => "Estonia", + 'EG' => "Egypt", + 'EH' => "Western Sahara", + 'ER' => "Eritrea", + 'ES' => "Spain", + 'ET' => "Ethiopia", + 'EU' => "Europe", + 'FI' => "Finland", + 'FJ' => "Fiji", + 'FK' => "Falkland Islands (Malvinas)", + 'FM' => "Micronesia - Federated States of", + 'FO' => "Faroe Islands", + 'FR' => "France", + 'FX' => "France (Metropolitan)", + 'GA' => "Gabon", + 'GB' => "United Kingdom", + 'GD' => "Grenada", + 'GE' => "Georgia", + 'GF' => "French Guiana", + 'GH' => "Ghana", + 'GI' => "Gibraltar", + 'GL' => "Greenland", + 'GM' => "Gambia", + 'GN' => "Guinea", + 'GP' => "Guadeloupe", + 'GQ' => "Equatorial Guinea", + 'GR' => "Greece", + 'GS' => "South Georgia and the South Sandwich Islands", + 'GT' => "Guatemala", + 'GU' => "Guam", + 'GW' => "Guinea-Bissau", + 'GY' => "Guyana", + 'HK' => "Hong Kong", + 'HM' => "Heard Island and McDonald Islands", + 'HN' => "Honduras", + 'HR' => "Croatia", + 'HT' => "Haiti", + 'HU' => "Hungary", + 'ID' => "Indonesia", + 'IE' => "Ireland", + 'IL' => "Israel", + 'IN' => "India", + 'IO' => "British Indian Ocean Territory", + 'IQ' => "Iraq", + 'IR' => "Iran - Islamic Republic of", + 'IS' => "Iceland", + 'IT' => "Italy", + 'JM' => "Jamaica", + 'JO' => "Jordan", + 'JP' => "Japan", + 'KE' => "Kenya", + 'KG' => "Kyrgyzstan", + 'KH' => "Cambodia", + 'KI' => "Kiribati", + 'KM' => "Comoros", + 'KN' => "Saint Kitts and Nevis", + 'KP' => "Korea - Democratic People's Republic of", + 'KR' => "Korea - Republic of", + 'KW' => "Kuwait", + 'KY' => "Cayman Islands", + 'KZ' => "Kazakhstan", + 'LA' => "Lao People's Democratic Republic", + 'LB' => "Lebanon", + 'LC' => "Saint Lucia", + 'LI' => "Liechtenstein", + 'LK' => "Sri Lanka", + 'LR' => "Liberia", + 'LS' => "Lesotho", + 'LT' => "Lithuania", + 'LU' => "Luxembourg", + 'LV' => "Latvia", + 'LY' => "Libyan Arab Jamahiriya", + 'MA' => "Morocco", + 'MC' => "Monaco", + 'MD' => "Moldova - Republic of", + 'MG' => "Madagascar", + 'MH' => "Marshall Islands", + 'MK' => "Macedonia - the Former Yugoslav Republic of", + 'ML' => "Mali", + 'MM' => "Myanmar", + 'MN' => "Mongolia", + 'MO' => "Macao", + 'MP' => "Northern Mariana Islands", + 'MQ' => "Martinique", + 'MR' => "Mauritania", + 'MS' => "Montserrat", + 'MT' => "Malta", + 'MU' => "Mauritius", + 'MV' => "Maldives", + 'MW' => "Malawi", + 'MX' => "Mexico", + 'MY' => "Malaysia", + 'MZ' => "Mozambique", + 'NA' => "Namibia", + 'NC' => "New Caledonia", + 'NE' => "Niger", + 'NF' => "Norfolk Island", + 'NG' => "Nigeria", + 'NI' => "Nicaragua", + 'NL' => "Netherlands", + 'NO' => "Norway", + 'NP' => "Nepal", + 'NR' => "Nauru", + 'NU' => "Niue", + 'NZ' => "New Zealand", + 'OM' => "Oman", + 'PA' => "Panama", + 'PE' => "Peru", + 'PF' => "French Polynesia", + 'PG' => "Papua New Guinea", + 'PH' => "Philippines", + 'PK' => "Pakistan", + 'PL' => "Poland", + 'PM' => "Saint Pierre and Miquelon", + 'PN' => "Pitcairn", + 'PR' => "Puerto Rico", + 'PS' => "Palestinian Territory - Occupied", + 'PT' => "Portugal", + 'PW' => "Palau", + 'PY' => "Paraguay", + 'QA' => "Qatar", + 'RE' => "Reunion", + 'RO' => "Romania", + 'RU' => "Russian Federation", + 'RW' => "Rwanda", + 'SA' => "Saudi Arabia", + 'SB' => "Solomon Islands", + 'SC' => "Seychelles", + 'SD' => "Sudan", + 'SE' => "Sweden", + 'SG' => "Singapore", + 'SH' => "Saint Helena", + 'SI' => "Slovenia", + 'SJ' => "Svalbard and Jan Mayen", + 'SK' => "Slovakia", + 'SL' => "Sierra Leone", + 'SM' => "San Marino", + 'SN' => "Senegal", + 'SO' => "Somalia", + 'SR' => "Suriname", + 'ST' => "Sao Tome and Principe", + 'SV' => "El Salvador", + 'SY' => "Syrian Arab Republic", + 'SZ' => "Swaziland", + 'TC' => "Turks and Caicos Islands", + 'TD' => "Chad", + 'TF' => "French Southern Territories", + 'TG' => "Togo", + 'TH' => "Thailand", + 'TJ' => "Tajikistan", + 'TK' => "Tokelau", + 'TL' => "East Timor", + 'TM' => "Turkmenistan", + 'TN' => "Tunisia", + 'TO' => "Tonga", + 'TR' => "Turkey", + 'TT' => "Trinidad and Tobago", + 'TV' => "Tuvalu", + 'TW' => "Taiwan", + 'TZ' => "Tanzania (United Republic of)", + 'UA' => "Ukraine", + 'UG' => "Uganda", + 'UM' => "United States Minor Outlying Islands", + 'US' => "United States", + 'UY' => "Uruguay", + 'UZ' => "Uzbekistan", + 'VA' => "Holy See (Vatican City State)", + 'VC' => "Saint Vincent and the Grenadines", + 'VE' => "Venezuela", + 'VG' => "Virgin Islands - British", + 'VI' => "Virgin Islands - U.S.", + 'VN' => "Vietnam", + 'VU' => "Vanuatu", + 'WF' => "Wallis and Futuna", + 'WS' => "Samoa", + 'YE' => "Yemen", + 'YT' => "Mayotte", + 'YU' => "Yugoslavia", + 'ZA' => "South Africa", + 'ZM' => "Zambia", + 'ZR' => "Zaire", + 'ZW' => "Zimbabwe" + ); - /** - * Basic fields for country code and title - * - * @var Array - */ - private static $db = array( - 'Code' => 'Varchar(2)', //ISO 3166 - 'Title' => 'Varchar' - ); - - /** - * Associated with SiteConfig to enable editing - * - * @var Array - */ - private static $has_one = array ( - 'ShopConfig' => 'ShopConfig' - ); - - /** - * Countries can have many regions - * - * @var Array - */ - private static $has_many = array ( - 'Regions' => 'Region' - ); - - /** - * Summary fields - * - * @var Array - */ - private static $summary_fields = array( - 'Title' => 'Title', - 'Code' => 'Code' - ); + /** + * Basic fields for country code and title + * + * @var Array + */ + private static $db = array( + 'Code' => 'Varchar(2)', //ISO 3166 + 'Title' => 'Varchar' + ); + + /** + * Associated with SiteConfig to enable editing + * + * @var Array + */ + private static $has_one = array( + 'ShopConfig' => 'ShopConfig' + ); + + /** + * Countries can have many regions + * + * @var Array + */ + private static $has_many = array( + 'Regions' => 'Region' + ); + + /** + * Summary fields + * + * @var Array + */ + private static $summary_fields = array( + 'Title' => 'Title', + 'Code' => 'Code' + ); - private static $default_sort = 'Title ASC'; + private static $default_sort = 'Title ASC'; - public static function get_codes() { - return self::$iso_3166_countryCodes; - } + public static function get_codes() + { + return self::$iso_3166_countryCodes; + } - //TODO validate that Code is unique for each country + //TODO validate that Code is unique for each country } /** @@ -329,42 +331,43 @@ public static function get_codes() { * @package swipestripe * @subpackage order */ -class Country_Shipping extends Country { +class Country_Shipping extends Country +{ - public function getCMSFields() { + public function getCMSFields() + { + $fields = new FieldList( + $rootTab = new TabSet('Root', + $tabMain = new Tab('Country', + TextField::create('Code', _t('Country.CODE', 'Code')), + TextField::create('Title', _t('Country.TITLE', 'Title')) + ) + ) + ); - $fields = new FieldList( - $rootTab = new TabSet('Root', - $tabMain = new Tab('Country', - TextField::create('Code', _t('Country.CODE', 'Code')), - TextField::create('Title', _t('Country.TITLE', 'Title')) - ) - ) - ); + if ($this->isInDB()) { + $config = GridFieldConfig_BasicSortable::create(); + // $detailForm = $config->getComponentByType('GridFieldDetailForm'); + // $detailForm->setItemRequestClass('GridFieldDetailForm_HasManyItemRequest'); - if ($this->isInDB()) { + $listField = new GridField( + 'Regions', + 'Regions', + $this->Regions(), + $config + ); - $config = GridFieldConfig_BasicSortable::create(); - // $detailForm = $config->getComponentByType('GridFieldDetailForm'); - // $detailForm->setItemRequestClass('GridFieldDetailForm_HasManyItemRequest'); + $fields->addFieldToTab('Root.Regions', $listField); + } - $listField = new GridField( - 'Regions', - 'Regions', - $this->Regions(), - $config - ); + return $fields; + } - $fields->addFieldToTab('Root.Regions', $listField); - } - - return $fields; - } - - public function Regions() { - return Region_Shipping::get() - ->where("\"CountryID\" = " . $this->ID); - } + public function Regions() + { + return Region_Shipping::get() + ->where("\"CountryID\" = " . $this->ID); + } } /** @@ -375,42 +378,40 @@ public function Regions() { * @package swipestripe * @subpackage order */ -class Country_Billing extends Country { - - /** - * Build default list of billing countries - * - * @see Country::$iso_3166_countryCodes - * @see DataObject::requireDefaultRecords() - */ - public function requireDefaultRecords() { - - parent::requireDefaultRecords(); +class Country_Billing extends Country +{ + + /** + * Build default list of billing countries + * + * @see Country::$iso_3166_countryCodes + * @see DataObject::requireDefaultRecords() + */ + public function requireDefaultRecords() + { + parent::requireDefaultRecords(); - if (!DataObject::get_one('Country_Billing')) { + if (!DataObject::get_one('Country_Billing')) { + $shopConfig = ShopConfig::current_shop_config(); - $shopConfig = ShopConfig::current_shop_config(); + if (isset($shopConfig->ID)) { + foreach (self::$iso_3166_countryCodes as $code => $title) { + $country = new Country_Billing(); + $country->Code = $code; + $country->Title = $title; + $country->ShopConfigID = $shopConfig->ID; + $country->write(); + } + DB::alteration_message('Billing countries created', 'created'); + } else { + DB::alteration_message('Billing countries not created, please re-run /dev/build', 'error'); + } + } + } - if (isset($shopConfig->ID)) { - foreach (self::$iso_3166_countryCodes as $code => $title) { - $country = new Country_Billing(); - $country->Code = $code; - $country->Title = $title; - $country->ShopConfigID = $shopConfig->ID; - $country->write(); - } - DB::alteration_message('Billing countries created', 'created'); - } else { - DB::alteration_message('Billing countries not created, please re-run /dev/build', 'error'); - } - - - } - } - - public function Regions() { - return Region_Billing::get() - ->where("\"CountryID\" = " . $this->ID); - } + public function Regions() + { + return Region_Billing::get() + ->where("\"CountryID\" = " . $this->ID); + } } - diff --git a/code/Region.php b/code/Region.php index 1ef204b..614c963 100755 --- a/code/Region.php +++ b/code/Region.php @@ -7,81 +7,82 @@ * @package swipestripe * @subpackage order */ -class Region extends DataObject { - - /** - * Singular name - * - * @var String - */ - private static $singular_name = 'Region'; - - /** - * Plural name - * - * @var String - */ - private static $plural_name = 'Regions'; - - /** - * Fields - * - * @var Array - */ - private static $db = array( - 'Code' => "Varchar", - 'Title' => 'Varchar', - 'SortOrder' => 'Int' - ); - - /** - * Managed via the SiteConfig, regions are related to Countries - * - * @var Array - */ - private static $has_one = array ( - 'ShopConfig' => 'ShopConfig', - 'Country' => 'Country' - ); - - /** - * Summary fields - * - * @var Array - */ - private static $summary_fields = array( - 'Title' => 'Title', - 'Code' => 'Code', - 'Country.Title' => 'Country' - ); +class Region extends DataObject +{ + + /** + * Singular name + * + * @var String + */ + private static $singular_name = 'Region'; + + /** + * Plural name + * + * @var String + */ + private static $plural_name = 'Regions'; + + /** + * Fields + * + * @var Array + */ + private static $db = array( + 'Code' => "Varchar", + 'Title' => 'Varchar', + 'SortOrder' => 'Int' + ); + + /** + * Managed via the SiteConfig, regions are related to Countries + * + * @var Array + */ + private static $has_one = array( + 'ShopConfig' => 'ShopConfig', + 'Country' => 'Country' + ); + + /** + * Summary fields + * + * @var Array + */ + private static $summary_fields = array( + 'Title' => 'Title', + 'Code' => 'Code', + 'Country.Title' => 'Country' + ); - private static $default_sort = 'SortOrder'; + private static $default_sort = 'SortOrder'; - /** - * Convenience function to prevent errors thrown - */ - public function forTemplate() { - return; - } - - /** - * Retrieve map of shipping regions including Country code - * - * @return Array - */ - public static function shipping_map() { - - $countryRegions = array(); - $regions = Region_Shipping::get(); - if ($regions && $regions->exists()) { - - foreach ($regions as $region) { - $country = $region->Country(); - $countryRegions[$country->Code][$region->Code] = $region->Title; - } - } - return $countryRegions; - } + /** + * Convenience function to prevent errors thrown + */ + public function forTemplate() + { + return; + } + + /** + * Retrieve map of shipping regions including Country code + * + * @return Array + */ + public static function shipping_map() + { + $countryRegions = array(); + $regions = Region_Shipping::get(); + if ($regions && $regions->exists()) { + foreach ($regions as $region) { + $country = $region->Country(); + $countryRegions[$country->Code][$region->Code] = $region->Title; + } + } + return $countryRegions; + } } /** @@ -92,32 +93,34 @@ public static function shipping_map() { * @package swipestripe * @subpackage order */ -class Region_Shipping extends Region { +class Region_Shipping extends Region +{ - /** - * Fields for CRUD of shipping regions - * - * @see DataObject::getCMSFields() - */ - public function getCMSFields() { + /** + * Fields for CRUD of shipping regions + * + * @see DataObject::getCMSFields() + */ + public function getCMSFields() + { - // $fields = new FieldList( - // $rootTab = new TabSet('Root', - // $tabMain = new Tab('Region', - // TextField::create('Code', _t('Region.CODE', 'Code')), - // TextField::create('Title', _t('Region.TITLE', 'Title')), - // DropdownField::create('CountryID', 'Country', Country_Shipping::get()->map()->toArray()) - // ) - // ) - // ); - // return $fields; + // $fields = new FieldList( + // $rootTab = new TabSet('Root', + // $tabMain = new Tab('Region', + // TextField::create('Code', _t('Region.CODE', 'Code')), + // TextField::create('Title', _t('Region.TITLE', 'Title')), + // DropdownField::create('CountryID', 'Country', Country_Shipping::get()->map()->toArray()) + // ) + // ) + // ); + // return $fields; - $fields = parent::getCMSFields(); - $fields->replaceField('CountryID', DropdownField::create('CountryID', 'Country', Country_Shipping::get()->map()->toArray())); - $fields->removeByName('SortOrder'); - $fields->removeByName('ShopConfigID'); - return $fields; - } + $fields = parent::getCMSFields(); + $fields->replaceField('CountryID', DropdownField::create('CountryID', 'Country', Country_Shipping::get()->map()->toArray())); + $fields->removeByName('SortOrder'); + $fields->removeByName('ShopConfigID'); + return $fields; + } } /** @@ -128,7 +131,6 @@ public function getCMSFields() { * @package swipestripe * @subpackage order */ -class Region_Billing extends Region { - +class Region_Billing extends Region +{ } -