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