Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 | 2 | /* |
bc77d7c0 TO |
3 | +--------------------------------------------------------------------+ |
4 | | Copyright CiviCRM LLC. All rights reserved. | | |
5 | | | | |
6 | | This work is published under the GNU AGPLv3 license with some | | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
9 | +--------------------------------------------------------------------+ | |
e70a7fc0 | 10 | */ |
6a488035 | 11 | |
50bfb460 SB |
12 | /** |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
50bfb460 SB |
16 | */ |
17 | ||
6a488035 TO |
18 | /* |
19 | * These functions have been deprecated out of API v3 Utils folder as they are not part of the | |
20 | * API. Calling API functions directly is not supported & these functions are not called by any | |
21 | * part of the API so are not really part of the api | |
22 | * | |
23 | */ | |
24 | ||
6a488035 TO |
25 | require_once 'api/v3/utils.php'; |
26 | ||
6a488035 | 27 | /** |
ea3ddccf | 28 | * Check duplicate contacts based on de-dupe parameters. |
29 | * | |
30 | * @param array $params | |
31 | * | |
32 | * @return array | |
6a488035 TO |
33 | */ |
34 | function _civicrm_api3_deprecated_check_contact_dedupe($params) { | |
35 | static $cIndieFields = NULL; | |
36 | static $defaultLocationId = NULL; | |
37 | ||
38 | $contactType = $params['contact_type']; | |
39 | if ($cIndieFields == NULL) { | |
40 | require_once 'CRM/Contact/BAO/Contact.php'; | |
41 | $cTempIndieFields = CRM_Contact_BAO_Contact::importableFields($contactType); | |
42 | $cIndieFields = $cTempIndieFields; | |
43 | ||
44 | require_once "CRM/Core/BAO/LocationType.php"; | |
45 | $defaultLocation = CRM_Core_BAO_LocationType::getDefault(); | |
46 | ||
50bfb460 | 47 | // set the value to default location id else set to 1 |
e7292422 | 48 | if (!$defaultLocationId = (int) $defaultLocation->id) { |
6a488035 TO |
49 | $defaultLocationId = 1; |
50 | } | |
51 | } | |
52 | ||
53 | require_once 'CRM/Contact/BAO/Query.php'; | |
54 | $locationFields = CRM_Contact_BAO_Query::$_locationSpecificFields; | |
55 | ||
be2fb01f | 56 | $contactFormatted = []; |
6a488035 TO |
57 | foreach ($params as $key => $field) { |
58 | if ($field == NULL || $field === '') { | |
59 | continue; | |
60 | } | |
6ce097e8 JF |
61 | // CRM-17040, Considering only primary contact when importing contributions. So contribution inserts into primary contact |
62 | // instead of soft credit contact. | |
63 | if (is_array($field) && $key != "soft_credit") { | |
6a488035 TO |
64 | foreach ($field as $value) { |
65 | $break = FALSE; | |
66 | if (is_array($value)) { | |
67 | foreach ($value as $name => $testForEmpty) { | |
68 | if ($name !== 'phone_type' && | |
69 | ($testForEmpty === '' || $testForEmpty == NULL) | |
70 | ) { | |
71 | $break = TRUE; | |
72 | break; | |
73 | } | |
74 | } | |
75 | } | |
76 | else { | |
77 | $break = TRUE; | |
78 | } | |
79 | if (!$break) { | |
80 | _civicrm_api3_deprecated_add_formatted_param($value, $contactFormatted); | |
81 | } | |
82 | } | |
83 | continue; | |
84 | } | |
85 | ||
be2fb01f | 86 | $value = [$key => $field]; |
6a488035 TO |
87 | |
88 | // check if location related field, then we need to add primary location type | |
89 | if (in_array($key, $locationFields)) { | |
90 | $value['location_type_id'] = $defaultLocationId; | |
91 | } | |
92 | elseif (array_key_exists($key, $cIndieFields)) { | |
93 | $value['contact_type'] = $contactType; | |
94 | } | |
95 | ||
96 | _civicrm_api3_deprecated_add_formatted_param($value, $contactFormatted); | |
97 | } | |
98 | ||
99 | $contactFormatted['contact_type'] = $contactType; | |
100 | ||
101 | return _civicrm_api3_deprecated_duplicate_formatted_contact($contactFormatted); | |
102 | } | |
103 | ||
104 | /** | |
105 | * take the input parameter list as specified in the data model and | |
106 | * convert it into the same format that we use in QF and BAO object | |
107 | * | |
77855840 TO |
108 | * @param array $params |
109 | * Associative array of property name/value. | |
6a488035 | 110 | * pairs to insert in new contact. |
77855840 TO |
111 | * @param array $values |
112 | * The reformatted properties that we can use internally. | |
6a488035 | 113 | * |
f4aaa82a | 114 | * @param array|bool $create Is the formatted Values array going to |
6a488035 TO |
115 | * be used for CRM_Activity_BAO_Activity::create() |
116 | * | |
117 | * @return array|CRM_Error | |
6a488035 TO |
118 | */ |
119 | function _civicrm_api3_deprecated_activity_formatted_param(&$params, &$values, $create = FALSE) { | |
120 | // copy all the activity fields as is | |
121 | $fields = CRM_Activity_DAO_Activity::fields(); | |
122 | _civicrm_api3_store_values($fields, $params, $values); | |
123 | ||
124 | require_once 'CRM/Core/OptionGroup.php'; | |
125 | $customFields = CRM_Core_BAO_CustomField::getFields('Activity'); | |
126 | ||
127 | foreach ($params as $key => $value) { | |
128 | // ignore empty values or empty arrays etc | |
129 | if (CRM_Utils_System::isNull($value)) { | |
130 | continue; | |
131 | } | |
132 | ||
133 | //Handling Custom Data | |
134 | if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) { | |
135 | $values[$key] = $value; | |
136 | $type = $customFields[$customFieldID]['html_type']; | |
137 | if ($type == 'CheckBox' || $type == 'Multi-Select') { | |
91bb24a7 | 138 | $mulValues = explode(',', $value); |
6a488035 | 139 | $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE); |
be2fb01f | 140 | $values[$key] = []; |
6a488035 TO |
141 | foreach ($mulValues as $v1) { |
142 | foreach ($customOption as $customValueID => $customLabel) { | |
143 | $customValue = $customLabel['value']; | |
144 | if ((strtolower(trim($customLabel['label'])) == strtolower(trim($v1))) || | |
145 | (strtolower(trim($customValue)) == strtolower(trim($v1))) | |
146 | ) { | |
147 | if ($type == 'CheckBox') { | |
148 | $values[$key][$customValue] = 1; | |
149 | } | |
150 | else { | |
151 | $values[$key][] = $customValue; | |
152 | } | |
153 | } | |
154 | } | |
155 | } | |
156 | } | |
157 | elseif ($type == 'Select' || $type == 'Radio') { | |
158 | $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE); | |
159 | foreach ($customOption as $customFldID => $customValue) { | |
91bb24a7 | 160 | $val = CRM_Utils_Array::value('value', $customValue); |
6a488035 TO |
161 | $label = CRM_Utils_Array::value('label', $customValue); |
162 | $label = strtolower($label); | |
163 | $value = strtolower(trim($value)); | |
164 | if (($value == $label) || ($value == strtolower($val))) { | |
165 | $values[$key] = $val; | |
166 | } | |
167 | } | |
168 | } | |
169 | } | |
170 | ||
171 | if ($key == 'target_contact_id') { | |
172 | if (!CRM_Utils_Rule::integer($value)) { | |
173 | return civicrm_api3_create_error("contact_id not valid: $value"); | |
174 | } | |
175 | $contactID = CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_contact WHERE id = $value"); | |
176 | if (!$contactID) { | |
177 | return civicrm_api3_create_error("Invalid Contact ID: There is no contact record with contact_id = $value."); | |
178 | } | |
179 | } | |
180 | } | |
181 | return NULL; | |
182 | } | |
183 | ||
184 | /** | |
185 | * This function adds the contact variable in $values to the | |
186 | * parameter list $params. For most cases, $values should have length 1. If | |
187 | * the variable being added is a child of Location, a location_type_id must | |
188 | * also be included. If it is a child of phone, a phone_type must be included. | |
189 | * | |
77855840 TO |
190 | * @param array $values |
191 | * The variable(s) to be added. | |
192 | * @param array $params | |
193 | * The structured parameter list. | |
6a488035 TO |
194 | * |
195 | * @return bool|CRM_Utils_Error | |
6a488035 TO |
196 | */ |
197 | function _civicrm_api3_deprecated_add_formatted_param(&$values, &$params) { | |
50bfb460 SB |
198 | // Crawl through the possible classes: |
199 | // Contact | |
200 | // Individual | |
201 | // Household | |
202 | // Organization | |
203 | // Location | |
204 | // Address | |
205 | ||
206 | // Phone | |
207 | // IM | |
208 | // Note | |
209 | // Custom | |
210 | ||
211 | // Cache the various object fields | |
6a488035 TO |
212 | static $fields = NULL; |
213 | ||
214 | if ($fields == NULL) { | |
be2fb01f | 215 | $fields = []; |
6a488035 TO |
216 | } |
217 | ||
50bfb460 | 218 | // first add core contact values since for other Civi modules they are not added |
6a488035 TO |
219 | require_once 'CRM/Contact/BAO/Contact.php'; |
220 | $contactFields = CRM_Contact_DAO_Contact::fields(); | |
221 | _civicrm_api3_store_values($contactFields, $values, $params); | |
222 | ||
223 | if (isset($values['contact_type'])) { | |
50bfb460 | 224 | // we're an individual/household/org property |
6a488035 | 225 | |
6a488035 TO |
226 | $fields[$values['contact_type']] = CRM_Contact_DAO_Contact::fields(); |
227 | ||
228 | _civicrm_api3_store_values($fields[$values['contact_type']], $values, $params); | |
229 | return TRUE; | |
230 | } | |
231 | ||
232 | if (isset($values['individual_prefix'])) { | |
a7488080 | 233 | if (!empty($params['prefix_id'])) { |
91bb24a7 | 234 | $prefixes = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'prefix_id'); |
6a488035 TO |
235 | $params['prefix'] = $prefixes[$params['prefix_id']]; |
236 | } | |
237 | else { | |
238 | $params['prefix'] = $values['individual_prefix']; | |
239 | } | |
240 | return TRUE; | |
241 | } | |
242 | ||
243 | if (isset($values['individual_suffix'])) { | |
a7488080 | 244 | if (!empty($params['suffix_id'])) { |
91bb24a7 | 245 | $suffixes = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'suffix_id'); |
6a488035 TO |
246 | $params['suffix'] = $suffixes[$params['suffix_id']]; |
247 | } | |
248 | else { | |
249 | $params['suffix'] = $values['individual_suffix']; | |
250 | } | |
251 | return TRUE; | |
252 | } | |
253 | ||
50bfb460 | 254 | // CRM-4575 |
6a488035 | 255 | if (isset($values['email_greeting'])) { |
a7488080 | 256 | if (!empty($params['email_greeting_id'])) { |
be2fb01f | 257 | $emailGreetingFilter = [ |
91bb24a7 | 258 | 'contact_type' => CRM_Utils_Array::value('contact_type', $params), |
6a488035 | 259 | 'greeting_type' => 'email_greeting', |
be2fb01f | 260 | ]; |
6a488035 TO |
261 | $emailGreetings = CRM_Core_PseudoConstant::greeting($emailGreetingFilter); |
262 | $params['email_greeting'] = $emailGreetings[$params['email_greeting_id']]; | |
263 | } | |
264 | else { | |
265 | $params['email_greeting'] = $values['email_greeting']; | |
266 | } | |
267 | ||
268 | return TRUE; | |
269 | } | |
270 | ||
271 | if (isset($values['postal_greeting'])) { | |
a7488080 | 272 | if (!empty($params['postal_greeting_id'])) { |
be2fb01f | 273 | $postalGreetingFilter = [ |
91bb24a7 | 274 | 'contact_type' => CRM_Utils_Array::value('contact_type', $params), |
6a488035 | 275 | 'greeting_type' => 'postal_greeting', |
be2fb01f | 276 | ]; |
6a488035 TO |
277 | $postalGreetings = CRM_Core_PseudoConstant::greeting($postalGreetingFilter); |
278 | $params['postal_greeting'] = $postalGreetings[$params['postal_greeting_id']]; | |
279 | } | |
280 | else { | |
281 | $params['postal_greeting'] = $values['postal_greeting']; | |
282 | } | |
283 | return TRUE; | |
284 | } | |
285 | ||
286 | if (isset($values['addressee'])) { | |
a7488080 | 287 | if (!empty($params['addressee_id'])) { |
be2fb01f | 288 | $addresseeFilter = [ |
91bb24a7 | 289 | 'contact_type' => CRM_Utils_Array::value('contact_type', $params), |
6a488035 | 290 | 'greeting_type' => 'addressee', |
be2fb01f | 291 | ]; |
6a488035 TO |
292 | $addressee = CRM_Core_PseudoConstant::addressee($addresseeFilter); |
293 | $params['addressee'] = $addressee[$params['addressee_id']]; | |
294 | } | |
295 | else { | |
296 | $params['addressee'] = $values['addressee']; | |
297 | } | |
298 | return TRUE; | |
299 | } | |
300 | ||
301 | if (isset($values['gender'])) { | |
a7488080 | 302 | if (!empty($params['gender_id'])) { |
91bb24a7 | 303 | $genders = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id'); |
6a488035 TO |
304 | $params['gender'] = $genders[$params['gender_id']]; |
305 | } | |
306 | else { | |
307 | $params['gender'] = $values['gender']; | |
308 | } | |
309 | return TRUE; | |
310 | } | |
311 | ||
df5ad245 | 312 | if (!empty($values['preferred_communication_method'])) { |
be2fb01f | 313 | $comm = []; |
91bb24a7 | 314 | $pcm = array_change_key_case(array_flip(CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'preferred_communication_method')), CASE_LOWER); |
6a488035 TO |
315 | |
316 | $preffComm = explode(',', $values['preferred_communication_method']); | |
317 | foreach ($preffComm as $v) { | |
318 | $v = strtolower(trim($v)); | |
319 | if (array_key_exists($v, $pcm)) { | |
320 | $comm[$pcm[$v]] = 1; | |
321 | } | |
322 | } | |
323 | ||
324 | $params['preferred_communication_method'] = $comm; | |
325 | return TRUE; | |
326 | } | |
327 | ||
50bfb460 | 328 | // format the website params. |
a7488080 | 329 | if (!empty($values['url'])) { |
6a488035 TO |
330 | static $websiteFields; |
331 | if (!is_array($websiteFields)) { | |
332 | require_once 'CRM/Core/DAO/Website.php'; | |
333 | $websiteFields = CRM_Core_DAO_Website::fields(); | |
334 | } | |
335 | if (!array_key_exists('website', $params) || | |
336 | !is_array($params['website']) | |
337 | ) { | |
be2fb01f | 338 | $params['website'] = []; |
6a488035 TO |
339 | } |
340 | ||
341 | $websiteCount = count($params['website']); | |
342 | _civicrm_api3_store_values($websiteFields, $values, | |
343 | $params['website'][++$websiteCount] | |
344 | ); | |
345 | ||
346 | return TRUE; | |
347 | } | |
348 | ||
349 | // get the formatted location blocks into params - w/ 3.0 format, CRM-4605 | |
a7488080 | 350 | if (!empty($values['location_type_id'])) { |
c193e786 | 351 | static $fields = NULL; |
352 | if ($fields == NULL) { | |
be2fb01f | 353 | $fields = []; |
c193e786 | 354 | } |
355 | ||
be2fb01f | 356 | foreach ([ |
6714d8d2 SL |
357 | 'Phone', |
358 | 'Email', | |
359 | 'IM', | |
360 | 'OpenID', | |
361 | 'Phone_Ext', | |
362 | ] as $block) { | |
c193e786 | 363 | $name = strtolower($block); |
364 | if (!array_key_exists($name, $values)) { | |
365 | continue; | |
366 | } | |
367 | ||
368 | if ($name == 'phone_ext') { | |
369 | $block = 'Phone'; | |
370 | } | |
371 | ||
372 | // block present in value array. | |
373 | if (!array_key_exists($name, $params) || !is_array($params[$name])) { | |
be2fb01f | 374 | $params[$name] = []; |
c193e786 | 375 | } |
376 | ||
377 | if (!array_key_exists($block, $fields)) { | |
378 | $className = "CRM_Core_DAO_$block"; | |
379 | $fields[$block] =& $className::fields(); | |
380 | } | |
381 | ||
382 | $blockCnt = count($params[$name]); | |
383 | ||
384 | // copy value to dao field name. | |
385 | if ($name == 'im') { | |
386 | $values['name'] = $values[$name]; | |
387 | } | |
388 | ||
389 | _civicrm_api3_store_values($fields[$block], $values, | |
390 | $params[$name][++$blockCnt] | |
391 | ); | |
392 | ||
393 | if (empty($params['id']) && ($blockCnt == 1)) { | |
394 | $params[$name][$blockCnt]['is_primary'] = TRUE; | |
395 | } | |
396 | ||
397 | // we only process single block at a time. | |
398 | return TRUE; | |
399 | } | |
400 | ||
401 | // handle address fields. | |
402 | if (!array_key_exists('address', $params) || !is_array($params['address'])) { | |
be2fb01f | 403 | $params['address'] = []; |
c193e786 | 404 | } |
405 | ||
406 | $addressCnt = 1; | |
407 | foreach ($params['address'] as $cnt => $addressBlock) { | |
408 | if (CRM_Utils_Array::value('location_type_id', $values) == | |
409 | CRM_Utils_Array::value('location_type_id', $addressBlock) | |
410 | ) { | |
411 | $addressCnt = $cnt; | |
412 | break; | |
413 | } | |
414 | $addressCnt++; | |
415 | } | |
416 | ||
417 | if (!array_key_exists('Address', $fields)) { | |
418 | $fields['Address'] = CRM_Core_DAO_Address::fields(); | |
419 | } | |
420 | ||
421 | // Note: we doing multiple value formatting here for address custom fields, plus putting into right format. | |
422 | // The actual formatting (like date, country ..etc) for address custom fields is taken care of while saving | |
423 | // the address in CRM_Core_BAO_Address::create method | |
424 | if (!empty($values['location_type_id'])) { | |
be2fb01f | 425 | static $customFields = []; |
c193e786 | 426 | if (empty($customFields)) { |
427 | $customFields = CRM_Core_BAO_CustomField::getFields('Address'); | |
428 | } | |
429 | // make a copy of values, as we going to make changes | |
430 | $newValues = $values; | |
431 | foreach ($values as $key => $val) { | |
432 | $customFieldID = CRM_Core_BAO_CustomField::getKeyID($key); | |
433 | if ($customFieldID && array_key_exists($customFieldID, $customFields)) { | |
434 | // mark an entry in fields array since we want the value of custom field to be copied | |
435 | $fields['Address'][$key] = NULL; | |
436 | ||
437 | $htmlType = CRM_Utils_Array::value('html_type', $customFields[$customFieldID]); | |
438 | switch ($htmlType) { | |
439 | case 'CheckBox': | |
c193e786 | 440 | case 'Multi-Select': |
441 | if ($val) { | |
442 | $mulValues = explode(',', $val); | |
443 | $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE); | |
be2fb01f | 444 | $newValues[$key] = []; |
c193e786 | 445 | foreach ($mulValues as $v1) { |
446 | foreach ($customOption as $v2) { | |
447 | if ((strtolower($v2['label']) == strtolower(trim($v1))) || | |
448 | (strtolower($v2['value']) == strtolower(trim($v1))) | |
449 | ) { | |
450 | if ($htmlType == 'CheckBox') { | |
451 | $newValues[$key][$v2['value']] = 1; | |
452 | } | |
453 | else { | |
454 | $newValues[$key][] = $v2['value']; | |
455 | } | |
456 | } | |
457 | } | |
458 | } | |
459 | } | |
460 | break; | |
461 | } | |
462 | } | |
463 | } | |
464 | // consider new values | |
465 | $values = $newValues; | |
466 | } | |
467 | ||
468 | _civicrm_api3_store_values($fields['Address'], $values, $params['address'][$addressCnt]); | |
469 | ||
be2fb01f | 470 | $addressFields = [ |
c193e786 | 471 | 'county', |
472 | 'country', | |
473 | 'state_province', | |
474 | 'supplemental_address_1', | |
475 | 'supplemental_address_2', | |
476 | 'supplemental_address_3', | |
477 | 'StateProvince.name', | |
be2fb01f | 478 | ]; |
c193e786 | 479 | |
480 | foreach ($addressFields as $field) { | |
481 | if (array_key_exists($field, $values)) { | |
482 | if (!array_key_exists('address', $params)) { | |
be2fb01f | 483 | $params['address'] = []; |
c193e786 | 484 | } |
485 | $params['address'][$addressCnt][$field] = $values[$field]; | |
486 | } | |
487 | } | |
488 | ||
489 | if ($addressCnt == 1) { | |
490 | ||
491 | $params['address'][$addressCnt]['is_primary'] = TRUE; | |
492 | } | |
6a488035 TO |
493 | return TRUE; |
494 | } | |
495 | ||
496 | if (isset($values['note'])) { | |
50bfb460 | 497 | // add a note field |
6a488035 | 498 | if (!isset($params['note'])) { |
be2fb01f | 499 | $params['note'] = []; |
6a488035 TO |
500 | } |
501 | $noteBlock = count($params['note']) + 1; | |
502 | ||
be2fb01f | 503 | $params['note'][$noteBlock] = []; |
6a488035 TO |
504 | if (!isset($fields['Note'])) { |
505 | $fields['Note'] = CRM_Core_DAO_Note::fields(); | |
506 | } | |
507 | ||
508 | // get the current logged in civicrm user | |
509 | $session = CRM_Core_Session::singleton(); | |
510 | $userID = $session->get('userID'); | |
511 | ||
512 | if ($userID) { | |
513 | $values['contact_id'] = $userID; | |
514 | } | |
515 | ||
516 | _civicrm_api3_store_values($fields['Note'], $values, $params['note'][$noteBlock]); | |
517 | ||
518 | return TRUE; | |
519 | } | |
520 | ||
50bfb460 | 521 | // Check for custom field values |
6a488035 | 522 | |
a7488080 | 523 | if (empty($fields['custom'])) { |
6a488035 TO |
524 | $fields['custom'] = &CRM_Core_BAO_CustomField::getFields(CRM_Utils_Array::value('contact_type', $values), |
525 | FALSE, FALSE, NULL, NULL, FALSE, FALSE, FALSE | |
526 | ); | |
527 | } | |
528 | ||
529 | foreach ($values as $key => $value) { | |
530 | if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) { | |
50bfb460 | 531 | // check if it's a valid custom field id |
6a488035 | 532 | |
6a488035 TO |
533 | if (!array_key_exists($customFieldID, $fields['custom'])) { |
534 | return civicrm_api3_create_error('Invalid custom field ID'); | |
535 | } | |
536 | else { | |
537 | $params[$key] = $value; | |
538 | } | |
539 | } | |
540 | } | |
541 | } | |
542 | ||
6a488035 TO |
543 | /** |
544 | * | |
72b3a70c | 545 | * @param array $params |
6a488035 | 546 | * |
a6c01b45 | 547 | * @return array |
353ffa53 | 548 | * <type> |
6a488035 TO |
549 | */ |
550 | function _civicrm_api3_deprecated_duplicate_formatted_contact($params) { | |
551 | $id = CRM_Utils_Array::value('id', $params); | |
552 | $externalId = CRM_Utils_Array::value('external_identifier', $params); | |
553 | if ($id || $externalId) { | |
554 | $contact = new CRM_Contact_DAO_Contact(); | |
555 | ||
556 | $contact->id = $id; | |
557 | $contact->external_identifier = $externalId; | |
558 | ||
559 | if ($contact->find(TRUE)) { | |
560 | if ($params['contact_type'] != $contact->contact_type) { | |
561 | return civicrm_api3_create_error("Mismatched contact IDs OR Mismatched contact Types"); | |
562 | } | |
563 | ||
564 | $error = CRM_Core_Error::createError("Found matching contacts: $contact->id", | |
565 | CRM_Core_Error::DUPLICATE_CONTACT, | |
566 | 'Fatal', $contact->id | |
567 | ); | |
568 | return civicrm_api3_create_error($error->pop()); | |
569 | } | |
570 | } | |
571 | else { | |
03b40a7b | 572 | $ids = CRM_Contact_BAO_Contact::getDuplicateContacts($params, $params['contact_type'], 'Unsupervised'); |
6a488035 TO |
573 | |
574 | if (!empty($ids)) { | |
575 | $ids = implode(',', $ids); | |
576 | $error = CRM_Core_Error::createError("Found matching contacts: $ids", | |
577 | CRM_Core_Error::DUPLICATE_CONTACT, | |
578 | 'Fatal', $ids | |
579 | ); | |
580 | return civicrm_api3_create_error($error->pop()); | |
581 | } | |
582 | } | |
583 | return civicrm_api3_create_success(TRUE); | |
584 | } | |
585 | ||
586 | /** | |
587 | * Validate a formatted contact parameter list. | |
588 | * | |
77855840 TO |
589 | * @param array $params |
590 | * Structured parameter list (as in crm_format_params). | |
6a488035 TO |
591 | * |
592 | * @return bool|CRM_Core_Error | |
6a488035 TO |
593 | */ |
594 | function _civicrm_api3_deprecated_validate_formatted_contact(&$params) { | |
50bfb460 | 595 | // Look for offending email addresses |
6a488035 | 596 | |
6a488035 TO |
597 | if (array_key_exists('email', $params)) { |
598 | foreach ($params['email'] as $count => $values) { | |
599 | if (!is_array($values)) { | |
600 | continue; | |
601 | } | |
602 | if ($email = CRM_Utils_Array::value('email', $values)) { | |
50bfb460 | 603 | // validate each email |
6a488035 TO |
604 | if (!CRM_Utils_Rule::email($email)) { |
605 | return civicrm_api3_create_error('No valid email address'); | |
606 | } | |
607 | ||
50bfb460 | 608 | // check for loc type id. |
a7488080 | 609 | if (empty($values['location_type_id'])) { |
6a488035 TO |
610 | return civicrm_api3_create_error('Location Type Id missing.'); |
611 | } | |
612 | } | |
613 | } | |
614 | } | |
615 | ||
50bfb460 | 616 | // Validate custom data fields |
6a488035 TO |
617 | if (array_key_exists('custom', $params) && is_array($params['custom'])) { |
618 | foreach ($params['custom'] as $key => $custom) { | |
619 | if (is_array($custom)) { | |
620 | foreach ($custom as $fieldId => $value) { | |
621 | $valid = CRM_Core_BAO_CustomValue::typecheck(CRM_Utils_Array::value('type', $value), | |
622 | CRM_Utils_Array::value('value', $value) | |
623 | ); | |
70dd449f | 624 | if (!$valid && $value['is_required']) { |
6a488035 TO |
625 | return civicrm_api3_create_error('Invalid value for custom field \'' . |
626 | CRM_Utils_Array::value('name', $custom) . '\'' | |
627 | ); | |
628 | } | |
629 | if (CRM_Utils_Array::value('type', $custom) == 'Date') { | |
630 | $params['custom'][$key][$fieldId]['value'] = str_replace('-', '', $params['custom'][$key][$fieldId]['value']); | |
631 | } | |
632 | } | |
633 | } | |
634 | } | |
635 | } | |
636 | ||
637 | return civicrm_api3_create_success(TRUE); | |
638 | } | |
639 | ||
6a488035 TO |
640 | /** |
641 | * @deprecated - this is part of the import parser not the API & needs to be moved on out | |
642 | * | |
c490a46a | 643 | * @param array $params |
f4aaa82a | 644 | * @param $onDuplicate |
6a488035 | 645 | * |
72b3a70c CW |
646 | * @return array|bool |
647 | * <type> | |
6a488035 TO |
648 | */ |
649 | function _civicrm_api3_deprecated_create_participant_formatted($params, $onDuplicate) { | |
650 | require_once 'CRM/Event/Import/Parser.php'; | |
a05662ef | 651 | if ($onDuplicate != CRM_Import_Parser::DUPLICATE_NOCHECK) { |
6a488035 TO |
652 | CRM_Core_Error::reset(); |
653 | $error = _civicrm_api3_deprecated_participant_check_params($params, TRUE); | |
654 | if (civicrm_error($error)) { | |
655 | return $error; | |
656 | } | |
657 | } | |
658 | require_once "api/v3/Participant.php"; | |
659 | return civicrm_api3_participant_create($params); | |
660 | } | |
661 | ||
662 | /** | |
663 | * | |
72b3a70c | 664 | * @param array $params |
6a488035 | 665 | * |
f4aaa82a EM |
666 | * @param bool $checkDuplicate |
667 | * | |
72b3a70c CW |
668 | * @return array|bool |
669 | * <type> | |
6a488035 TO |
670 | */ |
671 | function _civicrm_api3_deprecated_participant_check_params($params, $checkDuplicate = FALSE) { | |
672 | ||
50bfb460 | 673 | // check if participant id is valid or not |
a7488080 | 674 | if (!empty($params['id'])) { |
6a488035 TO |
675 | $participant = new CRM_Event_BAO_Participant(); |
676 | $participant->id = $params['id']; | |
677 | if (!$participant->find(TRUE)) { | |
678 | return civicrm_api3_create_error(ts('Participant id is not valid')); | |
679 | } | |
680 | } | |
681 | require_once 'CRM/Contact/BAO/Contact.php'; | |
50bfb460 | 682 | // check if contact id is valid or not |
a7488080 | 683 | if (!empty($params['contact_id'])) { |
6a488035 TO |
684 | $contact = new CRM_Contact_BAO_Contact(); |
685 | $contact->id = $params['contact_id']; | |
686 | if (!$contact->find(TRUE)) { | |
687 | return civicrm_api3_create_error(ts('Contact id is not valid')); | |
688 | } | |
689 | } | |
690 | ||
50bfb460 | 691 | // check that event id is not an template |
a7488080 | 692 | if (!empty($params['event_id'])) { |
6a488035 TO |
693 | $isTemplate = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $params['event_id'], 'is_template'); |
694 | if (!empty($isTemplate)) { | |
3770b22a | 695 | return civicrm_api3_create_error(ts('Event templates are not meant to be registered.')); |
6a488035 TO |
696 | } |
697 | } | |
698 | ||
be2fb01f | 699 | $result = []; |
6a488035 TO |
700 | if ($checkDuplicate) { |
701 | if (CRM_Event_BAO_Participant::checkDuplicate($params, $result)) { | |
702 | $participantID = array_pop($result); | |
703 | ||
704 | $error = CRM_Core_Error::createError("Found matching participant record.", | |
705 | CRM_Core_Error::DUPLICATE_PARTICIPANT, | |
706 | 'Fatal', $participantID | |
707 | ); | |
708 | ||
709 | return civicrm_api3_create_error($error->pop(), | |
be2fb01f | 710 | [ |
6a488035 TO |
711 | 'contactID' => $params['contact_id'], |
712 | 'participantID' => $participantID, | |
be2fb01f | 713 | ] |
6a488035 TO |
714 | ); |
715 | } | |
716 | } | |
717 | return TRUE; | |
718 | } | |
719 | ||
5bc392e6 | 720 | /** |
c490a46a | 721 | * @param array $params |
5bc392e6 | 722 | * @param bool $dupeCheck |
100fef9d | 723 | * @param int $dedupeRuleGroupID |
5bc392e6 EM |
724 | * |
725 | * @return array|null | |
726 | */ | |
109ff2ac DL |
727 | function _civicrm_api3_deprecated_contact_check_params( |
728 | &$params, | |
729 | $dupeCheck = TRUE, | |
109ff2ac | 730 | $dedupeRuleGroupID = NULL) { |
ffec8e23 | 731 | |
732 | $requiredCheck = TRUE; | |
733 | ||
6a488035 TO |
734 | if (isset($params['id']) && is_numeric($params['id'])) { |
735 | $requiredCheck = FALSE; | |
736 | } | |
737 | if ($requiredCheck) { | |
738 | if (isset($params['id'])) { | |
be2fb01f | 739 | $required = ['Individual', 'Household', 'Organization']; |
6a488035 | 740 | } |
be2fb01f CW |
741 | $required = [ |
742 | 'Individual' => [ | |
743 | ['first_name', 'last_name'], | |
6a488035 | 744 | 'email', |
be2fb01f CW |
745 | ], |
746 | 'Household' => [ | |
6a488035 | 747 | 'household_name', |
be2fb01f CW |
748 | ], |
749 | 'Organization' => [ | |
6a488035 | 750 | 'organization_name', |
be2fb01f CW |
751 | ], |
752 | ]; | |
6a488035 | 753 | |
6a488035 | 754 | // contact_type has a limited number of valid values |
22e263ad | 755 | if (empty($params['contact_type'])) { |
81fc1677 E |
756 | return civicrm_api3_create_error("No Contact Type"); |
757 | } | |
6a488035 TO |
758 | $fields = CRM_Utils_Array::value($params['contact_type'], $required); |
759 | if ($fields == NULL) { | |
760 | return civicrm_api3_create_error("Invalid Contact Type: {$params['contact_type']}"); | |
761 | } | |
762 | ||
763 | if ($csType = CRM_Utils_Array::value('contact_sub_type', $params)) { | |
764 | if (!(CRM_Contact_BAO_ContactType::isExtendsContactType($csType, $params['contact_type']))) { | |
e7292422 | 765 | return civicrm_api3_create_error("Invalid or Mismatched Contact Subtype: " . implode(', ', (array) $csType)); |
6a488035 TO |
766 | } |
767 | } | |
768 | ||
8cc574cf | 769 | if (empty($params['contact_id']) && !empty($params['id'])) { |
6a488035 TO |
770 | $valid = FALSE; |
771 | $error = ''; | |
772 | foreach ($fields as $field) { | |
773 | if (is_array($field)) { | |
774 | $valid = TRUE; | |
775 | foreach ($field as $element) { | |
a7488080 | 776 | if (empty($params[$element])) { |
6a488035 TO |
777 | $valid = FALSE; |
778 | $error .= $element; | |
779 | break; | |
780 | } | |
781 | } | |
782 | } | |
783 | else { | |
a7488080 | 784 | if (!empty($params[$field])) { |
6a488035 TO |
785 | $valid = TRUE; |
786 | } | |
787 | } | |
788 | if ($valid) { | |
789 | break; | |
790 | } | |
791 | } | |
792 | ||
793 | if (!$valid) { | |
794 | return civicrm_api3_create_error("Required fields not found for {$params['contact_type']} : $error"); | |
795 | } | |
796 | } | |
797 | } | |
798 | ||
799 | if ($dupeCheck) { | |
03b40a7b | 800 | // @todo switch to using api version |
eb5f7260 | 801 | // $dupes = civicrm_api3('Contact', 'duplicatecheck', (array('match' => $params, 'dedupe_rule_id' => $dedupeRuleGroupID))); |
802 | // $ids = $dupes['count'] ? implode(',', array_keys($dupes['values'])) : NULL; | |
be2fb01f | 803 | $ids = CRM_Contact_BAO_Contact::getDuplicateContacts($params, $params['contact_type'], 'Unsupervised', [], CRM_Utils_Array::value('check_permissions', $params), $dedupeRuleGroupID); |
6a488035 | 804 | if ($ids != NULL) { |
ffec8e23 | 805 | $error = CRM_Core_Error::createError("Found matching contacts: " . implode(',', $ids), |
806 | CRM_Core_Error::DUPLICATE_CONTACT, | |
807 | 'Fatal', $ids | |
808 | ); | |
809 | return civicrm_api3_create_error($error->pop()); | |
6a488035 TO |
810 | } |
811 | } | |
812 | ||
50bfb460 | 813 | // check for organisations with same name |
a7488080 | 814 | if (!empty($params['current_employer'])) { |
be2fb01f CW |
815 | $organizationParams = ['organization_name' => $params['current_employer']]; |
816 | $dupeIds = CRM_Contact_BAO_Contact::getDuplicateContacts($organizationParams, 'Organization', 'Supervised', [], FALSE); | |
6a488035 TO |
817 | |
818 | // check for mismatch employer name and id | |
a7488080 | 819 | if (!empty($params['employer_id']) && !in_array($params['employer_id'], $dupeIds) |
6a488035 TO |
820 | ) { |
821 | return civicrm_api3_create_error('Employer name and Employer id Mismatch'); | |
822 | } | |
823 | ||
824 | // show error if multiple organisation with same name exist | |
a7488080 | 825 | if (empty($params['employer_id']) && (count($dupeIds) > 1) |
6a488035 TO |
826 | ) { |
827 | return civicrm_api3_create_error('Found more than one Organisation with same Name.'); | |
828 | } | |
829 | } | |
830 | ||
831 | return NULL; | |
832 | } | |
833 | ||
109ff2ac | 834 | /** |
f4aaa82a | 835 | * @param $result |
100fef9d | 836 | * @param int $activityTypeID |
f4aaa82a | 837 | * |
a6c01b45 | 838 | * @return array |
353ffa53 | 839 | * <type> $params |
109ff2ac DL |
840 | */ |
841 | function _civicrm_api3_deprecated_activity_buildmailparams($result, $activityTypeID) { | |
842 | // get ready for collecting data about activity to be created | |
be2fb01f | 843 | $params = []; |
109ff2ac DL |
844 | |
845 | $params['activity_type_id'] = $activityTypeID; | |
846 | ||
040073c9 | 847 | $params['status_id'] = 'Completed'; |
3ba820ea CB |
848 | if (!empty($result['from']['id'])) { |
849 | $params['source_contact_id'] = $params['assignee_contact_id'] = $result['from']['id']; | |
850 | } | |
be2fb01f CW |
851 | $params['target_contact_id'] = []; |
852 | $keys = ['to', 'cc', 'bcc']; | |
109ff2ac DL |
853 | foreach ($keys as $key) { |
854 | if (is_array($result[$key])) { | |
855 | foreach ($result[$key] as $key => $keyValue) { | |
856 | if (!empty($keyValue['id'])) { | |
857 | $params['target_contact_id'][] = $keyValue['id']; | |
858 | } | |
859 | } | |
860 | } | |
861 | } | |
862 | $params['subject'] = $result['subject']; | |
863 | $params['activity_date_time'] = $result['date']; | |
864 | $params['details'] = $result['body']; | |
865 | ||
8913e915 D |
866 | $numAttachments = Civi::settings()->get('max_attachments_backend') ?? CRM_Core_BAO_File::DEFAULT_MAX_ATTACHMENTS_BACKEND; |
867 | for ($i = 1; $i <= $numAttachments; $i++) { | |
109ff2ac DL |
868 | if (isset($result["attachFile_$i"])) { |
869 | $params["attachFile_$i"] = $result["attachFile_$i"]; | |
870 | } | |
d97af030 D |
871 | else { |
872 | // No point looping 100 times if there's only one attachment | |
873 | break; | |
874 | } | |
109ff2ac DL |
875 | } |
876 | ||
877 | return $params; | |
878 | } |