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
17 class CRM_Core_OptionValue
{
20 * Static field for all the option value information that we can potentially export.
24 public static $_exportableFields = NULL;
27 * Static field for all the option value information that we can potentially export.
31 public static $_importableFields = NULL;
34 * Static field for all the option value information that we can potentially export.
38 public static $_fields = NULL;
41 * Return option-values of a particular group
43 * @param array $groupParams
44 * Array containing group fields whose option-values is to retrieved.
46 * Has links like edit, delete, disable ..etc.
47 * @param string $orderBy
49 * @param bool $skipEmptyComponents
50 * Whether to skip OptionValue rows with empty Component name
51 * (i.e. when Extension providing the Component is disabled)
54 * Array of option-values
57 public static function getRows($groupParams, $links, $orderBy = 'weight', $skipEmptyComponents = TRUE) {
59 $optionGroupID = NULL;
60 $isGroupLocked = FALSE;
62 if (!isset($groupParams['id']) ||
!$groupParams['id']) {
63 if ($groupParams['name']) {
64 $optionGroup = CRM_Core_BAO_OptionGroup
::retrieve($groupParams, $dnc);
65 $optionGroupID = $optionGroup->id
;
66 $isGroupLocked = (bool) $optionGroup->is_locked
;
70 $optionGroupID = $groupParams['id'];
73 $groupName = CRM_Utils_Array
::value('name', $groupParams);
74 if (!$groupName && $optionGroupID) {
75 $groupName = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup',
76 $optionGroupID, 'name', 'id'
80 $dao = new CRM_Core_DAO_OptionValue();
83 $dao->option_group_id
= $optionGroupID;
85 if (in_array($groupName, CRM_Core_OptionGroup
::$_domainIDGroups)) {
86 $dao->domain_id
= CRM_Core_Config
::domainID();
89 $dao->orderBy($orderBy);
93 if ($groupName == 'case_type') {
94 $caseTypeIds = CRM_Case_BAO_Case
::getUsedCaseType();
96 elseif ($groupName == 'case_status') {
97 $caseStatusIds = CRM_Case_BAO_Case
::getUsedCaseStatuses();
100 $componentNames = CRM_Core_Component
::getNames();
101 $visibilityLabels = CRM_Core_PseudoConstant
::visibility();
102 while ($dao->fetch()) {
103 $optionValue[$dao->id
] = [];
104 CRM_Core_DAO
::storeValues($dao, $optionValue[$dao->id
]);
105 if (!empty($optionValue[$dao->id
]['component_id']) &&
106 empty($componentNames[$optionValue[$dao->id
]['component_id']]) &&
109 unset($optionValue[$dao->id
]);
112 // form all action links
113 $action = array_sum(array_keys($links));
115 // update enable/disable links depending on if it is is_reserved or is_active
116 if ($dao->is_reserved
) {
117 $action = CRM_Core_Action
::UPDATE
;
120 if ($dao->is_active
) {
121 $action -= CRM_Core_Action
::ENABLE
;
124 $action -= CRM_Core_Action
::DISABLE
;
126 if ((($groupName == 'case_type') && in_array($dao->value
, $caseTypeIds)) ||
127 (($groupName == 'case_status') && in_array($dao->value
, $caseStatusIds))
129 $action -= CRM_Core_Action
::DELETE
;
133 // disallow deletion of option values for locked groups
134 if (($action & CRM_Core_Action
::DELETE
) && $isGroupLocked) {
135 $action -= CRM_Core_Action
::DELETE
;
138 $optionValue[$dao->id
]['label'] = htmlspecialchars($optionValue[$dao->id
]['label']);
139 $optionValue[$dao->id
]['order'] = $optionValue[$dao->id
]['weight'];
140 $optionValue[$dao->id
]['icon'] = CRM_Utils_Array
::value('icon', $optionValue[$dao->id
], '');
141 $optionValue[$dao->id
]['action'] = CRM_Core_Action
::formLink($links, $action,
144 'gid' => $optionGroupID,
145 'value' => $dao->value
,
149 'optionValue.row.actions',
154 if (!empty($optionValue[$dao->id
]['component_id'])) {
155 $optionValue[$dao->id
]['component_name'] = $componentNames[$optionValue[$dao->id
]['component_id']];
158 $optionValue[$dao->id
]['component_name'] = 'Contact';
161 if (!empty($optionValue[$dao->id
]['visibility_id'])) {
162 $optionValue[$dao->id
]['visibility_label'] = $visibilityLabels[$optionValue[$dao->id
]['visibility_id']];
170 * Add/edit option-value of a particular group
172 * @param array $params
173 * Array containing exported values from the invoking form.
174 * @param string $optionGroupName
175 * Array containing group fields whose option-values is to retrieved/saved.
177 * @param int $optionValueID Has the id of the optionValue being edited, disabled ..etc.
178 * Has the id of the optionValue being edited, disabled ..etc.
180 * @return CRM_Core_DAO_OptionValue
183 public static function addOptionValue(&$params, $optionGroupName, $action, $optionValueID) {
184 $params['is_active'] = CRM_Utils_Array
::value('is_active', $params, FALSE);
185 // checking if the group name with the given id or name (in $groupParams) exists
186 $groupParams = ['name' => $optionGroupName, 'is_active' => 1];
187 $optionGroup = CRM_Core_BAO_OptionGroup
::retrieve($groupParams, $defaults);
189 // if the corresponding group doesn't exist, create one.
190 if (!$optionGroup->id
) {
191 $newOptionGroup = CRM_Core_BAO_OptionGroup
::add($groupParams);
192 $params['weight'] = 1;
193 $optionGroupID = $newOptionGroup->id
;
196 $optionGroupID = $optionGroup->id
;
198 if ($optionValueID) {
199 $oldWeight = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionValue', $optionValueID, 'weight', 'id');
201 $fieldValues = ['option_group_id' => $optionGroupID];
202 $params['weight'] = CRM_Utils_Weight
::updateOtherWeights('CRM_Core_DAO_OptionValue', $oldWeight, CRM_Utils_Array
::value('weight', $params), $fieldValues);
204 $params['option_group_id'] = $optionGroupID;
206 if (($action & CRM_Core_Action
::ADD
) && !isset($params['value'])) {
207 $fieldValues = ['option_group_id' => $optionGroupID];
208 // use the next available value
209 /* CONVERT(value, DECIMAL) is used to convert varchar
210 field 'value' to decimal->integer */
212 $params['value'] = (int) CRM_Utils_Weight
::getDefaultWeight('CRM_Core_DAO_OptionValue',
214 'CONVERT(value, DECIMAL)'
217 if (!$params['label'] && $params['name']) {
218 $params['label'] = $params['name'];
221 // set name to label if it's not set - but *only* for ADD action (CRM-3522)
222 if (($action & CRM_Core_Action
::ADD
) && empty($params['name']) && $params['label']) {
223 $params['name'] = $params['label'];
225 if ($action & CRM_Core_Action
::UPDATE
) {
226 $params['id'] = $optionValueID;
228 $optionValue = CRM_Core_BAO_OptionValue
::add($params);
233 * Check if there is a record with the same name in the db.
235 * @param string $value
236 * The value of the field we are checking.
237 * @param string $daoName
238 * The dao object name.
239 * @param string $daoID
240 * The id of the object being updated. u can change your name.
241 * as long as there is no conflict
242 * @param int $optionGroupID
243 * @param string $fieldName
244 * The name of the field in the DAO.
245 * @param bool $domainSpecific
246 * Filter this check to the current domain.
247 * Some optionGroups allow for same labels or same names but
248 * they must be in different domains, so filter the check to
249 * the current domain.
252 * true if object exists
254 public static function optionExists($value, $daoName, $daoID, $optionGroupID, $fieldName = 'name', $domainSpecific) {
255 $object = new $daoName();
256 $object->$fieldName = $value;
257 $object->option_group_id
= $optionGroupID;
259 if ($domainSpecific) {
260 $object->domain_id
= CRM_Core_Config
::domainID();
263 if ($object->find(TRUE)) {
264 return ($daoID && $object->id
== $daoID) ?
TRUE : FALSE;
272 * Check if there is a record with the same name in the db.
274 * @param string $mode
275 * @param string $contactType
279 public static function getFields($mode = '', $contactType = 'Individual') {
280 $key = "$mode $contactType";
281 if (empty(self
::$_fields[$key]) ||
!self
::$_fields[$key]) {
282 self
::$_fields[$key] = [];
284 $option = CRM_Core_DAO_OptionValue
::import();
286 foreach (array_keys($option) as $id) {
287 $optionName = $option[$id];
291 if ($mode == 'contribute') {
292 // This is part of a move towards standardising option values but we
293 // should derive them from the fields array so am deprecating it again...
294 // note that the reason this was needed was that payment_instrument_id was
295 // not set to exportable.
297 'payment_instrument' => [
298 'name' => 'payment_instrument',
299 'title' => ts('Payment Method'),
300 'headerPattern' => '/^payment|(p(ayment\s)?instrument)$/i',
304 elseif ($mode == '') {
305 //the fields email greeting and postal greeting are meant only for Individual and Household
306 //the field addressee is meant for all contact types, CRM-4575
307 if (in_array($contactType, [
315 'name' => 'addressee',
316 'title' => ts('Addressee'),
317 'headerPattern' => '/^addressee$/i',
321 'email_greeting' => [
322 'name' => 'email_greeting',
323 'title' => ts('Email Greeting'),
324 'headerPattern' => '/^email_greeting$/i',
326 'postal_greeting' => [
327 'name' => 'postal_greeting',
328 'title' => ts('Postal Greeting'),
329 'headerPattern' => '/^postal_greeting$/i',
332 $nameTitle = array_merge($nameTitle, $title);
336 if (is_array($nameTitle)) {
337 foreach ($nameTitle as $name => $attribs) {
338 self
::$_fields[$key][$name] = $optionName;
339 list($tableName, $fieldName) = explode('.', $optionName['where']);
340 self
::$_fields[$key][$name]['where'] = "{$name}.label";
341 foreach ($attribs as $k => $val) {
342 self
::$_fields[$key][$name][$k] = $val;
348 return self
::$_fields[$key];
352 * Build select query in case of option-values
356 public static function select(&$query) {
357 if (!empty($query->_params
) ||
!empty($query->_returnProperties
)) {
358 $field = self
::getFields();
359 foreach ($field as $name => $values) {
360 if (!empty($values['pseudoconstant'])) {
363 list($tableName, $fieldName) = explode('.', $values['where']);
364 if (!empty($query->_returnProperties
[$name])) {
365 $query->_select
["{$name}_id"] = "{$name}.value as {$name}_id";
366 $query->_element
["{$name}_id"] = 1;
367 $query->_select
[$name] = "{$name}.{$fieldName} as $name";
368 $query->_tables
[$tableName] = 1;
369 $query->_element
[$name] = 1;
376 * Return option-values of a particular group
378 * @param array $groupParams
379 * Array containing group fields.
380 * whose option-values is to retrieved.
381 * @param array $values
382 * (reference) to the array which.
383 * will have the values for the group
384 * @param string $orderBy
385 * For orderBy clause.
387 * @param bool $isActive Do you want only active option values?
390 * Array of option-values
393 public static function getValues($groupParams, &$values, $orderBy = 'weight', $isActive = FALSE) {
394 if (empty($groupParams)) {
399 option_value.id as id,
400 option_value.label as label,
401 option_value.value as value,
402 option_value.name as name,
403 option_value.description as description,
404 option_value.weight as weight,
405 option_value.is_active as is_active,
406 option_value.is_default as is_default";
410 civicrm_option_value as option_value,
411 civicrm_option_group as option_group ";
413 $where = " WHERE option_group.id = option_value.option_group_id ";
416 $where .= " AND option_value.is_active = " . $isActive;
419 $order = " ORDER BY " . $orderBy;
421 $groupId = CRM_Utils_Array
::value('id', $groupParams);
422 $groupName = CRM_Utils_Array
::value('name', $groupParams);
425 $where .= " AND option_group.id = %1";
426 $params[1] = [$groupId, 'Integer'];
428 $groupName = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup',
429 $groupId, 'name', 'id'
435 $where .= " AND option_group.name = %2";
436 $params[2] = [$groupName, 'String'];
439 if (in_array($groupName, CRM_Core_OptionGroup
::$_domainIDGroups)) {
440 $where .= " AND option_value.domain_id = " . CRM_Core_Config
::domainID();
443 $query = $select . $from . $where . $order;
445 $dao = CRM_Core_DAO
::executeQuery($query, $params);
447 while ($dao->fetch()) {
448 $values[$dao->id
] = [
450 'label' => $dao->label
,
451 'value' => $dao->value
,
452 'name' => $dao->name
,
453 'description' => $dao->description
,
454 'weight' => $dao->weight
,
455 'is_active' => $dao->is_active
,
456 'is_default' => $dao->is_default
,