Source of file SalesforceDefaultContactField.php
Size: 4,952 Bytes - Last Modified: 2021-12-23T10:46:09+00:00
/var/www/docs.ssmods.com/process/src/code/Model/SalesforceDefaultContactField.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 | <?php /** * this can be linked to pages / other objects using many_many relationships * so that you can send default record values to Salesforce. * It is NOT for user specific ones, but for generic fields. * * This class knows what format salesforce expects. */ class SalesforceDefaultContactField extends DataObject { /** * contact fields that should be created by default... * @var array */ private static $defaults_records = []; /** * Singular name for CMS * @var string */ private static $singular_name = 'Default Contact Field'; /** * Plural name for CMS * @var string */ private static $plural_name = 'Default Contact Fields'; /** * * @var array */ private static $db = [ 'Key' => 'Varchar', 'Value' => 'Varchar', 'ValueType' => 'Enum("String,Number,YesOrTrue,NoOrFalse,CurrentDate,CurrentDateAndTime,PageURL,WebsiteURL", "String")', ]; /** * Defines summary fields commonly used in table columns * as a quick overview of the data for this dataobject * @var array */ private static $summary_fields = [ 'Key' => 'Field Name', 'Value' => 'Field Value', 'ValueType' => 'Value Type', 'BetterValue' => 'Calculated Value', ]; /** * Defines summary fields commonly used in table columns * as a quick overview of the data for this dataobject * @var array */ /** * Defines a default list of filters for the search context * @var array */ private static $searchable_fields = [ 'Key', 'Value', 'ValueType', ]; /** * * @return string */ public function getTitle() { return $this->Key . ' = '.$this->BetterValueHumanReadable() . ' ('.$this->ValueType.')'; } public function requireDefaultRecords() { foreach($this->Config()->get('default_records') as $key => $details) { $value = isset($details['Value']) ? $details['Value'] : 'please set' ; $type = isset($details['ValueType']) ? $details['Value'] : 'String' ; $type = $details['ValueType']; $filter = [ 'Key' => $key ]; $obj = SalesforceDefaultContactField::get()->filter($filter)->first(); if(! $obj) { $obj = SalesforceDefaultContactField::create($filter); $obj->Value = $value; $obj->ValueType = $type; $obj->write(); } } } /** * @return mixed */ public function BetterValue() { switch($this->ValueType) { case 'Number': return floatval($this->Value); case 'CurrentDate': return Date('Y-m-d'); case 'CurrentDateAndTime': return Date('Y-m-d h:i:s'); case 'CurrentDateAndTime': return Date('Y-m-d h:i:s'); case 'YesOrTrue': return 'true'; case 'NoOrFalse': return 'false'; default: return trim($this->Value); } } /** * @return mixed */ public function BetterValueHumanReadable() { return $this->BetterValue(); } /** * CMS Fields * @return FieldList */ public function getCMSFields() { $fields = parent::getCMSFields(); $fields->addFieldsToTab( 'Root.Main', [ TextField::create( 'Value', 'Value' ), DropdownField::create( 'ValueType', 'Type / Predefined Value', $this->dbObject('ValueType')->enumValues() ), ReadonlyField::create( 'BetterValueHumanReadableNice', 'Calculated Value', $this->BetterValueHumanReadable() ), ] ); switch($this->ValueType) { case 'CurrentDate': case 'CurrentDateAndTime': case 'YesOrTrue': case 'NoOrFalse': $fields->removeFieldFromTab( 'Root.Main', 'Value' ); break; case 'Number': $fields->addFieldToTab( 'Root.Main', NumericField::create('Value', 'Value') ); break; } return $fields; } public function isValid($type, $data) { // switch ($type) { // case 'CREATE': // break; // case 'UPDATE': // break; // case 'FILTER': // break; // } return true; } } |