Source of file AddressItem.php
Size: 8,506 Bytes - Last Modified: 2021-12-23T10:36:44+00:00
/var/www/docs.ssmods.com/process/src/src/Items/AddressItem.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319 | <?php /** * This file is part of SilverWare. * * PHP version >=5.6.0 * * For full copyright and license information, please view the * LICENSE.md file that was distributed with this source code. * * @package SilverWare\Contact\Items * @author Colin Tucker <colin@praxis.net.au> * @copyright 2017 Praxis Interactive * @license https://opensource.org/licenses/BSD-3-Clause BSD-3-Clause * @link https://github.com/praxisnetau/silverware-contact */ namespace SilverWare\Contact\Items; use SilverStripe\Forms\CheckboxField; use SilverStripe\Forms\CompositeField; use SilverStripe\Forms\RequiredFields; use SilverStripe\Forms\TextField; use SilverStripe\i18n\i18n; use SilverWare\Contact\Model\ContactItem; use SilverWare\Countries\Forms\CountryDropdownField; use SilverWare\Forms\FieldSection; /** * An extension of the contact item class for an address item. * * @package SilverWare\Contact\Items * @author Colin Tucker <colin@praxis.net.au> * @copyright 2017 Praxis Interactive * @license https://opensource.org/licenses/BSD-3-Clause BSD-3-Clause * @link https://github.com/praxisnetau/silverware-contact */ class AddressItem extends ContactItem { /** * Human-readable singular name. * * @var string * @config */ private static $singular_name = 'Address Item'; /** * Human-readable plural name. * * @var string * @config */ private static $plural_name = 'Address Items'; /** * Description of this object. * * @var string * @config */ private static $description = 'A contact item to show an address'; /** * Defines the table name to use for this object. * * @var string * @config */ private static $table_name = 'SilverWare_Contact_AddressItem'; /** * Defines an ancestor class to hide from the admin interface. * * @var string * @config */ private static $hide_ancestor = ContactItem::class; /** * Maps field names to field types for this object. * * @var array * @config */ private static $db = [ 'Street' => 'Varchar(255)', 'StreetLine2' => 'Varchar(255)', 'Suburb' => 'Varchar(255)', 'StateTerritory' => 'Varchar(128)', 'PostalCode' => 'Varchar(32)', 'Country' => 'Varchar(2)', 'HideCountry' => 'Boolean' ]; /** * Defines the default values for the fields of this object. * * @var array * @config */ private static $defaults = [ 'FontIcon' => 'building', 'HideCountry' => 0 ]; /** * Maps field and method names to the class names of casting objects. * * @var array * @config */ private static $casting = [ 'FullAddress' => 'Text' ]; /** * Answers a list of field objects for the CMS interface. * * @return FieldList */ public function getCMSFields() { // Obtain Field Objects (from parent): $fields = parent::getCMSFields(); // Define Placeholder: $placeholder = _t(__CLASS__ . '.DROPDOWNSELECT', 'Select'); // Create Main Fields: $fields->addFieldsToTab( 'Root.Main', [ FieldSection::create( 'AddressSection', $this->fieldLabel('Address'), [ CompositeField::create([ TextField::create( 'Street', '' ), TextField::create( 'StreetLine2', '' )->addExtraClass('street-line-2') ])->setName('StreetWrapper')->setTitle($this->fieldLabel('Street')), TextField::create( 'Suburb', $this->fieldLabel('Suburb') ), TextField::create( 'StateTerritory', $this->fieldLabel('StateTerritory') ), TextField::create( 'PostalCode', $this->fieldLabel('PostalCode') ), CountryDropdownField::create( 'Country', $this->fieldLabel('Country') ) ] ) ] ); // Create Options Fields: $fields->addFieldToTab( 'Root.Options', FieldSection::create( 'AddressOptions', $this->fieldLabel('Address'), [ CheckboxField::create( 'HideCountry', $this->fieldLabel('HideCountry') ) ] ) ); // Answer Field Objects: return $fields; } /** * Answers a validator for the CMS interface. * * @return RequiredFields */ public function getCMSValidator() { return parent::getCMSValidator()->appendRequiredFields( RequiredFields::create([ 'Street', 'Suburb', 'StateTerritory', 'PostalCode', 'Country' ]) ); } /** * Answers the labels for the fields of the receiver. * * @param boolean $includerelations Include labels for relations. * * @return array */ public function fieldLabels($includerelations = true) { // Obtain Field Labels (from parent): $labels = parent::fieldLabels($includerelations); // Define Field Labels: $labels['Street'] = _t(__CLASS__ . '.STREET', 'Street'); $labels['Suburb'] = _t(__CLASS__ . '.SUBURB', 'Suburb'); $labels['Address'] = _t(__CLASS__ . '.ADDRESS', 'Address'); $labels['Country'] = _t(__CLASS__ . '.COUNTRY', 'Country'); $labels['PostalCode'] = _t(__CLASS__ . '.POSTALCODE', 'Postal code'); $labels['StateTerritory'] = _t(__CLASS__ . '.STATETERRITORY', 'State/Territory'); $labels['HideCountry'] = _t(__CLASS__ . '.HIDECOUNTRY', 'Hide country'); // Answer Field Labels: return $labels; } /** * Event method called before the receiver is written to the database. * * @return void */ public function onBeforeWrite() { // Call Parent Event: parent::onBeforeWrite(); // Correct Country Value: $this->Country = strtolower($this->Country); } /** * Answers the full name of the country. * * @return string */ public function getCountryName() { return i18n::getData()->countryName($this->Country); } /** * Answers the full address as a string. * * @return string */ public function getFullAddress() { $address = []; if ($this->Street) { $address[] = $this->Street; } if ($this->StreetLine2) { $address[] = $this->StreetLine2; } if ($this->Suburb || $this->PostalCode || $this->StateTerritory) { $line = []; if ($this->Suburb) { $line[] = $this->Suburb; } if ($this->PostalCode) { $line[] = $this->PostalCode; } if ($this->StateTerritory) { $line[] = $this->StateTerritory; } $address[] = implode(' ', $line); } if ($this->Country) { $address[] = $this->CountryName; } return implode("\n", $address); } /** * Answers true if the country is to be shown in the template. * * @return boolean */ public function getCountryShown() { return (!$this->HideCountry && $this->CountryName); } } |