Merge pull request #694 from ravishnair/CRM-12528
[civicrm-core.git] / CRM / Core / OptionValue.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2013
32 * $Id$
33 *
34 */
35class CRM_Core_OptionValue {
36
37 /**
38 * static field for all the option value information that we can potentially export
39 *
40 * @var array
41 * @static
42 */
43 static $_exportableFields = NULL;
44
45 /**
46 * static field for all the option value information that we can potentially export
47 *
48 * @var array
49 * @static
50 */
51 static $_importableFields = NULL;
52
53 /**
54 * static field for all the option value information that we can potentially export
55 *
56 * @var array
57 * @static
58 */
59 static $_fields = NULL;
60
61 /**
62 * Function to return option-values of a particular group
63 *
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
67 *
68 * @return array of option-values
69 *
70 * @access public
71 * @static
72 */
73 static function getRows($groupParams, $links, $orderBy = 'weight') {
74 $optionValue = array();
75
76 $optionGroupID = NULL;
77 if (!isset($groupParams['id']) || !$groupParams['id']) {
78 if ($groupParams['name']) {
79 $config = CRM_Core_Config::singleton();
80
81 $optionGroup = CRM_Core_BAO_OptionGroup::retrieve($groupParams, $dnc);
82 $optionGroupID = $optionGroup->id;
83 }
84 }
85 else {
86 $optionGroupID = $groupParams['id'];
87 }
88
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'
93 );
94 }
95
96 $dao = new CRM_Core_DAO_OptionValue();
97
98 if ($optionGroupID) {
99 $dao->option_group_id = $optionGroupID;
100
101 if (in_array($groupName, CRM_Core_OptionGroup::$_domainIDGroups)) {
102 $dao->domain_id = CRM_Core_Config::domainID();
103 }
104
105 $dao->orderBy($orderBy);
106 $dao->find();
107 }
108
109 if ($groupName == 'case_type') {
110 $caseTypeIds = CRM_Case_BAO_Case::getUsedCaseType();
111 }
112 elseif ($groupName == 'case_status') {
113 $caseStatusIds = CRM_Case_BAO_Case::getUsedCaseStatuses();
114 }
115
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));
123
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;
127 }
128 else {
129 if ($dao->is_active) {
130 $action -= CRM_Core_Action::ENABLE;
131 }
132 else {
133 $action -= CRM_Core_Action::DISABLE;
134 }
135 if ((($groupName == 'case_type') && in_array($dao->value, $caseTypeIds)) ||
136 (($groupName == 'case_status') && in_array($dao->value, $caseStatusIds))
137 ) {
138 $action -= CRM_Core_Action::DELETE;
139 }
140 }
141
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,
145 array(
146 'id' => $dao->id,
147 'gid' => $optionGroupID,
148 'value' => $dao->value,
149 )
150 );
151
152 if (CRM_Utils_Array::value('component_id', $optionValue[$dao->id])) {
153 $optionValue[$dao->id]['component_name'] = $componentNames[$optionValue[$dao->id]['component_id']];
154 }
155 else {
156 $optionValue[$dao->id]['component_name'] = 'Contact';
157 }
158
159 if (CRM_Utils_Array::value('visibility_id', $optionValue[$dao->id])) {
160 $optionValue[$dao->id]['visibility_label'] = $visibilityLabels[$optionValue[$dao->id]['visibility_id']];
161 }
162 }
163
164 return $optionValue;
165 }
166
167 /**
168 * Function to add/edit option-value of a particular group
169 *
170 * @param array $params Array containing exported values from the invoking form.
171 * @param array $groupParams Array containing group fields whose option-values is to retrieved/saved.
172 * @param string $orderBy for orderBy clause
173 * @param integer $optionValueID has the id of the optionValue being edited, disabled ..etc
174 *
175 * @return array of option-values
176 *
177 * @access public
178 * @static
179 */
180 static function addOptionValue(&$params, &$groupParams, &$action, &$optionValueID) {
181 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
182 // checking if the group name with the given id or name (in $groupParams) exists
183 if (!empty($groupParams)) {
184 $config = CRM_Core_Config::singleton();
185 $groupParams['is_active'] = 1;
186 $optionGroup = CRM_Core_BAO_OptionGroup::retrieve($groupParams, $defaults);
187 }
188
189 // if the corresponding group doesn't exist, create one, provided $groupParams has 'name' in it.
190 if (!$optionGroup->id) {
191 if ($groupParams['name']) {
192 $newOptionGroup = CRM_Core_BAO_OptionGroup::add($groupParams, $defaults);
193 $params['weight'] = 1;
194 $optionGroupID = $newOptionGroup->id;
195 }
196 }
197 else {
198 $optionGroupID = $optionGroup->id;
199 $oldWeight = NULL;
200 if ($optionValueID) {
201 $oldWeight = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $optionValueID, 'weight', 'id');
202 }
203 $fieldValues = array('option_group_id' => $optionGroupID);
204 $params['weight'] = CRM_Utils_Weight::updateOtherWeights('CRM_Core_DAO_OptionValue', $oldWeight, $params['weight'], $fieldValues);
205 }
206 $params['option_group_id'] = $optionGroupID;
207
208 if (($action & CRM_Core_Action::ADD) && !CRM_Utils_Array::value('value', $params)) {
209 $fieldValues = array('option_group_id' => $optionGroupID);
210 // use the next available value
211 /* CONVERT(value, DECIMAL) is used to convert varchar
212 field 'value' to decimal->integer */
213
214 $params['value'] = (int) CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_OptionValue',
215 $fieldValues,
216 'CONVERT(value, DECIMAL)'
217 );
218 }
219 if (!$params['label'] && $params['name']) {
220 $params['label'] = $params['name'];
221 }
222
223 // set name to label if it's not set - but *only* for ADD action (CRM-3522)
224 if (($action & CRM_Core_Action::ADD) && !CRM_Utils_Array::value('name', $params) && $params['label']) {
225 $params['name'] = $params['label'];
226 }
227 if ($action & CRM_Core_Action::UPDATE) {
228 $ids['optionValue'] = $optionValueID;
229 }
230 $optionValue = CRM_Core_BAO_OptionValue::add($params, $ids);
231 return $optionValue;
232 }
233
234 /**
235 * Check if there is a record with the same name in the db
236 *
237 * @param string $value the value of the field we are checking
238 * @param string $daoName the dao object name
239 * @param string $daoID the id of the object being updated. u can change your name
240 * as long as there is no conflict
241 * @param string $fieldName the name of the field in the DAO
242 *
243 * @return boolean true if object exists
244 * @access public
245 * @static
246 */
247 static function optionExists($value, $daoName, $daoID, $optionGroupID, $fieldName = 'name') {
248 require_once (str_replace('_', DIRECTORY_SEPARATOR, $daoName) . ".php");
249 eval('$object = new ' . $daoName . '( );');
250 $object->$fieldName = $value;
251 $object->option_group_id = $optionGroupID;
252
253 if ($object->find(TRUE)) {
254 return ($daoID && $object->id == $daoID) ? TRUE : FALSE;
255 }
256 else {
257 return TRUE;
258 }
259 }
260
261 /**
262 * Check if there is a record with the same name in the db
263 *
264 * @param string $value the value of the field we are checking
265 * @param string $daoName the dao object name
266 * @param string $daoID the id of the object being updated. u can change your name
267 * as long as there is no conflict
268 * @param string $fieldName the name of the field in the DAO
269 *
270 * @return boolean true if object exists
271 * @access public
272 * @static
273 */
274 static function getFields($mode = '', $contactType = 'Individual') {
275 $key = "$mode $contactType";
276 if (empty(self::$_fields[$key]) || !self::$_fields[$key]) {
277 self::$_fields[$key] = array();
278
279 $option = CRM_Core_DAO_OptionValue::import();
280
281 foreach (array_keys($option) as $id) {
282 $optionName = $option[$id];
283 }
284
285 $nameTitle = array();
286 if ($mode == 'contribute') {
287 $nameTitle = array(
288 'payment_instrument' => array(
289 'name' => 'payment_instrument',
290 'title' => ts('Payment Instrument'),
291 'headerPattern' => '/^payment|(p(ayment\s)?instrument)$/i',
292 ),
293 );
294 }
295 elseif ($mode == '') {
296 //the fields email greeting and postal greeting are meant only for Individual and Household
297 //the field addressee is meant for all contact types, CRM-4575
298 if (in_array($contactType, array(
299 'Individual', 'Household', 'Organization', 'All'))) {
300 $nameTitle = array(
301 'addressee' => array('name' => 'addressee',
302 'title' => ts('Addressee'),
303 'headerPattern' => '/^addressee$/i',
304 ),
305 );
306 $title = array(
307 'email_greeting' => array('name' => 'email_greeting',
308 'title' => ts('Email Greeting'),
309 'headerPattern' => '/^email_greeting$/i',
310 ),
311 'postal_greeting' => array(
312 'name' => 'postal_greeting',
313 'title' => ts('Postal Greeting'),
314 'headerPattern' => '/^postal_greeting$/i',
315 ),
316 );
317 $nameTitle = array_merge($nameTitle, $title);
318 }
319
320 if ($contactType == 'Individual' || $contactType == 'All') {
321 $title = array(
322 'gender' => array('name' => 'gender',
323 'title' => ts('Gender'),
324 'headerPattern' => '/^gender$/i',
325 ),
326 'individual_prefix' => array(
327 'name' => 'individual_prefix',
328 'title' => ts('Individual Prefix'),
329 'headerPattern' => '/^(prefix|title)/i',
330 ),
331 'individual_suffix' => array(
332 'name' => 'individual_suffix',
333 'title' => ts('Individual Suffix'),
334 'headerPattern' => '/^suffix$/i',
335 ),
336 );
337 $nameTitle = array_merge($nameTitle, $title);
338 }
339 }
340
341 if (is_array($nameTitle)) {
342 foreach ($nameTitle as $name => $attribs) {
343 self::$_fields[$key][$name] = $optionName;
344 list($tableName, $fieldName) = explode('.', $optionName['where']);
345 // not sure of this fix, so keeping it commented for now
346 // this is from CRM-1541
347 // self::$_fields[$mode][$name]['where'] = $name . '.' . $fieldName;
348 self::$_fields[$key][$name]['where'] = "{$name}.label";
349 foreach ($attribs as $k => $val) {
350 self::$_fields[$key][$name][$k] = $val;
351 }
352 }
353 }
354 }
355
356 return self::$_fields[$key];
357 }
358
359 /**
360 * build select query in case of option-values
361 *
362 * @return void
363 * @access public
364 */
365 static function select(&$query) {
366 if (!empty($query->_params) || !empty($query->_returnProperties)) {
367 $field = self::getFields();
368 foreach ($field as $name => $title) {
369 list($tableName, $fieldName) = explode('.', $title['where']);
370 if (CRM_Utils_Array::value($name, $query->_returnProperties)) {
371 $query->_select["{$name}_id"] = "{$name}.value as {$name}_id";
372 $query->_element["{$name}_id"] = 1;
373 $query->_select[$name] = "{$name}.{$fieldName} as $name";
374 $query->_tables[$tableName] = 1;
375 $query->_element[$name] = 1;
376 }
377 }
378 }
379 }
380
381 /**
382 * Function to return option-values of a particular group
383 *
384 * @param array $groupParams Array containing group fields
385 * whose option-values is to retrieved.
386 * @param array $values (referance) to the array which
387 * will have the values for the group
388 * @param string $orderBy for orderBy clause
389 *
390 * @param boolean $isActive do you want only active option values?
391 *
392 * @return array of option-values
393 *
394 * @access public
395 * @static
396 */
397 static function getValues($groupParams, &$values, $orderBy = 'weight', $isActive = FALSE) {
398 if (empty($groupParams)) {
399 return NULL;
400 }
401 $select = "
402SELECT
403 option_value.id as id,
404 option_value.label as label,
405 option_value.value as value,
406 option_value.name as name,
407 option_value.description as description,
408 option_value.weight as weight,
409 option_value.is_active as is_active,
410 option_value.is_default as is_default";
411
412 $from = "
413FROM
414 civicrm_option_value as option_value,
415 civicrm_option_group as option_group ";
416
417 $where = " WHERE option_group.id = option_value.option_group_id ";
418
419 if ($isActive) {
420 $where .= " AND option_value.is_active = " . $isActive;
421 }
422
423 $order = " ORDER BY " . $orderBy;
424
425 $groupId = CRM_Utils_Array::value('id', $groupParams);
426 $groupName = CRM_Utils_Array::value('name', $groupParams);
427
428 if ($groupId) {
429 $where .= " AND option_group.id = %1";
430 $params[1] = array($groupId, 'Integer');
431 if (!$groupName) {
432 $groupName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup',
433 $groupId, 'name', 'id'
434 );
435 }
436 }
437
438 if ($groupName) {
439 $where .= " AND option_group.name = %2";
440 $params[2] = array($groupName, 'String');
441 }
442
443 if (in_array($groupName, CRM_Core_OptionGroup::$_domainIDGroups)) {
444 $where .= " AND option_value.domain_id = " . CRM_Core_Config::domainID();
445 }
446
447 $query = $select . $from . $where . $order;
448
449 $dao = CRM_Core_DAO::executeQuery($query, $params);
450
451 while ($dao->fetch()) {
452 $values[$dao->id] = array(
453 'id' => $dao->id,
454 'label' => $dao->label,
455 'value' => $dao->value,
456 'name' => $dao->name,
457 'description' => $dao->description,
458 'weight' => $dao->weight,
459 'is_active' => $dao->is_active,
460 'is_default' => $dao->is_default,
461 );
462 }
463 }
464}
465