Source of file MySalesforceContactApi.php
Size: 10,066 Bytes - Last Modified: 2021-12-23T10:46:09+00:00
/var/www/docs.ssmods.com/process/src/code/Api/Contact/MySalesforceContactApi.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339 | <?php use SForce\SObject; use SForce\Wsdl\SaveResult; /** * This class adds / updates subscribers to Salesforce */ class MySalesforceContactApi extends Object { /** * @var boolean */ protected static $debug = false; protected static $my_singleton_connection = null; /** * @param string $email * @param string $phoneNumber - OPTIONAL * @param string $firstName - OPTIONAL * @param string $lastName - OPTIONAL * @param array $extraFields - OPTIONAL * @param array $extraFilterArray - OPTIONAL * * @return bool */ public static function add_email_subscriber( $email, $phoneNumber = null, $firstName = null, $lastName = null, $extraFields = [], $extraFilterArray = [] ) { self::assert_email($email); $fields = $extraFields; $fields['Email'] = $email; $fields['FirstName'] = '-'; $fields['LastName'] = '-'; if ($firstName) { $fields['FirstName'] = $firstName; } if ($lastName) { $fields['LastName'] = $lastName; } if ($phoneNumber) { $fields['Phone'] = $phoneNumber; } //doing it! return self::create_contact($fields, $extraFilterArray); } /** * @todo: allow adding by phone number * * @param string $email * @param array $extraFields - OPTIONAL * @param array $extraFilterArray - OPTIONAL * * @return bool */ public static function update_email_subscriber( $email, $extraFields = [], $extraFilterArray = [] ) { self::assert_email($email); $fields = $extraFields; $fields['Email'] = $email; //doing it return self::update_contact($fields, $extraFilterArray); } /** * @param string $email * @param array $extraFilterArray * * @return bool */ public static function is_email_registered($email, $extraFilterArray = []) { $fieldsArray = ['Email' => $email]; $subscriber = self::retrieve_contact($fieldsArray, $extraFilterArray); return $subscriber ? true : false; } /** * todo: all nullify thingy * assumes that all fields are correct (e.g. correct email format) * can use email or phone as identifier * @param array $fieldsArray * @param array $extraFilterArray -independent additional filters for retrieving contact * * @return bool */ public static function create_contact($fieldsArray, $extraFilterArray = []) { $connection = self::get_my_singleton_connection(); //get defaults $extraFilterArray = array_merge( MySalesforceContactConfigApi::get_fields_for_filter(), $extraFilterArray ); $fieldsArray = array_merge( MySalesforceContactConfigApi::get_fields_to_send_on_creation(), $fieldsArray, $extraFilterArray ); $log = SalesforceContactLog::create_contact_log( 'Created', $fieldsArray, $extraFilterArray ); $existingContact = self::retrieve_contact($fieldsArray, $extraFilterArray); // Contact not found. Create a new Contact and set the details if ($existingContact) { //we are out of here! return true; } $contact = new SObject(); $contact->setType('Contact'); foreach ($fieldsArray as $fieldName => $fieldValue) { $contact->{$fieldName} = $fieldValue; } //doing it! $error = ''; $response = null; try { $response = $connection->create([$contact]); } //catch exception catch (\Exception $e) { $error = $e->getMessage(); } if (self::$debug) { $connection->debug($response); } return $log->confirmContactLog($response, $error); } /** * todo: all nullify thingy * assumes that all fields are correct (e.g. correct email format) * can use email or phone as identifier * @param array $fieldsArray * @param array $extraFilterArray -independent additional filters for retrieving contact * * @return bool */ public static function update_contact($fieldsArray, $extraFilterArray = []) { $connection = self::get_my_singleton_connection(); //add defaults $extraFilterArray = array_merge( MySalesforceContactConfigApi::get_fields_for_filter(), $extraFilterArray ); $fieldsArray = array_merge( MySalesforceContactConfigApi::get_fields_to_send_on_update(), $fieldsArray, $extraFilterArray ); $log = SalesforceContactLog::create_contact_log( 'Updated', $fieldsArray, $extraFilterArray ); //find existing contact $existingContact = self::retrieve_contact($fieldsArray, $extraFilterArray); // Contact found. Update Contact with details if ($existingContact) { $contact = new SObject(); $contact->setType('Contact'); $contact->setId($existingContact->getId()); foreach ($fieldsArray as $fieldName => $fieldValue) { if ($fieldValue != $existingContact->{$fieldName}) { $contact->{$fieldName} = $fieldValue; } } //doing it! $error = ''; $response = null; try { $response = $connection->update([$contact]); } //catch exception catch (\Exception $e) { $error = $e->getMessage(); } } else { //we are out of here! return true; } if (self::$debug) { echo $error; $connection->debug($response); } return $log->confirmContactLog($response, $error); } /** * Retrive a contact by email address or by phone number * either the email (preferred) of phone needs to be set in the $fieldsArray * * @param array $fieldsArray * @param array $extraFilterArray additional filter key-value sets * * @return SForce\SObject|null */ public static function retrieve_contact($fieldsArray, $extraFilterArray = [], $otherFields = []) { $connection = self::get_my_singleton_connection(); // Check for existing Contact $existingContact = null; $filterArray = []; $finalFilterArray = []; $result = null; $extraFilterArray = array_merge( MySalesforceContactConfigApi::get_fields_for_filter(), $extraFilterArray ); $email = isset($fieldsArray['Email']) ? trim($fieldsArray['Email']) : null; if ($email) { $filterArray['Email'] = $email; } else { $phone = isset($fieldsArray['Phone']) ? trim($fieldsArray['Phone']) : null; if ($phone) { $filterArray['Phone'] = $phone; } } if (count($filterArray)) { $finalFilterArray[] = MySalesforcePartnerApi::array2sql($filterArray); if (count($extraFilterArray)) { $finalFilterArray[] = MySalesforcePartnerApi::array2sql($extraFilterArray); } $where = ' ( '.implode(' ) AND ( ', $finalFilterArray).' ) '; $querySelect = 'SELECT Id, FirstName, LastName, Phone, Email'; if(! empty($otherFields)) { foreach($otherFields as $otherField) { $querySelect .= ', '.$otherField; } } $queryFrom = 'FROM Contact'; $queryWhere = 'WHERE '.$where.' LIMIT 1'; $query = implode( ' ', [ $querySelect, $queryFrom, $queryWhere ] ); $result = $connection->query($query); if ($result) { $contacts = $result->getRecords(); if ($contacts) { $existingContact = $contacts[0]; } } } if (self::$debug) { $connection->debug($result); } return $existingContact; } /** * @return array */ public static function retrieve_contact_record_types() { $connection = self::get_my_singleton_connection(); $returnArray = []; $query = ' SELECT Id, Name, Description, DeveloperName, IsActive, sObjectType FROM RecordType WHERE IsActive = true AND sObjectType = \'Contact\''; $result = $connection->query($query); if ($result) { $records = $result->getRecords(); if ($records) { foreach ($records as $record) { $object = $record->getFields(); $object->Id = $record->getId(); $returnArray[] = $object; } } } if (self::$debug) { $connection->debug($result); } return $returnArray; } /** * @param string $email */ public static function assert_email($email) { if (! filter_var($email, FILTER_VALIDATE_EMAIL)) { throw new InvalidArgumentException('Wrong email address format'); } } /** * @param bool $b */ public static function set_debug($b = true) { self::$debug = $b; } protected static function get_my_singleton_connection() { if (self::$my_singleton_connection === null) { self::$my_singleton_connection = MySalesforcePartnerApiConnectionOnly::singleton(); } return self::$my_singleton_connection; } } |