3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2018
37 * Business objects for managing custom data options.
40 class CRM_Core_BAO_CustomOption
{
43 * Fetch object based on array of properties.
45 * @param array $params
46 * (reference ) an assoc array of name/value pairs.
47 * @param array $defaults
48 * (reference ) an assoc array to hold the flattened values.
50 * @return CRM_Core_BAO_CustomOption
52 public static function retrieve(&$params, &$defaults) {
53 $customOption = new CRM_Core_DAO_OptionValue();
54 $customOption->copyValues($params);
55 if ($customOption->find(TRUE)) {
56 CRM_Core_DAO
::storeValues($customOption, $defaults);
63 * Returns all active options ordered by weight for a given field.
66 * Field whose options are needed.
67 * @param bool $inactiveNeeded Do we need inactive options ?.
68 * Do we need inactive options ?.
71 * all active options for fieldId
73 public static function getCustomOption(
75 $inactiveNeeded = FALSE
82 $optionValues = CRM_Core_PseudoConstant
::get('CRM_Core_BAO_CustomField', 'custom_' . $fieldID, array(), $inactiveNeeded ?
'get' : 'create');
84 foreach ((array) $optionValues as $value => $label) {
95 * Wrapper for ajax option selector.
97 * @param array $params
98 * Associated array for params record id.
101 * associated array of option list
105 static public function getOptionListSelector(&$params) {
108 $field = CRM_Core_BAO_CustomField
::getFieldObject($params['fid']);
109 $defVal = CRM_Utils_Array
::explodePadded($field->default_value
);
112 $params['offset'] = ($params['page'] - 1) * $params['rp'];
113 $params['rowCount'] = $params['rp'];
115 if (!$field->option_group_id
) {
118 $queryParams = array(1 => array($field->option_group_id
, 'Integer'));
119 $total = "SELECT COUNT(*) FROM civicrm_option_value WHERE option_group_id = %1";
120 $params['total'] = CRM_Core_DAO
::singleValueQuery($total, $queryParams);
122 $limit = " LIMIT {$params['offset']}, {$params['rowCount']} ";
123 $orderBy = ' ORDER BY options.weight asc';
125 $query = "SELECT * FROM civicrm_option_value as options WHERE option_group_id = %1 {$orderBy} {$limit}";
126 $dao = CRM_Core_DAO
::executeQuery($query, $queryParams);
127 $links = CRM_Custom_Page_Option
::actionLinks();
129 $fields = array('id', 'label', 'value');
130 $config = CRM_Core_Config
::singleton();
131 while ($dao->fetch()) {
132 $options[$dao->id
] = array();
133 foreach ($fields as $k) {
134 $options[$dao->id
][$k] = $dao->$k;
136 $action = array_sum(array_keys($links));
137 $class = 'crm-entity';
138 // update enable/disable links depending on custom_field properties.
139 if ($dao->is_active
) {
140 $action -= CRM_Core_Action
::ENABLE
;
143 $class .= ' disabled';
144 $action -= CRM_Core_Action
::DISABLE
;
147 $isGroupLocked = (bool) CRM_Core_DAO
::getFieldValue(
148 CRM_Core_DAO_OptionGroup
::class,
149 $field->option_group_id
,
153 // disable deletion of option values for locked option groups
154 if (($action & CRM_Core_Action
::DELETE
) && $isGroupLocked) {
155 $action -= CRM_Core_Action
::DELETE
;
158 if (in_array($field->html_type
, ['CheckBox', 'Multi-Select'])) {
159 if (isset($defVal) && in_array($dao->value
, $defVal)) {
160 $options[$dao->id
]['is_default'] = '<img src="' . $config->resourceBase
. 'i/check.gif" />';
163 $options[$dao->id
]['is_default'] = '';
167 if ($field->default_value
== $dao->value
) {
168 $options[$dao->id
]['is_default'] = '<img src="' . $config->resourceBase
. 'i/check.gif" />';
171 $options[$dao->id
]['is_default'] = '';
174 $options[$dao->id
]['description'] = $dao->description
;
175 $options[$dao->id
]['class'] = $dao->id
. ',' . $class;
176 $options[$dao->id
]['is_active'] = empty($dao->is_active
) ?
ts('No') : ts('Yes');
177 $options[$dao->id
]['links'] = CRM_Core_Action
::formLink($links,
181 'fid' => $params['fid'],
182 'gid' => $params['gid'],
186 'customOption.row.actions',
198 * @param $optionId integer
202 public static function del($optionId) {
203 // get the customFieldID
205 SELECT f.id as id, f.data_type as dataType
206 FROM civicrm_option_value v,
207 civicrm_option_group g,
208 civicrm_custom_field f
210 AND g.id = f.option_group_id
211 AND g.id = v.option_group_id";
212 $params = array(1 => array($optionId, 'Integer'));
213 $dao = CRM_Core_DAO
::executeQuery($query, $params);
215 if (in_array($dao->dataType
,
216 array('Int', 'Float', 'Money', 'Boolean')
224 'optionId' => $optionId,
225 'fieldId' => $dao->id
,
228 // delete this value from the tables
229 self
::updateCustomValues($params);
231 // also delete this option value
234 FROM civicrm_option_value
236 $params = array(1 => array($optionId, 'Integer'));
237 CRM_Core_DAO
::executeQuery($query, $params);
242 * @param array $params
246 public static function updateCustomValues($params) {
247 $optionDAO = new CRM_Core_DAO_OptionValue();
248 $optionDAO->id
= $params['optionId'];
249 $optionDAO->find(TRUE);
250 $oldValue = $optionDAO->value
;
252 // get the table, column, html_type and data type for this field
254 SELECT g.table_name as tableName ,
255 f.column_name as columnName,
256 f.data_type as dataType,
257 f.html_type as htmlType
258 FROM civicrm_custom_group g,
259 civicrm_custom_field f
260 WHERE f.custom_group_id = g.id
262 $queryParams = array(1 => array($params['fieldId'], 'Integer'));
263 $dao = CRM_Core_DAO
::executeQuery($query, $queryParams);
265 if ($dao->dataType
== 'Money') {
266 $params['value'] = CRM_Utils_Rule
::cleanMoney($params['value']);
268 switch ($dao->htmlType
) {
269 case 'Autocomplete-Select':
273 UPDATE {$dao->tableName}
274 SET {$dao->columnName} = %1
276 if ($dao->dataType
== 'Auto-complete') {
277 $dataType = "String";
280 $dataType = $dao->dataType
;
282 $queryParams = array(
296 $oldString = CRM_Core_DAO
::VALUE_SEPARATOR
. $oldValue . CRM_Core_DAO
::VALUE_SEPARATOR
;
297 $newString = CRM_Core_DAO
::VALUE_SEPARATOR
. $params['value'] . CRM_Core_DAO
::VALUE_SEPARATOR
;
299 UPDATE {$dao->tableName}
300 SET {$dao->columnName} = REPLACE( {$dao->columnName}, %1, %2 )";
301 $queryParams = array(
302 1 => array($oldString, 'String'),
303 2 => array($newString, 'String'),
308 CRM_Core_Error
::fatal();
310 $dao = CRM_Core_DAO
::executeQuery($query, $queryParams);
315 * When changing the value of an option this is called to update all corresponding custom data
317 * @param int $optionId
318 * @param string $newValue
320 public static function updateValue($optionId, $newValue) {
321 $optionValue = new CRM_Core_DAO_OptionValue();
322 $optionValue->id
= $optionId;
323 $optionValue->find(TRUE);
324 $oldValue = $optionValue->value
;
325 if ($oldValue == $newValue) {
329 $customField = new CRM_Core_DAO_CustomField();
330 $customField->option_group_id
= $optionValue->option_group_id
;
331 $customField->find();
332 while ($customField->fetch()) {
333 $customGroup = new CRM_Core_DAO_CustomGroup();
334 $customGroup->id
= $customField->custom_group_id
;
335 $customGroup->find(TRUE);
336 if (CRM_Core_BAO_CustomField
::isSerialized($customField)) {
338 1 => array(CRM_Utils_Array
::implodePadded($oldValue), 'String'),
339 2 => array(CRM_Utils_Array
::implodePadded($newValue), 'String'),
340 3 => array('%' . CRM_Utils_Array
::implodePadded($oldValue) . '%', 'String'),
345 1 => array($oldValue, 'String'),
346 2 => array($newValue, 'String'),
347 3 => array($oldValue, 'String'),
350 $sql = "UPDATE `{$customGroup->table_name}` SET `{$customField->column_name}` = REPLACE(`{$customField->column_name}`, %1, %2) WHERE `{$customField->column_name}` LIKE %3";
351 $customGroup->free();
352 CRM_Core_DAO
::executeQuery($sql, $params);
354 $customField->free();