Merge pull request #17656 from civicrm/5.27
[civicrm-core.git] / api / v3 / UFField.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * This api exposes CiviCRM profile field.
14 *
15 * @package CiviCRM_APIv3
16 */
17
18 /**
19 * Defines 'uf field' within a group.
20 *
21 * @param array $params
22 * Array per getfields metadata.
23 *
24 * @throws API_Exception
25 *
26 * @return array
27 * Newly created $ufFieldArray
28 */
29 function civicrm_api3_uf_field_create($params) {
30 return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'UFField');
31 }
32
33 /**
34 * Adjust metadata for civicrm_uf_field create.
35 *
36 * @param array $params
37 */
38 function _civicrm_api3_uf_field_create_spec(&$params) {
39 $params['field_name']['api.required'] = TRUE;
40 $params['uf_group_id']['api.required'] = TRUE;
41
42 $params['option.autoweight'] = [
43 'title' => "Auto Weight",
44 'description' => "Automatically adjust weights in UFGroup to align with UFField",
45 'type' => CRM_Utils_Type::T_BOOLEAN,
46 'api.default' => TRUE,
47 ];
48 $params['is_active']['api.default'] = TRUE;
49 }
50
51 /**
52 * Returns array of uf groups (profiles) matching a set of one or more group properties.
53 *
54 * @param array $params
55 * Array per getfields metadata.
56 *
57 * @return array
58 */
59 function civicrm_api3_uf_field_get($params) {
60 return _civicrm_api3_basic_get('CRM_Core_BAO_UFField', $params);
61 }
62
63 /**
64 * Delete uf field.
65 *
66 * @param array $params
67 *
68 * @throws API_Exception
69 *
70 * @return array
71 */
72 function civicrm_api3_uf_field_delete($params) {
73 $fieldId = $params['id'];
74
75 $ufGroupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFField', $fieldId, 'uf_group_id');
76 if (!$ufGroupId) {
77 throw new API_Exception('Invalid value for field_id.');
78 }
79
80 $result = CRM_Core_BAO_UFField::del($fieldId);
81
82 $fieldsType = CRM_Core_BAO_UFGroup::calculateGroupType($ufGroupId, TRUE);
83 CRM_Core_BAO_UFGroup::updateGroupTypes($ufGroupId, $fieldsType);
84
85 return civicrm_api3_create_success($result, $params);
86 }
87
88 /**
89 * Field id accepted for backward compatibility - unset required on id.
90 *
91 * @param array $params
92 */
93 function _civicrm_api3_uf_field_delete_spec(&$params) {
94 // legacy support for field_id
95 $params['id']['api.aliases'] = ['field_id'];
96 }