Merge pull request #23999 from seamuslee001/null_part3_php81
[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 */
17
18 /**
19 * Business objects for managing custom data options.
20 *
21 */
22 class CRM_Core_BAO_CustomOption {
23
24 /**
25 * Retrieve DB object and copy to defaults array.
26 *
27 * @deprecated
28 * @param array $params
29 * @param array $defaults
30 *
31 * @return CRM_Core_DAO_OptionValue|NULL
32 */
33 public static function retrieve($params, &$defaults) {
34 return CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_OptionValue', $params, $defaults);
35 }
36
37 /**
38 * Returns all active options ordered by weight for a given field.
39 *
40 * @param int $fieldID
41 * Field whose options are needed.
42 * @param bool $inactiveNeeded Do we need inactive options ?.
43 * Do we need inactive options ?.
44 *
45 * @return array
46 * all active options for fieldId
47 */
48 public static function getCustomOption(
49 $fieldID,
50 $inactiveNeeded = FALSE
51 ) {
52 $options = [];
53 if (!$fieldID) {
54 return $options;
55 }
56
57 $optionValues = CRM_Core_PseudoConstant::get('CRM_Core_BAO_CustomField', 'custom_' . $fieldID, [], $inactiveNeeded ? 'get' : 'create');
58
59 foreach ((array) $optionValues as $value => $label) {
60 $options[] = [
61 'label' => $label,
62 'value' => $value,
63 ];
64 }
65
66 return $options;
67 }
68
69 /**
70 * Wrapper for ajax option selector.
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 */
80 public static function getOptionListSelector(&$params) {
81 $options = [];
82
83 $field = CRM_Core_BAO_CustomField::getFieldObject($params['fid']);
84 $defVal = CRM_Utils_Array::explodePadded($field->default_value);
85
86 // format the params
87 $params['offset'] = ($params['page'] - 1) * $params['rp'];
88 $params['rowCount'] = $params['rp'];
89
90 if (!$field->option_group_id) {
91 return $options;
92 }
93 $queryParams = [1 => [$field->option_group_id, 'Integer']];
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']} ";
98 $orderBy = ' ORDER BY options.weight asc';
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
104 $fields = ['id', 'label', 'value'];
105 $config = CRM_Core_Config::singleton();
106 while ($dao->fetch()) {
107 $options[$dao->id] = [];
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 }
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
129 if (($action & CRM_Core_Action::DELETE) && $isGroupLocked) {
130 $action -= CRM_Core_Action::DELETE;
131 }
132
133 if ($field->html_type == 'CheckBox' || ($field->html_type == 'Select' && $field->serialize == 1)) {
134 $options[$dao->id]['is_default'] = (isset($defVal) && in_array($dao->value, $defVal));
135 }
136 else {
137 $options[$dao->id]['is_default'] = ($field->default_value == $dao->value);
138 }
139 $options[$dao->id]['description'] = $dao->description;
140 $options[$dao->id]['class'] = $dao->id . ',' . $class;
141 $options[$dao->id]['is_active'] = empty($dao->is_active) ? ts('No') : ts('Yes');
142 $options[$dao->id]['links'] = CRM_Core_Action::formLink($links,
143 $action,
144 [
145 'id' => $dao->id,
146 'fid' => $params['fid'],
147 'gid' => $params['gid'],
148 ],
149 ts('more'),
150 FALSE,
151 'customOption.row.actions',
152 'customOption',
153 $dao->id
154 );
155 }
156
157 return $options;
158 }
159
160 /**
161 * Delete Option.
162 *
163 * @param int $optionId
164 * option id
165 *
166 */
167 public static function del($optionId) {
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";
177 $params = [1 => [$optionId, 'Integer']];
178 $dao = CRM_Core_DAO::executeQuery($query, $params);
179 if ($dao->fetch()) {
180 if (in_array($dao->dataType,
181 ['Int', 'Float', 'Money', 'Boolean']
182 )) {
183 $value = 0;
184 }
185 else {
186 $value = '';
187 }
188 $params = [
189 'optionId' => $optionId,
190 'fieldId' => $dao->id,
191 'value' => $value,
192 ];
193 // delete this value from the tables
194 self::updateValue($optionId, $value);
195
196 // also delete this option value
197 CRM_Core_BAO_OptionValue::deleteRecord(['id' => $optionId]);
198 }
199 }
200
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)) {
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 ];
229 }
230 else {
231 $params = [
232 1 => [$oldValue, 'String'],
233 2 => [$newValue, 'String'],
234 3 => [$oldValue, 'String'],
235 ];
236 }
237 $sql = "UPDATE `{$customGroup->table_name}` SET `{$customField->column_name}` = REPLACE(`{$customField->column_name}`, %1, %2) WHERE `{$customField->column_name}` LIKE %3";
238 CRM_Core_DAO::executeQuery($sql, $params);
239 }
240 }
241
242 }