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