3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
21 * Business objects for managing custom data options.
24 class CRM_Core_BAO_CustomOption
{
27 * Fetch object based on array of properties.
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.
34 * @return CRM_Core_BAO_CustomOption
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);
47 * Returns all active options ordered by weight for a given field.
50 * Field whose options are needed.
51 * @param bool $inactiveNeeded Do we need inactive options ?.
52 * Do we need inactive options ?.
55 * all active options for fieldId
57 public static function getCustomOption(
59 $inactiveNeeded = FALSE
66 $optionValues = CRM_Core_PseudoConstant
::get('CRM_Core_BAO_CustomField', 'custom_' . $fieldID, [], $inactiveNeeded ?
'get' : 'create');
68 foreach ((array) $optionValues as $value => $label) {
79 * Wrapper for ajax option selector.
81 * @param array $params
82 * Associated array for params record id.
85 * associated array of option list
89 public static function getOptionListSelector(&$params) {
92 $field = CRM_Core_BAO_CustomField
::getFieldObject($params['fid']);
93 $defVal = CRM_Utils_Array
::explodePadded($field->default_value
);
96 $params['offset'] = ($params['page'] - 1) * $params['rp'];
97 $params['rowCount'] = $params['rp'];
99 if (!$field->option_group_id
) {
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);
106 $limit = " LIMIT {$params['offset']}, {$params['rowCount']} ";
107 $orderBy = ' ORDER BY options.weight asc';
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();
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;
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
;
127 $class .= ' disabled';
128 $action -= CRM_Core_Action
::DISABLE
;
131 $isGroupLocked = (bool) CRM_Core_DAO
::getFieldValue(
132 CRM_Core_DAO_OptionGroup
::class,
133 $field->option_group_id
,
137 // disable deletion of option values for locked option groups
138 if (($action & CRM_Core_Action
::DELETE
) && $isGroupLocked) {
139 $action -= CRM_Core_Action
::DELETE
;
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" />';
147 $options[$dao->id
]['is_default'] = '';
151 if ($field->default_value
== $dao->value
) {
152 $options[$dao->id
]['is_default'] = '<img src="' . $config->resourceBase
. 'i/check.gif" />';
155 $options[$dao->id
]['is_default'] = '';
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,
165 'fid' => $params['fid'],
166 'gid' => $params['gid'],
170 'customOption.row.actions',
182 * @param $optionId integer
186 public static function del($optionId) {
187 // get the customFieldID
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
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);
199 if (in_array($dao->dataType
,
200 ['Int', 'Float', 'Money', 'Boolean']
208 'optionId' => $optionId,
209 'fieldId' => $dao->id
,
212 // delete this value from the tables
213 self
::updateCustomValues($params);
215 // also delete this option value
218 FROM civicrm_option_value
220 $params = [1 => [$optionId, 'Integer']];
221 CRM_Core_DAO
::executeQuery($query, $params);
226 * @param array $params
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
;
236 // get the table, column, html_type and data type for this field
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
246 $queryParams = [1 => [$params['fieldId'], 'Integer']];
247 $dao = CRM_Core_DAO
::executeQuery($query, $queryParams);
249 if ($dao->dataType
== 'Money') {
250 $params['value'] = CRM_Utils_Rule
::cleanMoney($params['value']);
252 switch ($dao->htmlType
) {
253 case 'Autocomplete-Select':
257 UPDATE {$dao->tableName}
258 SET {$dao->columnName} = %1
260 if ($dao->dataType
== 'Auto-complete') {
261 $dataType = "String";
264 $dataType = $dao->dataType
;
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
;
283 UPDATE {$dao->tableName}
284 SET {$dao->columnName} = REPLACE( {$dao->columnName}, %1, %2 )";
286 1 => [$oldString, 'String'],
287 2 => [$newString, 'String'],
292 CRM_Core_Error
::fatal();
294 $dao = CRM_Core_DAO
::executeQuery($query, $queryParams);
299 * When changing the value of an option this is called to update all corresponding custom data
301 * @param int $optionId
302 * @param string $newValue
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) {
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)) {
322 1 => [CRM_Utils_Array
::implodePadded($oldValue), 'String'],
323 2 => [CRM_Utils_Array
::implodePadded($newValue), 'String'],
324 3 => ['%' . CRM_Utils_Array
::implodePadded($oldValue) . '%', 'String'],
329 1 => [$oldValue, 'String'],
330 2 => [$newValue, 'String'],
331 3 => [$oldValue, 'String'],
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);