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 TO |
27 | /** |
28 | * This function adds the contact variable in $values to the | |
29 | * parameter list $params. For most cases, $values should have length 1. If | |
30 | * the variable being added is a child of Location, a location_type_id must | |
31 | * also be included. If it is a child of phone, a phone_type must be included. | |
32 | * | |
77855840 TO |
33 | * @param array $values |
34 | * The variable(s) to be added. | |
35 | * @param array $params | |
36 | * The structured parameter list. | |
6a488035 TO |
37 | * |
38 | * @return bool|CRM_Utils_Error | |
6a488035 TO |
39 | */ |
40 | function _civicrm_api3_deprecated_add_formatted_param(&$values, &$params) { | |
50bfb460 SB |
41 | // Crawl through the possible classes: |
42 | // Contact | |
43 | // Individual | |
44 | // Household | |
45 | // Organization | |
46 | // Location | |
47 | // Address | |
48 | ||
49 | // Phone | |
50 | // IM | |
51 | // Note | |
52 | // Custom | |
53 | ||
54 | // Cache the various object fields | |
6a488035 TO |
55 | static $fields = NULL; |
56 | ||
57 | if ($fields == NULL) { | |
be2fb01f | 58 | $fields = []; |
6a488035 TO |
59 | } |
60 | ||
50bfb460 | 61 | // first add core contact values since for other Civi modules they are not added |
6a488035 TO |
62 | require_once 'CRM/Contact/BAO/Contact.php'; |
63 | $contactFields = CRM_Contact_DAO_Contact::fields(); | |
64 | _civicrm_api3_store_values($contactFields, $values, $params); | |
65 | ||
66 | if (isset($values['contact_type'])) { | |
50bfb460 | 67 | // we're an individual/household/org property |
6a488035 | 68 | |
6a488035 TO |
69 | $fields[$values['contact_type']] = CRM_Contact_DAO_Contact::fields(); |
70 | ||
71 | _civicrm_api3_store_values($fields[$values['contact_type']], $values, $params); | |
72 | return TRUE; | |
73 | } | |
74 | ||
75 | if (isset($values['individual_prefix'])) { | |
a7488080 | 76 | if (!empty($params['prefix_id'])) { |
91bb24a7 | 77 | $prefixes = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'prefix_id'); |
6a488035 TO |
78 | $params['prefix'] = $prefixes[$params['prefix_id']]; |
79 | } | |
80 | else { | |
81 | $params['prefix'] = $values['individual_prefix']; | |
82 | } | |
83 | return TRUE; | |
84 | } | |
85 | ||
86 | if (isset($values['individual_suffix'])) { | |
a7488080 | 87 | if (!empty($params['suffix_id'])) { |
91bb24a7 | 88 | $suffixes = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'suffix_id'); |
6a488035 TO |
89 | $params['suffix'] = $suffixes[$params['suffix_id']]; |
90 | } | |
91 | else { | |
92 | $params['suffix'] = $values['individual_suffix']; | |
93 | } | |
94 | return TRUE; | |
95 | } | |
96 | ||
50bfb460 | 97 | // CRM-4575 |
6a488035 | 98 | if (isset($values['email_greeting'])) { |
a7488080 | 99 | if (!empty($params['email_greeting_id'])) { |
be2fb01f | 100 | $emailGreetingFilter = [ |
6b409353 | 101 | 'contact_type' => $params['contact_type'] ?? NULL, |
6a488035 | 102 | 'greeting_type' => 'email_greeting', |
be2fb01f | 103 | ]; |
6a488035 TO |
104 | $emailGreetings = CRM_Core_PseudoConstant::greeting($emailGreetingFilter); |
105 | $params['email_greeting'] = $emailGreetings[$params['email_greeting_id']]; | |
106 | } | |
107 | else { | |
108 | $params['email_greeting'] = $values['email_greeting']; | |
109 | } | |
110 | ||
111 | return TRUE; | |
112 | } | |
113 | ||
114 | if (isset($values['postal_greeting'])) { | |
a7488080 | 115 | if (!empty($params['postal_greeting_id'])) { |
be2fb01f | 116 | $postalGreetingFilter = [ |
6b409353 | 117 | 'contact_type' => $params['contact_type'] ?? NULL, |
6a488035 | 118 | 'greeting_type' => 'postal_greeting', |
be2fb01f | 119 | ]; |
6a488035 TO |
120 | $postalGreetings = CRM_Core_PseudoConstant::greeting($postalGreetingFilter); |
121 | $params['postal_greeting'] = $postalGreetings[$params['postal_greeting_id']]; | |
122 | } | |
123 | else { | |
124 | $params['postal_greeting'] = $values['postal_greeting']; | |
125 | } | |
126 | return TRUE; | |
127 | } | |
128 | ||
129 | if (isset($values['addressee'])) { | |
3ad34747 | 130 | $params['addressee'] = $values['addressee']; |
6a488035 TO |
131 | return TRUE; |
132 | } | |
133 | ||
134 | if (isset($values['gender'])) { | |
a7488080 | 135 | if (!empty($params['gender_id'])) { |
91bb24a7 | 136 | $genders = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id'); |
6a488035 TO |
137 | $params['gender'] = $genders[$params['gender_id']]; |
138 | } | |
139 | else { | |
140 | $params['gender'] = $values['gender']; | |
141 | } | |
142 | return TRUE; | |
143 | } | |
144 | ||
df5ad245 | 145 | if (!empty($values['preferred_communication_method'])) { |
be2fb01f | 146 | $comm = []; |
91bb24a7 | 147 | $pcm = array_change_key_case(array_flip(CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'preferred_communication_method')), CASE_LOWER); |
6a488035 TO |
148 | |
149 | $preffComm = explode(',', $values['preferred_communication_method']); | |
150 | foreach ($preffComm as $v) { | |
151 | $v = strtolower(trim($v)); | |
152 | if (array_key_exists($v, $pcm)) { | |
153 | $comm[$pcm[$v]] = 1; | |
154 | } | |
155 | } | |
156 | ||
157 | $params['preferred_communication_method'] = $comm; | |
158 | return TRUE; | |
159 | } | |
160 | ||
50bfb460 | 161 | // format the website params. |
a7488080 | 162 | if (!empty($values['url'])) { |
6a488035 TO |
163 | static $websiteFields; |
164 | if (!is_array($websiteFields)) { | |
165 | require_once 'CRM/Core/DAO/Website.php'; | |
166 | $websiteFields = CRM_Core_DAO_Website::fields(); | |
167 | } | |
168 | if (!array_key_exists('website', $params) || | |
169 | !is_array($params['website']) | |
170 | ) { | |
be2fb01f | 171 | $params['website'] = []; |
6a488035 TO |
172 | } |
173 | ||
174 | $websiteCount = count($params['website']); | |
175 | _civicrm_api3_store_values($websiteFields, $values, | |
176 | $params['website'][++$websiteCount] | |
177 | ); | |
178 | ||
179 | return TRUE; | |
180 | } | |
181 | ||
182 | // get the formatted location blocks into params - w/ 3.0 format, CRM-4605 | |
a7488080 | 183 | if (!empty($values['location_type_id'])) { |
c193e786 | 184 | static $fields = NULL; |
185 | if ($fields == NULL) { | |
be2fb01f | 186 | $fields = []; |
c193e786 | 187 | } |
188 | ||
be2fb01f | 189 | foreach ([ |
6714d8d2 SL |
190 | 'Phone', |
191 | 'Email', | |
192 | 'IM', | |
193 | 'OpenID', | |
194 | 'Phone_Ext', | |
195 | ] as $block) { | |
c193e786 | 196 | $name = strtolower($block); |
197 | if (!array_key_exists($name, $values)) { | |
198 | continue; | |
199 | } | |
200 | ||
201 | if ($name == 'phone_ext') { | |
202 | $block = 'Phone'; | |
203 | } | |
204 | ||
205 | // block present in value array. | |
206 | if (!array_key_exists($name, $params) || !is_array($params[$name])) { | |
be2fb01f | 207 | $params[$name] = []; |
c193e786 | 208 | } |
209 | ||
210 | if (!array_key_exists($block, $fields)) { | |
211 | $className = "CRM_Core_DAO_$block"; | |
212 | $fields[$block] =& $className::fields(); | |
213 | } | |
214 | ||
215 | $blockCnt = count($params[$name]); | |
216 | ||
217 | // copy value to dao field name. | |
218 | if ($name == 'im') { | |
219 | $values['name'] = $values[$name]; | |
220 | } | |
221 | ||
222 | _civicrm_api3_store_values($fields[$block], $values, | |
223 | $params[$name][++$blockCnt] | |
224 | ); | |
225 | ||
226 | if (empty($params['id']) && ($blockCnt == 1)) { | |
227 | $params[$name][$blockCnt]['is_primary'] = TRUE; | |
228 | } | |
229 | ||
230 | // we only process single block at a time. | |
231 | return TRUE; | |
232 | } | |
233 | ||
234 | // handle address fields. | |
235 | if (!array_key_exists('address', $params) || !is_array($params['address'])) { | |
be2fb01f | 236 | $params['address'] = []; |
c193e786 | 237 | } |
238 | ||
239 | $addressCnt = 1; | |
240 | foreach ($params['address'] as $cnt => $addressBlock) { | |
241 | if (CRM_Utils_Array::value('location_type_id', $values) == | |
242 | CRM_Utils_Array::value('location_type_id', $addressBlock) | |
243 | ) { | |
244 | $addressCnt = $cnt; | |
245 | break; | |
246 | } | |
247 | $addressCnt++; | |
248 | } | |
249 | ||
250 | if (!array_key_exists('Address', $fields)) { | |
251 | $fields['Address'] = CRM_Core_DAO_Address::fields(); | |
252 | } | |
253 | ||
254 | // Note: we doing multiple value formatting here for address custom fields, plus putting into right format. | |
255 | // The actual formatting (like date, country ..etc) for address custom fields is taken care of while saving | |
256 | // the address in CRM_Core_BAO_Address::create method | |
257 | if (!empty($values['location_type_id'])) { | |
be2fb01f | 258 | static $customFields = []; |
c193e786 | 259 | if (empty($customFields)) { |
260 | $customFields = CRM_Core_BAO_CustomField::getFields('Address'); | |
261 | } | |
262 | // make a copy of values, as we going to make changes | |
263 | $newValues = $values; | |
264 | foreach ($values as $key => $val) { | |
265 | $customFieldID = CRM_Core_BAO_CustomField::getKeyID($key); | |
266 | if ($customFieldID && array_key_exists($customFieldID, $customFields)) { | |
267 | // mark an entry in fields array since we want the value of custom field to be copied | |
268 | $fields['Address'][$key] = NULL; | |
269 | ||
9c1bc317 | 270 | $htmlType = $customFields[$customFieldID]['html_type'] ?? NULL; |
511d0374 CW |
271 | if (CRM_Core_BAO_CustomField::isSerialized($customFields[$customFieldID]) && $val) { |
272 | $mulValues = explode(',', $val); | |
273 | $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE); | |
274 | $newValues[$key] = []; | |
275 | foreach ($mulValues as $v1) { | |
276 | foreach ($customOption as $v2) { | |
277 | if ((strtolower($v2['label']) == strtolower(trim($v1))) || | |
278 | (strtolower($v2['value']) == strtolower(trim($v1))) | |
279 | ) { | |
280 | if ($htmlType == 'CheckBox') { | |
281 | $newValues[$key][$v2['value']] = 1; | |
282 | } | |
283 | else { | |
284 | $newValues[$key][] = $v2['value']; | |
c193e786 | 285 | } |
286 | } | |
287 | } | |
511d0374 | 288 | } |
c193e786 | 289 | } |
290 | } | |
291 | } | |
292 | // consider new values | |
293 | $values = $newValues; | |
294 | } | |
295 | ||
296 | _civicrm_api3_store_values($fields['Address'], $values, $params['address'][$addressCnt]); | |
297 | ||
be2fb01f | 298 | $addressFields = [ |
c193e786 | 299 | 'county', |
300 | 'country', | |
301 | 'state_province', | |
302 | 'supplemental_address_1', | |
303 | 'supplemental_address_2', | |
304 | 'supplemental_address_3', | |
305 | 'StateProvince.name', | |
be2fb01f | 306 | ]; |
c193e786 | 307 | |
308 | foreach ($addressFields as $field) { | |
309 | if (array_key_exists($field, $values)) { | |
310 | if (!array_key_exists('address', $params)) { | |
be2fb01f | 311 | $params['address'] = []; |
c193e786 | 312 | } |
313 | $params['address'][$addressCnt][$field] = $values[$field]; | |
314 | } | |
315 | } | |
316 | ||
317 | if ($addressCnt == 1) { | |
318 | ||
319 | $params['address'][$addressCnt]['is_primary'] = TRUE; | |
320 | } | |
6a488035 TO |
321 | return TRUE; |
322 | } | |
323 | ||
324 | if (isset($values['note'])) { | |
50bfb460 | 325 | // add a note field |
6a488035 | 326 | if (!isset($params['note'])) { |
be2fb01f | 327 | $params['note'] = []; |
6a488035 TO |
328 | } |
329 | $noteBlock = count($params['note']) + 1; | |
330 | ||
be2fb01f | 331 | $params['note'][$noteBlock] = []; |
6a488035 TO |
332 | if (!isset($fields['Note'])) { |
333 | $fields['Note'] = CRM_Core_DAO_Note::fields(); | |
334 | } | |
335 | ||
336 | // get the current logged in civicrm user | |
337 | $session = CRM_Core_Session::singleton(); | |
338 | $userID = $session->get('userID'); | |
339 | ||
340 | if ($userID) { | |
341 | $values['contact_id'] = $userID; | |
342 | } | |
343 | ||
344 | _civicrm_api3_store_values($fields['Note'], $values, $params['note'][$noteBlock]); | |
345 | ||
346 | return TRUE; | |
347 | } | |
348 | ||
50bfb460 | 349 | // Check for custom field values |
6a488035 | 350 | |
a7488080 | 351 | if (empty($fields['custom'])) { |
6a488035 TO |
352 | $fields['custom'] = &CRM_Core_BAO_CustomField::getFields(CRM_Utils_Array::value('contact_type', $values), |
353 | FALSE, FALSE, NULL, NULL, FALSE, FALSE, FALSE | |
354 | ); | |
355 | } | |
356 | ||
357 | foreach ($values as $key => $value) { | |
358 | if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) { | |
50bfb460 | 359 | // check if it's a valid custom field id |
6a488035 | 360 | |
6a488035 TO |
361 | if (!array_key_exists($customFieldID, $fields['custom'])) { |
362 | return civicrm_api3_create_error('Invalid custom field ID'); | |
363 | } | |
364 | else { | |
365 | $params[$key] = $value; | |
366 | } | |
367 | } | |
368 | } | |
369 | } | |
370 | ||
6a488035 TO |
371 | /** |
372 | * | |
72b3a70c | 373 | * @param array $params |
6a488035 | 374 | * |
a6c01b45 | 375 | * @return array |
353ffa53 | 376 | * <type> |
6a488035 TO |
377 | */ |
378 | function _civicrm_api3_deprecated_duplicate_formatted_contact($params) { | |
9c1bc317 CW |
379 | $id = $params['id'] ?? NULL; |
380 | $externalId = $params['external_identifier'] ?? NULL; | |
6a488035 TO |
381 | if ($id || $externalId) { |
382 | $contact = new CRM_Contact_DAO_Contact(); | |
383 | ||
384 | $contact->id = $id; | |
385 | $contact->external_identifier = $externalId; | |
386 | ||
387 | if ($contact->find(TRUE)) { | |
388 | if ($params['contact_type'] != $contact->contact_type) { | |
389 | return civicrm_api3_create_error("Mismatched contact IDs OR Mismatched contact Types"); | |
390 | } | |
391 | ||
392 | $error = CRM_Core_Error::createError("Found matching contacts: $contact->id", | |
393 | CRM_Core_Error::DUPLICATE_CONTACT, | |
394 | 'Fatal', $contact->id | |
395 | ); | |
396 | return civicrm_api3_create_error($error->pop()); | |
397 | } | |
398 | } | |
399 | else { | |
03b40a7b | 400 | $ids = CRM_Contact_BAO_Contact::getDuplicateContacts($params, $params['contact_type'], 'Unsupervised'); |
6a488035 TO |
401 | |
402 | if (!empty($ids)) { | |
403 | $ids = implode(',', $ids); | |
404 | $error = CRM_Core_Error::createError("Found matching contacts: $ids", | |
405 | CRM_Core_Error::DUPLICATE_CONTACT, | |
406 | 'Fatal', $ids | |
407 | ); | |
408 | return civicrm_api3_create_error($error->pop()); | |
409 | } | |
410 | } | |
411 | return civicrm_api3_create_success(TRUE); | |
412 | } |