Merge pull request #14981 from eileenmcnaughton/load_extract
[civicrm-core.git] / api / v3 / UFField.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * This api exposes CiviCRM profile field.
30 *
31 * @package CiviCRM_APIv3
32 */
33
34 /**
35 * Defines 'uf field' within a group.
36 *
37 * @param array $params
38 * Array per getfields metadata.
39 *
40 * @throws API_Exception
41 *
42 * @return array
43 * Newly created $ufFieldArray
44 */
45 function civicrm_api3_uf_field_create($params) {
46 return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'UFField');
47 }
48
49 /**
50 * Adjust metadata for civicrm_uf_field create.
51 *
52 * @param array $params
53 */
54 function _civicrm_api3_uf_field_create_spec(&$params) {
55 $params['field_name']['api.required'] = TRUE;
56 $params['uf_group_id']['api.required'] = TRUE;
57
58 $params['option.autoweight'] = [
59 'title' => "Auto Weight",
60 'description' => "Automatically adjust weights in UFGroup to align with UFField",
61 'type' => CRM_Utils_Type::T_BOOLEAN,
62 'api.default' => TRUE,
63 ];
64 $params['is_active']['api.default'] = TRUE;
65 }
66
67 /**
68 * Returns array of uf groups (profiles) matching a set of one or more group properties.
69 *
70 * @param array $params
71 * Array per getfields metadata.
72 *
73 * @return array
74 */
75 function civicrm_api3_uf_field_get($params) {
76 return _civicrm_api3_basic_get('CRM_Core_BAO_UFField', $params);
77 }
78
79 /**
80 * Delete uf field.
81 *
82 * @param array $params
83 *
84 * @throws API_Exception
85 *
86 * @return array
87 */
88 function civicrm_api3_uf_field_delete($params) {
89 $fieldId = $params['id'];
90
91 $ufGroupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFField', $fieldId, 'uf_group_id');
92 if (!$ufGroupId) {
93 throw new API_Exception('Invalid value for field_id.');
94 }
95
96 $result = CRM_Core_BAO_UFField::del($fieldId);
97
98 $fieldsType = CRM_Core_BAO_UFGroup::calculateGroupType($ufGroupId, TRUE);
99 CRM_Core_BAO_UFGroup::updateGroupTypes($ufGroupId, $fieldsType);
100
101 return civicrm_api3_create_success($result, $params);
102 }
103
104 /**
105 * Field id accepted for backward compatibility - unset required on id.
106 *
107 * @param array $params
108 */
109 function _civicrm_api3_uf_field_delete_spec(&$params) {
110 // legacy support for field_id
111 $params['id']['api.aliases'] = ['field_id'];
112 }