|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use SForce\Client\Partner; |
|
4
|
|
|
|
|
5
|
|
|
use SForce\SObject; |
|
6
|
|
|
use SForce\Wsdl\SaveResult; |
|
7
|
|
|
|
|
8
|
|
|
class MySalesForcePartnerAPI extends Partner |
|
9
|
|
|
{ |
|
10
|
|
|
|
|
11
|
|
|
protected $connection = null; |
|
12
|
|
|
|
|
13
|
|
|
public function __construct() |
|
14
|
|
|
{ |
|
15
|
|
|
parent::__construct(); |
|
16
|
|
|
$this->connection = MySalesForcePartnerAPIConnectionOnly::singleton(); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* todo: all nullify thingy |
|
21
|
|
|
* assumes that all fields are correct (e.g. correct email format) |
|
22
|
|
|
* can use email or phone as identifier |
|
23
|
|
|
* @param array $fieldsArray |
|
24
|
|
|
* @param array $extraFilterArray -independent additional filters for retrieving contact |
|
25
|
|
|
* |
|
26
|
|
|
* @return SForce\Wsdl\SaveResult|null |
|
27
|
|
|
*/ |
|
28
|
|
|
public static function create_contact($fieldsArray, $extraFilterArray = []) |
|
29
|
|
|
{ |
|
30
|
|
|
$response = null; |
|
|
|
|
|
|
31
|
|
|
$existingContact = self::array2sql($fieldsArray, $extraFilterArray); |
|
|
|
|
|
|
32
|
|
|
// Contact not found. Create a new Contact and set the details |
|
33
|
|
|
if ($existingContact) { |
|
34
|
|
|
return null; |
|
35
|
|
|
} else { |
|
36
|
|
|
$contact = new SObject(); |
|
37
|
|
|
$contact->setType('Contact'); |
|
38
|
|
|
foreach ($fieldsArray as $fieldName => $fieldValue) { |
|
39
|
|
|
$contact->$fieldName = $fieldValue; |
|
40
|
|
|
} |
|
41
|
|
|
$response = $this->connection->create([$contact]); |
|
|
|
|
|
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
return $response; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* todo: all nullify thingy |
|
49
|
|
|
* assumes that all fields are correct (e.g. correct email format) |
|
50
|
|
|
* can use email or phone as identifier |
|
51
|
|
|
* @param array $fieldsArray |
|
52
|
|
|
* @param array $extraFilterArray -independent additional filters for retrieving contact |
|
53
|
|
|
* |
|
54
|
|
|
* @return SForce\Wsdl\SaveResult|null |
|
55
|
|
|
*/ |
|
56
|
|
|
public static function update_contact($fieldsArray, $extraFilterArray = []) |
|
57
|
|
|
{ |
|
58
|
|
|
$response = null; |
|
|
|
|
|
|
59
|
|
|
$existingContact = self::array2sql($fieldsArray, $extraFilterArray); |
|
|
|
|
|
|
60
|
|
|
// Contact found. Update Contact with details |
|
61
|
|
|
if ($existingContact) { |
|
62
|
|
|
$contact = new SObject(); |
|
63
|
|
|
$contact->setType('Contact'); |
|
64
|
|
|
$contact->setId($existingContact->getId()); |
|
65
|
|
|
foreach ($fieldsArray as $fieldName => $fieldValue) { |
|
66
|
|
|
if ($existingContact->$fieldName != $fieldValue) { |
|
67
|
|
|
$contact->$fieldName = $fieldValue; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
$response = $this->connection->update([$contact]); |
|
|
|
|
|
|
71
|
|
|
} else { |
|
72
|
|
|
return null; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return $response; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Retrive a contact by email address or by phone number |
|
80
|
|
|
* either the email (preferred) of phone needs to be set in the $fieldsArray |
|
81
|
|
|
* |
|
82
|
|
|
* @param array $fieldsArray |
|
83
|
|
|
* @param array $extraFilterArray additional filter key-value sets |
|
84
|
|
|
* |
|
85
|
|
|
* @return SForce\SObject|null |
|
86
|
|
|
*/ |
|
87
|
|
|
public static function retrieve_contact($fieldsArray, $extraFilterArray = []) |
|
88
|
|
|
{ |
|
89
|
|
|
// Check for existing Contact |
|
90
|
|
|
$existingContact = null; |
|
91
|
|
|
$filterArray = []; |
|
92
|
|
|
$finalFilterArray = []; |
|
93
|
|
|
|
|
94
|
|
|
$email = isset($fieldsArray['Email']) ? trim($fieldsArray['Email']) : null; |
|
95
|
|
|
if ($email) { |
|
96
|
|
|
$filterArray['Email'] = $email; |
|
97
|
|
|
} else { |
|
98
|
|
|
$phone = isset($fieldsArray['Phone']) ? trim($fieldsArray['Phone']) : null; |
|
99
|
|
|
if ($phone) { |
|
100
|
|
|
$filterArray['Phone'] = $phone; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
if(count($filterArray)) { |
|
104
|
|
|
$finalFilterArray[] = self::array2sql($filterArray); |
|
105
|
|
|
if(count($extraFilterArray)) { |
|
106
|
|
|
$finalFilterArray[] = self::array2sql($extraFilterArray); |
|
107
|
|
|
} |
|
108
|
|
|
$where = ' ( '.implode(' ) AND ( ', $finalFilterArray).' ) '; |
|
109
|
|
|
$query = 'SELECT Id, FirstName, LastName, Phone, Email FROM Contact WHERE '.$where.' LIMIT 1'; |
|
110
|
|
|
|
|
111
|
|
|
$result = $this->connection->query($query); |
|
|
|
|
|
|
112
|
|
|
if ($result) { |
|
|
|
|
|
|
113
|
|
|
$contacts = $result->getRecords(); |
|
114
|
|
|
if ($contacts) { |
|
|
|
|
|
|
115
|
|
|
$existingContact = $contacts[0]; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
return $existingContact; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* turns key value pairs into string |
|
125
|
|
|
* @param array $array [description] |
|
126
|
|
|
* @return string |
|
127
|
|
|
*/ |
|
128
|
|
|
protected static function array2sql($array) : string |
|
129
|
|
|
{ |
|
130
|
|
|
if(count($array) === 0) { |
|
131
|
|
|
user_error('must have at least one entry'); |
|
132
|
|
|
} |
|
133
|
|
|
$inner = []; |
|
134
|
|
|
foreach($array as $field => $value) { |
|
135
|
|
|
$inner[$field] = $field.' = '.Convert::raw2sql($value, true); |
|
|
|
|
|
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
return implode(' AND ', $inner); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
public function debug() |
|
142
|
|
|
{ |
|
143
|
|
|
$xml = $this->getLastRequest(); |
|
144
|
|
|
|
|
145
|
|
|
if (! $xml) { |
|
146
|
|
|
return null; |
|
147
|
|
|
} |
|
148
|
|
|
$domxml = new \DOMDocument('1.0'); |
|
149
|
|
|
$domxml->preserveWhiteSpace = false; |
|
150
|
|
|
$domxml->formatOutput = true; |
|
151
|
|
|
/* @var $xml SimpleXMLElement */ |
|
152
|
|
|
$domxml->loadXML($xml); |
|
153
|
|
|
echo $domxml->saveXML(); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
##################################### |
|
157
|
|
|
# overriding parent class to fix bugs |
|
158
|
|
|
# in the parent class. |
|
159
|
|
|
##################################### |
|
160
|
|
|
/** |
|
161
|
|
|
* Adds one or more new individual objects to your organization's data. |
|
162
|
|
|
* |
|
163
|
|
|
* @param SObject[] $sObjects Array of one or more sObjects (up to 200) to create. |
|
164
|
|
|
* @param null|string $type Unused |
|
165
|
|
|
* |
|
166
|
|
|
* @return SaveResult |
|
167
|
|
|
*/ |
|
168
|
|
|
public function create($sObjects, $type = null) |
|
169
|
|
|
{ |
|
170
|
|
|
foreach ($sObjects as $sObject) { |
|
171
|
|
|
if (property_exists($sObject, 'fields')) { |
|
172
|
|
|
$sObject->setAny($this->_convertToAny($sObject->getFields())); |
|
|
|
|
|
|
173
|
|
|
// print_r($this->_convertToAny($sObject->getFields())); |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
$createObject = new SForce\Wsdl\create($sObjects); |
|
|
|
|
|
|
177
|
|
|
|
|
178
|
|
|
return parent::_create($createObject); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
##################################### |
|
182
|
|
|
# overriding parent class to fix bugs |
|
183
|
|
|
# in the parent class. |
|
184
|
|
|
##################################### |
|
185
|
|
|
/** |
|
186
|
|
|
* Updates one or more new individual objects to your organization's data. |
|
187
|
|
|
* |
|
188
|
|
|
* @param SObject[] $sObjects Array of one or more sObjects (up to 200) to update. |
|
189
|
|
|
* @param null|string $type Unused |
|
190
|
|
|
* |
|
191
|
|
|
* @return SaveResult |
|
192
|
|
|
*/ |
|
193
|
|
|
public function update($sObjects, $type = null) |
|
|
|
|
|
|
194
|
|
|
{ |
|
195
|
|
|
foreach ($sObjects as $sObject) { |
|
196
|
|
|
if (property_exists($sObject, 'fields')) { |
|
197
|
|
|
$sObject->setAny($this->_convertToAny($sObject->getFields())); |
|
|
|
|
|
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
$updateObject = new SForce\Wsdl\update($sObjects); |
|
|
|
|
|
|
201
|
|
|
|
|
202
|
|
|
return parent::_update($updateObject); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* NOTA BENE: |
|
207
|
|
|
* added here because Describe Layout occurs two times (double) with capital D |
|
208
|
|
|
* (DescribeLayout) and without capital D (changed to describeLayoutDouble) |
|
209
|
|
|
* |
|
210
|
|
|
* Use describeLayoutDouble to retrieve information about the layout (presentation |
|
211
|
|
|
* of data to users) for a given object type. The describeLayoutDouble call returns |
|
212
|
|
|
* metadata about a given page layout, including layouts for edit and |
|
213
|
|
|
* display-only views and record type mappings. Note that field-level security |
|
214
|
|
|
* and layout editability affects which fields appear in a layout. |
|
215
|
|
|
* |
|
216
|
|
|
* @param string $type Object Type |
|
217
|
|
|
* @param array $recordTypeIds |
|
218
|
|
|
* |
|
219
|
|
|
* @return Wsdl\DescribeLayoutResult |
|
|
|
|
|
|
220
|
|
|
*/ |
|
221
|
|
|
public function describeLayoutDouble($type, array $recordTypeIds = []) |
|
222
|
|
|
{ |
|
223
|
|
|
$this->setHeaders(static::CALL_DESCRIBE_LAYOUT); |
|
224
|
|
|
|
|
225
|
|
|
return $this->sforce |
|
|
|
|
|
|
226
|
|
|
->describeLayoutDouble(new Wsdl\describeLayoutDouble($type, $recordTypeIds)) |
|
|
|
|
|
|
227
|
|
|
->getResult(); |
|
228
|
|
|
} |
|
229
|
|
|
} |
|
230
|
|
|
|