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
33 class CRM_Core_OptionValue
{
36 * Static field for all the option value information that we can potentially export.
40 static $_exportableFields = NULL;
43 * Static field for all the option value information that we can potentially export.
47 static $_importableFields = NULL;
50 * Static field for all the option value information that we can potentially export.
54 static $_fields = NULL;
57 * Return option-values of a particular group
59 * @param array $groupParams
60 * Array containing group fields whose option-values is to retrieved.
62 * Has links like edit, delete, disable ..etc.
63 * @param string $orderBy
65 * @param bool $skipEmptyComponents
66 * Whether to skip OptionValue rows with empty Component name
67 * (i.e. when Extension providing the Component is disabled)
70 * Array of option-values
73 public static function getRows($groupParams, $links, $orderBy = 'weight', $skipEmptyComponents = TRUE) {
74 $optionValue = array();
75 $optionGroupID = NULL;
76 $isGroupLocked = FALSE;
78 if (!isset($groupParams['id']) ||
!$groupParams['id']) {
79 if ($groupParams['name']) {
80 $optionGroup = CRM_Core_BAO_OptionGroup
::retrieve($groupParams, $dnc);
81 $optionGroupID = $optionGroup->id
;
82 $isGroupLocked = (bool) $optionGroup->is_locked
;
86 $optionGroupID = $groupParams['id'];
89 $groupName = CRM_Utils_Array
::value('name', $groupParams);
90 if (!$groupName && $optionGroupID) {
91 $groupName = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup',
92 $optionGroupID, 'name', 'id'
96 $dao = new CRM_Core_DAO_OptionValue();
99 $dao->option_group_id
= $optionGroupID;
101 if (in_array($groupName, CRM_Core_OptionGroup
::$_domainIDGroups)) {
102 $dao->domain_id
= CRM_Core_Config
::domainID();
105 $dao->orderBy($orderBy);
109 if ($groupName == 'case_type') {
110 $caseTypeIds = CRM_Case_BAO_Case
::getUsedCaseType();
112 elseif ($groupName == 'case_status') {
113 $caseStatusIds = CRM_Case_BAO_Case
::getUsedCaseStatuses();
116 $componentNames = CRM_Core_Component
::getNames();
117 $visibilityLabels = CRM_Core_PseudoConstant
::visibility();
118 while ($dao->fetch()) {
119 $optionValue[$dao->id
] = array();
120 CRM_Core_DAO
::storeValues($dao, $optionValue[$dao->id
]);
121 if (!empty($optionValue[$dao->id
]['component_id']) &&
122 empty($componentNames[$optionValue[$dao->id
]['component_id']]) &&
125 unset($optionValue[$dao->id
]);
128 // form all action links
129 $action = array_sum(array_keys($links));
131 // update enable/disable links depending on if it is is_reserved or is_active
132 if ($dao->is_reserved
) {
133 $action = CRM_Core_Action
::UPDATE
;
136 if ($dao->is_active
) {
137 $action -= CRM_Core_Action
::ENABLE
;
140 $action -= CRM_Core_Action
::DISABLE
;
142 if ((($groupName == 'case_type') && in_array($dao->value
, $caseTypeIds)) ||
143 (($groupName == 'case_status') && in_array($dao->value
, $caseStatusIds))
145 $action -= CRM_Core_Action
::DELETE
;
149 // disallow deletion of option values for locked groups
150 if (($action & CRM_Core_Action
::DELETE
) && $isGroupLocked) {
151 $action -= CRM_Core_Action
::DELETE
;
154 $optionValue[$dao->id
]['label'] = htmlspecialchars($optionValue[$dao->id
]['label']);
155 $optionValue[$dao->id
]['order'] = $optionValue[$dao->id
]['weight'];
156 $optionValue[$dao->id
]['icon'] = CRM_Utils_Array
::value('icon', $optionValue[$dao->id
], '');
157 $optionValue[$dao->id
]['action'] = CRM_Core_Action
::formLink($links, $action,
160 'gid' => $optionGroupID,
161 'value' => $dao->value
,
165 'optionValue.row.actions',
170 if (!empty($optionValue[$dao->id
]['component_id'])) {
171 $optionValue[$dao->id
]['component_name'] = $componentNames[$optionValue[$dao->id
]['component_id']];
174 $optionValue[$dao->id
]['component_name'] = 'Contact';
177 if (!empty($optionValue[$dao->id
]['visibility_id'])) {
178 $optionValue[$dao->id
]['visibility_label'] = $visibilityLabels[$optionValue[$dao->id
]['visibility_id']];
186 * Add/edit option-value of a particular group
188 * @param array $params
189 * Array containing exported values from the invoking form.
190 * @param array $groupParams
191 * Array containing group fields whose option-values is to retrieved/saved.
193 * @param int $optionValueID Has the id of the optionValue being edited, disabled ..etc.
194 * Has the id of the optionValue being edited, disabled ..etc.
196 * @return CRM_Core_DAO_OptionValue
199 public static function addOptionValue(&$params, &$groupParams, &$action, &$optionValueID) {
201 $params['is_active'] = CRM_Utils_Array
::value('is_active', $params, FALSE);
202 // checking if the group name with the given id or name (in $groupParams) exists
203 if (!empty($groupParams)) {
204 $config = CRM_Core_Config
::singleton();
205 $groupParams['is_active'] = 1;
206 $optionGroup = CRM_Core_BAO_OptionGroup
::retrieve($groupParams, $defaults);
209 // if the corresponding group doesn't exist, create one, provided $groupParams has 'name' in it.
210 if (!$optionGroup->id
) {
211 if ($groupParams['name']) {
212 $newOptionGroup = CRM_Core_BAO_OptionGroup
::add($groupParams, $defaults);
213 $params['weight'] = 1;
214 $optionGroupID = $newOptionGroup->id
;
218 $optionGroupID = $optionGroup->id
;
220 if ($optionValueID) {
221 $oldWeight = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionValue', $optionValueID, 'weight', 'id');
223 $fieldValues = array('option_group_id' => $optionGroupID);
224 $params['weight'] = CRM_Utils_Weight
::updateOtherWeights('CRM_Core_DAO_OptionValue', $oldWeight, CRM_Utils_Array
::value('weight', $params), $fieldValues);
226 $params['option_group_id'] = $optionGroupID;
228 if (($action & CRM_Core_Action
::ADD
) && empty($params['value'])) {
229 $fieldValues = array('option_group_id' => $optionGroupID);
230 // use the next available value
231 /* CONVERT(value, DECIMAL) is used to convert varchar
232 field 'value' to decimal->integer */
234 $params['value'] = (int) CRM_Utils_Weight
::getDefaultWeight('CRM_Core_DAO_OptionValue',
236 'CONVERT(value, DECIMAL)'
239 if (!$params['label'] && $params['name']) {
240 $params['label'] = $params['name'];
243 // set name to label if it's not set - but *only* for ADD action (CRM-3522)
244 if (($action & CRM_Core_Action
::ADD
) && empty($params['name']) && $params['label']) {
245 $params['name'] = $params['label'];
247 if ($action & CRM_Core_Action
::UPDATE
) {
248 $ids['optionValue'] = $optionValueID;
250 $optionValue = CRM_Core_BAO_OptionValue
::add($params, $ids);
255 * Check if there is a record with the same name in the db.
257 * @param string $value
258 * The value of the field we are checking.
259 * @param string $daoName
260 * The dao object name.
261 * @param string $daoID
262 * The id of the object being updated. u can change your name.
263 * as long as there is no conflict
264 * @param int $optionGroupID
265 * @param string $fieldName
266 * The name of the field in the DAO.
267 * @param bool $domainSpecific
268 * Filter this check to the current domain.
269 * Some optionGroups allow for same labels or same names but
270 * they must be in different domains, so filter the check to
271 * the current domain.
274 * true if object exists
276 public static function optionExists($value, $daoName, $daoID, $optionGroupID, $fieldName = 'name', $domainSpecific) {
277 $object = new $daoName();
278 $object->$fieldName = $value;
279 $object->option_group_id
= $optionGroupID;
281 if ($domainSpecific) {
282 $object->domain_id
= CRM_Core_Config
::domainID();
285 if ($object->find(TRUE)) {
286 return ($daoID && $object->id
== $daoID) ?
TRUE : FALSE;
294 * Check if there is a record with the same name in the db.
296 * @param string $mode
297 * @param string $contactType
301 public static function getFields($mode = '', $contactType = 'Individual') {
302 $key = "$mode $contactType";
303 if (empty(self
::$_fields[$key]) ||
!self
::$_fields[$key]) {
304 self
::$_fields[$key] = array();
306 $option = CRM_Core_DAO_OptionValue
::import();
308 foreach (array_keys($option) as $id) {
309 $optionName = $option[$id];
312 $nameTitle = array();
313 if ($mode == 'contribute') {
314 // This is part of a move towards standardising option values but we
315 // should derive them from the fields array so am deprecating it again...
316 // note that the reason this was needed was that payment_instrument_id was
317 // not set to exportable.
319 'payment_instrument' => array(
320 'name' => 'payment_instrument',
321 'title' => ts('Payment Method'),
322 'headerPattern' => '/^payment|(p(ayment\s)?instrument)$/i',
326 elseif ($mode == '') {
327 //the fields email greeting and postal greeting are meant only for Individual and Household
328 //the field addressee is meant for all contact types, CRM-4575
329 if (in_array($contactType, array(
336 'addressee' => array(
337 'name' => 'addressee',
338 'title' => ts('Addressee'),
339 'headerPattern' => '/^addressee$/i',
343 'email_greeting' => array(
344 'name' => 'email_greeting',
345 'title' => ts('Email Greeting'),
346 'headerPattern' => '/^email_greeting$/i',
348 'postal_greeting' => array(
349 'name' => 'postal_greeting',
350 'title' => ts('Postal Greeting'),
351 'headerPattern' => '/^postal_greeting$/i',
354 $nameTitle = array_merge($nameTitle, $title);
358 if (is_array($nameTitle)) {
359 foreach ($nameTitle as $name => $attribs) {
360 self
::$_fields[$key][$name] = $optionName;
361 list($tableName, $fieldName) = explode('.', $optionName['where']);
362 self
::$_fields[$key][$name]['where'] = "{$name}.label";
363 foreach ($attribs as $k => $val) {
364 self
::$_fields[$key][$name][$k] = $val;
370 return self
::$_fields[$key];
374 * Build select query in case of option-values
378 public static function select(&$query) {
379 if (!empty($query->_params
) ||
!empty($query->_returnProperties
)) {
380 $field = self
::getFields();
381 foreach ($field as $name => $values) {
382 if (!empty($values['pseudoconstant'])) {
385 list($tableName, $fieldName) = explode('.', $values['where']);
386 if (!empty($query->_returnProperties
[$name])) {
387 $query->_select
["{$name}_id"] = "{$name}.value as {$name}_id";
388 $query->_element
["{$name}_id"] = 1;
389 $query->_select
[$name] = "{$name}.{$fieldName} as $name";
390 $query->_tables
[$tableName] = 1;
391 $query->_element
[$name] = 1;
398 * Return option-values of a particular group
400 * @param array $groupParams
401 * Array containing group fields.
402 * whose option-values is to retrieved.
403 * @param array $values
404 * (reference) to the array which.
405 * will have the values for the group
406 * @param string $orderBy
407 * For orderBy clause.
409 * @param bool $isActive Do you want only active option values?
412 * Array of option-values
415 public static function getValues($groupParams, &$values, $orderBy = 'weight', $isActive = FALSE) {
416 if (empty($groupParams)) {
421 option_value.id as id,
422 option_value.label as label,
423 option_value.value as value,
424 option_value.name as name,
425 option_value.description as description,
426 option_value.weight as weight,
427 option_value.is_active as is_active,
428 option_value.is_default as is_default";
432 civicrm_option_value as option_value,
433 civicrm_option_group as option_group ";
435 $where = " WHERE option_group.id = option_value.option_group_id ";
438 $where .= " AND option_value.is_active = " . $isActive;
441 $order = " ORDER BY " . $orderBy;
443 $groupId = CRM_Utils_Array
::value('id', $groupParams);
444 $groupName = CRM_Utils_Array
::value('name', $groupParams);
447 $where .= " AND option_group.id = %1";
448 $params[1] = array($groupId, 'Integer');
450 $groupName = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup',
451 $groupId, 'name', 'id'
457 $where .= " AND option_group.name = %2";
458 $params[2] = array($groupName, 'String');
461 if (in_array($groupName, CRM_Core_OptionGroup
::$_domainIDGroups)) {
462 $where .= " AND option_value.domain_id = " . CRM_Core_Config
::domainID();
465 $query = $select . $from . $where . $order;
467 $dao = CRM_Core_DAO
::executeQuery($query, $params);
469 while ($dao->fetch()) {
470 $values[$dao->id
] = array(
472 'label' => $dao->label
,
473 'value' => $dao->value
,
474 'name' => $dao->name
,
475 'description' => $dao->description
,
476 'weight' => $dao->weight
,
477 'is_active' => $dao->is_active
,
478 'is_default' => $dao->is_default
,