Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | |
3 | /* | |
4 | +--------------------------------------------------------------------+ | |
731a0992 | 5 | | CiviCRM version 4.5 | |
6a488035 | 6 | +--------------------------------------------------------------------+ |
731a0992 | 7 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
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 | |
731a0992 | 34 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
35 | * @version $Id: ActivityProfile.php 30486 2011-05-20 16:12:09Z rajan $ |
36 | * | |
37 | */ | |
38 | ||
6a488035 TO |
39 | /** |
40 | * Retrieve Profile field values. | |
41 | * | |
0d5cc439 | 42 | * @param array $params Associative array of property name/value |
6a488035 TO |
43 | * pairs to get profile field values |
44 | * | |
0d5cc439 | 45 | * @throws API_Exception |
6a488035 TO |
46 | * @return Profile field values|CRM_Error |
47 | * | |
f01ce56b | 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 | |
6a386447 | 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 | |
40a60af6 | 52 | * |
53 | * Note that if contact_id is empty an array of defaults is returned | |
6a488035 TO |
54 | */ |
55 | function civicrm_api3_profile_get($params) { | |
f01ce56b | 56 | $nonStandardLegacyBehaviour = is_numeric($params['profile_id']) ? TRUE : FALSE; |
40a60af6 | 57 | if(!empty($params['check_permissions']) && !empty($params['contact_id']) && !1 === civicrm_api3('contact', 'getcount', array('contact_id' => $params['contact_id'], 'check_permissions' => 1))) { |
c85e32fc | 58 | throw new API_Exception('permission denied'); |
59 | } | |
f01ce56b | 60 | $profiles = (array) $params['profile_id']; |
61 | $values = array(); | |
0d5cc439 | 62 | $ufGroupBAO = new CRM_Core_BAO_UFGroup(); |
f01ce56b | 63 | foreach ($profiles as $profileID) { |
6a386447 | 64 | $profileID = _civicrm_api3_profile_getProfileID($profileID); |
f01ce56b | 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 | } | |
6a488035 | 73 | |
f01ce56b | 74 | $isContactActivityProfile = CRM_Core_BAO_UFField::checkContactActivityProfileType($profileID); |
6a488035 | 75 | |
f01ce56b | 76 | $profileFields = CRM_Core_BAO_UFGroup::getFields($profileID, |
77 | FALSE, | |
78 | NULL, | |
79 | NULL, | |
80 | NULL, | |
81 | FALSE, | |
82 | NULL, | |
c85e32fc | 83 | empty($params['check_permissions']) ? FALSE : TRUE, |
f01ce56b | 84 | NULL, |
85 | CRM_Core_Permission::EDIT | |
86 | ); | |
6a488035 | 87 | |
6a488035 | 88 | |
6a488035 TO |
89 | if ($isContactActivityProfile) { |
90 | civicrm_api3_verify_mandatory($params, NULL, array('activity_id')); | |
91 | ||
6a488035 TO |
92 | $errors = CRM_Profile_Form::validateContactActivityProfile($params['activity_id'], |
93 | $params['contact_id'], | |
94 | $params['profile_id'] | |
95 | ); | |
96 | if (!empty($errors)) { | |
f01ce56b | 97 | throw new API_Exception(array_pop($errors)); |
6a488035 TO |
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; | |
c3d3e837 E |
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 | } | |
6a488035 | 111 | } |
6a488035 | 112 | |
0d5cc439 | 113 | $ufGroupBAO->setProfileDefaults($params['contact_id'], $contactFields, $values[$profileID], TRUE); |
6a488035 TO |
114 | |
115 | if ($params['activity_id']) { | |
0d5cc439 | 116 | $ufGroupBAO->setComponentDefaults($activityFields, $params['activity_id'], 'Activity', $values[$profileID], TRUE); |
6a488035 TO |
117 | } |
118 | } | |
40a60af6 | 119 | elseif(!empty($params['contact_id'])) { |
0d5cc439 | 120 | $ufGroupBAO->setProfileDefaults($params['contact_id'], $profileFields, $values[$profileID], TRUE); |
c3d3e837 E |
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 | } | |
f01ce56b | 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'); | |
6a488035 | 138 | } |
6a488035 TO |
139 | } |
140 | ||
aa1b1481 EM |
141 | /** |
142 | * @param $params | |
143 | */ | |
f01ce56b | 144 | function _civicrm_api3_profile_get_spec(&$params) { |
145 | $params['profile_id']['api.required'] = TRUE; | |
40a60af6 | 146 | $params['contact_id']['description'] = 'If no contact is specified an array of defaults will be returned'; |
f01ce56b | 147 | } |
6a386447 | 148 | |
6a488035 | 149 | /** |
6a386447 | 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 | |
0d5cc439 | 153 | * |
6a386447 | 154 | * @param array $params |
0d5cc439 E |
155 | * |
156 | * @throws API_Exception | |
6a386447 | 157 | * @return array API result array |
6a488035 | 158 | */ |
6a386447 | 159 | function civicrm_api3_profile_submit($params) { |
c1b19e8a | 160 | $profileID = _civicrm_api3_profile_getProfileID($params['profile_id']); |
4bcfd71f | 161 | if (!CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $profileID, 'is_active')) { |
162 | //@todo declare pseudoconstant & let api do this | |
f01ce56b | 163 | throw new API_Exception('Invalid value for profile_id'); |
6a488035 TO |
164 | } |
165 | ||
4bcfd71f | 166 | $isContactActivityProfile = CRM_Core_BAO_UFField::checkContactActivityProfileType($profileID); |
6a488035 | 167 | |
4bcfd71f | 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'); | |
6a488035 TO |
170 | } |
171 | ||
172 | $contactParams = $activityParams = $missingParams = array(); | |
173 | ||
c3d3e837 E |
174 | $profileFields = civicrm_api3('profile', 'getfields', array('action' => 'submit', 'profile_id' => $profileID)); |
175 | $profileFields = $profileFields['values']; | |
6a488035 TO |
176 | if ($isContactActivityProfile) { |
177 | civicrm_api3_verify_mandatory($params, NULL, array('activity_id')); | |
178 | ||
6a488035 TO |
179 | $errors = CRM_Profile_Form::validateContactActivityProfile($params['activity_id'], |
180 | $params['contact_id'], | |
4bcfd71f | 181 | $profileID |
6a488035 TO |
182 | ); |
183 | if (!empty($errors)) { | |
6a386447 | 184 | throw new API_Exception(array_pop($errors)); |
6a488035 TO |
185 | } |
186 | } | |
187 | ||
188 | foreach ($profileFields as $fieldName => $field) { | |
6a488035 TO |
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 | } | |
c3d3e837 E |
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; | |
599c61ac E |
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 | } | |
c3d3e837 E |
207 | if(isset($params[$entity . '_id'])) { |
208 | //todo possibly declare $entity_id in getfields ? | |
209 | $contactParams['api.' . $entity . '.create']['id'] = $params[$entity . '_id']; | |
210 | } | |
6a488035 TO |
211 | } |
212 | else { | |
c3d3e837 | 213 | $contactParams[_civicrm_api3_profile_translate_fieldnames_for_bao($fieldName)] = $value; |
6a488035 TO |
214 | } |
215 | } | |
599c61ac E |
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 | } | |
6a488035 | 229 | |
6a488035 | 230 | $contactParams['contact_id'] = CRM_Utils_Array::value('contact_id', $params); |
4bcfd71f | 231 | $contactParams['profile_id'] = $profileID; |
6a488035 TO |
232 | $contactParams['skip_custom'] = 1; |
233 | ||
234 | $contactProfileParams = civicrm_api3_profile_apply($contactParams); | |
6a488035 TO |
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 | } | |
f1a0080c | 255 | |
f01ce56b | 256 | return civicrm_api3('contact', 'create', $profileParams); |
6a488035 TO |
257 | |
258 | $ufGroupDetails = array(); | |
4bcfd71f | 259 | $ufGroupParams = array('id' => $profileID); |
6a488035 TO |
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'])) { | |
6a488035 TO |
271 | CRM_Core_BAO_EntityTag::create($tags, |
272 | 'civicrm_contact', | |
273 | $params['contact_id'] | |
274 | ); | |
275 | } | |
276 | ||
a7488080 | 277 | if (!empty($ufGroupDetails['add_to_group_id'])) { |
6a488035 TO |
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; | |
6a386447 | 285 | |
286 | } | |
c3d3e837 E |
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 | } | |
6a386447 | 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']); | |
174dbdd5 E |
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); | |
6a386447 | 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 | * | |
0d5cc439 | 333 | * @return array Updated Contact/ Activity object|CRM_Error |
6a386447 | 334 | * |
335 | * | |
336 | */ | |
337 | function civicrm_api3_profile_set($params) { | |
338 | return civicrm_api3('profile', 'submit', $params); | |
6a488035 TO |
339 | } |
340 | ||
341 | /** | |
6a386447 | 342 | * @deprecated - appears to be an internal function - should not be accessible via api |
6a488035 TO |
343 | * Provide formatted values for profile fields. |
344 | * | |
c3d3e837 | 345 | * @param array $params Associative array of property name/value |
6a488035 TO |
346 | * pairs to profile field values |
347 | * | |
c3d3e837 | 348 | * @throws API_Exception |
6a488035 TO |
349 | * @return formatted profile field values|CRM_Error |
350 | * | |
351 | * @todo add example | |
352 | * @todo add test cases | |
6a488035 TO |
353 | */ |
354 | function civicrm_api3_profile_apply($params) { | |
355 | ||
6a488035 TO |
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)) { | |
f2225b2c | 377 | throw new API_Exception('Enable to format profile parameters.'); |
6a488035 TO |
378 | } |
379 | ||
380 | return civicrm_api3_create_success($data); | |
381 | } | |
382 | ||
6a488035 | 383 | |
f01ce56b | 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 | |
40a60af6 | 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 | |
f1a0080c E |
392 | * |
393 | * @param $params | |
394 | * | |
395 | * @return array | |
f01ce56b | 396 | */ |
397 | function _civicrm_api3_profile_getbillingpseudoprofile(&$params) { | |
5a9e1452 | 398 | |
40a60af6 | 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'])) { | |
5a9e1452 | 403 | $config = CRM_Core_Config::singleton(); |
40a60af6 | 404 | $blanks = array( |
405 | 'billing_first_name' => '', | |
406 | 'billing_middle_name' => '', | |
407 | 'billing_last_name' => '', | |
5a9e1452 | 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, | |
40a60af6 | 415 | ); |
40a60af6 | 416 | return $blanks; |
417 | } | |
5a9e1452 | 418 | |
419 | $addressFields = array('street_address', 'city', 'state_province_id', 'country_id', 'postal_code'); | |
f01ce56b | 420 | $result = civicrm_api3('contact', 'getsingle', array( |
421 | 'id' => $params['contact_id'], | |
f01ce56b | 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,), | |
45c30250 | 427 | 'return' => 'api.email.get, api.address.get, api.address.getoptions, country, state_province, email, first_name, last_name, middle_name, ' . implode($addressFields, ','), |
f01ce56b | 428 | ) |
429 | ); | |
f01ce56b | 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 | } | |
40a60af6 | 462 | // return both variants of email to reflect inconsistencies in form layer |
463 | $values['email'. '-' . $locationTypeID] = $values['billing-email'. '-' . $locationTypeID]; | |
f01ce56b | 464 | return $values; |
465 | } | |
6a386447 | 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 | |
7c3f2c03 E |
480 | * @param $is_flush |
481 | * | |
482 | * @internal param $params | |
174dbdd5 E |
483 | * |
484 | * @return | |
6a386447 | 485 | */ |
486 | ||
174dbdd5 | 487 | function _civicrm_api3_buildprofile_submitfields($profileID, $optionsBehaviour = 1, $is_flush) { |
6a386447 | 488 | static $profileFields = array(); |
174dbdd5 E |
489 | if($is_flush) { |
490 | $profileFields = array(); | |
491 | if(empty($profileID)) { | |
492 | return; | |
493 | } | |
494 | } | |
6a386447 | 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(); | |
c1fec147 | 500 | foreach ($fields['values'] as $field) { |
6a386447 | 501 | if(!$field['is_active']) { |
502 | continue; | |
503 | } | |
504 | list($entity, $fieldName) = _civicrm_api3_map_profile_fields_to_entity($field); | |
c3d3e837 E |
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( | |
6a386447 | 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), | |
c3d3e837 | 515 | 'entity' => $entity, |
f5c68f3c | 516 | 'weight' => CRM_Utils_Array::value('weight', $field), |
c3d3e837 | 517 | ), $aliasArray); |
6a386447 | 518 | |
f5c68f3c E |
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 | } | |
6a386447 | 525 | //see function notes |
b0b44427 | 526 | // as we build up a list of these we should be able to determine a generic approach |
527 | // | |
6a386447 | 528 | $hardCodedEntityFields = array( |
529 | 'state_province' => 'state_province_id', | |
530 | 'country' => 'country_id', | |
531 | 'participant_status' => 'status_id', | |
532 | 'gender' => 'gender_id', | |
b0b44427 | 533 | 'financial_type' => 'financial_type_id', |
534 | 'soft_credit' => 'soft_credit_to', | |
535 | 'group' => 'group_id', | |
536 | 'tag' => 'tag_id', | |
3ebd4b5c | 537 | 'soft_credit_type' => 'soft_credit_type_id', |
6a386447 | 538 | ); |
b0b44427 | 539 | |
f5c68f3c E |
540 | if(array_key_exists($ufFieldTaleFieldName, $hardCodedEntityFields)) { |
541 | $ufFieldTaleFieldName = $hardCodedEntityFields[$ufFieldTaleFieldName]; | |
6a386447 | 542 | } |
b0b44427 | 543 | |
f5c68f3c | 544 | $entities[$entity][$fieldName] = $ufFieldTaleFieldName; |
6a386447 | 545 | } |
546 | ||
547 | foreach ($entities as $entity => $entityFields) { | |
548 | $result = civicrm_api3($entity, 'getfields', array('action' => 'create')); | |
b0b44427 | 549 | $entityGetFieldsResult = _civicrm_api3_profile_appendaliases($result['values'], $entity); |
6a386447 | 550 | foreach ($entityFields as $entityfield => $realName) { |
7c3f2c03 | 551 | $fieldName = strtolower($entityfield); |
29fbb90a E |
552 | if(!strstr($fieldName, '-')) { |
553 | if(strtolower($realName) != $fieldName) { | |
7c3f2c03 E |
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 | |
29fbb90a E |
558 | $fieldName = strtolower($realName); |
559 | } | |
560 | if(isset($entityGetFieldsResult[$realName]['uniqueName'])) { | |
f5c68f3c E |
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 | |
29fbb90a E |
563 | $fieldName = $entityGetFieldsResult[$realName]['uniqueName']; |
564 | } | |
f5c68f3c E |
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 | } | |
7c3f2c03 | 571 | } |
f5c68f3c | 572 | $profileFields[$profileID][$fieldName] = array_merge($entityGetFieldsResult[$realName], $profileFields[$profileID][$entityfield]); |
29fbb90a E |
573 | if(!isset($profileFields[$profileID][$fieldName]['api.aliases'])) { |
574 | $profileFields[$profileID][$fieldName]['api.aliases'] = array(); | |
575 | } | |
b0b44427 | 576 | if($optionsBehaviour && !empty($entityGetFieldsResult[$realName]['pseudoconstant'])) { |
6a386447 | 577 | if($optionsBehaviour > 1 || !in_array($realName, array('state_province_id', 'county_id', 'country_id'))) { |
578 | $options = civicrm_api3($entity, 'getoptions', array('field' => $realName)); | |
7c3f2c03 | 579 | $profileFields[$profileID][$fieldName]['options'] = $options['values']; |
6a386447 | 580 | } |
581 | } | |
c3d3e837 | 582 | |
7c3f2c03 E |
583 | if($entityfield != $fieldName) { |
584 | if(isset($profileFields[$profileID][$entityfield])) { | |
585 | unset($profileFields[$profileID][$entityfield]); | |
586 | } | |
dd9d7b83 EM |
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 | } | |
c3d3e837 | 591 | } |
6a386447 | 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'])) { | |
c3d3e837 E |
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']; | |
6a386447 | 597 | } |
c3d3e837 | 598 | */ |
6a386447 | 599 | } |
600 | } | |
f5c68f3c | 601 | uasort($profileFields[$profileID], "_civicrm_api3_order_by_weight"); |
6a386447 | 602 | return $profileFields[$profileID]; |
603 | } | |
604 | ||
aa1b1481 EM |
605 | /** |
606 | * @param $a | |
607 | * @param $b | |
608 | * | |
609 | * @return bool | |
610 | */ | |
f5c68f3c E |
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 | } | |
f1a0080c | 614 | |
6a386447 | 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 | * | |
f1a0080c E |
619 | * @param $field |
620 | * | |
621 | * @return array | |
6a386447 | 622 | */ |
623 | function _civicrm_api3_map_profile_fields_to_entity(&$field) { | |
7c3f2c03 | 624 | $entity = $field['field_type']; |
6a386447 | 625 | $contactTypes = civicrm_api3('contact', 'getoptions', array('field' => 'contact_type')); |
626 | if(in_array($entity, $contactTypes['values'])) { | |
174dbdd5 | 627 | $entity = 'contact'; |
6a386447 | 628 | } |
7c3f2c03 E |
629 | $entity = _civicrm_api_get_entity_name_from_camel($entity); |
630 | $locationFields = array('email' => 'email'); | |
6a386447 | 631 | $fieldName = $field['field_name']; |
632 | if(!empty($field['location_type_id'])) { | |
633 | if($fieldName == 'email') { | |
174dbdd5 | 634 | $entity = 'email'; |
6a386447 | 635 | } |
636 | else{ | |
174dbdd5 | 637 | $entity = 'address'; |
6a386447 | 638 | } |
639 | $fieldName .= '-' . $field['location_type_id']; | |
640 | } | |
c3d3e837 E |
641 | elseif(array_key_exists($fieldName, $locationFields)) { |
642 | $fieldName .= '-Primary'; | |
174dbdd5 | 643 | $entity = 'email'; |
c3d3e837 | 644 | } |
6a386447 | 645 | if(!empty($field['phone_type_id'])) { |
646 | $fieldName .= '-' . $field['location_type_id']; | |
174dbdd5 | 647 | $entity = 'phone'; |
6a386447 | 648 | } |
c3d3e837 | 649 | |
b0b44427 | 650 | // @todo - sort this out! |
6a386447 | 651 | //here we do a hard-code list of known fields that don't map to where they are mapped to |
b0b44427 | 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 | |
6a386447 | 655 | $hardCodedEntityMappings = array( |
174dbdd5 E |
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', | |
b0b44427 | 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 | |
174dbdd5 E |
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', | |
3ebd4b5c | 677 | 'soft_credit_type' => 'contribution_soft', |
174dbdd5 E |
678 | 'group' => 'group_contact', |
679 | 'tag' => 'entity_tag', | |
6a386447 | 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 | |
23fb5e08 EM |
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 | |
6a386447 | 695 | */ |
696 | function _civicrm_api3_profile_getProfileID($profileID) { | |
4bcfd71f | 697 | if(!empty($profileID) && strtolower($profileID) != 'billing' && !is_numeric($profileID)) { |
6a386447 | 698 | $profileID = civicrm_api3('uf_group', 'getvalue', array('return' => 'id', 'name' => $profileID)); |
699 | } | |
700 | return $profileID; | |
b0b44427 | 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 | |
0d5cc439 | 706 | * |
b0b44427 | 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) | |
0d5cc439 E |
711 | * @param $entity |
712 | * | |
713 | * @return array | |
b0b44427 | 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 | |
174dbdd5 | 727 | if(in_array($entity, array('membership', 'contribution'))) { |
4bcfd71f | 728 | $values['send_receipt'] = array('title' => 'Send Receipt', 'type' => (int) 16); |
b0b44427 | 729 | } |
730 | return $values; | |
0d5cc439 | 731 | } |