| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.6 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
| 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 | +--------------------------------------------------------------------+ |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * This api exposes CiviCRM profiles. |
| 30 | * |
| 31 | * Profiles are collections of fields used as forms, listings, search columns, etc. |
| 32 | * |
| 33 | * @package CiviCRM_APIv3 |
| 34 | */ |
| 35 | |
| 36 | /** |
| 37 | * Retrieve Profile field values. |
| 38 | * |
| 39 | * NOTE this api is not standard & since it is tested we need to honour that |
| 40 | * but the correct behaviour is for it to return an id indexed array as this supports |
| 41 | * multiple instances - if a single profile is passed in we will not return a normal api result array |
| 42 | * in order to avoid breaking code. (This could still be confusing :-( but we have to keep the tested behaviour working |
| 43 | * |
| 44 | * Note that if contact_id is empty an array of defaults is returned |
| 45 | * |
| 46 | * @param array $params |
| 47 | * Associative array of property name/value. |
| 48 | * pairs to get profile field values |
| 49 | * |
| 50 | * @throws API_Exception |
| 51 | * @return array |
| 52 | */ |
| 53 | function civicrm_api3_profile_get($params) { |
| 54 | $nonStandardLegacyBehaviour = is_numeric($params['profile_id']) ? TRUE : FALSE; |
| 55 | if (!empty($params['check_permissions']) && !empty($params['contact_id']) && !1 === civicrm_api3('contact', 'getcount', array('contact_id' => $params['contact_id'], 'check_permissions' => 1))) { |
| 56 | throw new API_Exception('permission denied'); |
| 57 | } |
| 58 | $profiles = (array) $params['profile_id']; |
| 59 | $values = array(); |
| 60 | $ufGroupBAO = new CRM_Core_BAO_UFGroup(); |
| 61 | foreach ($profiles as $profileID) { |
| 62 | $profileID = _civicrm_api3_profile_getProfileID($profileID); |
| 63 | $values[$profileID] = array(); |
| 64 | if (strtolower($profileID) == 'billing') { |
| 65 | $values[$profileID] = _civicrm_api3_profile_getbillingpseudoprofile($params); |
| 66 | continue; |
| 67 | } |
| 68 | if (!CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $profileID, 'is_active')) { |
| 69 | throw new API_Exception('Invalid value for profile_id : ' . $profileID); |
| 70 | } |
| 71 | |
| 72 | $isContactActivityProfile = CRM_Core_BAO_UFField::checkContactActivityProfileType($profileID); |
| 73 | |
| 74 | $profileFields = CRM_Core_BAO_UFGroup::getFields($profileID, |
| 75 | FALSE, |
| 76 | NULL, |
| 77 | NULL, |
| 78 | NULL, |
| 79 | FALSE, |
| 80 | NULL, |
| 81 | empty($params['check_permissions']) ? FALSE : TRUE, |
| 82 | NULL, |
| 83 | CRM_Core_Permission::EDIT |
| 84 | ); |
| 85 | |
| 86 | if ($isContactActivityProfile) { |
| 87 | civicrm_api3_verify_mandatory($params, NULL, array('activity_id')); |
| 88 | |
| 89 | $errors = CRM_Profile_Form::validateContactActivityProfile($params['activity_id'], |
| 90 | $params['contact_id'], |
| 91 | $params['profile_id'] |
| 92 | ); |
| 93 | if (!empty($errors)) { |
| 94 | throw new API_Exception(array_pop($errors)); |
| 95 | } |
| 96 | |
| 97 | $contactFields = $activityFields = array(); |
| 98 | foreach ($profileFields as $fieldName => $field) { |
| 99 | if (CRM_Utils_Array::value('field_type', $field) == 'Activity') { |
| 100 | $activityFields[$fieldName] = $field; |
| 101 | } |
| 102 | else { |
| 103 | $contactFields[$fieldName] = $field; |
| 104 | // we should return 'Primary' with & without capitalisation. it is more consistent with api to not |
| 105 | // capitalise, but for form support we need it for now. Hopefully we can move away from it |
| 106 | $contactFields[strtolower($fieldName)] = $field; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | $ufGroupBAO->setProfileDefaults($params['contact_id'], $contactFields, $values[$profileID], TRUE); |
| 111 | |
| 112 | if ($params['activity_id']) { |
| 113 | $ufGroupBAO->setComponentDefaults($activityFields, $params['activity_id'], 'Activity', $values[$profileID], TRUE); |
| 114 | } |
| 115 | } |
| 116 | elseif (!empty($params['contact_id'])) { |
| 117 | $ufGroupBAO->setProfileDefaults($params['contact_id'], $profileFields, $values[$profileID], TRUE); |
| 118 | foreach ($values[$profileID] as $fieldName => $field) { |
| 119 | // we should return 'Primary' with & without capitalisation. it is more consistent with api to not |
| 120 | // capitalise, but for form support we need it for now. Hopefully we can move away from it |
| 121 | $values[$profileID][strtolower($fieldName)] = $field; |
| 122 | } |
| 123 | } |
| 124 | else { |
| 125 | $values[$profileID] = array_fill_keys(array_keys($profileFields), ''); |
| 126 | } |
| 127 | } |
| 128 | if ($nonStandardLegacyBehaviour) { |
| 129 | $result = civicrm_api3_create_success(); |
| 130 | $result['values'] = $values[$profileID]; |
| 131 | return $result; |
| 132 | } |
| 133 | else { |
| 134 | return civicrm_api3_create_success($values, $params, 'Profile', 'Get'); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Adjust profile get function metadata. |
| 140 | * |
| 141 | * @param array $params |
| 142 | */ |
| 143 | function _civicrm_api3_profile_get_spec(&$params) { |
| 144 | $params['profile_id']['api.required'] = TRUE; |
| 145 | $params['profile_id']['title'] = 'Profile ID'; |
| 146 | $params['contact_id']['description'] = 'If no contact is specified an array of defaults will be returned'; |
| 147 | $params['contact_id']['title'] = 'Contact ID'; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Submit a set of fields against a profile. |
| 152 | * |
| 153 | * Note choice of submit versus create is discussed CRM-13234 & related to the fact |
| 154 | * 'profile' is being treated as a data-entry entity |
| 155 | * |
| 156 | * @param array $params |
| 157 | * |
| 158 | * @throws API_Exception |
| 159 | * @return array |
| 160 | * API result array |
| 161 | */ |
| 162 | function civicrm_api3_profile_submit($params) { |
| 163 | $profileID = _civicrm_api3_profile_getProfileID($params['profile_id']); |
| 164 | if (!CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $profileID, 'is_active')) { |
| 165 | //@todo declare pseudoconstant & let api do this |
| 166 | throw new API_Exception('Invalid value for profile_id'); |
| 167 | } |
| 168 | |
| 169 | $isContactActivityProfile = CRM_Core_BAO_UFField::checkContactActivityProfileType($profileID); |
| 170 | |
| 171 | if (!empty($params['id']) && CRM_Core_BAO_UFField::checkProfileType($profileID) && !$isContactActivityProfile) { |
| 172 | throw new API_Exception('Update profiles including more than one entity not currently supported'); |
| 173 | } |
| 174 | |
| 175 | $contactParams = $activityParams = $missingParams = array(); |
| 176 | |
| 177 | $profileFields = civicrm_api3('Profile', 'getfields', array('action' => 'submit', 'profile_id' => $profileID)); |
| 178 | $profileFields = $profileFields['values']; |
| 179 | if ($isContactActivityProfile) { |
| 180 | civicrm_api3_verify_mandatory($params, NULL, array('activity_id')); |
| 181 | |
| 182 | $errors = CRM_Profile_Form::validateContactActivityProfile($params['activity_id'], |
| 183 | $params['contact_id'], |
| 184 | $profileID |
| 185 | ); |
| 186 | if (!empty($errors)) { |
| 187 | throw new API_Exception(array_pop($errors)); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | foreach ($profileFields as $fieldName => $field) { |
| 192 | if (!isset($params[$fieldName])) { |
| 193 | continue; |
| 194 | } |
| 195 | |
| 196 | $value = $params[$fieldName]; |
| 197 | if ($params[$fieldName] && isset($params[$fieldName . '_id'])) { |
| 198 | $value = $params[$fieldName . '_id']; |
| 199 | } |
| 200 | $contactEntities = array('contact', 'individual', 'organization', 'household'); |
| 201 | $locationEntities = array('email', 'address', 'phone', 'website', 'im'); |
| 202 | |
| 203 | $entity = strtolower(CRM_Utils_Array::value('entity', $field)); |
| 204 | if ($entity && !in_array($entity, array_merge($contactEntities, $locationEntities))) { |
| 205 | $contactParams['api.' . $entity . '.create'][$fieldName] = $value; |
| 206 | //@todo we are not currently declaring this option |
| 207 | if (isset($params['batch_id']) && strtolower($entity) == 'contribution') { |
| 208 | $contactParams['api.' . $entity . '.create']['batch_id'] = $params['batch_id']; |
| 209 | } |
| 210 | if (isset($params[$entity . '_id'])) { |
| 211 | //todo possibly declare $entity_id in getfields ? |
| 212 | $contactParams['api.' . $entity . '.create']['id'] = $params[$entity . '_id']; |
| 213 | } |
| 214 | } |
| 215 | else { |
| 216 | $contactParams[_civicrm_api3_profile_translate_fieldnames_for_bao($fieldName)] = $value; |
| 217 | } |
| 218 | } |
| 219 | if (isset($contactParams['api.contribution.create']) && isset($contactParams['api.membership.create'])) { |
| 220 | $contactParams['api.membership_payment.create'] = array( |
| 221 | 'contribution_id' => '$value.api.contribution.create.id', |
| 222 | 'membership_id' => '$value.api.membership.create.id', |
| 223 | ); |
| 224 | } |
| 225 | |
| 226 | if (isset($contactParams['api.contribution.create']) && isset($contactParams['api.participant.create'])) { |
| 227 | $contactParams['api.participant_payment.create'] = array( |
| 228 | 'contribution_id' => '$value.api.contribution.create.id', |
| 229 | 'participant_id' => '$value.api.participant.create.id', |
| 230 | ); |
| 231 | } |
| 232 | |
| 233 | $contactParams['contact_id'] = CRM_Utils_Array::value('contact_id', $params); |
| 234 | $contactParams['profile_id'] = $profileID; |
| 235 | $contactParams['skip_custom'] = 1; |
| 236 | |
| 237 | $contactProfileParams = civicrm_api3_profile_apply($contactParams); |
| 238 | |
| 239 | // Contact profile fields |
| 240 | $profileParams = $contactProfileParams['values']; |
| 241 | |
| 242 | // If profile having activity fields |
| 243 | if ($isContactActivityProfile && !empty($activityParams)) { |
| 244 | $activityParams['id'] = $params['activity_id']; |
| 245 | $profileParams['api.activity.create'] = $activityParams; |
| 246 | } |
| 247 | |
| 248 | return civicrm_api3('contact', 'create', $profileParams); |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Translate field names for BAO. |
| 253 | * |
| 254 | * The api standards expect field names to be lower case but the BAO uses mixed case |
| 255 | * so we accept 'email-primary' but pass 'email-Primary' to the BAO |
| 256 | * we could make the BAO handle email-primary but this would alter the fieldname seen by hooks |
| 257 | * & we would need to consider that change |
| 258 | * |
| 259 | * @param string $fieldName |
| 260 | * API field name. |
| 261 | * |
| 262 | * @return string |
| 263 | * BAO Field Name |
| 264 | */ |
| 265 | function _civicrm_api3_profile_translate_fieldnames_for_bao($fieldName) { |
| 266 | $fieldName = str_replace('url', 'URL', $fieldName); |
| 267 | return str_replace('primary', 'Primary', $fieldName); |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * Metadata for submit action. |
| 272 | * |
| 273 | * @param array $params |
| 274 | * @param array $apirequest |
| 275 | */ |
| 276 | function _civicrm_api3_profile_submit_spec(&$params, $apirequest) { |
| 277 | if (isset($apirequest['params']['profile_id'])) { |
| 278 | // we will return what is required for this profile |
| 279 | // note the problem with simply over-riding getfields & then calling generic if needbe is we don't have the |
| 280 | // api request array to pass to it. |
| 281 | //@todo - it may make more sense just to pass the apiRequest to getfields |
| 282 | //@todo get_options should take an array - @ the moment it is only takes 'all' - which is supported |
| 283 | // by other getfields fn |
| 284 | // we don't resolve state, country & county for performance reasons |
| 285 | $resolveOptions = CRM_Utils_Array::value('get_options', $apirequest['params']) == 'all' ? TRUE : FALSE; |
| 286 | $profileID = _civicrm_api3_profile_getProfileID($apirequest['params']['profile_id']); |
| 287 | $params = _civicrm_api3_buildprofile_submitfields($profileID, $resolveOptions, CRM_Utils_Array::value('cache_clear', $params)); |
| 288 | } |
| 289 | elseif (isset($apirequest['params']['cache_clear'])) { |
| 290 | _civicrm_api3_buildprofile_submitfields(FALSE, FALSE, TRUE); |
| 291 | } |
| 292 | $params['profile_id']['api.required'] = TRUE; |
| 293 | $params['profile_id']['title'] = 'Profile ID'; |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Update Profile field values. |
| 298 | * |
| 299 | * @deprecated - calling this function directly is deprecated as 'set' is not a clear action |
| 300 | * use submit |
| 301 | * |
| 302 | * @param array $params |
| 303 | * Array of property name/value. |
| 304 | * pairs to update profile field values |
| 305 | * |
| 306 | * @return array |
| 307 | * Updated Contact/ Activity object|CRM_Error |
| 308 | */ |
| 309 | function civicrm_api3_profile_set($params) { |
| 310 | return civicrm_api3('profile', 'submit', $params); |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * Apply profile. |
| 315 | * |
| 316 | * @deprecated - appears to be an internal function - should not be accessible via api |
| 317 | * Provide formatted values for profile fields. |
| 318 | * |
| 319 | * @param array $params |
| 320 | * Array of property name/value. |
| 321 | * pairs to profile field values |
| 322 | * |
| 323 | * @throws API_Exception |
| 324 | * @return array |
| 325 | * |
| 326 | * @todo add example |
| 327 | * @todo add test cases |
| 328 | */ |
| 329 | function civicrm_api3_profile_apply($params) { |
| 330 | $profileFields = CRM_Core_BAO_UFGroup::getFields($params['profile_id'], |
| 331 | FALSE, |
| 332 | NULL, |
| 333 | NULL, |
| 334 | NULL, |
| 335 | FALSE, |
| 336 | NULL, |
| 337 | TRUE, |
| 338 | NULL, |
| 339 | CRM_Core_Permission::EDIT |
| 340 | ); |
| 341 | |
| 342 | list($data, $contactDetails) = CRM_Contact_BAO_Contact::formatProfileContactParams($params, |
| 343 | $profileFields, |
| 344 | CRM_Utils_Array::value('contact_id', $params), |
| 345 | $params['profile_id'], |
| 346 | CRM_Utils_Array::value('contact_type', $params), |
| 347 | CRM_Utils_Array::value('skip_custom', $params, FALSE) |
| 348 | ); |
| 349 | |
| 350 | if (empty($data)) { |
| 351 | throw new API_Exception('Enable to format profile parameters.'); |
| 352 | } |
| 353 | |
| 354 | return civicrm_api3_create_success($data); |
| 355 | } |
| 356 | |
| 357 | |
| 358 | /** |
| 359 | * Get pseudo profile 'billing'. |
| 360 | * |
| 361 | * This is a function to help us 'pretend' billing is a profile & treat it like it is one. |
| 362 | * It gets standard credit card address fields etc |
| 363 | * Note this is 'better' that the inbuilt version as it will pull in fallback values |
| 364 | * billing location -> is_billing -> primary |
| 365 | * |
| 366 | * Note that that since the existing code for deriving a blank profile is not easily accessible our |
| 367 | * interim solution is just to return an empty array |
| 368 | * |
| 369 | * @param array $params |
| 370 | * |
| 371 | * @return array |
| 372 | */ |
| 373 | function _civicrm_api3_profile_getbillingpseudoprofile(&$params) { |
| 374 | |
| 375 | $locations = civicrm_api3('address', 'getoptions', array('field' => 'location_type_id', 'context' => 'validate')); |
| 376 | $locationTypeID = array_search('Billing', $locations['values']); |
| 377 | |
| 378 | if (empty($params['contact_id'])) { |
| 379 | $config = CRM_Core_Config::singleton(); |
| 380 | $blanks = array( |
| 381 | 'billing_first_name' => '', |
| 382 | 'billing_middle_name' => '', |
| 383 | 'billing_last_name' => '', |
| 384 | 'email-' . $locationTypeID => '', |
| 385 | 'billing_email-' . $locationTypeID => '', |
| 386 | 'billing_city-' . $locationTypeID => '', |
| 387 | 'billing_postal_code-' . $locationTypeID => '', |
| 388 | 'billing_street_address-' . $locationTypeID => '', |
| 389 | 'billing_country_id-' . $locationTypeID => $config->defaultContactCountry, |
| 390 | 'billing_state_province_id-' . $locationTypeID => $config->defaultContactStateProvince, |
| 391 | ); |
| 392 | return $blanks; |
| 393 | } |
| 394 | |
| 395 | $addressFields = array('street_address', 'city', 'state_province_id', 'country_id', 'postal_code'); |
| 396 | $result = civicrm_api3('contact', 'getsingle', array( |
| 397 | 'id' => $params['contact_id'], |
| 398 | 'api.address.get.1' => array('location_type_id' => 'Billing', 'return' => $addressFields), |
| 399 | // getting the is_billing required or not is an extra db call but probably cheap enough as this isn't an import api |
| 400 | 'api.address.get.2' => array('is_billing' => TRUE, 'return' => $addressFields), |
| 401 | 'api.email.get.1' => array('location_type_id' => 'Billing'), |
| 402 | 'api.email.get.2' => array('is_billing' => TRUE), |
| 403 | 'return' => 'api.email.get, api.address.get, api.address.getoptions, country, state_province, email, first_name, last_name, middle_name, ' . implode($addressFields, ','), |
| 404 | ) |
| 405 | ); |
| 406 | |
| 407 | $values = array( |
| 408 | 'billing_first_name' => $result['first_name'], |
| 409 | 'billing_middle_name' => $result['middle_name'], |
| 410 | 'billing_last_name' => $result['last_name'], |
| 411 | ); |
| 412 | |
| 413 | if (!empty($result['api.address.get.1']['count'])) { |
| 414 | foreach ($addressFields as $fieldname) { |
| 415 | $values['billing_' . $fieldname . '-' . $locationTypeID] = isset($result['api.address.get.1']['values'][0][$fieldname]) ? $result['api.address.get.1']['values'][0][$fieldname] : ''; |
| 416 | } |
| 417 | } |
| 418 | elseif (!empty($result['api.address.get.2']['count'])) { |
| 419 | foreach ($addressFields as $fieldname) { |
| 420 | $values['billing_' . $fieldname . '-' . $locationTypeID] = isset($result['api.address.get.2']['values'][0][$fieldname]) ? $result['api.address.get.2']['values'][0][$fieldname] : ''; |
| 421 | } |
| 422 | } |
| 423 | else { |
| 424 | foreach ($addressFields as $fieldname) { |
| 425 | $values['billing_' . $fieldname . '-' . $locationTypeID] = isset($result[$fieldname]) ? $result[$fieldname] : ''; |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | if (!empty($result['api.email.get.1']['count'])) { |
| 430 | $values['billing-email' . '-' . $locationTypeID] = $result['api.email.get.1']['values'][0]['email']; |
| 431 | } |
| 432 | elseif (!empty($result['api.email.get.2']['count'])) { |
| 433 | $values['billing-email' . '-' . $locationTypeID] = $result['api.email.get.2']['values'][0]['email']; |
| 434 | } |
| 435 | else { |
| 436 | $values['billing-email' . '-' . $locationTypeID] = $result['email']; |
| 437 | } |
| 438 | // return both variants of email to reflect inconsistencies in form layer |
| 439 | $values['email' . '-' . $locationTypeID] = $values['billing-email' . '-' . $locationTypeID]; |
| 440 | return $values; |
| 441 | } |
| 442 | |
| 443 | /** |
| 444 | * Here we will build up getfields type data for all the fields in the profile. |
| 445 | * |
| 446 | * Because the integration with the form layer in core is so hard-coded we are not going to attempt to re-use it |
| 447 | * However, as this function is unit-tested & hence 'locked in' we can aspire to extract sharable |
| 448 | * code out of the form-layer over time. |
| 449 | * |
| 450 | * The function deciphers which fields belongs to which entites & retrieves metadata about the entities |
| 451 | * Unfortunately we have inconsistencies such as 'contribution' uses contribution_status_id |
| 452 | * & participant has 'participant_status' so we have to standardise from the outside in here - |
| 453 | * find the oddities, 'mask them' at this layer, add tests & work to standardise over time so we can remove this handling |
| 454 | * |
| 455 | * @param int $profileID |
| 456 | * @param int $optionsBehaviour |
| 457 | * 0 = don't resolve, 1 = resolve non-aggressively, 2 = resolve aggressively - ie include country & state. |
| 458 | * @param $is_flush |
| 459 | * |
| 460 | * @return array|void |
| 461 | */ |
| 462 | function _civicrm_api3_buildprofile_submitfields($profileID, $optionsBehaviour = 1, $is_flush) { |
| 463 | static $profileFields = array(); |
| 464 | if ($is_flush) { |
| 465 | $profileFields = array(); |
| 466 | if (empty($profileID)) { |
| 467 | return NULL; |
| 468 | } |
| 469 | } |
| 470 | if (isset($profileFields[$profileID])) { |
| 471 | return $profileFields[$profileID]; |
| 472 | } |
| 473 | $fields = civicrm_api3('uf_field', 'get', array('uf_group_id' => $profileID)); |
| 474 | $entities = array(); |
| 475 | foreach ($fields['values'] as $field) { |
| 476 | if (!$field['is_active']) { |
| 477 | continue; |
| 478 | } |
| 479 | list($entity, $fieldName) = _civicrm_api3_map_profile_fields_to_entity($field); |
| 480 | $aliasArray = array(); |
| 481 | if (strtolower($fieldName) != $fieldName) { |
| 482 | $aliasArray['api.aliases'] = array($fieldName); |
| 483 | $fieldName = strtolower($fieldName); |
| 484 | } |
| 485 | $profileFields[$profileID][$fieldName] = array_merge(array( |
| 486 | 'api.required' => $field['is_required'], |
| 487 | 'title' => $field['label'], |
| 488 | 'help_pre' => CRM_Utils_Array::value('help_pre', $field), |
| 489 | 'help_post' => CRM_Utils_Array::value('help_post', $field), |
| 490 | 'entity' => $entity, |
| 491 | 'weight' => CRM_Utils_Array::value('weight', $field), |
| 492 | ), $aliasArray); |
| 493 | |
| 494 | $ufFieldTaleFieldName = $field['field_name']; |
| 495 | if (isset($entity[$ufFieldTaleFieldName]['name'])) { |
| 496 | // in the case where we are dealing with an alias we map back to a name |
| 497 | // this will be tested by 'membership_type_id' field |
| 498 | $ufFieldTaleFieldName = $entity[$ufFieldTaleFieldName]['name']; |
| 499 | } |
| 500 | //see function notes |
| 501 | // as we build up a list of these we should be able to determine a generic approach |
| 502 | // |
| 503 | $hardCodedEntityFields = array( |
| 504 | 'state_province' => 'state_province_id', |
| 505 | 'country' => 'country_id', |
| 506 | 'participant_status' => 'status_id', |
| 507 | 'gender' => 'gender_id', |
| 508 | 'financial_type' => 'financial_type_id', |
| 509 | 'soft_credit' => 'soft_credit_to', |
| 510 | 'group' => 'group_id', |
| 511 | 'tag' => 'tag_id', |
| 512 | 'soft_credit_type' => 'soft_credit_type_id', |
| 513 | ); |
| 514 | |
| 515 | if (array_key_exists($ufFieldTaleFieldName, $hardCodedEntityFields)) { |
| 516 | $ufFieldTaleFieldName = $hardCodedEntityFields[$ufFieldTaleFieldName]; |
| 517 | } |
| 518 | |
| 519 | $entities[$entity][$fieldName] = $ufFieldTaleFieldName; |
| 520 | } |
| 521 | |
| 522 | foreach ($entities as $entity => $entityFields) { |
| 523 | $result = civicrm_api3($entity, 'getfields', array('action' => 'create')); |
| 524 | $entityGetFieldsResult = _civicrm_api3_profile_appendaliases($result['values'], $entity); |
| 525 | foreach ($entityFields as $entityfield => $realName) { |
| 526 | $fieldName = strtolower($entityfield); |
| 527 | if (!strstr($fieldName, '-')) { |
| 528 | if (strtolower($realName) != $fieldName) { |
| 529 | // we want to keep the '-' pattern for locations but otherwise |
| 530 | // we are going to make the api-standard field the main / preferred name but support the db name |
| 531 | // in future naming the fields in the DB to reflect the way the rest of the api / BAO / metadata works would |
| 532 | // reduce code |
| 533 | $fieldName = strtolower($realName); |
| 534 | } |
| 535 | if (isset($entityGetFieldsResult[$realName]['uniqueName'])) { |
| 536 | // we won't alias the field name on here are we are using uniqueNames for the possibility of needing to differentiate |
| 537 | // which entity 'status_id' belongs to |
| 538 | $fieldName = $entityGetFieldsResult[$realName]['uniqueName']; |
| 539 | } |
| 540 | else { |
| 541 | if (isset($entityGetFieldsResult[$realName]['name'])) { |
| 542 | // this will sort out membership_type_id vs membership_type |
| 543 | $fieldName = $entityGetFieldsResult[$realName]['name']; |
| 544 | } |
| 545 | } |
| 546 | } |
| 547 | $profileFields[$profileID][$fieldName] = array_merge($entityGetFieldsResult[$realName], $profileFields[$profileID][$entityfield]); |
| 548 | if (!isset($profileFields[$profileID][$fieldName]['api.aliases'])) { |
| 549 | $profileFields[$profileID][$fieldName]['api.aliases'] = array(); |
| 550 | } |
| 551 | if ($optionsBehaviour && !empty($entityGetFieldsResult[$realName]['pseudoconstant'])) { |
| 552 | if ($optionsBehaviour > 1 || !in_array($realName, array('state_province_id', 'county_id', 'country_id'))) { |
| 553 | $options = civicrm_api3($entity, 'getoptions', array('field' => $realName)); |
| 554 | $profileFields[$profileID][$fieldName]['options'] = $options['values']; |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | if ($entityfield != $fieldName) { |
| 559 | if (isset($profileFields[$profileID][$entityfield])) { |
| 560 | unset($profileFields[$profileID][$entityfield]); |
| 561 | } |
| 562 | if (!in_array($entityfield, $profileFields[$profileID][$fieldName]['api.aliases'])) { |
| 563 | // we will make the mixed case version (e.g. of 'Primary') an alias |
| 564 | $profileFields[$profileID][$fieldName]['api.aliases'][] = $entityfield; |
| 565 | } |
| 566 | } |
| 567 | /** |
| 568 | * putting this on hold -this would cause the api to set the default - but could have unexpected behaviour |
| 569 | if (isset($result['values'][$realName]['default_value'])) { |
| 570 | //this would be the case for a custom field with a configured default |
| 571 | $profileFields[$profileID][$entityfield]['api.default'] = $result['values'][$realName]['default_value']; |
| 572 | } |
| 573 | */ |
| 574 | } |
| 575 | } |
| 576 | uasort($profileFields[$profileID], "_civicrm_api3_order_by_weight"); |
| 577 | return $profileFields[$profileID]; |
| 578 | } |
| 579 | |
| 580 | /** |
| 581 | * @param $a |
| 582 | * @param $b |
| 583 | * |
| 584 | * @return bool |
| 585 | */ |
| 586 | function _civicrm_api3_order_by_weight($a, $b) { |
| 587 | return CRM_Utils_Array::value('weight', $b) < CRM_Utils_Array::value('weight', $a) ? TRUE : FALSE; |
| 588 | } |
| 589 | |
| 590 | /** |
| 591 | * Here we map the profile fields as stored in the uf_field table to their 'real entity' |
| 592 | * we also return the profile fieldname |
| 593 | * |
| 594 | * @param $field |
| 595 | * |
| 596 | * @return array |
| 597 | */ |
| 598 | function _civicrm_api3_map_profile_fields_to_entity(&$field) { |
| 599 | $entity = $field['field_type']; |
| 600 | $contactTypes = civicrm_api3('contact', 'getoptions', array('field' => 'contact_type')); |
| 601 | if (in_array($entity, $contactTypes['values'])) { |
| 602 | $entity = 'contact'; |
| 603 | } |
| 604 | $entity = _civicrm_api_get_entity_name_from_camel($entity); |
| 605 | $locationFields = array('email' => 'email'); |
| 606 | $fieldName = $field['field_name']; |
| 607 | if (!empty($field['location_type_id'])) { |
| 608 | if ($fieldName == 'email') { |
| 609 | $entity = 'email'; |
| 610 | } |
| 611 | else { |
| 612 | $entity = 'address'; |
| 613 | } |
| 614 | $fieldName .= '-' . $field['location_type_id']; |
| 615 | } |
| 616 | elseif (array_key_exists($fieldName, $locationFields)) { |
| 617 | $fieldName .= '-Primary'; |
| 618 | $entity = 'email'; |
| 619 | } |
| 620 | if (!empty($field['phone_type_id'])) { |
| 621 | $fieldName .= '-' . $field['location_type_id']; |
| 622 | $entity = 'phone'; |
| 623 | } |
| 624 | |
| 625 | // @todo - sort this out! |
| 626 | //here we do a hard-code list of known fields that don't map to where they are mapped to |
| 627 | // not a great solution but probably if we looked in the BAO we'd find a scary switch statement |
| 628 | // in a perfect world the uf_field table would hold the correct entity for each item |
| 629 | // & only the relationships between entities would need to be coded |
| 630 | $hardCodedEntityMappings = array( |
| 631 | 'street_address' => 'address', |
| 632 | 'street_number' => 'address', |
| 633 | 'supplemental_address_1' => 'address', |
| 634 | 'supplemental_address_2' => 'address', |
| 635 | 'supplemental_address_3' => 'address', |
| 636 | 'postal_code' => 'address', |
| 637 | 'city' => 'address', |
| 638 | 'email' => 'email', |
| 639 | 'state_province' => 'address', |
| 640 | 'country' => 'address', |
| 641 | 'county' => 'address', |
| 642 | //note that in discussions about how to restructure the api we discussed making these membership |
| 643 | // fields into 'membership_payment' fields - which would entail declaring them in getfields |
| 644 | // & renaming them in existing profiles |
| 645 | 'financial_type' => 'contribution', |
| 646 | 'total_amount' => 'contribution', |
| 647 | 'receive_date' => 'contribution', |
| 648 | 'payment_instrument' => 'contribution', |
| 649 | 'check_number' => 'contribution', |
| 650 | 'contribution_status_id' => 'contribution', |
| 651 | 'soft_credit' => 'contribution', |
| 652 | 'soft_credit_type' => 'contribution_soft', |
| 653 | 'group' => 'group_contact', |
| 654 | 'tag' => 'entity_tag', |
| 655 | ); |
| 656 | if (array_key_exists($fieldName, $hardCodedEntityMappings)) { |
| 657 | $entity = $hardCodedEntityMappings[$fieldName]; |
| 658 | } |
| 659 | return array($entity, $fieldName); |
| 660 | } |
| 661 | |
| 662 | /** |
| 663 | * @todo this should be handled by the api wrapper using getfields info - need to check |
| 664 | * how we add a a pseudoconstant to this pseudo api to make that work |
| 665 | * |
| 666 | * @param int $profileID |
| 667 | * |
| 668 | * @return int|string |
| 669 | * @throws CiviCRM_API3_Exception |
| 670 | */ |
| 671 | function _civicrm_api3_profile_getProfileID($profileID) { |
| 672 | if (!empty($profileID) && strtolower($profileID) != 'billing' && !is_numeric($profileID)) { |
| 673 | $profileID = civicrm_api3('uf_group', 'getvalue', array('return' => 'id', 'name' => $profileID)); |
| 674 | } |
| 675 | return $profileID; |
| 676 | } |
| 677 | |
| 678 | /** |
| 679 | * helper function to add all aliases as keys to getfields response so we can look for keys within it |
| 680 | * since the relationship between profile fields & api / metadata based fields is a bit inconsistent |
| 681 | * |
| 682 | * @param array $values |
| 683 | * |
| 684 | * e.g getfields response incl 'membership_type_id' - with api.aliases = 'membership_type' |
| 685 | * returned array will include both as keys (with the same values) |
| 686 | * @param $entity |
| 687 | * |
| 688 | * @return array |
| 689 | */ |
| 690 | function _civicrm_api3_profile_appendaliases($values, $entity) { |
| 691 | foreach ($values as $field => $spec) { |
| 692 | if (!empty($spec['api.aliases'])) { |
| 693 | foreach ($spec['api.aliases'] as $alias) { |
| 694 | $values[$alias] = $spec; |
| 695 | } |
| 696 | } |
| 697 | if (!empty($spec['uniqueName'])) { |
| 698 | $values[$spec['uniqueName']] = $spec; |
| 699 | } |
| 700 | } |
| 701 | //special case on membership & contribution - can't see how to handle in a generic way |
| 702 | if (in_array($entity, array('membership', 'contribution'))) { |
| 703 | $values['send_receipt'] = array('title' => 'Send Receipt', 'type' => (int) 16); |
| 704 | } |
| 705 | return $values; |
| 706 | } |
| 707 | |
| 708 | /** |
| 709 | * @deprecated api notice |
| 710 | * @return array |
| 711 | * Array of deprecated actions |
| 712 | */ |
| 713 | function _civicrm_api3_profile_deprecation() { |
| 714 | return array( |
| 715 | 'set' => 'Profile api "set" action is deprecated in favor of "submit".', |
| 716 | 'apply' => 'Profile api "apply" action is deprecated in favor of "submit".', |
| 717 | ); |
| 718 | } |