1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* returns a bunch of form fields that inform the user about the configuration |
4
|
|
|
* of the connection and communication with Salesforce. |
5
|
|
|
*/ |
6
|
|
|
class MySalesforceContactConfigApi extends Object |
7
|
|
|
{ |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* |
11
|
|
|
* @var array |
12
|
|
|
*/ |
13
|
|
|
private static $site_wide_fields_to_send_on_creation = []; |
|
|
|
|
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
private static $site_wide_fields_to_send_on_update = []; |
|
|
|
|
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
private static $site_wide_filter_values = []; |
|
|
|
|
26
|
|
|
|
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
private static $run_time_fields_to_send_on_creation = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* |
36
|
|
|
* @param array|DataList|string $mixed fields to send |
37
|
|
|
* |
38
|
|
|
*/ |
39
|
|
|
public static function add_fields_to_send_on_creation($mixed) |
40
|
|
|
{ |
41
|
|
|
$array = self::mixed_to_array($mixed); |
|
|
|
|
42
|
|
|
|
43
|
|
|
self::$run_time_fields_to_send_on_creation += $array; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* |
48
|
|
|
* @var array |
49
|
|
|
*/ |
50
|
|
|
private static $run_time_fields_to_send_on_update = []; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* |
54
|
|
|
* @param array|DataList|string $mixed fields to send |
55
|
|
|
* |
56
|
|
|
* @return array |
57
|
|
|
*/ |
58
|
|
|
public static function add_fields_to_send_on_update($mixed) |
59
|
|
|
{ |
60
|
|
|
$array = self::mixed_to_array($mixed); |
|
|
|
|
61
|
|
|
|
62
|
|
|
self::$run_time_fields_to_send_on_update += $array; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* |
67
|
|
|
* @var array |
68
|
|
|
*/ |
69
|
|
|
private static $run_time_fields_for_filter = []; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* |
73
|
|
|
* @param array|DataList|string $mixed fields to send |
74
|
|
|
* |
75
|
|
|
* @return array |
76
|
|
|
*/ |
77
|
|
|
public static function add_fields_to_use_for_filter($mixed) |
78
|
|
|
{ |
79
|
|
|
$array = self::mixed_to_array($mixed); |
|
|
|
|
80
|
|
|
|
81
|
|
|
self::$run_time_fields_for_filter += $array; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* |
87
|
|
|
* @param array|DataList|null $mixed fields to send |
88
|
|
|
* |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
|
|
public static function get_fields_to_send_on_creation($mixed = null) |
92
|
|
|
{ |
93
|
|
|
$array = self::mixed_to_array($mixed); |
94
|
|
|
|
95
|
|
|
return array_merge( |
96
|
|
|
Config::inst()->get('SalesforceDefaultContactField', 'site_wide_fields_to_send_on_creation'), |
|
|
|
|
97
|
|
|
$array, |
98
|
|
|
self::$run_time_fields_to_send_on_creation |
99
|
|
|
); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* |
104
|
|
|
* @param array|DataList|null $mixed fields to send |
105
|
|
|
* |
106
|
|
|
* @return array |
107
|
|
|
*/ |
108
|
|
|
public static function get_fields_to_send_on_update($mixed = null) |
109
|
|
|
{ |
110
|
|
|
$array = self::mixed_to_array($mixed); |
111
|
|
|
|
112
|
|
|
return array_merge( |
113
|
|
|
Config::inst()->get('SalesforceDefaultContactField', 'site_wide_fields_to_send_on_update'), |
|
|
|
|
114
|
|
|
$array, |
115
|
|
|
self::$run_time_fields_to_send_on_update |
116
|
|
|
); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* |
121
|
|
|
* @param array|DataList|null $mixed fields to send |
122
|
|
|
* |
123
|
|
|
* @return array|DataList|null |
124
|
|
|
*/ |
125
|
|
|
public static function get_fields_for_filter($mixed = null) |
126
|
|
|
{ |
127
|
|
|
$array = self::mixed_to_array($mixed); |
128
|
|
|
|
129
|
|
|
return array_merge( |
130
|
|
|
Config::inst()->get('SalesforceDefaultContactField', 'site_wide_filter_values'), |
|
|
|
|
131
|
|
|
$array, |
132
|
|
|
self::$run_time_fields_for_filter |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* |
138
|
|
|
* @param DataList|array|null $mixed |
139
|
|
|
* @return array |
140
|
|
|
*/ |
141
|
|
|
protected static function mixed_to_array($mixed = null) |
142
|
|
|
{ |
143
|
|
|
if($mixed === null) { |
144
|
|
|
$array = []; |
145
|
|
|
} elseif(is_string($mixed)) { |
|
|
|
|
146
|
|
|
$array = [ $mixed ]; |
147
|
|
|
} |
148
|
|
|
elseif($mixed instanceof DataList) { |
149
|
|
|
$array = []; |
150
|
|
|
foreach($mixed as $object) { |
151
|
|
|
$array[trim($object->Field)] = $object->BetterValue(); |
152
|
|
|
} |
153
|
|
|
} elseif(! is_array($array)) { |
|
|
|
|
154
|
|
|
user_error('Variable '.vardump($mixed).'Should be an array'); |
|
|
|
|
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return $array; |
|
|
|
|
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public static function login_details_field( |
161
|
|
|
$title = 'Account Used', |
162
|
|
|
$fieldName = 'SalesforceLoginDetailsField' |
163
|
|
|
) |
164
|
|
|
{ |
165
|
|
|
$userName = Config::inst()->get('MySalesforcePartnerApiConnectionOnly', 'username'); |
166
|
|
|
$password = Config::inst()->get('MySalesforcePartnerApiConnectionOnly', 'password'); |
167
|
|
|
$security_token = Config::inst()->get('MySalesforcePartnerApiConnectionOnly', 'security_token'); |
168
|
|
|
$wsdl_location = Config::inst()->get('MySalesforcePartnerApiConnectionOnly', 'wsdl_location'); |
169
|
|
|
$array = [ |
170
|
|
|
'UserName' => $userName, |
171
|
|
|
'Password' => substr($password, 0, 2) . '...'.substr($password, -2), |
|
|
|
|
172
|
|
|
'Token' => substr($security_token, 0, 2) . '...'.substr($security_token, -2), |
173
|
|
|
'WSDLLocation' => $wsdl_location, |
174
|
|
|
]; |
175
|
|
|
return LiteralField::create( |
176
|
|
|
$fieldName, |
177
|
|
|
'<h2>'.$title.'</h2>'. |
178
|
|
|
self::array_to_html($array) |
179
|
|
|
); |
180
|
|
|
} |
181
|
|
|
/** |
182
|
|
|
* Field with list of contact record types shown |
183
|
|
|
* @param string $fieldName |
184
|
|
|
* @param string $title |
185
|
|
|
* |
186
|
|
|
* @return LiteralField |
187
|
|
|
*/ |
188
|
|
|
public static function list_of_contact_record_types_field( |
189
|
|
|
$fieldName = 'ListOfContactRecordTypes', |
|
|
|
|
190
|
|
|
$title = 'Contact Record Types Available' |
191
|
|
|
) |
192
|
|
|
{ |
193
|
|
|
$data = MySalesforcePartnerApi::retrieve_contact_record_types(); |
|
|
|
|
194
|
|
|
|
195
|
|
|
return LiteralField::create( |
196
|
|
|
'ListOfContactRecordTypes', |
197
|
|
|
'<h2>'.$title.'</h2>'. |
198
|
|
|
'<pre>'.print_r($data).'</pre>' |
199
|
|
|
); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Needs to link to a many-many relationship (SalesforceDefaultContactFields) |
204
|
|
|
* @param array $array fields to send |
205
|
|
|
* |
206
|
|
|
* @return CheckboxSetField |
207
|
|
|
*/ |
208
|
|
|
public static function select_default_contact_fields_field($fieldName, $title, $desc = '') |
209
|
|
|
{ |
210
|
|
|
$count = SalesforceDefaultContactField::get()->count(); |
211
|
|
|
if($count === 0) { |
212
|
|
|
return HiddenField::create( |
|
|
|
|
213
|
|
|
$fieldName, |
214
|
|
|
$title |
215
|
|
|
); |
216
|
|
|
} |
217
|
|
|
return CheckboxSetField::create( |
218
|
|
|
$fieldName, |
219
|
|
|
$title, |
220
|
|
|
SalesforceDefaultContactField::get()->map()->toArray() |
221
|
|
|
) |
222
|
|
|
->setDescription(' |
223
|
|
|
'.$desc.' |
224
|
|
|
<br /> |
225
|
|
|
You can |
226
|
|
|
<a href="/admin/salesforceadmin/'.SalesforceDefaultContactField::class.'/">Add or Edit the options</a> |
227
|
|
|
as required. Please change with care. |
228
|
|
|
'); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* |
233
|
|
|
* @param array|DataList|null $array fields to send |
234
|
|
|
* @param string $fieldName fields to send |
235
|
|
|
* @param string $title fields to send |
236
|
|
|
* |
237
|
|
|
* @return LiteralField |
238
|
|
|
*/ |
239
|
|
|
public static function fields_to_send_on_field( |
240
|
|
|
$type, |
241
|
|
|
$mixed = null, |
242
|
|
|
$fieldName = 'FieldsToSendToSalesforce', |
243
|
|
|
$title = 'Default Fields for' |
244
|
|
|
) |
245
|
|
|
{ |
246
|
|
|
$fieldName .= ucfirst($type); |
247
|
|
|
$title .= ' '.ucfirst($type); |
248
|
|
|
|
249
|
|
|
switch( $type ) { |
250
|
|
|
case 'create': |
251
|
|
|
$array = self::get_fields_to_send_on_creation($mixed); |
252
|
|
|
break; |
253
|
|
|
case 'update': |
254
|
|
|
$array = self::get_fields_to_send_on_update($mixed); |
255
|
|
|
break; |
256
|
|
|
case 'filter': |
257
|
|
|
$array = self::get_fields_for_filter($mixed); |
258
|
|
|
break; |
259
|
|
|
default: |
260
|
|
|
user_error('type needs to be create, update or filter - currently set to: '.$type); |
261
|
|
|
break; |
262
|
|
|
} |
263
|
|
|
return LiteralField::create( |
264
|
|
|
$fieldName, |
265
|
|
|
'<h2>'.$title.'</h2>'. |
266
|
|
|
self::array_to_html($array) |
|
|
|
|
267
|
|
|
); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* |
272
|
|
|
* @param [type] $array [description] |
|
|
|
|
273
|
|
|
* @return [type] [description] |
|
|
|
|
274
|
|
|
*/ |
275
|
|
|
protected static function array_to_html($array) : string |
276
|
|
|
{ |
277
|
|
|
$htmlArray = []; |
278
|
|
|
foreach($array as $field => $value) { |
279
|
|
|
$htmlArray[] = $field.' = '.$value.' ('.gettype($value).')'; |
280
|
|
|
} |
281
|
|
|
if(count($htmlArray) == 0) { |
282
|
|
|
$htmlArray[] = 'none'; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
return '<p>- '.implode('</p><p> - ', $htmlArray).'</p>'; |
286
|
|
|
} |
287
|
|
|
} |
288
|
|
|
|