1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use SForce\SObject; |
4
|
|
|
use SForce\Wsdl\SaveResult; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* This class adds / updates subscribers to Salesforce |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
class MySalesforceContactApi extends Object |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @param string $email |
15
|
|
|
* @param string $phoneNumber - OPTIONAL |
16
|
|
|
* @param string $firstName - OPTIONAL |
17
|
|
|
* @param string $lastName - OPTIONAL |
18
|
|
|
* @param array $extraFields - OPTIONAL |
19
|
|
|
* @param array $extraFilterArray - OPTIONAL |
20
|
|
|
* |
21
|
|
|
* @return bool |
22
|
|
|
*/ |
23
|
|
|
public static function add_email_subscriber( |
24
|
|
|
$email, |
25
|
|
|
$phoneNumber = null, |
26
|
|
|
$firstName = null, |
27
|
|
|
$lastName = null, |
28
|
|
|
$extraFields = [], |
29
|
|
|
$extraFilterArray = [] |
30
|
|
|
): bool |
31
|
|
|
{ |
32
|
|
|
self::assert_email($email); |
33
|
|
|
|
34
|
|
|
$fields = $extraFields; |
35
|
|
|
$fields['Email'] = $email; |
36
|
|
|
$fields['FirstName'] = '-'; |
37
|
|
|
$fields['LastName'] = '-'; |
38
|
|
|
|
39
|
|
|
if ($firstName) { |
40
|
|
|
$fields['FirstName'] = $firstName; |
41
|
|
|
} |
42
|
|
|
if ($lastName) { |
43
|
|
|
$fields['LastName'] = $lastName; |
44
|
|
|
} |
45
|
|
|
if ($phoneNumber) { |
46
|
|
|
$fields['Phone'] = $phoneNumber; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
//doing it! |
50
|
|
|
return self::create_contact($fields, $extraFilterArray); |
51
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param string $email |
56
|
|
|
* @param array $extraFields - OPTIONAL |
57
|
|
|
* @param array $extraFilterArray - OPTIONAL |
58
|
|
|
* |
59
|
|
|
* @return bool |
60
|
|
|
*/ |
61
|
|
|
public function update_email_subscriber( |
62
|
|
|
$email, |
63
|
|
|
$extraFields = [], |
64
|
|
|
$extraFilterArray = [] |
65
|
|
|
): bool |
66
|
|
|
{ |
67
|
|
|
self::assert_email($email); |
68
|
|
|
$fields = $extraFields; |
69
|
|
|
$fields['Email'] = $email; |
70
|
|
|
|
71
|
|
|
//doing it |
72
|
|
|
return self::update_contact($fields, $extraFilterArray); |
|
|
|
|
73
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param $email |
79
|
|
|
* |
80
|
|
|
* @return bool |
81
|
|
|
*/ |
82
|
|
|
public static function is_email_registered($email) : bool |
83
|
|
|
{ |
84
|
|
|
$subscriber = MySalesforcePartnerApi::retrieve_contact($email); |
|
|
|
|
85
|
|
|
|
86
|
|
|
return $subscriber ? true : false; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* todo: all nullify thingy |
92
|
|
|
* assumes that all fields are correct (e.g. correct email format) |
93
|
|
|
* can use email or phone as identifier |
94
|
|
|
* @param array $fieldsArray |
95
|
|
|
* @param array $extraFilterArray -independent additional filters for retrieving contact |
96
|
|
|
* |
97
|
|
|
* @return bool |
98
|
|
|
*/ |
99
|
|
|
public static function create_contact($fieldsArray, $extraFilterArray = []) |
100
|
|
|
{ |
101
|
|
|
$connection = self::get_my_singleton_connection(); |
102
|
|
|
//get defaults |
103
|
|
|
$extraFilterArray = array_merge( |
104
|
|
|
SalesforceDefaultContactField::get_fields_for_filter(), |
105
|
|
|
$extraFilterArray |
106
|
|
|
); |
107
|
|
|
$fieldsArray = array_merge( |
108
|
|
|
SalesforceDefaultContactField::get_fields_to_send(), |
109
|
|
|
$fieldsArray, |
110
|
|
|
$extraFilterArray |
111
|
|
|
); |
112
|
|
|
$response = null; |
|
|
|
|
113
|
|
|
$existingContact = self::retrieve_contact($fieldsArray, $extraFilterArray); |
114
|
|
|
// Contact not found. Create a new Contact and set the details |
115
|
|
|
if ($existingContact) { |
116
|
|
|
return true; |
117
|
|
|
} else { |
118
|
|
|
$contact = new SObject(); |
119
|
|
|
$contact->setType('Contact'); |
120
|
|
|
foreach ($fieldsArray as $fieldName => $fieldValue) { |
121
|
|
|
$contact->$fieldName = $fieldValue; |
122
|
|
|
} |
123
|
|
|
$response = $connection->create([$contact]); |
124
|
|
|
} |
125
|
|
|
if(self::$debug) { |
126
|
|
|
$connection->debug($response); |
127
|
|
|
} |
128
|
|
|
$success = SalesforceContactLog::create_contact_log( |
129
|
|
|
'Created', |
130
|
|
|
$fieldsArray, |
131
|
|
|
$extraFilterArray, |
132
|
|
|
$response |
|
|
|
|
133
|
|
|
); |
134
|
|
|
|
135
|
|
|
return $success; |
|
|
|
|
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* todo: all nullify thingy |
140
|
|
|
* assumes that all fields are correct (e.g. correct email format) |
141
|
|
|
* can use email or phone as identifier |
142
|
|
|
* @param array $fieldsArray |
143
|
|
|
* @param array $extraFilterArray -independent additional filters for retrieving contact |
144
|
|
|
* |
145
|
|
|
* @return SForce\Wsdl\SaveResult|null |
146
|
|
|
*/ |
147
|
|
|
public static function update_contact($fieldsArray, $extraFilterArray = []) |
148
|
|
|
{ |
149
|
|
|
$connection = self::get_my_singleton_connection(); |
150
|
|
|
$response = null; |
|
|
|
|
151
|
|
|
|
152
|
|
|
//add defaults |
153
|
|
|
$extraFilterArray = array_merge( |
154
|
|
|
SalesforceDefaultContactField::get_fields_for_filter(), |
155
|
|
|
$extraFilterArray |
156
|
|
|
); |
157
|
|
|
$fieldsArray = array_merge( |
158
|
|
|
SalesforceDefaultContactField::get_fields_to_send(), |
159
|
|
|
$fieldsArray, |
160
|
|
|
$extraFilterArray |
161
|
|
|
); |
162
|
|
|
|
163
|
|
|
//find existing contact |
164
|
|
|
$existingContact = MySalesforcePartnerApi::array2sql($fieldsArray, $extraFilterArray); |
165
|
|
|
// Contact found. Update Contact with details |
166
|
|
|
if ($existingContact) { |
167
|
|
|
$contact = new SObject(); |
168
|
|
|
$contact->setType('Contact'); |
169
|
|
|
$contact->setId($existingContact->getId()); |
170
|
|
|
foreach ($fieldsArray as $fieldName => $fieldValue) { |
171
|
|
|
if ($existingContact->$fieldName != $fieldValue) { |
172
|
|
|
$contact->$fieldName = $fieldValue; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
$response = $connection->update([$contact]); |
176
|
|
|
} else { |
177
|
|
|
return null; |
178
|
|
|
} |
179
|
|
|
if(self::$debug) { |
180
|
|
|
$connection->debug($response); |
181
|
|
|
} |
182
|
|
|
$success = SalesforceContactLog::create_contact_log( |
183
|
|
|
'Updated', |
184
|
|
|
$fieldsArray, |
185
|
|
|
$extraFilterArray, |
186
|
|
|
$response |
|
|
|
|
187
|
|
|
); |
188
|
|
|
|
189
|
|
|
return $success; |
|
|
|
|
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Retrive a contact by email address or by phone number |
194
|
|
|
* either the email (preferred) of phone needs to be set in the $fieldsArray |
195
|
|
|
* |
196
|
|
|
* @param array $fieldsArray |
197
|
|
|
* @param array $extraFilterArray additional filter key-value sets |
198
|
|
|
* |
199
|
|
|
* @return SForce\SObject|null |
200
|
|
|
*/ |
201
|
|
|
public static function retrieve_contact($fieldsArray, $extraFilterArray = []) |
202
|
|
|
{ |
203
|
|
|
$connection = self::get_my_singleton_connection(); |
204
|
|
|
// Check for existing Contact |
205
|
|
|
$existingContact = null; |
206
|
|
|
$filterArray = []; |
207
|
|
|
$finalFilterArray = []; |
208
|
|
|
$result = null; |
209
|
|
|
|
210
|
|
|
$email = isset($fieldsArray['Email']) ? trim($fieldsArray['Email']) : null; |
211
|
|
|
if ($email) { |
212
|
|
|
$filterArray['Email'] = $email; |
213
|
|
|
} else { |
214
|
|
|
$phone = isset($fieldsArray['Phone']) ? trim($fieldsArray['Phone']) : null; |
215
|
|
|
if ($phone) { |
216
|
|
|
$filterArray['Phone'] = $phone; |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
if(count($filterArray)) { |
220
|
|
|
$finalFilterArray[] = MySalesforcePartnerApi::array2sql($filterArray); |
221
|
|
|
if(count($extraFilterArray)) { |
222
|
|
|
$finalFilterArray[] = MySalesforcePartnerApi::array2sql($extraFilterArray); |
223
|
|
|
} |
224
|
|
|
$where = ' ( '.implode(' ) AND ( ', $finalFilterArray).' ) '; |
225
|
|
|
$query = 'SELECT Id, FirstName, LastName, Phone, Email FROM Contact WHERE '.$where.' LIMIT 1'; |
226
|
|
|
|
227
|
|
|
$result = $connection->query($query); |
228
|
|
|
if ($result) { |
229
|
|
|
$contacts = $result->getRecords(); |
230
|
|
|
if ($contacts) { |
231
|
|
|
$existingContact = $contacts[0]; |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
if(self::$debug) { |
236
|
|
|
$connection->debug($result); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
return $existingContact; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
public static function retrieve_contact_record_types() : array |
243
|
|
|
{ |
244
|
|
|
$connection = self::get_my_singleton_connection(); |
245
|
|
|
$returnArray = []; |
246
|
|
|
$query = ' |
247
|
|
|
SELECT Id, Name, Description, DeveloperName, IsActive, sObjectType |
248
|
|
|
FROM RecordType |
249
|
|
|
WHERE IsActive = true AND sObjectType = \'Contact\''; |
250
|
|
|
|
251
|
|
|
$result = $connection->query($query); |
252
|
|
|
if($result) { |
253
|
|
|
$records = $result->getRecords(); |
254
|
|
|
if ($records) { |
255
|
|
|
foreach($records as $record) { |
256
|
|
|
$object = $record->getFields(); |
257
|
|
|
$object->Id = $record->getId(); |
258
|
|
|
$returnArray[] = $object; |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
if(self::$debug) { |
263
|
|
|
$connection->debug($result); |
264
|
|
|
} |
265
|
|
|
return $returnArray; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* @param string $email |
271
|
|
|
*/ |
272
|
|
|
public static function assert_email($email) |
273
|
|
|
{ |
274
|
|
|
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { |
275
|
|
|
throw new InvalidArgumentException('Wrong email address format'); |
276
|
|
|
} |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* |
281
|
|
|
* @var boolean |
282
|
|
|
*/ |
283
|
|
|
protected static $debug = true; |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* @param bool |
287
|
|
|
*/ |
288
|
|
|
public static function set_debug($b = true) |
289
|
|
|
{ |
290
|
|
|
self::$debug = $b; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
protected static $my_singleton_connection = null; |
294
|
|
|
|
295
|
|
|
protected static function get_my_singleton_connection() |
296
|
|
|
{ |
297
|
|
|
if(self::$my_singleton_connection === null){ |
298
|
|
|
self::$my_singleton_connection = MySalesforcePartnerApiConnectionOnly::singleton(); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
return self::$my_singleton_connection; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
|
305
|
|
|
} |
306
|
|
|
|