Source of file AddressFinderField.php
Size: 8,034 Bytes - Last Modified: 2021-12-24T06:39:33+00:00
/var/www/docs.ssmods.com/process/src/code/AddressFinderField.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 | <?php class AddressFinderField extends FormField { protected $data; protected $children; protected $database_fields; protected $hidden_fields; // Config variables private static $Key; private static $Secret; private static $Placeholder_text; // Specifies which fields to save into the database // database field => api reference private static $default_database_fields = array(); /** * @param DataObject $data The controlling dataobject * @param string $title The title of the field * @param array $databaseFields Various extra fields to store into */ public function __construct(DataObject $data, $name, $title = null, $databaseFields = array(), $hiddenFields = false) { $this->data = $data; $this->name = $name; $this->hidden_fields = $hiddenFields; $title = !empty($title) ? $title : 'Search An Address'; // check if the config is set $key = Config::inst()->get('AddressFinderField', 'Key'); $secret = Config::inst()->get('AddressFinderField', 'Secret'); if($this->CheckKeys()){ $this->setupFields($databaseFields); parent::__construct($this->getName(), $title); } return; } public function CheckKeys(){ // check if the config is set $key = Config::inst()->get('AddressFinderField', 'Key'); $secret = Config::inst()->get('AddressFinderField', 'Secret'); if(isset($key) && isset($secret)){ return true; } return false; } // Auto generate a name public function getName() { $name = empty($this->name) ? 'addresFinderField' : $this->name; return $name; } /** * Set up child fields */ public function setupFields($databaseFields) { $name = $this->getName(); // Check if the passed one is empty if(empty($databaseFields)){ $this->database_fields = Config::inst()->get('AddressFinderField', 'default_database_fields'); } else { $this->database_fields = $databaseFields; } // Set the address finder field $this->children = new FieldList(); if ($this->hidden_fields) { $this->children->push( TextField::create($name, '') ->setAttribute('placeholder', Config::inst()->get('AddressFinderField', 'Placeholder_text')) ); } // Add all data fields foreach($this->database_fields as $db => $meta){ // Adding via array naming allows for data extraction at the setting value stage if($this->hidden_fields){ $field = HiddenField::create($this->fullChildFieldName($db), $this->data->fieldLabel($db), $this->recordFieldData($db)); } else { if ($meta == 'postcode') { $field = NumericField::create($this->fullChildFieldName($db), $this->data->fieldLabel($db), $this->recordFieldData($db)); } else { $field = TextField::create($this->fullChildFieldName($db), $this->data->fieldLabel($db), $this->recordFieldData($db)); } } $field->addExtraClass('addressfinderfield-metafield')->setAttribute('data-metatype', $meta); $this->children->push($field); } $this->children->first() ->setAttribute('data-key', Config::inst()->get('AddressFinderField', 'Key')) ->addExtraClass('address-finder-field'); return $this->children; } /** * @param array $properties * {@inheritdoc} */ public function Field($properties = array()) { $this->requireDependencies(); $this->customise(array( 'HiddenFields' => $this->hidden_fields )); foreach($this->getAttributes() as $k => $v) { if (!$this->children[0]->getAttribute($k))$this->children[0]->setAttribute($k,$v); } return parent::Field($properties); } /** * Set up and include any frontend requirements * @return void */ protected function requireDependencies() { if($this->CheckKeys()){ // Reset for the first field //reset($this->database_fields); $fieldID = $this->getName(); // key($this->database_fields); $vars = array( 'Key' => Config::inst()->get('AddressFinderField', 'Key'), 'AddressFieldID' => $fieldID ); // CSS Requirements::css(ADDRESSFINDERFIELD_BASE .'/css/AddressFinderField.css'); // Javascript if(!$this->isCMS()){ Requirements::javascript(FRAMEWORK_DIR . '/thirdparty/jquery/jquery.min.js'); Requirements::javascript(FRAMEWORK_DIR . '/thirdparty/jquery-entwine/dist/jquery.entwine-dist.js'); } //Requirements::javascriptTemplate(ADDRESSFINDERFIELD_BASE .'/javascript/AddressFinderField.js', $vars); Requirements::javascript(ADDRESSFINDERFIELD_BASE .'/javascript/AddressFinderField.js'); } } /** * {@inheritdoc} */ public function setValue($record) { // Check for defined databasefields if(!empty($this->database_fields)){ $dbFields = $this->database_fields; } else { $dbFields = Config::inst()->get('AddressFinderField', 'default_database_fields'); } // If fields are still empty then load into main field if (empty($dbFields) && is_string($record)) { $this->value = $record; $this->getChildFields()->fieldByName($this->getName())->setValue($record); } // Loop fields and save the info foreach($dbFields as $db => $meta){ $fieldName = $this->fullChildFieldName($db); $field = $this->getChildFields()->fieldByName($fieldName); if (is_array($record)) { $field->setValue( $record[$db] ); } else if (is_object($record)) { $field->setValue( $record->$db ); } } $this->value = $record; return $this; } /** * Take the fields and save them to the DataObject. * {@inheritdoc} */ public function saveInto(DataObjectInterface $record) { // Check for defined databasefields if(!empty($this->database_fields)){ $dbFields = $this->database_fields; } else { $dbFields = Config::inst()->get('AddressFinderField', 'default_database_fields'); } // If fields are still empty then load into main field if (empty($dbFields)) { $this->value = $record; $field = $this->getChildFields()->fieldByName($this->getName()); $record->setCastedField($this->getName(), $field->dataValue()); } // Loop fields and save the info foreach($dbFields as $db => $meta){ $fieldName = $this->fullChildFieldName($db); $field = $this->getChildFields()->fieldByName($fieldName); $record->setCastedField($db, $field->dataValue()); } return $this; } /** * @return FieldList The Latitude/Longitude fields */ public function getChildFields() { return $this->children; } protected function fullChildFieldName($name){ return $this->getName() . '[' . $name . ']'; } protected function recordFieldData($name) { $fieldName = $name; return $this->data->$fieldName ?: $this->getDefaultValue($name); } public function getDefaultValue($name) { return null; } public function isCMS() { return Controller::curr() instanceof LeftAndMain; } // Exclude value from attributes because it's an array and causes issues public function getAttributes($attributes = null) { $attributes = parent::getAttributes($attributes); foreach($attributes as $k => $v) { if (!is_string($v)) unset($attributes[$k]); } return $attributes; } } |