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