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
43 static $_exportableFields = NULL;
46 * Static field for all the option value information that we can potentially export
51 static $_importableFields = NULL;
54 * Static field for all the option value information that we can potentially export
59 static $_fields = NULL;
62 * Return option-values of a particular group
64 * @param array $groupParams
65 * Array containing group fields whose option-values is to retrieved.
66 * @param string $orderBy
69 * Has links like edit, delete, disable ..etc.
71 * @return array of option-values
75 public static function getRows($groupParams, $links, $orderBy = 'weight') {
76 $optionValue = array();
78 $optionGroupID = NULL;
79 if (!isset($groupParams['id']) ||
!$groupParams['id']) {
80 if ($groupParams['name']) {
81 $config = CRM_Core_Config
::singleton();
83 $optionGroup = CRM_Core_BAO_OptionGroup
::retrieve($groupParams, $dnc);
84 $optionGroupID = $optionGroup->id
;
88 $optionGroupID = $groupParams['id'];
91 $groupName = CRM_Utils_Array
::value('name', $groupParams);
92 if (!$groupName && $optionGroupID) {
93 $groupName = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup',
94 $optionGroupID, 'name', 'id'
98 $dao = new CRM_Core_DAO_OptionValue();
100 if ($optionGroupID) {
101 $dao->option_group_id
= $optionGroupID;
103 if (in_array($groupName, CRM_Core_OptionGroup
::$_domainIDGroups)) {
104 $dao->domain_id
= CRM_Core_Config
::domainID();
107 $dao->orderBy($orderBy);
111 if ($groupName == 'case_type') {
112 $caseTypeIds = CRM_Case_BAO_Case
::getUsedCaseType();
114 elseif ($groupName == 'case_status') {
115 $caseStatusIds = CRM_Case_BAO_Case
::getUsedCaseStatuses();
118 $componentNames = CRM_Core_Component
::getNames();
119 $visibilityLabels = CRM_Core_PseudoConstant
::visibility();
120 while ($dao->fetch()) {
121 $optionValue[$dao->id
] = array();
122 CRM_Core_DAO
::storeValues($dao, $optionValue[$dao->id
]);
123 // form all action links
124 $action = array_sum(array_keys($links));
126 // update enable/disable links depending on if it is is_reserved or is_active
127 if ($dao->is_reserved
) {
128 $action = CRM_Core_Action
::UPDATE
;
131 if ($dao->is_active
) {
132 $action -= CRM_Core_Action
::ENABLE
;
135 $action -= CRM_Core_Action
::DISABLE
;
137 if ((($groupName == 'case_type') && in_array($dao->value
, $caseTypeIds)) ||
138 (($groupName == 'case_status') && in_array($dao->value
, $caseStatusIds))
140 $action -= CRM_Core_Action
::DELETE
;
144 $optionValue[$dao->id
]['label'] = htmlspecialchars($optionValue[$dao->id
]['label']);
145 $optionValue[$dao->id
]['order'] = $optionValue[$dao->id
]['weight'];
146 $optionValue[$dao->id
]['action'] = CRM_Core_Action
::formLink($links, $action,
149 'gid' => $optionGroupID,
150 'value' => $dao->value
,
154 'optionValue.row.actions',
159 if (!empty($optionValue[$dao->id
]['component_id'])) {
160 $optionValue[$dao->id
]['component_name'] = $componentNames[$optionValue[$dao->id
]['component_id']];
163 $optionValue[$dao->id
]['component_name'] = 'Contact';
166 if (!empty($optionValue[$dao->id
]['visibility_id'])) {
167 $optionValue[$dao->id
]['visibility_label'] = $visibilityLabels[$optionValue[$dao->id
]['visibility_id']];
175 * Add/edit option-value of a particular group
177 * @param array $params
178 * Array containing exported values from the invoking form.
179 * @param array $groupParams
180 * Array containing group fields whose option-values is to retrieved/saved.
182 * @param int $optionValueIDHas the id of the optionValue being edited, disabled ..etc.
183 * Has the id of the optionValue being edited, disabled ..etc.
185 * @return CRM_Core_DAO_OptionValue
189 public static function addOptionValue(&$params, &$groupParams, &$action, &$optionValueID) {
190 $params['is_active'] = CRM_Utils_Array
::value('is_active', $params, FALSE);
191 // checking if the group name with the given id or name (in $groupParams) exists
192 if (!empty($groupParams)) {
193 $config = CRM_Core_Config
::singleton();
194 $groupParams['is_active'] = 1;
195 $optionGroup = CRM_Core_BAO_OptionGroup
::retrieve($groupParams, $defaults);
198 // if the corresponding group doesn't exist, create one, provided $groupParams has 'name' in it.
199 if (!$optionGroup->id
) {
200 if ($groupParams['name']) {
201 $newOptionGroup = CRM_Core_BAO_OptionGroup
::add($groupParams, $defaults);
202 $params['weight'] = 1;
203 $optionGroupID = $newOptionGroup->id
;
207 $optionGroupID = $optionGroup->id
;
209 if ($optionValueID) {
210 $oldWeight = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionValue', $optionValueID, 'weight', 'id');
212 $fieldValues = array('option_group_id' => $optionGroupID);
213 $params['weight'] = CRM_Utils_Weight
::updateOtherWeights('CRM_Core_DAO_OptionValue', $oldWeight, CRM_Utils_Array
::value('weight', $params), $fieldValues);
215 $params['option_group_id'] = $optionGroupID;
217 if (($action & CRM_Core_Action
::ADD
) && empty($params['value'])) {
218 $fieldValues = array('option_group_id' => $optionGroupID);
219 // use the next available value
220 /* CONVERT(value, DECIMAL) is used to convert varchar
221 field 'value' to decimal->integer */
223 $params['value'] = (int) CRM_Utils_Weight
::getDefaultWeight('CRM_Core_DAO_OptionValue',
225 'CONVERT(value, DECIMAL)'
228 if (!$params['label'] && $params['name']) {
229 $params['label'] = $params['name'];
232 // set name to label if it's not set - but *only* for ADD action (CRM-3522)
233 if (($action & CRM_Core_Action
::ADD
) && empty($params['name']) && $params['label']) {
234 $params['name'] = $params['label'];
236 if ($action & CRM_Core_Action
::UPDATE
) {
237 $ids['optionValue'] = $optionValueID;
239 $optionValue = CRM_Core_BAO_OptionValue
::add($params, $ids);
244 * Check if there is a record with the same name in the db
246 * @param string $value
247 * The value of the field we are checking.
248 * @param string $daoName
249 * The dao object name.
250 * @param string $daoID
251 * The id of the object being updated. u can change your name.
252 * as long as there is no conflict
253 * @param int $optionGroupID
254 * @param string $fieldName
255 * The name of the field in the DAO.
257 * @return boolean true if object exists
260 public static function optionExists($value, $daoName, $daoID, $optionGroupID, $fieldName = 'name') {
261 $object = new $daoName();
262 $object->$fieldName = $value;
263 $object->option_group_id
= $optionGroupID;
265 if ($object->find(TRUE)) {
266 return ($daoID && $object->id
== $daoID) ?
TRUE : FALSE;
274 * Check if there is a record with the same name in the db
276 * @param string $mode
277 * @param string $contactType
279 * @return boolean true if object exists
282 public static function getFields($mode = '', $contactType = 'Individual') {
283 $key = "$mode $contactType";
284 if (empty(self
::$_fields[$key]) ||
!self
::$_fields[$key]) {
285 self
::$_fields[$key] = array();
287 $option = CRM_Core_DAO_OptionValue
::import();
289 foreach (array_keys($option) as $id) {
290 $optionName = $option[$id];
293 $nameTitle = array();
294 if ($mode == 'contribute') {
296 'payment_instrument' => array(
297 'name' => 'payment_instrument',
298 'title' => ts('Payment Instrument'),
299 'headerPattern' => '/^payment|(p(ayment\s)?instrument)$/i',
303 elseif ($mode == '') {
304 //the fields email greeting and postal greeting are meant only for Individual and Household
305 //the field addressee is meant for all contact types, CRM-4575
306 if (in_array($contactType, array(
307 'Individual', 'Household', 'Organization', 'All'))) {
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 $isActiveDo you want only active option values?.
385 * Do you want only active option values?.
387 * @return array of option-values
391 public static function getValues($groupParams, &$values, $orderBy = 'weight', $isActive = FALSE) {
392 if (empty($groupParams)) {
397 option_value.id as id,
398 option_value.label as label,
399 option_value.value as value,
400 option_value.name as name,
401 option_value.description as description,
402 option_value.weight as weight,
403 option_value.is_active as is_active,
404 option_value.is_default as is_default";
408 civicrm_option_value as option_value,
409 civicrm_option_group as option_group ";
411 $where = " WHERE option_group.id = option_value.option_group_id ";
414 $where .= " AND option_value.is_active = " . $isActive;
417 $order = " ORDER BY " . $orderBy;
419 $groupId = CRM_Utils_Array
::value('id', $groupParams);
420 $groupName = CRM_Utils_Array
::value('name', $groupParams);
423 $where .= " AND option_group.id = %1";
424 $params[1] = array($groupId, 'Integer');
426 $groupName = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup',
427 $groupId, 'name', 'id'
433 $where .= " AND option_group.name = %2";
434 $params[2] = array($groupName, 'String');
437 if (in_array($groupName, CRM_Core_OptionGroup
::$_domainIDGroups)) {
438 $where .= " AND option_value.domain_id = " . CRM_Core_Config
::domainID();
441 $query = $select . $from . $where . $order;
443 $dao = CRM_Core_DAO
::executeQuery($query, $params);
445 while ($dao->fetch()) {
446 $values[$dao->id
] = array(
448 'label' => $dao->label
,
449 'value' => $dao->value
,
450 'name' => $dao->name
,
451 'description' => $dao->description
,
452 'weight' => $dao->weight
,
453 'is_active' => $dao->is_active
,
454 'is_default' => $dao->is_default
,