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
|
|
|
) |
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 static function update_email_subscriber( |
62
|
|
|
$email, |
63
|
|
|
$extraFields = [], |
64
|
|
|
$extraFilterArray = [] |
65
|
|
|
) |
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 string $email |
79
|
|
|
* @param array $extraFilterArray |
80
|
|
|
* |
81
|
|
|
* @return bool |
82
|
|
|
*/ |
83
|
|
|
public static function is_email_registered($email, $extraFilterArray = []) |
84
|
|
|
{ |
85
|
|
|
$fieldsArray = ['Email' => $email]; |
86
|
|
|
$subscriber = MySalesforceContactApi::retrieve_contact($fieldsArray, $extraFilterArray); |
87
|
|
|
|
88
|
|
|
return $subscriber ? true : false; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* todo: all nullify thingy |
94
|
|
|
* assumes that all fields are correct (e.g. correct email format) |
95
|
|
|
* can use email or phone as identifier |
96
|
|
|
* @param array $fieldsArray |
97
|
|
|
* @param array $extraFilterArray -independent additional filters for retrieving contact |
98
|
|
|
* |
99
|
|
|
* @return bool |
100
|
|
|
*/ |
101
|
|
|
public static function create_contact($fieldsArray, $extraFilterArray = []) |
102
|
|
|
{ |
103
|
|
|
$connection = self::get_my_singleton_connection(); |
104
|
|
|
//get defaults |
105
|
|
|
$extraFilterArray = array_merge( |
106
|
|
|
MySalesforceContactConfigApi::get_fields_for_filter(), |
107
|
|
|
$extraFilterArray |
108
|
|
|
); |
109
|
|
|
$fieldsArray = array_merge( |
110
|
|
|
MySalesforceContactConfigApi::get_fields_to_send_on_creation(), |
111
|
|
|
$fieldsArray, |
112
|
|
|
$extraFilterArray |
113
|
|
|
); |
114
|
|
|
$log = SalesforceContactLog::create_contact_log( |
115
|
|
|
'Created', |
116
|
|
|
$fieldsArray, |
117
|
|
|
$extraFilterArray |
118
|
|
|
); |
119
|
|
|
|
120
|
|
|
$existingContact = self::retrieve_contact($fieldsArray, $extraFilterArray); |
121
|
|
|
// Contact not found. Create a new Contact and set the details |
122
|
|
|
if ($existingContact) { |
123
|
|
|
|
124
|
|
|
//we are out of here! |
125
|
|
|
return true; |
126
|
|
|
} else { |
127
|
|
|
$contact = new SObject(); |
128
|
|
|
$contact->setType('Contact'); |
129
|
|
|
foreach ($fieldsArray as $fieldName => $fieldValue) { |
130
|
|
|
$contact->$fieldName = $fieldValue; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
//doing it! |
134
|
|
|
$error = ''; |
135
|
|
|
$response = null; |
|
|
|
|
136
|
|
|
try { |
137
|
|
|
$response = $connection->create([$contact]); |
138
|
|
|
} |
139
|
|
|
//catch exception |
140
|
|
|
catch(\Exception $e) { |
141
|
|
|
$error = $e->getMessage(); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
if(self::$debug) { |
145
|
|
|
$connection->debug($response); |
146
|
|
|
} |
147
|
|
|
$success = $log->confirmContactLog($response, $error); |
148
|
|
|
|
149
|
|
|
return $success; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* todo: all nullify thingy |
154
|
|
|
* assumes that all fields are correct (e.g. correct email format) |
155
|
|
|
* can use email or phone as identifier |
156
|
|
|
* @param array $fieldsArray |
157
|
|
|
* @param array $extraFilterArray -independent additional filters for retrieving contact |
158
|
|
|
* |
159
|
|
|
* @return bool |
160
|
|
|
*/ |
161
|
|
|
public static function update_contact($fieldsArray, $extraFilterArray = []) |
162
|
|
|
{ |
163
|
|
|
$connection = self::get_my_singleton_connection(); |
164
|
|
|
|
165
|
|
|
//add defaults |
166
|
|
|
$extraFilterArray = array_merge( |
167
|
|
|
MySalesforceContactConfigApi::get_fields_for_filter(), |
168
|
|
|
$extraFilterArray |
169
|
|
|
); |
170
|
|
|
$fieldsArray = array_merge( |
171
|
|
|
MySalesforceContactConfigApi::get_fields_to_send_on_update(), |
172
|
|
|
$fieldsArray, |
173
|
|
|
$extraFilterArray |
174
|
|
|
); |
175
|
|
|
$log = SalesforceContactLog::create_contact_log( |
176
|
|
|
'Updated', |
177
|
|
|
$fieldsArray, |
178
|
|
|
$extraFilterArray |
179
|
|
|
); |
180
|
|
|
|
181
|
|
|
//find existing contact |
182
|
|
|
$existingContact = self::retrieve_contact($fieldsArray, $extraFilterArray); |
183
|
|
|
// Contact found. Update Contact with details |
184
|
|
|
if ($existingContact) { |
185
|
|
|
$contact = new SObject(); |
186
|
|
|
$contact->setType('Contact'); |
187
|
|
|
$contact->setId($existingContact->getId()); |
188
|
|
|
foreach ($fieldsArray as $fieldName => $fieldValue) { |
189
|
|
|
if ($existingContact->$fieldName != $fieldValue) { |
190
|
|
|
$contact->$fieldName = $fieldValue; |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
//doing it! |
194
|
|
|
$error = ''; |
195
|
|
|
$response = null; |
|
|
|
|
196
|
|
|
try { |
197
|
|
|
$response = $connection->update([$contact]); |
198
|
|
|
} |
199
|
|
|
//catch exception |
200
|
|
|
catch(\Exception $e) { |
201
|
|
|
$error = $e->getMessage(); |
202
|
|
|
} |
203
|
|
|
} else { |
204
|
|
|
|
205
|
|
|
//we are out of here! |
206
|
|
|
return true; |
207
|
|
|
} |
208
|
|
|
if(self::$debug) { |
209
|
|
|
echo $error; |
210
|
|
|
$connection->debug($response); |
211
|
|
|
} |
212
|
|
|
$success = $log->confirmContactLog($response, $error); |
213
|
|
|
|
214
|
|
|
return $success; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Retrive a contact by email address or by phone number |
219
|
|
|
* either the email (preferred) of phone needs to be set in the $fieldsArray |
220
|
|
|
* |
221
|
|
|
* @param array $fieldsArray |
222
|
|
|
* @param array $extraFilterArray additional filter key-value sets |
223
|
|
|
* |
224
|
|
|
* @return SForce\SObject|null |
225
|
|
|
*/ |
226
|
|
|
public static function retrieve_contact($fieldsArray, $extraFilterArray = []) |
227
|
|
|
{ |
228
|
|
|
$connection = self::get_my_singleton_connection(); |
229
|
|
|
// Check for existing Contact |
230
|
|
|
$existingContact = null; |
231
|
|
|
$filterArray = []; |
232
|
|
|
$finalFilterArray = []; |
233
|
|
|
$result = null; |
234
|
|
|
|
235
|
|
|
$extraFilterArray = array_merge( |
236
|
|
|
MySalesforceContactConfigApi::get_fields_for_filter(), |
237
|
|
|
$extraFilterArray |
238
|
|
|
); |
239
|
|
|
|
240
|
|
|
$email = isset($fieldsArray['Email']) ? trim($fieldsArray['Email']) : null; |
241
|
|
|
if ($email) { |
242
|
|
|
$filterArray['Email'] = $email; |
243
|
|
|
} else { |
244
|
|
|
$phone = isset($fieldsArray['Phone']) ? trim($fieldsArray['Phone']) : null; |
245
|
|
|
if ($phone) { |
246
|
|
|
$filterArray['Phone'] = $phone; |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
if(count($filterArray)) { |
250
|
|
|
$finalFilterArray[] = MySalesforcePartnerApi::array2sql($filterArray); |
251
|
|
|
if(count($extraFilterArray)) { |
252
|
|
|
$finalFilterArray[] = MySalesforcePartnerApi::array2sql($extraFilterArray); |
253
|
|
|
} |
254
|
|
|
$where = ' ( '.implode(' ) AND ( ', $finalFilterArray).' ) '; |
255
|
|
|
$query = 'SELECT Id, FirstName, LastName, Phone, Email FROM Contact WHERE '.$where.' LIMIT 1'; |
256
|
|
|
|
257
|
|
|
$result = $connection->query($query); |
258
|
|
|
if ($result) { |
259
|
|
|
$contacts = $result->getRecords(); |
260
|
|
|
if ($contacts) { |
261
|
|
|
$existingContact = $contacts[0]; |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
} |
265
|
|
|
if(self::$debug) { |
266
|
|
|
$connection->debug($result); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
return $existingContact; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* |
274
|
|
|
* @return array |
275
|
|
|
*/ |
276
|
|
|
public static function retrieve_contact_record_types() |
277
|
|
|
{ |
278
|
|
|
$connection = self::get_my_singleton_connection(); |
279
|
|
|
$returnArray = []; |
280
|
|
|
|
281
|
|
|
$query = ' |
282
|
|
|
SELECT Id, Name, Description, DeveloperName, IsActive, sObjectType |
283
|
|
|
FROM RecordType |
284
|
|
|
WHERE IsActive = true AND sObjectType = \'Contact\''; |
285
|
|
|
|
286
|
|
|
$result = $connection->query($query); |
287
|
|
|
if($result) { |
288
|
|
|
$records = $result->getRecords(); |
289
|
|
|
if ($records) { |
290
|
|
|
foreach($records as $record) { |
291
|
|
|
$object = $record->getFields(); |
292
|
|
|
$object->Id = $record->getId(); |
293
|
|
|
$returnArray[] = $object; |
294
|
|
|
} |
295
|
|
|
} |
296
|
|
|
} |
297
|
|
|
if(self::$debug) { |
298
|
|
|
$connection->debug($result); |
299
|
|
|
} |
300
|
|
|
return $returnArray; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* @param string $email |
306
|
|
|
*/ |
307
|
|
|
public static function assert_email($email) |
308
|
|
|
{ |
309
|
|
|
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { |
310
|
|
|
throw new InvalidArgumentException('Wrong email address format'); |
311
|
|
|
} |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* |
316
|
|
|
* @var boolean |
317
|
|
|
*/ |
318
|
|
|
protected static $debug = false; |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* @param bool |
322
|
|
|
*/ |
323
|
|
|
public static function set_debug($b = true) |
324
|
|
|
{ |
325
|
|
|
self::$debug = $b; |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
protected static $my_singleton_connection = null; |
329
|
|
|
|
330
|
|
|
protected static function get_my_singleton_connection() |
331
|
|
|
{ |
332
|
|
|
if(self::$my_singleton_connection === null){ |
333
|
|
|
self::$my_singleton_connection = MySalesforcePartnerApiConnectionOnly::singleton(); |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
return self::$my_singleton_connection; |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
|
340
|
|
|
} |
341
|
|
|
|