3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
35 class CRM_Core_OptionValue
{
38 * Static field for all the option value information that we can potentially export
42 static $_exportableFields = NULL;
45 * Static field for all the option value information that we can potentially export
49 static $_importableFields = NULL;
52 * Static field for all the option value information that we can potentially export
56 static $_fields = NULL;
59 * Return option-values of a particular group
61 * @param array $groupParams
62 * Array containing group fields whose option-values is to retrieved.
64 * Has links like edit, delete, disable ..etc.
65 * @param string $orderBy
69 * Array of option-values
72 public static function getRows($groupParams, $links, $orderBy = 'weight') {
73 $optionValue = array();
75 $optionGroupID = NULL;
76 if (!isset($groupParams['id']) ||
!$groupParams['id']) {
77 if ($groupParams['name']) {
78 $config = CRM_Core_Config
::singleton();
80 $optionGroup = CRM_Core_BAO_OptionGroup
::retrieve($groupParams, $dnc);
81 $optionGroupID = $optionGroup->id
;
85 $optionGroupID = $groupParams['id'];
88 $groupName = CRM_Utils_Array
::value('name', $groupParams);
89 if (!$groupName && $optionGroupID) {
90 $groupName = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup',
91 $optionGroupID, 'name', 'id'
95 $dao = new CRM_Core_DAO_OptionValue();
98 $dao->option_group_id
= $optionGroupID;
100 if (in_array($groupName, CRM_Core_OptionGroup
::$_domainIDGroups)) {
101 $dao->domain_id
= CRM_Core_Config
::domainID();
104 $dao->orderBy($orderBy);
108 if ($groupName == 'case_type') {
109 $caseTypeIds = CRM_Case_BAO_Case
::getUsedCaseType();
111 elseif ($groupName == 'case_status') {
112 $caseStatusIds = CRM_Case_BAO_Case
::getUsedCaseStatuses();
115 $componentNames = CRM_Core_Component
::getNames();
116 $visibilityLabels = CRM_Core_PseudoConstant
::visibility();
117 while ($dao->fetch()) {
118 $optionValue[$dao->id
] = array();
119 CRM_Core_DAO
::storeValues($dao, $optionValue[$dao->id
]);
120 // form all action links
121 $action = array_sum(array_keys($links));
123 // update enable/disable links depending on if it is is_reserved or is_active
124 if ($dao->is_reserved
) {
125 $action = CRM_Core_Action
::UPDATE
;
128 if ($dao->is_active
) {
129 $action -= CRM_Core_Action
::ENABLE
;
132 $action -= CRM_Core_Action
::DISABLE
;
134 if ((($groupName == 'case_type') && in_array($dao->value
, $caseTypeIds)) ||
135 (($groupName == 'case_status') && in_array($dao->value
, $caseStatusIds))
137 $action -= CRM_Core_Action
::DELETE
;
141 $optionValue[$dao->id
]['label'] = htmlspecialchars($optionValue[$dao->id
]['label']);
142 $optionValue[$dao->id
]['order'] = $optionValue[$dao->id
]['weight'];
143 $optionValue[$dao->id
]['action'] = CRM_Core_Action
::formLink($links, $action,
146 'gid' => $optionGroupID,
147 'value' => $dao->value
,
151 'optionValue.row.actions',
156 if (!empty($optionValue[$dao->id
]['component_id'])) {
157 $optionValue[$dao->id
]['component_name'] = $componentNames[$optionValue[$dao->id
]['component_id']];
160 $optionValue[$dao->id
]['component_name'] = 'Contact';
163 if (!empty($optionValue[$dao->id
]['visibility_id'])) {
164 $optionValue[$dao->id
]['visibility_label'] = $visibilityLabels[$optionValue[$dao->id
]['visibility_id']];
172 * Add/edit option-value of a particular group
174 * @param array $params
175 * Array containing exported values from the invoking form.
176 * @param array $groupParams
177 * Array containing group fields whose option-values is to retrieved/saved.
179 * @param int $optionValueID Has the id of the optionValue being edited, disabled ..etc.
180 * Has the id of the optionValue being edited, disabled ..etc.
182 * @return CRM_Core_DAO_OptionValue
185 public static function addOptionValue(&$params, &$groupParams, &$action, &$optionValueID) {
186 $params['is_active'] = CRM_Utils_Array
::value('is_active', $params, FALSE);
187 // checking if the group name with the given id or name (in $groupParams) exists
188 if (!empty($groupParams)) {
189 $config = CRM_Core_Config
::singleton();
190 $groupParams['is_active'] = 1;
191 $optionGroup = CRM_Core_BAO_OptionGroup
::retrieve($groupParams, $defaults);
194 // if the corresponding group doesn't exist, create one, provided $groupParams has 'name' in it.
195 if (!$optionGroup->id
) {
196 if ($groupParams['name']) {
197 $newOptionGroup = CRM_Core_BAO_OptionGroup
::add($groupParams, $defaults);
198 $params['weight'] = 1;
199 $optionGroupID = $newOptionGroup->id
;
203 $optionGroupID = $optionGroup->id
;
205 if ($optionValueID) {
206 $oldWeight = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionValue', $optionValueID, 'weight', 'id');
208 $fieldValues = array('option_group_id' => $optionGroupID);
209 $params['weight'] = CRM_Utils_Weight
::updateOtherWeights('CRM_Core_DAO_OptionValue', $oldWeight, CRM_Utils_Array
::value('weight', $params), $fieldValues);
211 $params['option_group_id'] = $optionGroupID;
213 if (($action & CRM_Core_Action
::ADD
) && empty($params['value'])) {
214 $fieldValues = array('option_group_id' => $optionGroupID);
215 // use the next available value
216 /* CONVERT(value, DECIMAL) is used to convert varchar
217 field 'value' to decimal->integer */
219 $params['value'] = (int) CRM_Utils_Weight
::getDefaultWeight('CRM_Core_DAO_OptionValue',
221 'CONVERT(value, DECIMAL)'
224 if (!$params['label'] && $params['name']) {
225 $params['label'] = $params['name'];
228 // set name to label if it's not set - but *only* for ADD action (CRM-3522)
229 if (($action & CRM_Core_Action
::ADD
) && empty($params['name']) && $params['label']) {
230 $params['name'] = $params['label'];
232 if ($action & CRM_Core_Action
::UPDATE
) {
233 $ids['optionValue'] = $optionValueID;
235 $optionValue = CRM_Core_BAO_OptionValue
::add($params, $ids);
240 * Check if there is a record with the same name in the db
242 * @param string $value
243 * The value of the field we are checking.
244 * @param string $daoName
245 * The dao object name.
246 * @param string $daoID
247 * The id of the object being updated. u can change your name.
248 * as long as there is no conflict
249 * @param int $optionGroupID
250 * @param string $fieldName
251 * The name of the field in the DAO.
254 * true if object exists
256 public static function optionExists($value, $daoName, $daoID, $optionGroupID, $fieldName = 'name') {
257 $object = new $daoName();
258 $object->$fieldName = $value;
259 $object->option_group_id
= $optionGroupID;
261 if ($object->find(TRUE)) {
262 return ($daoID && $object->id
== $daoID) ?
TRUE : FALSE;
270 * Check if there is a record with the same name in the db
272 * @param string $mode
273 * @param string $contactType
276 * true if object exists
278 public static function getFields($mode = '', $contactType = 'Individual') {
279 $key = "$mode $contactType";
280 if (empty(self
::$_fields[$key]) ||
!self
::$_fields[$key]) {
281 self
::$_fields[$key] = array();
283 $option = CRM_Core_DAO_OptionValue
::import();
285 foreach (array_keys($option) as $id) {
286 $optionName = $option[$id];
289 $nameTitle = array();
290 if ($mode == 'contribute') {
292 'payment_instrument' => array(
293 'name' => 'payment_instrument',
294 'title' => ts('Payment Instrument'),
295 'headerPattern' => '/^payment|(p(ayment\s)?instrument)$/i',
299 elseif ($mode == '') {
300 //the fields email greeting and postal greeting are meant only for Individual and Household
301 //the field addressee is meant for all contact types, CRM-4575
302 if (in_array($contactType, array(
309 'addressee' => array(
310 'name' => 'addressee',
311 'title' => ts('Addressee'),
312 'headerPattern' => '/^addressee$/i',
316 'email_greeting' => array(
317 'name' => 'email_greeting',
318 'title' => ts('Email Greeting'),
319 'headerPattern' => '/^email_greeting$/i',
321 'postal_greeting' => array(
322 'name' => 'postal_greeting',
323 'title' => ts('Postal Greeting'),
324 'headerPattern' => '/^postal_greeting$/i',
327 $nameTitle = array_merge($nameTitle, $title);
331 if (is_array($nameTitle)) {
332 foreach ($nameTitle as $name => $attribs) {
333 self
::$_fields[$key][$name] = $optionName;
334 list($tableName, $fieldName) = explode('.', $optionName['where']);
335 self
::$_fields[$key][$name]['where'] = "{$name}.label";
336 foreach ($attribs as $k => $val) {
337 self
::$_fields[$key][$name][$k] = $val;
343 return self
::$_fields[$key];
347 * Build select query in case of option-values
353 public static function select(&$query) {
354 if (!empty($query->_params
) ||
!empty($query->_returnProperties
)) {
355 $field = self
::getFields();
356 foreach ($field as $name => $values) {
357 if (!empty($values['pseudoconstant'])) {
360 list($tableName, $fieldName) = explode('.', $values['where']);
361 if (!empty($query->_returnProperties
[$name])) {
362 $query->_select
["{$name}_id"] = "{$name}.value as {$name}_id";
363 $query->_element
["{$name}_id"] = 1;
364 $query->_select
[$name] = "{$name}.{$fieldName} as $name";
365 $query->_tables
[$tableName] = 1;
366 $query->_element
[$name] = 1;
373 * Return option-values of a particular group
375 * @param array $groupParams
376 * Array containing group fields.
377 * whose option-values is to retrieved.
378 * @param array $values
379 * (reference) to the array which.
380 * will have the values for the group
381 * @param string $orderBy
382 * For orderBy clause.
384 * @param bool $isActive Do you want only active option values?
387 * Array of option-values
390 public static function getValues($groupParams, &$values, $orderBy = 'weight', $isActive = FALSE) {
391 if (empty($groupParams)) {
396 option_value.id as id,
397 option_value.label as label,
398 option_value.value as value,
399 option_value.name as name,
400 option_value.description as description,
401 option_value.weight as weight,
402 option_value.is_active as is_active,
403 option_value.is_default as is_default";
407 civicrm_option_value as option_value,
408 civicrm_option_group as option_group ";
410 $where = " WHERE option_group.id = option_value.option_group_id ";
413 $where .= " AND option_value.is_active = " . $isActive;
416 $order = " ORDER BY " . $orderBy;
418 $groupId = CRM_Utils_Array
::value('id', $groupParams);
419 $groupName = CRM_Utils_Array
::value('name', $groupParams);
422 $where .= " AND option_group.id = %1";
423 $params[1] = array($groupId, 'Integer');
425 $groupName = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup',
426 $groupId, 'name', 'id'
432 $where .= " AND option_group.name = %2";
433 $params[2] = array($groupName, 'String');
436 if (in_array($groupName, CRM_Core_OptionGroup
::$_domainIDGroups)) {
437 $where .= " AND option_value.domain_id = " . CRM_Core_Config
::domainID();
440 $query = $select . $from . $where . $order;
442 $dao = CRM_Core_DAO
::executeQuery($query, $params);
444 while ($dao->fetch()) {
445 $values[$dao->id
] = array(
447 'label' => $dao->label
,
448 'value' => $dao->value
,
449 'name' => $dao->name
,
450 'description' => $dao->description
,
451 'weight' => $dao->weight
,
452 'is_active' => $dao->is_active
,
453 'is_default' => $dao->is_default
,