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