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