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