Merge pull request #16671 from eileenmcnaughton/acl
[civicrm-core.git] / CRM / Core / BAO / CustomOption.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 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 * $Id$
17 *
18 */
19
20 /**
21 * Business objects for managing custom data options.
22 *
23 */
24 class CRM_Core_BAO_CustomOption {
25
26 /**
27 * Fetch object based on array of properties.
28 *
29 * @param array $params
30 * (reference ) an assoc array of name/value pairs.
31 * @param array $defaults
32 * (reference ) an assoc array to hold the flattened values.
33 *
34 * @return CRM_Core_BAO_CustomOption
35 */
36 public static function retrieve(&$params, &$defaults) {
37 $customOption = new CRM_Core_DAO_OptionValue();
38 $customOption->copyValues($params);
39 if ($customOption->find(TRUE)) {
40 CRM_Core_DAO::storeValues($customOption, $defaults);
41 return $customOption;
42 }
43 return NULL;
44 }
45
46 /**
47 * Returns all active options ordered by weight for a given field.
48 *
49 * @param int $fieldID
50 * Field whose options are needed.
51 * @param bool $inactiveNeeded Do we need inactive options ?.
52 * Do we need inactive options ?.
53 *
54 * @return array
55 * all active options for fieldId
56 */
57 public static function getCustomOption(
58 $fieldID,
59 $inactiveNeeded = FALSE
60 ) {
61 $options = [];
62 if (!$fieldID) {
63 return $options;
64 }
65
66 $optionValues = CRM_Core_PseudoConstant::get('CRM_Core_BAO_CustomField', 'custom_' . $fieldID, [], $inactiveNeeded ? 'get' : 'create');
67
68 foreach ((array) $optionValues as $value => $label) {
69 $options[] = [
70 'label' => $label,
71 'value' => $value,
72 ];
73 }
74
75 return $options;
76 }
77
78 /**
79 * Wrapper for ajax option selector.
80 *
81 * @param array $params
82 * Associated array for params record id.
83 *
84 * @return array
85 * associated array of option list
86 * -rp = rowcount
87 * -page= offset
88 */
89 public static function getOptionListSelector(&$params) {
90 $options = [];
91
92 $field = CRM_Core_BAO_CustomField::getFieldObject($params['fid']);
93 $defVal = CRM_Utils_Array::explodePadded($field->default_value);
94
95 // format the params
96 $params['offset'] = ($params['page'] - 1) * $params['rp'];
97 $params['rowCount'] = $params['rp'];
98
99 if (!$field->option_group_id) {
100 return $options;
101 }
102 $queryParams = [1 => [$field->option_group_id, 'Integer']];
103 $total = "SELECT COUNT(*) FROM civicrm_option_value WHERE option_group_id = %1";
104 $params['total'] = CRM_Core_DAO::singleValueQuery($total, $queryParams);
105
106 $limit = " LIMIT {$params['offset']}, {$params['rowCount']} ";
107 $orderBy = ' ORDER BY options.weight asc';
108
109 $query = "SELECT * FROM civicrm_option_value as options WHERE option_group_id = %1 {$orderBy} {$limit}";
110 $dao = CRM_Core_DAO::executeQuery($query, $queryParams);
111 $links = CRM_Custom_Page_Option::actionLinks();
112
113 $fields = ['id', 'label', 'value'];
114 $config = CRM_Core_Config::singleton();
115 while ($dao->fetch()) {
116 $options[$dao->id] = [];
117 foreach ($fields as $k) {
118 $options[$dao->id][$k] = $dao->$k;
119 }
120 $action = array_sum(array_keys($links));
121 $class = 'crm-entity';
122 // update enable/disable links depending on custom_field properties.
123 if ($dao->is_active) {
124 $action -= CRM_Core_Action::ENABLE;
125 }
126 else {
127 $class .= ' disabled';
128 $action -= CRM_Core_Action::DISABLE;
129 }
130
131 $isGroupLocked = (bool) CRM_Core_DAO::getFieldValue(
132 CRM_Core_DAO_OptionGroup::class,
133 $field->option_group_id,
134 'is_locked'
135 );
136
137 // disable deletion of option values for locked option groups
138 if (($action & CRM_Core_Action::DELETE) && $isGroupLocked) {
139 $action -= CRM_Core_Action::DELETE;
140 }
141
142 if (in_array($field->html_type, ['CheckBox', 'Multi-Select'])) {
143 if (isset($defVal) && in_array($dao->value, $defVal)) {
144 $options[$dao->id]['is_default'] = '<img src="' . $config->resourceBase . 'i/check.gif" />';
145 }
146 else {
147 $options[$dao->id]['is_default'] = '';
148 }
149 }
150 else {
151 if ($field->default_value == $dao->value) {
152 $options[$dao->id]['is_default'] = '<img src="' . $config->resourceBase . 'i/check.gif" />';
153 }
154 else {
155 $options[$dao->id]['is_default'] = '';
156 }
157 }
158 $options[$dao->id]['description'] = $dao->description;
159 $options[$dao->id]['class'] = $dao->id . ',' . $class;
160 $options[$dao->id]['is_active'] = empty($dao->is_active) ? ts('No') : ts('Yes');
161 $options[$dao->id]['links'] = CRM_Core_Action::formLink($links,
162 $action,
163 [
164 'id' => $dao->id,
165 'fid' => $params['fid'],
166 'gid' => $params['gid'],
167 ],
168 ts('more'),
169 FALSE,
170 'customOption.row.actions',
171 'customOption',
172 $dao->id
173 );
174 }
175
176 return $options;
177 }
178
179 /**
180 * Delete Option.
181 *
182 * @param $optionId integer
183 * option id
184 *
185 */
186 public static function del($optionId) {
187 // get the customFieldID
188 $query = "
189 SELECT f.id as id, f.data_type as dataType
190 FROM civicrm_option_value v,
191 civicrm_option_group g,
192 civicrm_custom_field f
193 WHERE v.id = %1
194 AND g.id = f.option_group_id
195 AND g.id = v.option_group_id";
196 $params = [1 => [$optionId, 'Integer']];
197 $dao = CRM_Core_DAO::executeQuery($query, $params);
198 if ($dao->fetch()) {
199 if (in_array($dao->dataType,
200 ['Int', 'Float', 'Money', 'Boolean']
201 )) {
202 $value = 0;
203 }
204 else {
205 $value = '';
206 }
207 $params = [
208 'optionId' => $optionId,
209 'fieldId' => $dao->id,
210 'value' => $value,
211 ];
212 // delete this value from the tables
213 self::updateCustomValues($params);
214
215 // also delete this option value
216 $query = "
217 DELETE
218 FROM civicrm_option_value
219 WHERE id = %1";
220 $params = [1 => [$optionId, 'Integer']];
221 CRM_Core_DAO::executeQuery($query, $params);
222 }
223 }
224
225 /**
226 * @param array $params
227 *
228 * @throws Exception
229 */
230 public static function updateCustomValues($params) {
231 $optionDAO = new CRM_Core_DAO_OptionValue();
232 $optionDAO->id = $params['optionId'];
233 $optionDAO->find(TRUE);
234 $oldValue = $optionDAO->value;
235
236 // get the table, column, html_type and data type for this field
237 $query = "
238 SELECT g.table_name as tableName ,
239 f.column_name as columnName,
240 f.data_type as dataType,
241 f.html_type as htmlType
242 FROM civicrm_custom_group g,
243 civicrm_custom_field f
244 WHERE f.custom_group_id = g.id
245 AND f.id = %1";
246 $queryParams = [1 => [$params['fieldId'], 'Integer']];
247 $dao = CRM_Core_DAO::executeQuery($query, $queryParams);
248 if ($dao->fetch()) {
249 if ($dao->dataType == 'Money') {
250 $params['value'] = CRM_Utils_Rule::cleanMoney($params['value']);
251 }
252 switch ($dao->htmlType) {
253 case 'Autocomplete-Select':
254 case 'Select':
255 case 'Radio':
256 $query = "
257 UPDATE {$dao->tableName}
258 SET {$dao->columnName} = %1
259 WHERE id = %2";
260 if ($dao->dataType == 'Auto-complete') {
261 $dataType = "String";
262 }
263 else {
264 $dataType = $dao->dataType;
265 }
266 $queryParams = [
267 1 => [
268 $params['value'],
269 $dataType,
270 ],
271 2 => [
272 $params['optionId'],
273 'Integer',
274 ],
275 ];
276 break;
277
278 case 'Multi-Select':
279 case 'CheckBox':
280 $oldString = CRM_Core_DAO::VALUE_SEPARATOR . $oldValue . CRM_Core_DAO::VALUE_SEPARATOR;
281 $newString = CRM_Core_DAO::VALUE_SEPARATOR . $params['value'] . CRM_Core_DAO::VALUE_SEPARATOR;
282 $query = "
283 UPDATE {$dao->tableName}
284 SET {$dao->columnName} = REPLACE( {$dao->columnName}, %1, %2 )";
285 $queryParams = [
286 1 => [$oldString, 'String'],
287 2 => [$newString, 'String'],
288 ];
289 break;
290
291 default:
292 CRM_Core_Error::fatal();
293 }
294 $dao = CRM_Core_DAO::executeQuery($query, $queryParams);
295 }
296 }
297
298 /**
299 * When changing the value of an option this is called to update all corresponding custom data
300 *
301 * @param int $optionId
302 * @param string $newValue
303 */
304 public static function updateValue($optionId, $newValue) {
305 $optionValue = new CRM_Core_DAO_OptionValue();
306 $optionValue->id = $optionId;
307 $optionValue->find(TRUE);
308 $oldValue = $optionValue->value;
309 if ($oldValue == $newValue) {
310 return;
311 }
312
313 $customField = new CRM_Core_DAO_CustomField();
314 $customField->option_group_id = $optionValue->option_group_id;
315 $customField->find();
316 while ($customField->fetch()) {
317 $customGroup = new CRM_Core_DAO_CustomGroup();
318 $customGroup->id = $customField->custom_group_id;
319 $customGroup->find(TRUE);
320 if (CRM_Core_BAO_CustomField::isSerialized($customField)) {
321 $params = [
322 1 => [CRM_Utils_Array::implodePadded($oldValue), 'String'],
323 2 => [CRM_Utils_Array::implodePadded($newValue), 'String'],
324 3 => ['%' . CRM_Utils_Array::implodePadded($oldValue) . '%', 'String'],
325 ];
326 }
327 else {
328 $params = [
329 1 => [$oldValue, 'String'],
330 2 => [$newValue, 'String'],
331 3 => [$oldValue, 'String'],
332 ];
333 }
334 $sql = "UPDATE `{$customGroup->table_name}` SET `{$customField->column_name}` = REPLACE(`{$customField->column_name}`, %1, %2) WHERE `{$customField->column_name}` LIKE %3";
335 CRM_Core_DAO::executeQuery($sql, $params);
336 }
337 }
338
339 }