Merge pull request #12234 from colemanw/CustomForm
[civicrm-core.git] / CRM / Core / OptionValue.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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-2018
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 $optionGroupID = NULL;
76 $isGroupLocked = FALSE;
77
78 if (!isset($groupParams['id']) || !$groupParams['id']) {
79 if ($groupParams['name']) {
80 $optionGroup = CRM_Core_BAO_OptionGroup::retrieve($groupParams, $dnc);
81 $optionGroupID = $optionGroup->id;
82 $isGroupLocked = (bool) $optionGroup->is_locked;
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 // disallow deletion of option values for locked groups
150 if (($action & CRM_Core_Action::DELETE) && $isGroupLocked) {
151 $action -= CRM_Core_Action::DELETE;
152 }
153
154 $optionValue[$dao->id]['label'] = htmlspecialchars($optionValue[$dao->id]['label']);
155 $optionValue[$dao->id]['order'] = $optionValue[$dao->id]['weight'];
156 $optionValue[$dao->id]['icon'] = CRM_Utils_Array::value('icon', $optionValue[$dao->id], '');
157 $optionValue[$dao->id]['action'] = CRM_Core_Action::formLink($links, $action,
158 array(
159 'id' => $dao->id,
160 'gid' => $optionGroupID,
161 'value' => $dao->value,
162 ),
163 ts('more'),
164 FALSE,
165 'optionValue.row.actions',
166 'optionValue',
167 $dao->id
168 );
169
170 if (!empty($optionValue[$dao->id]['component_id'])) {
171 $optionValue[$dao->id]['component_name'] = $componentNames[$optionValue[$dao->id]['component_id']];
172 }
173 else {
174 $optionValue[$dao->id]['component_name'] = 'Contact';
175 }
176
177 if (!empty($optionValue[$dao->id]['visibility_id'])) {
178 $optionValue[$dao->id]['visibility_label'] = $visibilityLabels[$optionValue[$dao->id]['visibility_id']];
179 }
180 }
181
182 return $optionValue;
183 }
184
185 /**
186 * Add/edit option-value of a particular group
187 *
188 * @param array $params
189 * Array containing exported values from the invoking form.
190 * @param array $groupParams
191 * Array containing group fields whose option-values is to retrieved/saved.
192 * @param $action
193 * @param int $optionValueID Has the id of the optionValue being edited, disabled ..etc.
194 * Has the id of the optionValue being edited, disabled ..etc.
195 *
196 * @return CRM_Core_DAO_OptionValue
197 *
198 */
199 public static function addOptionValue(&$params, &$groupParams, $action, $optionValueID) {
200 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
201 // checking if the group name with the given id or name (in $groupParams) exists
202 if (!empty($groupParams)) {
203 $config = CRM_Core_Config::singleton();
204 $groupParams['is_active'] = 1;
205 $optionGroup = CRM_Core_BAO_OptionGroup::retrieve($groupParams, $defaults);
206 }
207
208 // if the corresponding group doesn't exist, create one, provided $groupParams has 'name' in it.
209 if (!$optionGroup->id) {
210 if ($groupParams['name']) {
211 $newOptionGroup = CRM_Core_BAO_OptionGroup::add($groupParams, $defaults);
212 $params['weight'] = 1;
213 $optionGroupID = $newOptionGroup->id;
214 }
215 }
216 else {
217 $optionGroupID = $optionGroup->id;
218 $oldWeight = NULL;
219 if ($optionValueID) {
220 $oldWeight = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $optionValueID, 'weight', 'id');
221 }
222 $fieldValues = array('option_group_id' => $optionGroupID);
223 $params['weight'] = CRM_Utils_Weight::updateOtherWeights('CRM_Core_DAO_OptionValue', $oldWeight, CRM_Utils_Array::value('weight', $params), $fieldValues);
224 }
225 $params['option_group_id'] = $optionGroupID;
226
227 if (($action & CRM_Core_Action::ADD) && !isset($params['value'])) {
228 $fieldValues = array('option_group_id' => $optionGroupID);
229 // use the next available value
230 /* CONVERT(value, DECIMAL) is used to convert varchar
231 field 'value' to decimal->integer */
232
233 $params['value'] = (int) CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_OptionValue',
234 $fieldValues,
235 'CONVERT(value, DECIMAL)'
236 );
237 }
238 if (!$params['label'] && $params['name']) {
239 $params['label'] = $params['name'];
240 }
241
242 // set name to label if it's not set - but *only* for ADD action (CRM-3522)
243 if (($action & CRM_Core_Action::ADD) && empty($params['name']) && $params['label']) {
244 $params['name'] = $params['label'];
245 }
246 if ($action & CRM_Core_Action::UPDATE) {
247 $params['id'] = $optionValueID;
248 }
249 $optionValue = CRM_Core_BAO_OptionValue::add($params);
250 return $optionValue;
251 }
252
253 /**
254 * Check if there is a record with the same name in the db.
255 *
256 * @param string $value
257 * The value of the field we are checking.
258 * @param string $daoName
259 * The dao object name.
260 * @param string $daoID
261 * The id of the object being updated. u can change your name.
262 * as long as there is no conflict
263 * @param int $optionGroupID
264 * @param string $fieldName
265 * The name of the field in the DAO.
266 * @param bool $domainSpecific
267 * Filter this check to the current domain.
268 * Some optionGroups allow for same labels or same names but
269 * they must be in different domains, so filter the check to
270 * the current domain.
271 *
272 * @return bool
273 * true if object exists
274 */
275 public static function optionExists($value, $daoName, $daoID, $optionGroupID, $fieldName = 'name', $domainSpecific) {
276 $object = new $daoName();
277 $object->$fieldName = $value;
278 $object->option_group_id = $optionGroupID;
279
280 if ($domainSpecific) {
281 $object->domain_id = CRM_Core_Config::domainID();
282 }
283
284 if ($object->find(TRUE)) {
285 return ($daoID && $object->id == $daoID) ? TRUE : FALSE;
286 }
287 else {
288 return TRUE;
289 }
290 }
291
292 /**
293 * Check if there is a record with the same name in the db.
294 *
295 * @param string $mode
296 * @param string $contactType
297 *
298 * @return array
299 */
300 public static function getFields($mode = '', $contactType = 'Individual') {
301 $key = "$mode $contactType";
302 if (empty(self::$_fields[$key]) || !self::$_fields[$key]) {
303 self::$_fields[$key] = array();
304
305 $option = CRM_Core_DAO_OptionValue::import();
306
307 foreach (array_keys($option) as $id) {
308 $optionName = $option[$id];
309 }
310
311 $nameTitle = array();
312 if ($mode == 'contribute') {
313 // This is part of a move towards standardising option values but we
314 // should derive them from the fields array so am deprecating it again...
315 // note that the reason this was needed was that payment_instrument_id was
316 // not set to exportable.
317 $nameTitle = array(
318 'payment_instrument' => array(
319 'name' => 'payment_instrument',
320 'title' => ts('Payment Method'),
321 'headerPattern' => '/^payment|(p(ayment\s)?instrument)$/i',
322 ),
323 );
324 }
325 elseif ($mode == '') {
326 //the fields email greeting and postal greeting are meant only for Individual and Household
327 //the field addressee is meant for all contact types, CRM-4575
328 if (in_array($contactType, array(
329 'Individual',
330 'Household',
331 'Organization',
332 'All',
333 ))) {
334 $nameTitle = array(
335 'addressee' => array(
336 'name' => 'addressee',
337 'title' => ts('Addressee'),
338 'headerPattern' => '/^addressee$/i',
339 ),
340 );
341 $title = array(
342 'email_greeting' => array(
343 'name' => 'email_greeting',
344 'title' => ts('Email Greeting'),
345 'headerPattern' => '/^email_greeting$/i',
346 ),
347 'postal_greeting' => array(
348 'name' => 'postal_greeting',
349 'title' => ts('Postal Greeting'),
350 'headerPattern' => '/^postal_greeting$/i',
351 ),
352 );
353 $nameTitle = array_merge($nameTitle, $title);
354 }
355 }
356
357 if (is_array($nameTitle)) {
358 foreach ($nameTitle as $name => $attribs) {
359 self::$_fields[$key][$name] = $optionName;
360 list($tableName, $fieldName) = explode('.', $optionName['where']);
361 self::$_fields[$key][$name]['where'] = "{$name}.label";
362 foreach ($attribs as $k => $val) {
363 self::$_fields[$key][$name][$k] = $val;
364 }
365 }
366 }
367 }
368
369 return self::$_fields[$key];
370 }
371
372 /**
373 * Build select query in case of option-values
374 *
375 * @param $query
376 */
377 public static function select(&$query) {
378 if (!empty($query->_params) || !empty($query->_returnProperties)) {
379 $field = self::getFields();
380 foreach ($field as $name => $values) {
381 if (!empty($values['pseudoconstant'])) {
382 continue;
383 }
384 list($tableName, $fieldName) = explode('.', $values['where']);
385 if (!empty($query->_returnProperties[$name])) {
386 $query->_select["{$name}_id"] = "{$name}.value as {$name}_id";
387 $query->_element["{$name}_id"] = 1;
388 $query->_select[$name] = "{$name}.{$fieldName} as $name";
389 $query->_tables[$tableName] = 1;
390 $query->_element[$name] = 1;
391 }
392 }
393 }
394 }
395
396 /**
397 * Return option-values of a particular group
398 *
399 * @param array $groupParams
400 * Array containing group fields.
401 * whose option-values is to retrieved.
402 * @param array $values
403 * (reference) to the array which.
404 * will have the values for the group
405 * @param string $orderBy
406 * For orderBy clause.
407 *
408 * @param bool $isActive Do you want only active option values?
409 *
410 * @return array
411 * Array of option-values
412 *
413 */
414 public static function getValues($groupParams, &$values, $orderBy = 'weight', $isActive = FALSE) {
415 if (empty($groupParams)) {
416 return NULL;
417 }
418 $select = "
419 SELECT
420 option_value.id as id,
421 option_value.label as label,
422 option_value.value as value,
423 option_value.name as name,
424 option_value.description as description,
425 option_value.weight as weight,
426 option_value.is_active as is_active,
427 option_value.is_default as is_default";
428
429 $from = "
430 FROM
431 civicrm_option_value as option_value,
432 civicrm_option_group as option_group ";
433
434 $where = " WHERE option_group.id = option_value.option_group_id ";
435
436 if ($isActive) {
437 $where .= " AND option_value.is_active = " . $isActive;
438 }
439
440 $order = " ORDER BY " . $orderBy;
441
442 $groupId = CRM_Utils_Array::value('id', $groupParams);
443 $groupName = CRM_Utils_Array::value('name', $groupParams);
444
445 if ($groupId) {
446 $where .= " AND option_group.id = %1";
447 $params[1] = array($groupId, 'Integer');
448 if (!$groupName) {
449 $groupName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup',
450 $groupId, 'name', 'id'
451 );
452 }
453 }
454
455 if ($groupName) {
456 $where .= " AND option_group.name = %2";
457 $params[2] = array($groupName, 'String');
458 }
459
460 if (in_array($groupName, CRM_Core_OptionGroup::$_domainIDGroups)) {
461 $where .= " AND option_value.domain_id = " . CRM_Core_Config::domainID();
462 }
463
464 $query = $select . $from . $where . $order;
465
466 $dao = CRM_Core_DAO::executeQuery($query, $params);
467
468 while ($dao->fetch()) {
469 $values[$dao->id] = array(
470 'id' => $dao->id,
471 'label' => $dao->label,
472 'value' => $dao->value,
473 'name' => $dao->name,
474 'description' => $dao->description,
475 'weight' => $dao->weight,
476 'is_active' => $dao->is_active,
477 'is_default' => $dao->is_default,
478 );
479 }
480 }
481
482 }