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 * Retrieve DB object and copy to defaults array.
28 * @param array $params
29 * @param array $defaults
31 * @return CRM_Core_DAO_OptionValue|NULL
33 public static function retrieve($params, &$defaults) {
34 return CRM_Core_DAO
::commonRetrieve('CRM_Core_DAO_OptionValue', $params, $defaults);
38 * Returns all active options ordered by weight for a given field.
41 * Field whose options are needed.
42 * @param bool $inactiveNeeded Do we need inactive options ?.
43 * Do we need inactive options ?.
46 * all active options for fieldId
48 public static function getCustomOption(
50 $inactiveNeeded = FALSE
57 $optionValues = CRM_Core_PseudoConstant
::get('CRM_Core_BAO_CustomField', 'custom_' . $fieldID, [], $inactiveNeeded ?
'get' : 'create');
59 foreach ((array) $optionValues as $value => $label) {
70 * Wrapper for ajax option selector.
72 * @param array $params
73 * Associated array for params record id.
76 * associated array of option list
80 public static function getOptionListSelector(&$params) {
83 $field = CRM_Core_BAO_CustomField
::getFieldObject($params['fid']);
84 $defVal = CRM_Utils_Array
::explodePadded($field->default_value
);
87 $params['offset'] = ($params['page'] - 1) * $params['rp'];
88 $params['rowCount'] = $params['rp'];
90 if (!$field->option_group_id
) {
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);
97 $limit = " LIMIT {$params['offset']}, {$params['rowCount']} ";
98 $orderBy = ' ORDER BY options.weight asc';
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();
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;
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
;
118 $class .= ' disabled';
119 $action -= CRM_Core_Action
::DISABLE
;
122 $isGroupLocked = (bool) CRM_Core_DAO
::getFieldValue(
123 CRM_Core_DAO_OptionGroup
::class,
124 $field->option_group_id
,
128 // disable deletion of option values for locked option groups
129 if (($action & CRM_Core_Action
::DELETE
) && $isGroupLocked) {
130 $action -= CRM_Core_Action
::DELETE
;
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));
137 $options[$dao->id
]['is_default'] = ($field->default_value
== $dao->value
);
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,
146 'fid' => $params['fid'],
147 'gid' => $params['gid'],
151 'customOption.row.actions',
163 * @param int $optionId
167 public static function del($optionId) {
168 // get the customFieldID
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
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);
180 if (in_array($dao->dataType
,
181 ['Int', 'Float', 'Money', 'Boolean']
189 'optionId' => $optionId,
190 'fieldId' => $dao->id
,
193 // delete this value from the tables
194 self
::updateValue($optionId, $value);
196 // also delete this option value
199 FROM civicrm_option_value
201 $params = [1 => [$optionId, 'Integer']];
202 CRM_Core_DAO
::executeQuery($query, $params);
207 * When changing the value of an option this is called to update all corresponding custom data
209 * @param int $optionId
210 * @param string $newValue
212 public static function updateValue($optionId, $newValue) {
213 $optionValue = new CRM_Core_DAO_OptionValue();
214 $optionValue->id
= $optionId;
215 $optionValue->find(TRUE);
216 $oldValue = $optionValue->value
;
217 if ($oldValue == $newValue) {
221 $customField = new CRM_Core_DAO_CustomField();
222 $customField->option_group_id
= $optionValue->option_group_id
;
223 $customField->find();
224 while ($customField->fetch()) {
225 $customGroup = new CRM_Core_DAO_CustomGroup();
226 $customGroup->id
= $customField->custom_group_id
;
227 $customGroup->find(TRUE);
228 if (CRM_Core_BAO_CustomField
::isSerialized($customField)) {
230 1 => [CRM_Utils_Array
::implodePadded($oldValue), 'String'],
231 2 => [CRM_Utils_Array
::implodePadded($newValue), 'String'],
232 3 => ['%' . CRM_Utils_Array
::implodePadded($oldValue) . '%', 'String'],
237 1 => [$oldValue, 'String'],
238 2 => [$newValue, 'String'],
239 3 => [$oldValue, 'String'],
242 $sql = "UPDATE `{$customGroup->table_name}` SET `{$customField->column_name}` = REPLACE(`{$customField->column_name}`, %1, %2) WHERE `{$customField->column_name}` LIKE %3";
243 CRM_Core_DAO
::executeQuery($sql, $params);