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