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 | /** | |
4f940304 | 25 | * Retrieve DB object and copy to defaults array. |
6a488035 | 26 | * |
4f940304 | 27 | * @deprecated |
6a0b768e | 28 | * @param array $params |
6a0b768e | 29 | * @param array $defaults |
6a488035 | 30 | * |
4f940304 | 31 | * @return CRM_Core_DAO_OptionValue|NULL |
6a488035 | 32 | */ |
4f940304 CW |
33 | public static function retrieve($params, &$defaults) { |
34 | return CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_OptionValue', $params, $defaults); | |
6a488035 TO |
35 | } |
36 | ||
37 | /** | |
fe482240 | 38 | * Returns all active options ordered by weight for a given field. |
6a488035 | 39 | * |
6a0b768e TO |
40 | * @param int $fieldID |
41 | * Field whose options are needed. | |
317fceb4 | 42 | * @param bool $inactiveNeeded Do we need inactive options ?. |
6a0b768e | 43 | * Do we need inactive options ?. |
6a488035 | 44 | * |
a6c01b45 CW |
45 | * @return array |
46 | * all active options for fieldId | |
6a488035 | 47 | */ |
317fceb4 | 48 | public static function getCustomOption( |
6a488035 TO |
49 | $fieldID, |
50 | $inactiveNeeded = FALSE | |
51 | ) { | |
be2fb01f | 52 | $options = []; |
6a488035 TO |
53 | if (!$fieldID) { |
54 | return $options; | |
55 | } | |
56 | ||
be2fb01f | 57 | $optionValues = CRM_Core_PseudoConstant::get('CRM_Core_BAO_CustomField', 'custom_' . $fieldID, [], $inactiveNeeded ? 'get' : 'create'); |
6a488035 | 58 | |
e525d6af | 59 | foreach ((array) $optionValues as $value => $label) { |
be2fb01f | 60 | $options[] = [ |
b4fb2d23 CW |
61 | 'label' => $label, |
62 | 'value' => $value, | |
be2fb01f | 63 | ]; |
6a488035 TO |
64 | } |
65 | ||
6a488035 TO |
66 | return $options; |
67 | } | |
68 | ||
57c9c217 | 69 | /** |
09b6d7cd | 70 | * Wrapper for ajax option selector. |
57c9c217 | 71 | * |
72 | * @param array $params | |
73 | * Associated array for params record id. | |
74 | * | |
75 | * @return array | |
76 | * associated array of option list | |
77 | * -rp = rowcount | |
78 | * -page= offset | |
79 | */ | |
518fa0ee | 80 | public static function getOptionListSelector(&$params) { |
be2fb01f | 81 | $options = []; |
57c9c217 | 82 | |
09b6d7cd CW |
83 | $field = CRM_Core_BAO_CustomField::getFieldObject($params['fid']); |
84 | $defVal = CRM_Utils_Array::explodePadded($field->default_value); | |
57c9c217 | 85 | |
86 | // format the params | |
87 | $params['offset'] = ($params['page'] - 1) * $params['rp']; | |
88 | $params['rowCount'] = $params['rp']; | |
57c9c217 | 89 | |
09b6d7cd | 90 | if (!$field->option_group_id) { |
57c9c217 | 91 | return $options; |
92 | } | |
be2fb01f | 93 | $queryParams = [1 => [$field->option_group_id, 'Integer']]; |
57c9c217 | 94 | $total = "SELECT COUNT(*) FROM civicrm_option_value WHERE option_group_id = %1"; |
95 | $params['total'] = CRM_Core_DAO::singleValueQuery($total, $queryParams); | |
96 | ||
97 | $limit = " LIMIT {$params['offset']}, {$params['rowCount']} "; | |
57c9c217 | 98 | $orderBy = ' ORDER BY options.weight asc'; |
57c9c217 | 99 | |
100 | $query = "SELECT * FROM civicrm_option_value as options WHERE option_group_id = %1 {$orderBy} {$limit}"; | |
101 | $dao = CRM_Core_DAO::executeQuery($query, $queryParams); | |
102 | $links = CRM_Custom_Page_Option::actionLinks(); | |
103 | ||
be2fb01f | 104 | $fields = ['id', 'label', 'value']; |
57c9c217 | 105 | $config = CRM_Core_Config::singleton(); |
106 | while ($dao->fetch()) { | |
be2fb01f | 107 | $options[$dao->id] = []; |
57c9c217 | 108 | foreach ($fields as $k) { |
109 | $options[$dao->id][$k] = $dao->$k; | |
110 | } | |
111 | $action = array_sum(array_keys($links)); | |
112 | $class = 'crm-entity'; | |
113 | // update enable/disable links depending on custom_field properties. | |
114 | if ($dao->is_active) { | |
115 | $action -= CRM_Core_Action::ENABLE; | |
116 | } | |
117 | else { | |
118 | $class .= ' disabled'; | |
119 | $action -= CRM_Core_Action::DISABLE; | |
120 | } | |
87660a87 MD |
121 | |
122 | $isGroupLocked = (bool) CRM_Core_DAO::getFieldValue( | |
123 | CRM_Core_DAO_OptionGroup::class, | |
124 | $field->option_group_id, | |
125 | 'is_locked' | |
126 | ); | |
127 | ||
128 | // disable deletion of option values for locked option groups | |
218d9fd2 | 129 | if (($action & CRM_Core_Action::DELETE) && $isGroupLocked) { |
87660a87 MD |
130 | $action -= CRM_Core_Action::DELETE; |
131 | } | |
132 | ||
114e8771 | 133 | if ($field->html_type == 'CheckBox' || ($field->html_type == 'Select' && $field->serialize == 1)) { |
bc37053c | 134 | $options[$dao->id]['is_default'] = (isset($defVal) && in_array($dao->value, $defVal)); |
57c9c217 | 135 | } |
136 | else { | |
bc37053c | 137 | $options[$dao->id]['is_default'] = ($field->default_value == $dao->value); |
57c9c217 | 138 | } |
4247b886 | 139 | $options[$dao->id]['description'] = $dao->description; |
57c9c217 | 140 | $options[$dao->id]['class'] = $dao->id . ',' . $class; |
09b6d7cd | 141 | $options[$dao->id]['is_active'] = empty($dao->is_active) ? ts('No') : ts('Yes'); |
57c9c217 | 142 | $options[$dao->id]['links'] = CRM_Core_Action::formLink($links, |
143 | $action, | |
be2fb01f | 144 | [ |
57c9c217 | 145 | 'id' => $dao->id, |
146 | 'fid' => $params['fid'], | |
147 | 'gid' => $params['gid'], | |
be2fb01f | 148 | ], |
57c9c217 | 149 | ts('more'), |
150 | FALSE, | |
151 | 'customOption.row.actions', | |
152 | 'customOption', | |
153 | $dao->id | |
154 | ); | |
155 | } | |
57c9c217 | 156 | |
157 | return $options; | |
158 | } | |
159 | ||
6a488035 | 160 | /** |
fe482240 | 161 | * Delete Option. |
6a488035 | 162 | * |
a2f24340 | 163 | * @param int $optionId |
16b10e64 | 164 | * option id |
6a488035 | 165 | * |
6a488035 | 166 | */ |
00be9182 | 167 | public static function del($optionId) { |
6a488035 TO |
168 | // get the customFieldID |
169 | $query = " | |
170 | SELECT f.id as id, f.data_type as dataType | |
171 | FROM civicrm_option_value v, | |
172 | civicrm_option_group g, | |
173 | civicrm_custom_field f | |
174 | WHERE v.id = %1 | |
175 | AND g.id = f.option_group_id | |
176 | AND g.id = v.option_group_id"; | |
be2fb01f | 177 | $params = [1 => [$optionId, 'Integer']]; |
6a488035 TO |
178 | $dao = CRM_Core_DAO::executeQuery($query, $params); |
179 | if ($dao->fetch()) { | |
180 | if (in_array($dao->dataType, | |
be2fb01f | 181 | ['Int', 'Float', 'Money', 'Boolean'] |
353ffa53 | 182 | )) { |
6a488035 TO |
183 | $value = 0; |
184 | } | |
185 | else { | |
186 | $value = ''; | |
187 | } | |
be2fb01f | 188 | $params = [ |
6a488035 TO |
189 | 'optionId' => $optionId, |
190 | 'fieldId' => $dao->id, | |
191 | 'value' => $value, | |
be2fb01f | 192 | ]; |
6a488035 | 193 | // delete this value from the tables |
53c38f51 | 194 | self::updateValue($optionId, $value); |
6a488035 TO |
195 | |
196 | // also delete this option value | |
5d9ff47e | 197 | CRM_Core_BAO_OptionValue::deleteRecord(['id' => $optionId]); |
6a488035 TO |
198 | } |
199 | } | |
200 | ||
58eaa092 CW |
201 | /** |
202 | * When changing the value of an option this is called to update all corresponding custom data | |
203 | * | |
204 | * @param int $optionId | |
205 | * @param string $newValue | |
206 | */ | |
207 | public static function updateValue($optionId, $newValue) { | |
208 | $optionValue = new CRM_Core_DAO_OptionValue(); | |
209 | $optionValue->id = $optionId; | |
210 | $optionValue->find(TRUE); | |
211 | $oldValue = $optionValue->value; | |
212 | if ($oldValue == $newValue) { | |
213 | return; | |
214 | } | |
215 | ||
216 | $customField = new CRM_Core_DAO_CustomField(); | |
217 | $customField->option_group_id = $optionValue->option_group_id; | |
218 | $customField->find(); | |
219 | while ($customField->fetch()) { | |
220 | $customGroup = new CRM_Core_DAO_CustomGroup(); | |
221 | $customGroup->id = $customField->custom_group_id; | |
222 | $customGroup->find(TRUE); | |
223 | if (CRM_Core_BAO_CustomField::isSerialized($customField)) { | |
be2fb01f CW |
224 | $params = [ |
225 | 1 => [CRM_Utils_Array::implodePadded($oldValue), 'String'], | |
226 | 2 => [CRM_Utils_Array::implodePadded($newValue), 'String'], | |
227 | 3 => ['%' . CRM_Utils_Array::implodePadded($oldValue) . '%', 'String'], | |
228 | ]; | |
58eaa092 CW |
229 | } |
230 | else { | |
be2fb01f CW |
231 | $params = [ |
232 | 1 => [$oldValue, 'String'], | |
233 | 2 => [$newValue, 'String'], | |
234 | 3 => [$oldValue, 'String'], | |
235 | ]; | |
58eaa092 CW |
236 | } |
237 | $sql = "UPDATE `{$customGroup->table_name}` SET `{$customField->column_name}` = REPLACE(`{$customField->column_name}`, %1, %2) WHERE `{$customField->column_name}` LIKE %3"; | |
58eaa092 CW |
238 | CRM_Core_DAO::executeQuery($sql, $params); |
239 | } | |
58eaa092 CW |
240 | } |
241 | ||
6a488035 | 242 | } |