3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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 * Function to return option-values of a particular group
64 * @param array $groupParams Array containing group fields whose option-values is to retrieved.
65 * @param string $orderBy for orderBy clause
66 * @param array $links has links like edit, delete, disable ..etc
68 * @return array of option-values
73 static function getRows($groupParams, $links, $orderBy = 'weight') {
74 $optionValue = array();
76 $optionGroupID = NULL;
77 if (!isset($groupParams['id']) ||
!$groupParams['id']) {
78 if ($groupParams['name']) {
79 $config = CRM_Core_Config
::singleton();
81 $optionGroup = CRM_Core_BAO_OptionGroup
::retrieve($groupParams, $dnc);
82 $optionGroupID = $optionGroup->id
;
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 // form all action links
122 $action = array_sum(array_keys($links));
124 // update enable/disable links depending on if it is is_reserved or is_active
125 if ($dao->is_reserved
) {
126 $action = CRM_Core_Action
::UPDATE
;
129 if ($dao->is_active
) {
130 $action -= CRM_Core_Action
::ENABLE
;
133 $action -= CRM_Core_Action
::DISABLE
;
135 if ((($groupName == 'case_type') && in_array($dao->value
, $caseTypeIds)) ||
136 (($groupName == 'case_status') && in_array($dao->value
, $caseStatusIds))
138 $action -= CRM_Core_Action
::DELETE
;
142 $optionValue[$dao->id
]['label'] = htmlspecialchars($optionValue[$dao->id
]['label']);
143 $optionValue[$dao->id
]['order'] = $optionValue[$dao->id
]['weight'];
144 $optionValue[$dao->id
]['action'] = CRM_Core_Action
::formLink($links, $action,
147 'gid' => $optionGroupID,
148 'value' => $dao->value
,
152 'optionValue.row.actions',
157 if (!empty($optionValue[$dao->id
]['component_id'])) {
158 $optionValue[$dao->id
]['component_name'] = $componentNames[$optionValue[$dao->id
]['component_id']];
161 $optionValue[$dao->id
]['component_name'] = 'Contact';
164 if (!empty($optionValue[$dao->id
]['visibility_id'])) {
165 $optionValue[$dao->id
]['visibility_label'] = $visibilityLabels[$optionValue[$dao->id
]['visibility_id']];
173 * Function to add/edit option-value of a particular group
175 * @param array $params Array containing exported values from the invoking form.
176 * @param array $groupParams Array containing group fields whose option-values is to retrieved/saved.
178 * @param integer $optionValueID has the id of the optionValue being edited, disabled ..etc
180 * @internal param string $orderBy for orderBy clause
181 * @return CRM_Core_DAO_OptionValue
186 static function addOptionValue(&$params, &$groupParams, &$action, &$optionValueID) {
187 $params['is_active'] = CRM_Utils_Array
::value('is_active', $params, FALSE);
188 // checking if the group name with the given id or name (in $groupParams) exists
189 if (!empty($groupParams)) {
190 $config = CRM_Core_Config
::singleton();
191 $groupParams['is_active'] = 1;
192 $optionGroup = CRM_Core_BAO_OptionGroup
::retrieve($groupParams, $defaults);
195 // if the corresponding group doesn't exist, create one, provided $groupParams has 'name' in it.
196 if (!$optionGroup->id
) {
197 if ($groupParams['name']) {
198 $newOptionGroup = CRM_Core_BAO_OptionGroup
::add($groupParams, $defaults);
199 $params['weight'] = 1;
200 $optionGroupID = $newOptionGroup->id
;
204 $optionGroupID = $optionGroup->id
;
206 if ($optionValueID) {
207 $oldWeight = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionValue', $optionValueID, 'weight', 'id');
209 $fieldValues = array('option_group_id' => $optionGroupID);
210 $params['weight'] = CRM_Utils_Weight
::updateOtherWeights('CRM_Core_DAO_OptionValue', $oldWeight, CRM_Utils_Array
::value('weight', $params), $fieldValues);
212 $params['option_group_id'] = $optionGroupID;
214 if (($action & CRM_Core_Action
::ADD
) && empty($params['value'])) {
215 $fieldValues = array('option_group_id' => $optionGroupID);
216 // use the next available value
217 /* CONVERT(value, DECIMAL) is used to convert varchar
218 field 'value' to decimal->integer */
220 $params['value'] = (int) CRM_Utils_Weight
::getDefaultWeight('CRM_Core_DAO_OptionValue',
222 'CONVERT(value, DECIMAL)'
225 if (!$params['label'] && $params['name']) {
226 $params['label'] = $params['name'];
229 // set name to label if it's not set - but *only* for ADD action (CRM-3522)
230 if (($action & CRM_Core_Action
::ADD
) && empty($params['name']) && $params['label']) {
231 $params['name'] = $params['label'];
233 if ($action & CRM_Core_Action
::UPDATE
) {
234 $ids['optionValue'] = $optionValueID;
236 $optionValue = CRM_Core_BAO_OptionValue
::add($params, $ids);
241 * Check if there is a record with the same name in the db
243 * @param string $value the value of the field we are checking
244 * @param string $daoName the dao object name
245 * @param string $daoID the id of the object being updated. u can change your name
246 * as long as there is no conflict
247 * @param $optionGroupID
248 * @param string $fieldName the name of the field in the DAO
250 * @return boolean true if object exists
254 static function optionExists($value, $daoName, $daoID, $optionGroupID, $fieldName = 'name') {
255 $object = new $daoName();
256 $object->$fieldName = $value;
257 $object->option_group_id
= $optionGroupID;
259 if ($object->find(TRUE)) {
260 return ($daoID && $object->id
== $daoID) ?
TRUE : FALSE;
268 * Check if there is a record with the same name in the db
270 * @param string $mode
271 * @param string $contactType
273 * @internal param string $value the value of the field we are checking
274 * @internal param string $daoName the dao object name
275 * @internal param string $daoID the id of the object being updated. u can change your name
276 * as long as there is no conflict
277 * @internal param string $fieldName the name of the field in the DAO
279 * @return boolean true if object exists
283 static function getFields($mode = '', $contactType = 'Individual') {
284 $key = "$mode $contactType";
285 if (empty(self
::$_fields[$key]) ||
!self
::$_fields[$key]) {
286 self
::$_fields[$key] = array();
288 $option = CRM_Core_DAO_OptionValue
::import();
290 foreach (array_keys($option) as $id) {
291 $optionName = $option[$id];
294 $nameTitle = array();
295 if ($mode == 'contribute') {
297 'payment_instrument' => array(
298 'name' => 'payment_instrument',
299 'title' => ts('Payment Instrument'),
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, array(
308 'Individual', 'Household', 'Organization', 'All'))) {
310 'addressee' => array(
311 'name' => 'addressee',
312 'title' => ts('Addressee'),
313 'headerPattern' => '/^addressee$/i',
317 'email_greeting' => array(
318 'name' => 'email_greeting',
319 'title' => ts('Email Greeting'),
320 'headerPattern' => '/^email_greeting$/i',
322 'postal_greeting' => array(
323 'name' => 'postal_greeting',
324 'title' => ts('Postal Greeting'),
325 'headerPattern' => '/^postal_greeting$/i',
328 $nameTitle = array_merge($nameTitle, $title);
332 if (is_array($nameTitle)) {
333 foreach ($nameTitle as $name => $attribs) {
334 self
::$_fields[$key][$name] = $optionName;
335 list($tableName, $fieldName) = explode('.', $optionName['where']);
336 self
::$_fields[$key][$name]['where'] = "{$name}.label";
337 foreach ($attribs as $k => $val) {
338 self
::$_fields[$key][$name][$k] = $val;
344 return self
::$_fields[$key];
348 * build select query in case of option-values
355 static function select(&$query) {
356 if (!empty($query->_params
) ||
!empty($query->_returnProperties
)) {
357 $field = self
::getFields();
358 foreach ($field as $name => $values) {
359 if (!empty($values['pseudoconstant'])) {
362 list($tableName, $fieldName) = explode('.', $values['where']);
363 if (!empty($query->_returnProperties
[$name])) {
364 $query->_select
["{$name}_id"] = "{$name}.value as {$name}_id";
365 $query->_element
["{$name}_id"] = 1;
366 $query->_select
[$name] = "{$name}.{$fieldName} as $name";
367 $query->_tables
[$tableName] = 1;
368 $query->_element
[$name] = 1;
375 * Function to return option-values of a particular group
377 * @param array $groupParams Array containing group fields
378 * whose option-values is to retrieved.
379 * @param array $values (reference) to the array which
380 * will have the values for the group
381 * @param string $orderBy for orderBy clause
383 * @param boolean $isActive do you want only active option values?
385 * @return array of option-values
390 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
,