| 1 | <?php |
| 2 | /* |
| 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 | +--------------------------------------------------------------------+ |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * |
| 14 | * @package CRM |
| 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
| 16 | */ |
| 17 | |
| 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 | |
| 25 | require_once 'api/v3/utils.php'; |
| 26 | |
| 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 | * |
| 33 | * @param array $values |
| 34 | * The variable(s) to be added. |
| 35 | * @param array $params |
| 36 | * The structured parameter list. |
| 37 | * |
| 38 | * @return bool|CRM_Utils_Error |
| 39 | */ |
| 40 | function _civicrm_api3_deprecated_add_formatted_param(&$values, &$params) { |
| 41 | // Crawl through the possible classes: |
| 42 | // Contact |
| 43 | // Individual |
| 44 | // Household |
| 45 | // Organization |
| 46 | // Location |
| 47 | // Address |
| 48 | // Email |
| 49 | // Phone |
| 50 | // IM |
| 51 | // Note |
| 52 | // Custom |
| 53 | |
| 54 | // Cache the various object fields |
| 55 | static $fields = NULL; |
| 56 | |
| 57 | if ($fields == NULL) { |
| 58 | $fields = []; |
| 59 | } |
| 60 | |
| 61 | // first add core contact values since for other Civi modules they are not added |
| 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'])) { |
| 67 | // we're an individual/household/org property |
| 68 | |
| 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'])) { |
| 76 | if (!empty($params['prefix_id'])) { |
| 77 | $prefixes = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'prefix_id'); |
| 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'])) { |
| 87 | if (!empty($params['suffix_id'])) { |
| 88 | $suffixes = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'suffix_id'); |
| 89 | $params['suffix'] = $suffixes[$params['suffix_id']]; |
| 90 | } |
| 91 | else { |
| 92 | $params['suffix'] = $values['individual_suffix']; |
| 93 | } |
| 94 | return TRUE; |
| 95 | } |
| 96 | |
| 97 | // CRM-4575 |
| 98 | if (isset($values['email_greeting'])) { |
| 99 | if (!empty($params['email_greeting_id'])) { |
| 100 | $emailGreetingFilter = [ |
| 101 | 'contact_type' => $params['contact_type'] ?? NULL, |
| 102 | 'greeting_type' => 'email_greeting', |
| 103 | ]; |
| 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'])) { |
| 115 | if (!empty($params['postal_greeting_id'])) { |
| 116 | $postalGreetingFilter = [ |
| 117 | 'contact_type' => $params['contact_type'] ?? NULL, |
| 118 | 'greeting_type' => 'postal_greeting', |
| 119 | ]; |
| 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'])) { |
| 130 | $params['addressee'] = $values['addressee']; |
| 131 | return TRUE; |
| 132 | } |
| 133 | |
| 134 | if (isset($values['gender'])) { |
| 135 | if (!empty($params['gender_id'])) { |
| 136 | $genders = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id'); |
| 137 | $params['gender'] = $genders[$params['gender_id']]; |
| 138 | } |
| 139 | else { |
| 140 | $params['gender'] = $values['gender']; |
| 141 | } |
| 142 | return TRUE; |
| 143 | } |
| 144 | |
| 145 | if (!empty($values['preferred_communication_method'])) { |
| 146 | $comm = []; |
| 147 | $pcm = array_change_key_case(array_flip(CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'preferred_communication_method')), CASE_LOWER); |
| 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 | |
| 161 | // format the website params. |
| 162 | if (!empty($values['url'])) { |
| 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 | ) { |
| 171 | $params['website'] = []; |
| 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 |
| 183 | if (!empty($values['location_type_id'])) { |
| 184 | static $fields = NULL; |
| 185 | if ($fields == NULL) { |
| 186 | $fields = []; |
| 187 | } |
| 188 | |
| 189 | foreach ([ |
| 190 | 'Phone', |
| 191 | 'Email', |
| 192 | 'IM', |
| 193 | 'OpenID', |
| 194 | 'Phone_Ext', |
| 195 | ] as $block) { |
| 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])) { |
| 207 | $params[$name] = []; |
| 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'])) { |
| 236 | $params['address'] = []; |
| 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'])) { |
| 258 | static $customFields = []; |
| 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 | |
| 270 | $htmlType = $customFields[$customFieldID]['html_type'] ?? NULL; |
| 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']; |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | // consider new values |
| 293 | $values = $newValues; |
| 294 | } |
| 295 | |
| 296 | _civicrm_api3_store_values($fields['Address'], $values, $params['address'][$addressCnt]); |
| 297 | |
| 298 | $addressFields = [ |
| 299 | 'county', |
| 300 | 'country', |
| 301 | 'state_province', |
| 302 | 'supplemental_address_1', |
| 303 | 'supplemental_address_2', |
| 304 | 'supplemental_address_3', |
| 305 | 'StateProvince.name', |
| 306 | ]; |
| 307 | |
| 308 | foreach ($addressFields as $field) { |
| 309 | if (array_key_exists($field, $values)) { |
| 310 | if (!array_key_exists('address', $params)) { |
| 311 | $params['address'] = []; |
| 312 | } |
| 313 | $params['address'][$addressCnt][$field] = $values[$field]; |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | if ($addressCnt == 1) { |
| 318 | |
| 319 | $params['address'][$addressCnt]['is_primary'] = TRUE; |
| 320 | } |
| 321 | return TRUE; |
| 322 | } |
| 323 | |
| 324 | if (isset($values['note'])) { |
| 325 | // add a note field |
| 326 | if (!isset($params['note'])) { |
| 327 | $params['note'] = []; |
| 328 | } |
| 329 | $noteBlock = count($params['note']) + 1; |
| 330 | |
| 331 | $params['note'][$noteBlock] = []; |
| 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 | |
| 349 | // Check for custom field values |
| 350 | |
| 351 | if (empty($fields['custom'])) { |
| 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)) { |
| 359 | // check if it's a valid custom field id |
| 360 | |
| 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 | |
| 371 | /** |
| 372 | * |
| 373 | * @param array $params |
| 374 | * |
| 375 | * @return array |
| 376 | * <type> |
| 377 | */ |
| 378 | function _civicrm_api3_deprecated_duplicate_formatted_contact($params) { |
| 379 | $id = $params['id'] ?? NULL; |
| 380 | $externalId = $params['external_identifier'] ?? NULL; |
| 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 { |
| 400 | $ids = CRM_Contact_BAO_Contact::getDuplicateContacts($params, $params['contact_type'], 'Unsupervised'); |
| 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 | } |