Source of file BillingAddress.php
Size: 10,501 Bytes - Last Modified: 2021-12-23T10:39:35+00:00
/var/www/docs.ssmods.com/process/src/src/Model/Address/BillingAddress.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344 | <?php namespace Sunnysideup\Ecommerce\Model\Address; use SilverStripe\Control\Controller; use SilverStripe\Core\Injector\Injector; use SilverStripe\Forms\CompositeField; use SilverStripe\Forms\EmailField; use SilverStripe\Forms\HeaderField; use SilverStripe\Forms\NumericField; use SilverStripe\Forms\TextField; use SilverStripe\Security\Member; use SilverStripe\Security\Security; use Sunnysideup\CmsEditLinkField\Forms\Fields\CMSEditLinkField; use Sunnysideup\Ecommerce\Config\EcommerceConfig; use Sunnysideup\Ecommerce\Forms\Fields\SelectOrderAddressField; use Sunnysideup\Ecommerce\Model\Order; use Sunnysideup\GoogleAddressField\GoogleAddressField; /** * @description: each order has a billing address. * * @authors: Nicolaas [at] Sunny Side Up .co.nz * @package: ecommerce * @sub-package: address */ class BillingAddress extends OrderAddress { /** * what variables are accessible through http://mysite.com/api/ecommerce/v1/BillingAddress/. * * @var array */ private static $api_access = [ 'view' => [ 'Prefix', 'FirstName', 'Surname', 'Address', 'Address2', 'City', 'PostalCode', 'RegionCode', 'Country', 'Phone', 'Email', ], ]; /** * @var bool */ private static $allow_selection_of_previous_addresses_in_checkout = false; /** * standard SS variable. * * @return array */ private static $table_name = 'BillingAddress'; private static $db = [ 'Prefix' => 'Varchar(10)', 'FirstName' => 'Varchar(100)', 'Surname' => 'Varchar(100)', 'Address' => 'Varchar(255)', 'Address2' => 'Varchar(200)', 'City' => 'Varchar(100)', 'PostalCode' => 'Varchar(30)', 'Country' => 'Varchar(4)', 'RegionCode' => 'Varchar(100)', 'Phone' => 'Varchar(50)', 'Email' => 'EmailAddress', 'Obsolete' => 'Boolean', 'OrderID' => 'Int', //NOTE: we have this here for faster look-ups and to make addresses behave similar to has_many dataobjects ]; /** * HAS_ONE =array(ORDER => ORDER); * we place this relationship here * (rather than in the parent class: OrderAddress) * because that makes for a cleaner relationship * (otherwise we ended up with a "has two" relationship in Order). */ private static $has_one = [ 'Region' => EcommerceRegion::class, ]; /** * standard SS static definition. */ private static $belongs_to = [ 'Order' => Order::class, ]; /** * standard SS static definition. */ private static $default_sort = [ 'ID' => 'DESC', ]; /** * standard SS variable. * * @return array */ private static $indexes = [ 'Obsolete' => true, 'OrderID' => true, 'Country' => true, ]; /** * standard SS variable. * * @return array */ private static $casting = [ 'FullCountryName' => 'Varchar', ]; /** * standard SS variable. * * @return array */ private static $searchable_fields = [ 'OrderID' => [ 'field' => NumericField::class, 'title' => 'Order Number', ], 'Email' => 'PartialMatchFilter', 'FirstName' => 'PartialMatchFilter', 'Surname' => 'PartialMatchFilter', 'Address' => 'PartialMatchFilter', 'City' => 'PartialMatchFilter', 'Country' => 'PartialMatchFilter', 'Obsolete', ]; /** * standard SS variable. * * @return array */ private static $summary_fields = [ 'Order.Title', 'FirstName', 'Surname', 'City', 'PostalCode', 'Country', 'Phone', ]; /** * @var array */ private static $required_fields = [ 'Phone', 'Email', 'FirstName', 'Surname', 'Address', 'City', 'PostalCode', ]; /** * standard SS variable. * * @return array */ private static $field_labels = [ 'Order.Title' => 'Order', 'Obsolete' => 'Do not use for future transactions', 'Email' => 'Email', ]; /** * standard SS variable. * * @return string */ private static $singular_name = 'Billing Address'; /** * standard SS variable. * * @return string */ private static $plural_name = 'Billing Addresses'; /** * Standard SS variable. * * @var string */ private static $description = 'The details of the person buying the order.'; public function i18n_singular_name() { return _t('BillingAddress.BILLINGADDRESS', 'Billing Address'); } public function i18n_plural_name() { return _t('BillingAddress.BILLINGADDRESSES', 'Billing Addresses'); } /** * method for casted variable. * * @return string */ public function FullCountryName() { return $this->getFullCountryName(); } public function getFullCountryName() { return EcommerceCountry::find_title($this->Country); } /** * @return \SilverStripe\Forms\FieldList */ public function getCMSFields() { $fields = parent::getCMSFields(); $fields->replaceField( 'OrderID', CMSEditLinkField::create( 'OrderID', Injector::inst()->get(Order::class)->singular_name(), $this->getOrderCached() ) ); $fields->replaceField('Email', new EmailField('Email', _t('BillingAddress.EMAIL', 'Email'))); //We remove both the RegionCode and RegionID field and then add only the one we need directly after the country field. $fields->removeByName('RegionCode'); $fields->removeByName('RegionID'); $fields->insertBefore($this->getRegionField('RegionID'), 'Country'); $fields->replaceField('Country', $this->getCountryField('Country')); return $fields; } /** * @return \SilverStripe\Forms\FieldList */ public function getFields(Member $member = null) { $fields = parent::getEcommerceFields(); $headerTitle = _t('BillingAddress.DELIVERY_AND_BILLING_ADDRESS', 'Delivery and Billing Address'); $fields->push( HeaderField::create( 'BillingDetails', $headerTitle, 3 ) ->setAttribute('data-title-with-shipping-address', _t('BillingAddress.BILLING_ADDRESS_ONLY', 'Billing Address Only')) ->setAttribute('data-title-with-shipping-address_default', $headerTitle) ); $fields->push(new TextField('Phone', _t('BillingAddress.PHONE', 'Phone'))); $billingFields = new CompositeField(); if ($member && Security::getCurrentUser()) { if ($member->exists() && ! $member->IsShopAdmin()) { $this->FillWithLastAddressFromMember($member, true); if (EcommerceConfig::get(BillingAddress::class, 'allow_selection_of_previous_addresses_in_checkout')) { $addresses = $member->previousOrderAddresses( $this->baseClassLinkingToOrder(), $this->ID, $onlyLastRecord = false, $keepDoubles = false ); //we want MORE than one here not just one. if ($addresses->count() > 1) { $fields->push( SelectOrderAddressField::create( 'SelectBillingAddressField', _t('BillingAddress.SELECTBILLINGADDRESS', 'Select Billing Address'), $addresses ) ); } } } } $mappingArray = $this->Config()->get('fields_to_google_geocode_conversion'); if (is_array($mappingArray) && count($mappingArray)) { if (! class_exists(GoogleAddressField::class)) { user_error('You must install the Sunny Side Up google_address_field module OR remove entries from: BillingAddress.fields_to_google_geocode_conversion'); } $billingFields->push( $billingEcommerceGeocodingField = GoogleAddressField::create( 'BillingEcommerceGeocodingField', _t('BillingAddress.FIND_ADDRESS', 'Find address'), Controller::curr()->getRequest()->getSession()->get('BillingEcommerceGeocodingFieldValue') ) ); $billingEcommerceGeocodingField->setFieldMap($mappingArray); //$billingFields->push(new HiddenField('Address2', "NOT SET", "NOT SET")); //$billingFields->push(new HiddenField('City', "NOT SET", "NOT SET")); } $billingFields->push(new TextField('Address', _t('BillingAddress.ADDRESS', 'Address'))); $billingFields->push(new TextField('Address2', _t('BillingAddress.ADDRESS2', 'Address Line 2'))); $billingFields->push(new TextField('City', _t('BillingAddress.CITY', 'Town'))); $billingFields->push($this->getPostalCodeField('PostalCode')); $billingFields->push($this->getRegionField('RegionID', 'RegionCode')); $billingFields->push($this->getCountryField('Country')); $billingFields->addExtraClass('billingFields'); $billingFields->addExtraClass('orderAddressHolder'); $this->makeSelectedFieldsReadOnly($billingFields); $fields->push($billingFields); $this->extend('augmentEcommerceBillingAddressFields', $fields); return $fields; } /** * Return which billing address fields should be required on {@link OrderFormAddress}. * * @return array */ public function getRequiredFields() { return $this->Config()->get('required_fields'); } /* * standard SS method * sets the country to the best known country {@link EcommerceCountry} */ //function populateDefaults() { //parent::populateDefaults(); //$this->Country = EcommerceCountry::get_country(false, $this->OrderID); //} } |