More comment fixes
[civicrm-core.git] / api / v3 / UFField.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 * File for the CiviCRM APIv3 user framework group functions
30 *
31 * @package CiviCRM_APIv3
32 * @subpackage API_UF
33 *
34 * @copyright CiviCRM LLC (c) 2004-2014
35 * @version $Id: UFField.php 30171 2010-10-14 09:11:27Z mover $
36 */
37
38 /**
39 * Defines 'uf field' within a group.
40 *
41 * @param array $params
42 * Array per getfields metadata.
43 *
44 * @throws API_Exception
45 *
46 * @return array
47 * Newly created $ufFieldArray
48 */
49 function civicrm_api3_uf_field_create($params) {
50 civicrm_api3_verify_one_mandatory($params, NULL, array('field_name', 'uf_group_id'));
51 $groupId = CRM_Utils_Array::value('uf_group_id', $params);
52 if ((int) $groupId < 1) {
53 throw new API_Exception('Params must be a field_name-carrying array and a positive integer.');
54 }
55
56 $field_type = CRM_Utils_Array::value('field_type', $params);
57 $field_name = CRM_Utils_Array::value('field_name', $params);
58 $location_type_id = CRM_Utils_Array::value('location_type_id', $params);
59 $phone_type = CRM_Utils_Array::value('phone_type_id', $params, CRM_Utils_Array::value('phone_type', $params));
60
61 if (!CRM_Core_BAO_UFField::isValidFieldName($field_name)) {
62 throw new API_Exception('The field_name is not valid');
63 }
64 $params['field_name'] = array($field_type, $field_name, $location_type_id, $phone_type);
65
66 if (!(CRM_Utils_Array::value('group_id', $params))) {
67 $params['group_id'] = $groupId;
68 }
69
70 $ids = $ufFieldArray = array();
71 $ids['uf_group'] = $groupId;
72
73 $fieldId = CRM_Utils_Array::value('id', $params);
74 if (!empty($fieldId)) {
75 $UFField = new CRM_Core_BAO_UFField();
76 $UFField->id = $fieldId;
77 if ($UFField->find(TRUE)) {
78 $ids['uf_group'] = $UFField->uf_group_id;
79 if (!(CRM_Utils_Array::value('group_id', $params))) {
80 // this copied here from previous api function - not sure if required
81 $params['group_id'] = $UFField->uf_group_id;
82 }
83 }
84 else {
85 throw new API_Exception("there is no field for this fieldId");
86 }
87 $ids['uf_field'] = $fieldId;
88 }
89
90 if (CRM_Core_BAO_UFField::duplicateField($params, $ids)) {
91 throw new API_Exception("The field was not added. It already exists in this profile.");
92 }
93 //@todo why is this even optional? Surely weight should just be 'managed' ??
94 if (CRM_Utils_Array::value('option.autoweight', $params, TRUE)) {
95 $params['weight'] = CRM_Core_BAO_UFField::autoWeight($params);
96 }
97 $ufField = CRM_Core_BAO_UFField::add($params, $ids);
98
99 $fieldsType = CRM_Core_BAO_UFGroup::calculateGroupType($groupId, TRUE);
100 CRM_Core_BAO_UFGroup::updateGroupTypes($groupId, $fieldsType);
101
102 _civicrm_api3_object_to_array($ufField, $ufFieldArray[$ufField->id]);
103 civicrm_api3('profile', 'getfields', array('cache_clear' => TRUE));
104 return civicrm_api3_create_success($ufFieldArray, $params);
105 }
106
107 /**
108 * Adjust metadata for civicrm_uf_field create.
109 *
110 * @param array $params
111 */
112 function _civicrm_api3_uf_field_create_spec(&$params) {
113 $params['option.autoweight'] = array(
114 'title' => "Automatically adjust weights in UFGroup to align with UFField",
115 'type' => CRM_Utils_Type::T_BOOLEAN,
116 'api.default' => TRUE,
117 );
118 $params['created_id']['api.default'] = 'user_contact_id';
119 $params['created_id']['title'] = 'Created By';
120 $params['is_active']['api.default'] = TRUE;
121 }
122
123 /**
124 * Returns array of uf groups (profiles) matching a set of one or more group properties.
125 *
126 * @param array $params
127 * Array per getfields metadata.
128 *
129 * @return array
130 */
131 function civicrm_api3_uf_field_get($params) {
132 return _civicrm_api3_basic_get('CRM_Core_BAO_UFField', $params);
133 }
134
135 /**
136 * Delete uf field.
137 *
138 * @param array $params
139 *
140 * @throws API_Exception
141 *
142 * @return array
143 */
144 function civicrm_api3_uf_field_delete($params) {
145 $fieldId = $params['id'];
146
147 $ufGroupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFField', $fieldId, 'uf_group_id');
148 if (!$ufGroupId) {
149 throw new API_Exception('Invalid value for field_id.');
150 }
151
152 $result = CRM_Core_BAO_UFField::del($fieldId);
153
154 $fieldsType = CRM_Core_BAO_UFGroup::calculateGroupType($ufGroupId, TRUE);
155 CRM_Core_BAO_UFGroup::updateGroupTypes($ufGroupId, $fieldsType);
156
157 return civicrm_api3_create_success($result, $params);
158 }
159
160 /**
161 * Field id accepted for backward compatibility - unset required on id.
162 *
163 * @param array $params
164 */
165 function _civicrm_api3_uf_field_delete_spec(&$params) {
166 // legacy support for field_id
167 $params['id']['api.aliases'] = array('field_id');
168 }