profile api test fixes (caching & case on entities)
[civicrm-core.git] / api / v3 / Profile.php
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, CRM_Utils_Array::value('cache_clear', $params));
299 }
300 elseif (isset($apirequest['params']['cache_clear'])) {
301 _civicrm_api3_buildprofile_submitfields(FALSE, FALSE, True);
302 }
303 $params['profile_id']['api.required'] = TRUE;
304 }
305
306 /**
307 * @deprecated - calling this function directly is deprecated as 'set' is not a clear action
308 * use submit
309 * Update Profile field values.
310 *
311 * @param array $params Associative array of property name/value
312 * pairs to update profile field values
313 *
314 * @return array Updated Contact/ Activity object|CRM_Error
315 *
316 *
317 */
318 function civicrm_api3_profile_set($params) {
319 return civicrm_api3('profile', 'submit', $params);
320 }
321
322 /**
323 * @deprecated - appears to be an internal function - should not be accessible via api
324 * Provide formatted values for profile fields.
325 *
326 * @param array $params Associative array of property name/value
327 * pairs to profile field values
328 *
329 * @throws API_Exception
330 * @return formatted profile field values|CRM_Error
331 *
332 * @todo add example
333 * @todo add test cases
334 */
335 function civicrm_api3_profile_apply($params) {
336
337 $profileFields = CRM_Core_BAO_UFGroup::getFields($params['profile_id'],
338 FALSE,
339 NULL,
340 NULL,
341 NULL,
342 FALSE,
343 NULL,
344 TRUE,
345 NULL,
346 CRM_Core_Permission::EDIT
347 );
348
349 list($data, $contactDetails) = CRM_Contact_BAO_Contact::formatProfileContactParams($params,
350 $profileFields,
351 CRM_Utils_Array::value('contact_id', $params),
352 $params['profile_id'],
353 CRM_Utils_Array::value('contact_type', $params),
354 CRM_Utils_Array::value('skip_custom', $params, FALSE)
355 );
356
357 if (empty($data)) {
358 throw new API_Exception('Enable to format profile parameters.');
359 }
360
361 return civicrm_api3_create_success($data);
362 }
363
364
365 /**
366 * This is a function to help us 'pretend' billing is a profile & treat it like it is one.
367 * It gets standard credit card address fields etc
368 * Note this is 'better' that the inbuilt version as it will pull in fallback values
369 * billing location -> is_billing -> primary
370 *
371 * Note that that since the existing code for deriving a blank profile is not easily accessible our
372 * interim solution is just to return an empty array
373 */
374 function _civicrm_api3_profile_getbillingpseudoprofile(&$params) {
375
376 $locations = civicrm_api3('address', 'getoptions', array('field' => 'location_type_id'));
377 $locationTypeID = array_search('Billing', $locations['values']);
378
379 if(empty($params['contact_id'])) {
380 $config = CRM_Core_Config::singleton();
381 $blanks = array(
382 'billing_first_name' => '',
383 'billing_middle_name' => '',
384 'billing_last_name' => '',
385 'email-' . $locationTypeID => '',
386 'billing_email-' . $locationTypeID => '',
387 'billing_city-' . $locationTypeID => '',
388 'billing_postal_code-' . $locationTypeID => '',
389 'billing_street_address-' . $locationTypeID => '',
390 'billing_country_id-' . $locationTypeID => $config->defaultContactCountry,
391 'billing_state_province_id-' . $locationTypeID => $config->defaultContactStateProvince,
392 );
393 return $blanks;
394 }
395
396 $addressFields = array('street_address', 'city', 'state_province_id', 'country_id', 'postal_code');
397 $result = civicrm_api3('contact', 'getsingle', array(
398 'id' => $params['contact_id'],
399 'api.address.get.1' => array('location_type_id' => 'Billing', 'return' => $addressFields),
400 // getting the is_billing required or not is an extra db call but probably cheap enough as this isn't an import api
401 'api.address.get.2' => array('is_billing' => True, 'return' => $addressFields),
402 'api.email.get.1' => array('location_type_id' => 'Billing',),
403 'api.email.get.2' => array('is_billing' => True,),
404 'return' => 'api.email.get, api.address.get, api.address.getoptions, country, state_province, email, first_name, last_name, middle_name, ' . implode($addressFields, ','),
405 )
406 );
407
408 $values = array(
409 'billing_first_name' => $result['first_name'],
410 'billing_middle_name' => $result['middle_name'],
411 'billing_last_name' => $result['last_name'],
412 );
413
414 if(!empty($result['api.address.get.1']['count'])) {
415 foreach ($addressFields as $fieldname) {
416 $values['billing_' . $fieldname . '-' . $locationTypeID] = isset($result['api.address.get.1']['values'][0][$fieldname]) ? $result['api.address.get.1']['values'][0][$fieldname] : '';
417 }
418 }
419 elseif(!empty($result['api.address.get.2']['count'])) {
420 foreach ($addressFields as $fieldname) {
421 $values['billing_' . $fieldname . '-' . $locationTypeID] = isset($result['api.address.get.2']['values'][0][$fieldname]) ? $result['api.address.get.2']['values'][0][$fieldname] : '';
422 }
423 }
424 else{
425 foreach ($addressFields as $fieldname) {
426 $values['billing_' . $fieldname . '-' . $locationTypeID] = isset($result[$fieldname]) ? $result[$fieldname] : '';
427 }
428 }
429
430 if(!empty($result['api.email.get.1']['count'])) {
431 $values['billing-email'. '-' . $locationTypeID] = $result['api.email.get.1']['values'][0]['email'];
432 }
433 elseif(!empty($result['api.email.get.2']['count'])) {
434 $values['billing-email'. '-' . $locationTypeID] = $result['api.email.get.2']['values'][0]['email'];
435 }
436 else{
437 $values['billing-email'. '-' . $locationTypeID] = $result['email'];
438 }
439 // return both variants of email to reflect inconsistencies in form layer
440 $values['email'. '-' . $locationTypeID] = $values['billing-email'. '-' . $locationTypeID];
441 return $values;
442 }
443
444 /**
445 * Here we will build up getfields type data for all the fields in the profile. Because the integration with the
446 * 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 integer $profileID
456 * @param integer $optionsBehaviour 0 = don't resolve, 1 = resolve non-aggressively, 2 = resolve aggressively - ie include country & state
457 * @param $params
458 *
459 * @return
460 */
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;
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 ), $aliasArray);
492
493 $realFieldName = $field['field_name'];
494 //see function notes
495 // as we build up a list of these we should be able to determine a generic approach
496 //
497 $hardCodedEntityFields = array(
498 'state_province' => 'state_province_id',
499 'country' => 'country_id',
500 'participant_status' => 'status_id',
501 'gender' => 'gender_id',
502 'financial_type' => 'financial_type_id',
503 'soft_credit' => 'soft_credit_to',
504 'group' => 'group_id',
505 'tag' => 'tag_id',
506 );
507
508 if(array_key_exists($realFieldName, $hardCodedEntityFields)) {
509 $realFieldName = $hardCodedEntityFields[$realFieldName];
510 }
511
512 $entities[$entity][$fieldName] = $realFieldName;
513 }
514
515 foreach ($entities as $entity => $entityFields) {
516 $result = civicrm_api3($entity, 'getfields', array('action' => 'create'));
517 $entityGetFieldsResult = _civicrm_api3_profile_appendaliases($result['values'], $entity);
518 foreach ($entityFields as $entityfield => $realName) {
519 $profileFields[$profileID][strtolower($entityfield)] = array_merge($profileFields[$profileID][$entityfield], $entityGetFieldsResult[$realName]);
520 if($optionsBehaviour && !empty($entityGetFieldsResult[$realName]['pseudoconstant'])) {
521 if($optionsBehaviour > 1 || !in_array($realName, array('state_province_id', 'county_id', 'country_id'))) {
522 $options = civicrm_api3($entity, 'getoptions', array('field' => $realName));
523 $profileFields[$profileID][$entityfield]['options'] = $options['values'];
524 }
525 }
526
527 if($entityfield != strtolower($entityfield)) {
528 // we will make the mixed case version (e.g. of 'Primary') an aliase
529 if(!isset($profileFields[$profileID][strtolower($entityfield)])) {
530 $profileFields[$profileID][strtolower($entityfield)]['api.aliases'] = array();
531 }
532 $profileFields[$profileID][strtolower($entityfield)]['api.aliases'][] = $entityfield;
533 }
534 /**
535 * putting this on hold -this would cause the api to set the default - but could have unexpected behaviour
536 if(isset($result['values'][$realName]['default_value'])) {
537 //this would be the case for a custom field with a configured default
538 $profileFields[$profileID][$entityfield]['api.default'] = $result['values'][$realName]['default_value'];
539 }
540 */
541 }
542 }
543 return $profileFields[$profileID];
544 }
545
546 /**
547 * Here we map the profile fields as stored in the uf_field table to their 'real entity'
548 * we also return the profile fieldname
549 *
550 */
551 function _civicrm_api3_map_profile_fields_to_entity(&$field) {
552 $entity = _civicrm_api_get_entity_name_from_camel($field['field_type']);
553 $contactTypes = civicrm_api3('contact', 'getoptions', array('field' => 'contact_type'));
554 $locationFields = array('email' => 'email');
555 if(in_array($entity, $contactTypes['values'])) {
556 $entity = 'contact';
557 }
558 $fieldName = $field['field_name'];
559 if(!empty($field['location_type_id'])) {
560 if($fieldName == 'email') {
561 $entity = 'email';
562 }
563 else{
564 $entity = 'address';
565 }
566 $fieldName .= '-' . $field['location_type_id'];
567 }
568 elseif(array_key_exists($fieldName, $locationFields)) {
569 $fieldName .= '-Primary';
570 $entity = 'email';
571 }
572 if(!empty($field['phone_type_id'])) {
573 $fieldName .= '-' . $field['location_type_id'];
574 $entity = 'phone';
575 }
576
577 // @todo - sort this out!
578 //here we do a hard-code list of known fields that don't map to where they are mapped to
579 // not a great solution but probably if we looked in the BAO we'd find a scary switch statement
580 // in a perfect world the uf_field table would hold the correct entity for each item
581 // & only the relationships between entities would need to be coded
582 $hardCodedEntityMappings = array(
583 'street_address' => 'address',
584 'street_number' => 'address',
585 'supplemental_address_1' => 'address',
586 'supplemental_address_2' => 'address',
587 'supplemental_address_3' => 'address',
588 'postal_code' => 'address',
589 'city' => 'address',
590 'email' => 'email',
591 'state_province' => 'address',
592 'country' => 'address',
593 'county' => 'address',
594 //note that in discussions about how to restructure the api we discussed making these membership
595 // fields into 'membership_payment' fields - which would entail declaring them in getfields
596 // & renaming them in existing profiles
597 'financial_type' => 'contribution',
598 'total_amount' => 'contribution',
599 'receive_date' => 'contribution',
600 'payment_instrument' => 'contribution',
601 'check_number' => 'contribution',
602 'contribution_status_id' => 'contribution',
603 'soft_credit' => 'contribution',
604 'group' => 'group_contact',
605 'tag' => 'entity_tag',
606 );
607 if(array_key_exists($fieldName, $hardCodedEntityMappings)) {
608 $entity = $hardCodedEntityMappings[$fieldName];
609 }
610 return array($entity, $fieldName);
611 }
612
613 /**
614 * @todo this should be handled by the api wrapper using getfields info - need to check
615 * how we add a a pseudoconstant to this pseudoapi to make that work
616 */
617 function _civicrm_api3_profile_getProfileID($profileID) {
618 if(!empty($profileID) && strtolower($profileID) != 'billing' && !is_numeric($profileID)) {
619 $profileID = civicrm_api3('uf_group', 'getvalue', array('return' => 'id', 'name' => $profileID));
620 }
621 return $profileID;
622 }
623
624 /**
625 * helper function to add all aliases as keys to getfields response so we can look for keys within it
626 * since the relationship between profile fields & api / metadata based fields is a bit inconsistent
627 *
628 * @param array $values
629 *
630 * e.g getfields response incl 'membership_type_id' - with api.aliases = 'membership_type'
631 * returned array will include both as keys (with the same values)
632 * @param $entity
633 *
634 * @return array
635 */
636 function _civicrm_api3_profile_appendaliases($values, $entity) {
637 foreach ($values as $field => $spec) {
638 if(!empty($spec['api.aliases'])) {
639 foreach ($spec['api.aliases'] as $alias) {
640 $values[$alias] = $spec;
641 }
642 }
643 if(!empty($spec['uniqueName'])) {
644 $values[$spec['uniqueName']] = $spec;
645 }
646 }
647 //special case on membership & contribution - can't see how to handle in a generic way
648 if(in_array($entity, array('membership', 'contribution'))) {
649 $values['send_receipt'] = array('title' => 'Send Receipt', 'type' => (int) 16);
650 }
651 return $values;
652 }