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