CRM-15854 - Fix crash on review form
[civicrm-core.git] / CRM / Core / OptionValue.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35class CRM_Core_OptionValue {
36
37 /**
d09edf64 38 * Static field for all the option value information that we can potentially export.
6a488035
TO
39 *
40 * @var array
6a488035
TO
41 */
42 static $_exportableFields = NULL;
43
44 /**
d09edf64 45 * Static field for all the option value information that we can potentially export.
6a488035
TO
46 *
47 * @var array
6a488035
TO
48 */
49 static $_importableFields = NULL;
50
51 /**
d09edf64 52 * Static field for all the option value information that we can potentially export.
6a488035
TO
53 *
54 * @var array
6a488035
TO
55 */
56 static $_fields = NULL;
57
58 /**
100fef9d 59 * Return option-values of a particular group
6a488035 60 *
6a0b768e
TO
61 * @param array $groupParams
62 * Array containing group fields whose option-values is to retrieved.
6a0b768e
TO
63 * @param array $links
64 * Has links like edit, delete, disable ..etc.
8d7a9d07
CB
65 * @param string $orderBy
66 * For orderBy clause.
6a488035 67 *
a6c01b45 68 * @return array
16b10e64 69 * Array of option-values
6a488035 70 *
6a488035 71 */
00be9182 72 public static function getRows($groupParams, $links, $orderBy = 'weight') {
6a488035
TO
73 $optionValue = array();
74
75 $optionGroupID = NULL;
76 if (!isset($groupParams['id']) || !$groupParams['id']) {
77 if ($groupParams['name']) {
78 $config = CRM_Core_Config::singleton();
79
80 $optionGroup = CRM_Core_BAO_OptionGroup::retrieve($groupParams, $dnc);
81 $optionGroupID = $optionGroup->id;
82 }
83 }
84 else {
85 $optionGroupID = $groupParams['id'];
86 }
87
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'
92 );
93 }
94
95 $dao = new CRM_Core_DAO_OptionValue();
96
97 if ($optionGroupID) {
98 $dao->option_group_id = $optionGroupID;
99
100 if (in_array($groupName, CRM_Core_OptionGroup::$_domainIDGroups)) {
101 $dao->domain_id = CRM_Core_Config::domainID();
102 }
103
104 $dao->orderBy($orderBy);
105 $dao->find();
106 }
107
108 if ($groupName == 'case_type') {
109 $caseTypeIds = CRM_Case_BAO_Case::getUsedCaseType();
110 }
111 elseif ($groupName == 'case_status') {
112 $caseStatusIds = CRM_Case_BAO_Case::getUsedCaseStatuses();
113 }
114
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));
122
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;
126 }
127 else {
128 if ($dao->is_active) {
129 $action -= CRM_Core_Action::ENABLE;
130 }
131 else {
132 $action -= CRM_Core_Action::DISABLE;
133 }
134 if ((($groupName == 'case_type') && in_array($dao->value, $caseTypeIds)) ||
135 (($groupName == 'case_status') && in_array($dao->value, $caseStatusIds))
136 ) {
137 $action -= CRM_Core_Action::DELETE;
138 }
139 }
140
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,
144 array(
145 'id' => $dao->id,
146 'gid' => $optionGroupID,
147 'value' => $dao->value,
87dab4a4
AH
148 ),
149 ts('more'),
150 FALSE,
151 'optionValue.row.actions',
152 'optionValue',
153 $dao->id
6a488035
TO
154 );
155
a7488080 156 if (!empty($optionValue[$dao->id]['component_id'])) {
6a488035
TO
157 $optionValue[$dao->id]['component_name'] = $componentNames[$optionValue[$dao->id]['component_id']];
158 }
159 else {
160 $optionValue[$dao->id]['component_name'] = 'Contact';
161 }
162
a7488080 163 if (!empty($optionValue[$dao->id]['visibility_id'])) {
6a488035
TO
164 $optionValue[$dao->id]['visibility_label'] = $visibilityLabels[$optionValue[$dao->id]['visibility_id']];
165 }
166 }
167
168 return $optionValue;
169 }
170
171 /**
100fef9d 172 * Add/edit option-value of a particular group
6a488035 173 *
6a0b768e
TO
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.
fd31fa4c 178 * @param $action
8d7a9d07 179 * @param int $optionValueID Has the id of the optionValue being edited, disabled ..etc.
6a0b768e 180 * Has the id of the optionValue being edited, disabled ..etc.
6a488035 181 *
7c285037 182 * @return CRM_Core_DAO_OptionValue
6a488035 183 *
6a488035 184 */
00be9182 185 public static function addOptionValue(&$params, &$groupParams, &$action, &$optionValueID) {
6a488035
TO
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);
192 }
193
194 // if the corresponding group doesn't exist, create one, provided $groupParams has 'name' in it.
195 if (!$optionGroup->id) {
196 if ($groupParams['name']) {
353ffa53 197 $newOptionGroup = CRM_Core_BAO_OptionGroup::add($groupParams, $defaults);
6a488035 198 $params['weight'] = 1;
353ffa53 199 $optionGroupID = $newOptionGroup->id;
6a488035
TO
200 }
201 }
202 else {
203 $optionGroupID = $optionGroup->id;
204 $oldWeight = NULL;
205 if ($optionValueID) {
206 $oldWeight = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $optionValueID, 'weight', 'id');
207 }
208 $fieldValues = array('option_group_id' => $optionGroupID);
fc161185 209 $params['weight'] = CRM_Utils_Weight::updateOtherWeights('CRM_Core_DAO_OptionValue', $oldWeight, CRM_Utils_Array::value('weight', $params), $fieldValues);
6a488035
TO
210 }
211 $params['option_group_id'] = $optionGroupID;
212
8cc574cf 213 if (($action & CRM_Core_Action::ADD) && empty($params['value'])) {
6a488035
TO
214 $fieldValues = array('option_group_id' => $optionGroupID);
215 // use the next available value
216 /* CONVERT(value, DECIMAL) is used to convert varchar
e70a7fc0 217 field 'value' to decimal->integer */
6a488035
TO
218
219 $params['value'] = (int) CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_OptionValue',
220 $fieldValues,
221 'CONVERT(value, DECIMAL)'
222 );
223 }
224 if (!$params['label'] && $params['name']) {
225 $params['label'] = $params['name'];
226 }
227
228 // set name to label if it's not set - but *only* for ADD action (CRM-3522)
8cc574cf 229 if (($action & CRM_Core_Action::ADD) && empty($params['name']) && $params['label']) {
6a488035
TO
230 $params['name'] = $params['label'];
231 }
232 if ($action & CRM_Core_Action::UPDATE) {
233 $ids['optionValue'] = $optionValueID;
234 }
235 $optionValue = CRM_Core_BAO_OptionValue::add($params, $ids);
236 return $optionValue;
237 }
238
239 /**
d09edf64 240 * Check if there is a record with the same name in the db.
6a488035 241 *
6a0b768e
TO
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.
6a488035 248 * as long as there is no conflict
100fef9d 249 * @param int $optionGroupID
6a0b768e
TO
250 * @param string $fieldName
251 * The name of the field in the DAO.
6a488035 252 *
8d7a9d07 253 * @return bool
a6c01b45 254 * true if object exists
6a488035 255 */
00be9182 256 public static function optionExists($value, $daoName, $daoID, $optionGroupID, $fieldName = 'name') {
4d5c2eb5 257 $object = new $daoName();
6a488035
TO
258 $object->$fieldName = $value;
259 $object->option_group_id = $optionGroupID;
260
261 if ($object->find(TRUE)) {
262 return ($daoID && $object->id == $daoID) ? TRUE : FALSE;
263 }
264 else {
265 return TRUE;
266 }
267 }
268
269 /**
d09edf64 270 * Check if there is a record with the same name in the db.
6a488035 271 *
da6b46f4
EM
272 * @param string $mode
273 * @param string $contactType
274 *
8d7a9d07 275 * @return bool
a6c01b45 276 * true if object exists
6a488035 277 */
00be9182 278 public static function getFields($mode = '', $contactType = 'Individual') {
6a488035
TO
279 $key = "$mode $contactType";
280 if (empty(self::$_fields[$key]) || !self::$_fields[$key]) {
281 self::$_fields[$key] = array();
282
283 $option = CRM_Core_DAO_OptionValue::import();
284
285 foreach (array_keys($option) as $id) {
286 $optionName = $option[$id];
287 }
288
289 $nameTitle = array();
290 if ($mode == 'contribute') {
291 $nameTitle = array(
292 'payment_instrument' => array(
293 'name' => 'payment_instrument',
294 'title' => ts('Payment Instrument'),
295 'headerPattern' => '/^payment|(p(ayment\s)?instrument)$/i',
296 ),
297 );
298 }
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(
353ffa53
TO
303 'Individual',
304 'Household',
305 'Organization',
8d7a9d07 306 'All',
353ffa53 307 ))) {
6a488035 308 $nameTitle = array(
1071730c
DL
309 'addressee' => array(
310 'name' => 'addressee',
6a488035
TO
311 'title' => ts('Addressee'),
312 'headerPattern' => '/^addressee$/i',
313 ),
314 );
315 $title = array(
1071730c
DL
316 'email_greeting' => array(
317 'name' => 'email_greeting',
6a488035
TO
318 'title' => ts('Email Greeting'),
319 'headerPattern' => '/^email_greeting$/i',
320 ),
321 'postal_greeting' => array(
322 'name' => 'postal_greeting',
323 'title' => ts('Postal Greeting'),
324 'headerPattern' => '/^postal_greeting$/i',
325 ),
326 );
327 $nameTitle = array_merge($nameTitle, $title);
328 }
0c145cc0 329 }
6a488035
TO
330
331 if (is_array($nameTitle)) {
332 foreach ($nameTitle as $name => $attribs) {
333 self::$_fields[$key][$name] = $optionName;
334 list($tableName, $fieldName) = explode('.', $optionName['where']);
6a488035
TO
335 self::$_fields[$key][$name]['where'] = "{$name}.label";
336 foreach ($attribs as $k => $val) {
337 self::$_fields[$key][$name][$k] = $val;
338 }
339 }
340 }
341 }
342
343 return self::$_fields[$key];
344 }
345
346 /**
100fef9d 347 * Build select query in case of option-values
6a488035 348 *
2a6da8d7
EM
349 * @param $query
350 *
6a488035 351 * @return void
6a488035 352 */
00be9182 353 public static function select(&$query) {
6a488035
TO
354 if (!empty($query->_params) || !empty($query->_returnProperties)) {
355 $field = self::getFields();
314dbef8 356 foreach ($field as $name => $values) {
a7488080 357 if (!empty($values['pseudoconstant'])) {
314dbef8
DL
358 continue;
359 }
360 list($tableName, $fieldName) = explode('.', $values['where']);
a7488080 361 if (!empty($query->_returnProperties[$name])) {
6a488035
TO
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;
367 }
368 }
369 }
370 }
371
372 /**
100fef9d 373 * Return option-values of a particular group
6a488035 374 *
6a0b768e
TO
375 * @param array $groupParams
376 * Array containing group fields.
6a488035 377 * whose option-values is to retrieved.
6a0b768e
TO
378 * @param array $values
379 * (reference) to the array which.
6a488035 380 * will have the values for the group
6a0b768e
TO
381 * @param string $orderBy
382 * For orderBy clause.
6a488035 383 *
8d7a9d07 384 * @param bool $isActive Do you want only active option values?
6a488035 385 *
a6c01b45 386 * @return array
16b10e64 387 * Array of option-values
6a488035 388 *
6a488035 389 */
00be9182 390 public static function getValues($groupParams, &$values, $orderBy = 'weight', $isActive = FALSE) {
6a488035
TO
391 if (empty($groupParams)) {
392 return NULL;
393 }
394 $select = "
395SELECT
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";
404
405 $from = "
406FROM
407 civicrm_option_value as option_value,
408 civicrm_option_group as option_group ";
409
410 $where = " WHERE option_group.id = option_value.option_group_id ";
411
412 if ($isActive) {
413 $where .= " AND option_value.is_active = " . $isActive;
414 }
415
416 $order = " ORDER BY " . $orderBy;
417
418 $groupId = CRM_Utils_Array::value('id', $groupParams);
419 $groupName = CRM_Utils_Array::value('name', $groupParams);
420
421 if ($groupId) {
422 $where .= " AND option_group.id = %1";
423 $params[1] = array($groupId, 'Integer');
424 if (!$groupName) {
425 $groupName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup',
426 $groupId, 'name', 'id'
427 );
428 }
429 }
430
431 if ($groupName) {
432 $where .= " AND option_group.name = %2";
433 $params[2] = array($groupName, 'String');
434 }
435
436 if (in_array($groupName, CRM_Core_OptionGroup::$_domainIDGroups)) {
437 $where .= " AND option_value.domain_id = " . CRM_Core_Config::domainID();
438 }
439
440 $query = $select . $from . $where . $order;
441
442 $dao = CRM_Core_DAO::executeQuery($query, $params);
443
444 while ($dao->fetch()) {
445 $values[$dao->id] = array(
446 'id' => $dao->id,
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,
454 );
455 }
456 }
96025800 457
6a488035 458}