Source of file SalesforceContactLog.php
Size: 7,178 Bytes - Last Modified: 2021-12-23T10:46:09+00:00
/var/www/docs.ssmods.com/process/src/code/Model/SalesforceContactLog.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 | <?php class SalesforceContactLog extends DataObject { /** * Singular name for CMS * @var string */ private static $singular_name = 'Sales Force Contact Log'; /** * Plural name for CMS * @var string */ private static $plural_name = 'Sales Force Contact Log Entries'; /** * SS4 prep! * @var string */ private static $table_name = 'SalesforceContactLog'; private static $db = [ 'SalesforceIdentifier' => 'Varchar(40)', 'Type' => 'Enum("Created,Updated")', 'Email' => 'Varchar', 'Phone' => 'Varchar', 'FirstName' => 'Varchar', 'LastName' => 'Varchar', 'Executed' => 'Boolean', 'FieldsSent' => 'Text', 'Filters' => 'Text', 'HasError' => 'Boolean', 'Errors' => 'Text', ]; private static $default_sort = [ 'ID' => 'DESC', ]; /** * Defines a default list of filters for the search context * @var array */ private static $searchable_fields = [ 'Type' => 'ExactMatchFilter', 'Executed' => 'ExactMatchFilter', 'HasError' => 'ExactMatchFilter', 'SalesforceIdentifier' => 'PartialMatchField', 'Email' => 'PartialMatchField', 'Phone' => 'PartialMatchField', 'FirstName' => 'PartialMatchField', 'LastName' => 'PartialMatchField', 'FieldsSent' => 'PartialMatchField', 'Filters' => 'PartialMatchField', 'Errors' => 'PartialMatchField', ]; /** * Defines summary fields commonly used in table columns * as a quick overview of the data for this dataobject * @var array */ private static $summary_fields = [ 'Created.Nice' => 'When', 'SalesforceIdentifier' => 'Code', 'Type' => 'Type', 'Email' => 'Email', 'Phone' => 'Phone', 'FirstName' => 'First', 'LastName' => 'Last', 'Executed.Nice' => 'Executed', 'HasError.Nice' => 'Errors', ]; private static $indexes = [ 'SalesforceIdentifier' => true, 'Email' => 'Email', 'Phone' => 'Phone', ]; /** * create contact log * @param string $type * @param array $fieldsSent * @param array $filters * * @return SalesforceContactLog|null */ public static function create_contact_log($type, array $fieldsSent, array $filters) { //basic fields $email = isset($fieldsSent['Email']) ? $fieldsSent['Email'] : ''; $phone = isset($fieldsSent['Phone']) ? $fieldsSent['Phone'] : ''; $firstName = isset($fieldsSent['FirstName']) ? $fieldsSent['FirstName'] : ''; $lastName = isset($fieldsSent['LastName']) ? $fieldsSent['LastName'] : ''; //serialize $fieldsSent = serialize($fieldsSent); $filters = serialize($filters); $obj = self::create( [ 'Type' => $type, 'FieldsSent' => $fieldsSent, 'Filters' => $filters, 'Email' => $email, 'Phone' => $phone, 'FirstName' => $firstName, 'LastName' => $lastName, ] ); $id = $obj->write(); return self::get()->byID($id); } /** * returns true if the contact update was successful * @param mixed $response * * @return bool */ public function confirmContactLog($response, $error = '') { $id = ''; $errors = ''; $hasError = false; if (is_array($response)) { $response = $response[0]; } if ($error) { $hasError = true; $errors = $error; } if ($response instanceof \SForce\Wsdl\SaveResult) { $id = $response->getId(); if ($response->getSuccess()) { $hasError = false; } else { $errors = serialize($response->getErrors()); $hasError = true; } } else { $errors .= '||| Unexpected response: ' . serialize($response); } $this->SalesforceIdentifier = $id; $this->Errors = $errors; $this->HasError = $hasError; $this->Executed = true; $this->write(); return $this->HasError ? false : true; } /** * CMS Fields * @return FieldList */ public function getCMSFields() { $fields = parent::getCMSFields(); $fields->removeFieldsFromTab( 'Root.Main', [ 'FieldsSent', 'Filters', 'HasError', 'Executed', ] ); $fields->addFieldsToTab( 'Root.Main', [ ReadonlyField::create( 'Created', 'Created', DBField::create_field('SS_DateTime', $this->Created)->Nice() ), ReadonlyField::create( 'Type', 'Type' ), ReadonlyField::create( 'SalesforceIdentifier', 'Sales Force Identifier' ), ] ); $fields->addFieldsToTab( 'Root.Debug', [ LiteralField::create( 'FieldsSentNice', '<h2>Fields Sent</h2>' . $this->serializedToHTML($this->FieldsSent) ), LiteralField::create( 'FiltersNice', '<h2>Filters</h2>' . $this->serializedToHTML($this->Filters) ), ReadonlyField::create( 'ExecutedNice', 'Executed', DBField::create_field('Boolean', $this->Executed)->Nice() ), ReadonlyField::create( 'HasErrorNice', 'HasError', DBField::create_field('Boolean', $this->HasError)->Nice() ), ReadonlyField::create( 'Errors', 'Communication Errors' )->setRightTitle('Separated by three ||| symbols.'), ] ); return $fields; } /** * DataObject create permissions * @param Member $member * @return bool */ public function canCreate($member = null) { return false; } /** * DataObject edit permissions * @param Member $member * @return bool */ public function canEdit($member = null) { return false; } /** * DataObject delete permissions * @param Member $member * @return bool */ public function canDelete($member = null) { return false; } /** * @param string $serializedString (serialised data) * @return string (html) */ protected function serializedToHTML($serializedString) { $s = unserialize($serializedString); return '<pre>' . print_r($s, 1) . '</pre>'; } } |