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
19 * Business objects for managing custom data options.
22 class CRM_Core_BAO_CustomOption
{
25 * Fetch object based on array of properties.
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.
32 * @return CRM_Core_BAO_CustomOption
34 public static function retrieve(&$params, &$defaults) {
35 $customOption = new CRM_Core_DAO_OptionValue();
36 $customOption->copyValues($params);
37 if ($customOption->find(TRUE)) {
38 CRM_Core_DAO
::storeValues($customOption, $defaults);
45 * Returns all active options ordered by weight for a given field.
48 * Field whose options are needed.
49 * @param bool $inactiveNeeded Do we need inactive options ?.
50 * Do we need inactive options ?.
53 * all active options for fieldId
55 public static function getCustomOption(
57 $inactiveNeeded = FALSE
64 $optionValues = CRM_Core_PseudoConstant
::get('CRM_Core_BAO_CustomField', 'custom_' . $fieldID, [], $inactiveNeeded ?
'get' : 'create');
66 foreach ((array) $optionValues as $value => $label) {
77 * Wrapper for ajax option selector.
79 * @param array $params
80 * Associated array for params record id.
83 * associated array of option list
87 public static function getOptionListSelector(&$params) {
90 $field = CRM_Core_BAO_CustomField
::getFieldObject($params['fid']);
91 $defVal = CRM_Utils_Array
::explodePadded($field->default_value
);
94 $params['offset'] = ($params['page'] - 1) * $params['rp'];
95 $params['rowCount'] = $params['rp'];
97 if (!$field->option_group_id
) {
100 $queryParams = [1 => [$field->option_group_id
, 'Integer']];
101 $total = "SELECT COUNT(*) FROM civicrm_option_value WHERE option_group_id = %1";
102 $params['total'] = CRM_Core_DAO
::singleValueQuery($total, $queryParams);
104 $limit = " LIMIT {$params['offset']}, {$params['rowCount']} ";
105 $orderBy = ' ORDER BY options.weight asc';
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();
111 $fields = ['id', 'label', 'value'];
112 $config = CRM_Core_Config
::singleton();
113 while ($dao->fetch()) {
114 $options[$dao->id
] = [];
115 foreach ($fields as $k) {
116 $options[$dao->id
][$k] = $dao->$k;
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
;
125 $class .= ' disabled';
126 $action -= CRM_Core_Action
::DISABLE
;
129 $isGroupLocked = (bool) CRM_Core_DAO
::getFieldValue(
130 CRM_Core_DAO_OptionGroup
::class,
131 $field->option_group_id
,
135 // disable deletion of option values for locked option groups
136 if (($action & CRM_Core_Action
::DELETE
) && $isGroupLocked) {
137 $action -= CRM_Core_Action
::DELETE
;
140 if ($field->html_type
== 'CheckBox' ||
($field->html_type
== 'Select' && $field->serialize
== 1)) {
141 $options[$dao->id
]['is_default'] = (isset($defVal) && in_array($dao->value
, $defVal));
144 $options[$dao->id
]['is_default'] = ($field->default_value
== $dao->value
);
146 $options[$dao->id
]['description'] = $dao->description
;
147 $options[$dao->id
]['class'] = $dao->id
. ',' . $class;
148 $options[$dao->id
]['is_active'] = empty($dao->is_active
) ?
ts('No') : ts('Yes');
149 $options[$dao->id
]['links'] = CRM_Core_Action
::formLink($links,
153 'fid' => $params['fid'],
154 'gid' => $params['gid'],
158 'customOption.row.actions',
170 * @param $optionId integer
174 public static function del($optionId) {
175 // get the customFieldID
177 SELECT f.id as id, f.data_type as dataType
178 FROM civicrm_option_value v,
179 civicrm_option_group g,
180 civicrm_custom_field f
182 AND g.id = f.option_group_id
183 AND g.id = v.option_group_id";
184 $params = [1 => [$optionId, 'Integer']];
185 $dao = CRM_Core_DAO
::executeQuery($query, $params);
187 if (in_array($dao->dataType
,
188 ['Int', 'Float', 'Money', 'Boolean']
196 'optionId' => $optionId,
197 'fieldId' => $dao->id
,
200 // delete this value from the tables
201 self
::updateValue($optionId, $value);
203 // also delete this option value
206 FROM civicrm_option_value
208 $params = [1 => [$optionId, 'Integer']];
209 CRM_Core_DAO
::executeQuery($query, $params);
214 * When changing the value of an option this is called to update all corresponding custom data
216 * @param int $optionId
217 * @param string $newValue
219 public static function updateValue($optionId, $newValue) {
220 $optionValue = new CRM_Core_DAO_OptionValue();
221 $optionValue->id
= $optionId;
222 $optionValue->find(TRUE);
223 $oldValue = $optionValue->value
;
224 if ($oldValue == $newValue) {
228 $customField = new CRM_Core_DAO_CustomField();
229 $customField->option_group_id
= $optionValue->option_group_id
;
230 $customField->find();
231 while ($customField->fetch()) {
232 $customGroup = new CRM_Core_DAO_CustomGroup();
233 $customGroup->id
= $customField->custom_group_id
;
234 $customGroup->find(TRUE);
235 if (CRM_Core_BAO_CustomField
::isSerialized($customField)) {
237 1 => [CRM_Utils_Array
::implodePadded($oldValue), 'String'],
238 2 => [CRM_Utils_Array
::implodePadded($newValue), 'String'],
239 3 => ['%' . CRM_Utils_Array
::implodePadded($oldValue) . '%', 'String'],
244 1 => [$oldValue, 'String'],
245 2 => [$newValue, 'String'],
246 3 => [$oldValue, 'String'],
249 $sql = "UPDATE `{$customGroup->table_name}` SET `{$customField->column_name}` = REPLACE(`{$customField->column_name}`, %1, %2) WHERE `{$customField->column_name}` LIKE %3";
250 CRM_Core_DAO
::executeQuery($sql, $params);