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