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