INFRA-132 - Drupal.Classes.ClassDeclaration
[civicrm-core.git] / CRM / Core / BAO / 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 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35class CRM_Core_BAO_OptionValue extends CRM_Core_DAO_OptionValue {
36
37 /**
100fef9d 38 * Class constructor
6a488035 39 */
00be9182 40 public function __construct() {
6a488035
TO
41 parent::__construct();
42 }
77b97be7 43
9fe6051a 44 /**
e99c9805 45 * Create option value - note that the create function calls 'add' but
77b97be7
EM
46 * has more business logic
47 *
6a0b768e
TO
48 * @param array $params
49 * Input parameters.
77b97be7
EM
50 *
51 * @return object
52 */
00be9182 53 public static function create($params) {
9b873358 54 if (empty($params['id'])) {
c87bbced 55 self::setDefaults($params);
56 }
57 $ids = array();
a7488080 58 if (!empty($params['id'])) {
c87bbced 59 $ids = array('optionValue' => $params['id']);
60 }
d3e86119 61 return CRM_Core_BAO_OptionValue::add($params, $ids);
c87bbced 62 }
353ffa53 63
c87bbced 64 /**
65 * Set default Parameters
66 * This functions sets default parameters if not set:
67 * - name & label are set to each other as required (it might make more sense for one
68 * to be required but this would mean a change to the api level)
9fe6051a 69 * - weight & value will be set to their respective option groups next values
70 * if nothing is passed in.
71 *
72 * Note this function does not check for presence of $params['id'] so should only be called
73 * if 'id' is not present
c87bbced 74 *
9fe6051a 75 * @param array $params
c87bbced 76 */
9b873358
TO
77 public static function setDefaults(&$params) {
78 if (CRM_Utils_Array::value('label', $params, NULL) === NULL) {
c87bbced 79 $params['label'] = $params['name'];
80 }
9b873358 81 if (CRM_Utils_Array::value('name', $params, NULL) === NULL) {
c87bbced 82 $params['name'] = $params['label'];
83 }
9b873358 84 if (CRM_Utils_Array::value('weight', $params, NULL) === NULL) {
983e888c 85 $params['weight'] = self::getDefaultWeight($params);
c87bbced 86 }
9b873358 87 if (CRM_Utils_Array::value('value', $params, NULL) === NULL) {
1c6b7d30 88 $params['value'] = self::getDefaultValue($params);
c87bbced 89 }
90 }
77b97be7 91
9fe6051a 92 /**
93 * Get next available value
94 * We will take the highest numeric value (or 0 if no numeric values exist)
95 * and add one. The calling function is responsible for any
96 * more complex decision making
77b97be7 97 *
9fe6051a 98 * @param array $params
77b97be7
EM
99 *
100 * @return int
9fe6051a 101 */
9b873358 102 public static function getDefaultWeight($params) {
1c6b7d30 103 return (int) CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_OptionValue',
353ffa53 104 array('option_group_id' => $params['option_group_id']));
1c6b7d30 105 }
106
107 /**
108 * Get next available value
109 * We will take the highest numeric value (or 0 if no numeric values exist)
110 * and add one. The calling function is responsible for any
111 * more complex decision making
112 * @param array $params
113 */
9b873358 114 public static function getDefaultValue($params) {
353ffa53
TO
115 $bao = new CRM_Core_BAO_OptionValue();
116 $bao->option_group_id = $params['option_group_id'];
117 if (isset($params['domain_id'])) {
118 $bao->domain_id = $params['domain_id'];
119 }
120 $bao->selectAdd();
121 $bao->whereAdd("value REGEXP '^[0-9]+$'");
122 $bao->selectAdd('(ROUND(COALESCE(MAX(CONVERT(value, UNSIGNED)),0)) +1) as nextvalue');
123 $bao->find(TRUE);
124 return $bao->nextvalue;
9fe6051a 125 }
353ffa53 126
6a488035 127 /**
c490a46a 128 * Fetch object based on array of properties
6a488035 129 *
6a0b768e
TO
130 * @param array $params
131 * (reference ) an assoc array of name/value pairs.
132 * @param array $defaults
133 * (reference ) an assoc array to hold the flattened values.
6a488035 134 *
16b10e64 135 * @return CRM_Core_BAO_OptionValue
6a488035 136 */
00be9182 137 public static function retrieve(&$params, &$defaults) {
6a488035
TO
138 $optionValue = new CRM_Core_DAO_OptionValue();
139 $optionValue->copyValues($params);
140 if ($optionValue->find(TRUE)) {
141 CRM_Core_DAO::storeValues($optionValue, $defaults);
142 return $optionValue;
143 }
144 return NULL;
145 }
146
147 /**
100fef9d 148 * Update the is_active flag in the db
6a488035 149 *
6a0b768e
TO
150 * @param int $id
151 * Id of the database record.
152 * @param bool $is_active
153 * Value we want to set the is_active field.
6a488035 154 *
a6c01b45
CW
155 * @return Object
156 * DAO object on sucess, null otherwise
6a488035 157 */
00be9182 158 public static function setIsActive($id, $is_active) {
6a488035
TO
159 return CRM_Core_DAO::setFieldValue('CRM_Core_DAO_OptionValue', $id, 'is_active', $is_active);
160 }
161
162 /**
100fef9d 163 * Add an Option Value
6a488035 164 *
6a0b768e
TO
165 * @param array $params
166 * Reference array contains the values submitted by the form.
167 * @param array $ids
168 * Reference array contains the id.
6a488035 169 *
6a488035 170 *
7c285037 171 * @return CRM_Core_DAO_OptionValue
6a488035 172 */
00be9182 173 public static function add(&$params, &$ids) {
6a488035 174 // CRM-10921: do not reset attributes to default if this is an update
9fe6051a 175 //@todo consider if defaults are being set in the right place. 'dumb' defaults like
176 // these would be usefully set @ the api layer so they are visible to api users
177 // complex defaults like the domain id below would make sense in the setDefauls function
178 // but unclear what other ways this function is being used
a7488080 179 if (empty($ids['optionValue'])) {
6a488035
TO
180 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
181 $params['is_default'] = CRM_Utils_Array::value('is_default', $params, FALSE);
182 $params['is_optgroup'] = CRM_Utils_Array::value('is_optgroup', $params, FALSE);
183 $params['filter'] = CRM_Utils_Array::value('filter', $params, FALSE);
184 }
185
186 // action is taken depending upon the mode
187 $optionValue = new CRM_Core_DAO_OptionValue();
188 $optionValue->copyValues($params);
189
a7488080 190 if (!empty($params['is_default'])) {
6a488035
TO
191 $query = 'UPDATE civicrm_option_value SET is_default = 0 WHERE option_group_id = %1';
192
193 // tweak default reset, and allow multiple default within group.
194 if ($resetDefaultFor = CRM_Utils_Array::value('reset_default_for', $params)) {
195 if (is_array($resetDefaultFor)) {
196 $colName = key($resetDefaultFor);
197 $colVal = $resetDefaultFor[$colName];
198 $query .= " AND ( $colName IN ( $colVal ) )";
199 }
200 }
201
202 $p = array(1 => array($params['option_group_id'], 'Integer'));
203 CRM_Core_DAO::executeQuery($query, $p);
204 }
89ab5601
PJ
205
206 // CRM-13814 : evalute option group id
207 if (!array_key_exists('option_group_id', $params) && !empty($ids['optionValue'])) {
208 $groupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue',
209 $ids['optionValue'], 'option_group_id', 'id'
210 );
211 }
212 else {
213 $groupId = $params['option_group_id'];
214 }
215
6a488035 216 $groupName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup',
89ab5601 217 $groupId, 'name', 'id'
6a488035
TO
218 );
219 if (in_array($groupName, CRM_Core_OptionGroup::$_domainIDGroups)) {
220 $optionValue->domain_id = CRM_Utils_Array::value('domain_id', $params, CRM_Core_Config::domainID());
221 }
222
223 $optionValue->id = CRM_Utils_Array::value('optionValue', $ids);
224 $optionValue->save();
a4a33486 225 CRM_Core_PseudoConstant::flush();
6a488035
TO
226 return $optionValue;
227 }
228
229 /**
100fef9d 230 * Delete Option Value
6a488035 231 *
c490a46a 232 * @param int $optionValueId
6a488035
TO
233 *
234 * @return boolean
235 *
6a488035 236 */
00be9182 237 public static function del($optionValueId) {
6a488035
TO
238 $optionValue = new CRM_Core_DAO_OptionValue();
239 $optionValue->id = $optionValueId;
240 if (self::updateRecords($optionValueId, CRM_Core_Action::DELETE)) {
a4a33486 241 CRM_Core_PseudoConstant::flush();
6a488035
TO
242 return $optionValue->delete();
243 }
244 return FALSE;
245 }
246
247 /**
100fef9d 248 * Retrieve activity type label and description
6a488035 249 *
6a0b768e
TO
250 * @param int $activityTypeId
251 * Activity type id.
6a488035 252 *
a6c01b45
CW
253 * @return array
254 * label and description
6a488035 255 */
00be9182 256 public static function getActivityTypeDetails($activityTypeId) {
6a488035
TO
257 $query = "SELECT civicrm_option_value.label, civicrm_option_value.description
258 FROM civicrm_option_value
259 LEFT JOIN civicrm_option_group ON ( civicrm_option_value.option_group_id = civicrm_option_group.id )
260 WHERE civicrm_option_group.name = 'activity_type'
261 AND civicrm_option_value.value = {$activityTypeId} ";
262
263 $dao = CRM_Core_DAO::executeQuery($query);
264
265 $dao->fetch();
266
267 return array($dao->label, $dao->description);
268 }
269
270 /**
271 * Get the Option Value title.
272 *
6a0b768e
TO
273 * @param int $id
274 * Id of Option Value.
6a488035 275 *
a6c01b45
CW
276 * @return string
277 * title
6a488035 278 *
6a488035
TO
279 */
280 public static function getTitle($id) {
281 return CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $id, 'label');
282 }
283
284 /**
100fef9d 285 * Updates contacts affected by the option value passed.
6a488035 286 *
6a0b768e
TO
287 * @param int $optionValueId
288 * The option value id.
289 * @param int $action
290 * The action describing whether prefix/suffix was UPDATED or DELETED.
6a488035 291 *
7a6073fd 292 * @return bool
6a488035 293 */
00be9182 294 public static function updateRecords(&$optionValueId, $action) {
6a488035
TO
295 //finding group name
296 $optionValue = new CRM_Core_DAO_OptionValue();
297 $optionValue->id = $optionValueId;
298 $optionValue->find(TRUE);
299
300 $optionGroup = new CRM_Core_DAO_OptionGroup();
301 $optionGroup->id = $optionValue->option_group_id;
302 $optionGroup->find(TRUE);
303
304 // group name
305 $gName = $optionGroup->name;
306 // value
307 $value = $optionValue->value;
308
309 // get the proper group name & affected field name
67744c4e 310 // todo: this may no longer be needed for individuals - check inputs
6a488035
TO
311 $individuals = array(
312 'gender' => 'gender_id',
313 'individual_prefix' => 'prefix_id',
314 'individual_suffix' => 'suffix_id',
353ffa53
TO
315 'communication_style' => 'communication_style_id',
316 // Not only Individuals -- but the code seems to be generic for all contact types, despite the naming...
6a488035
TO
317 );
318 $contributions = array('payment_instrument' => 'payment_instrument_id');
353ffa53
TO
319 $activities = array('activity_type' => 'activity_type_id');
320 $participant = array('participant_role' => 'role_id');
321 $eventType = array('event_type' => 'event_type_id');
322 $aclRole = array('acl_role' => 'acl_role_id');
6a488035
TO
323
324 $all = array_merge($individuals, $contributions, $activities, $participant, $eventType, $aclRole);
325 $fieldName = '';
326
327 foreach ($all as $name => $id) {
328 if ($gName == $name) {
329 $fieldName = $id;
330 }
331 }
332 if ($fieldName == '') {
333 return TRUE;
334 }
335
336 if (array_key_exists($gName, $individuals)) {
337 $contactDAO = new CRM_Contact_DAO_Contact();
338
339 $contactDAO->$fieldName = $value;
340 $contactDAO->find();
341
342 while ($contactDAO->fetch()) {
343 if ($action == CRM_Core_Action::DELETE) {
344 $contact = new CRM_Contact_DAO_Contact();
345 $contact->id = $contactDAO->id;
346 $contact->find(TRUE);
347
348 // make sure dates doesn't get reset
349 $contact->birth_date = CRM_Utils_Date::isoToMysql($contact->birth_date);
350 $contact->deceased_date = CRM_Utils_Date::isoToMysql($contact->deceased_date);
351 $contact->$fieldName = 'NULL';
352 $contact->save();
353 }
354 }
355
356 return TRUE;
357 }
358
359 if (array_key_exists($gName, $contributions)) {
360 $contribution = new CRM_Contribute_DAO_Contribution();
361 $contribution->$fieldName = $value;
362 $contribution->find();
363 while ($contribution->fetch()) {
364 if ($action == CRM_Core_Action::DELETE) {
365 $contribution->$fieldName = 'NULL';
366 $contribution->save();
367 }
368 }
369 return TRUE;
370 }
371
372 if (array_key_exists($gName, $activities)) {
373 $activity = new CRM_Activity_DAO_Activity();
374 $activity->$fieldName = $value;
375 $activity->find();
376 while ($activity->fetch()) {
377 $activity->delete();
378 }
379 return TRUE;
380 }
381
382 //delete participant role, type and event type option value
383 if (array_key_exists($gName, $participant)) {
384 $participantValue = new CRM_Event_DAO_Participant();
385 $participantValue->$fieldName = $value;
386 if ($participantValue->find(TRUE)) {
387 return FALSE;
388 }
389 return TRUE;
390 }
391
392 //delete event type option value
393 if (array_key_exists($gName, $eventType)) {
394 $event = new CRM_Event_DAO_Event();
395 $event->$fieldName = $value;
396 if ($event->find(TRUE)) {
397 return FALSE;
398 }
399 return TRUE;
400 }
401
402 //delete acl_role option value
403 if (array_key_exists($gName, $aclRole)) {
404 $entityRole = new CRM_ACL_DAO_EntityRole();
405 $entityRole->$fieldName = $value;
406
407 $aclDAO = new CRM_ACL_DAO_ACL();
408 $aclDAO->entity_id = $value;
409 if ($entityRole->find(TRUE) || $aclDAO->find(TRUE)) {
410 return FALSE;
411 }
412 return TRUE;
413 }
414 }
415
416 /**
100fef9d 417 * Updates options values weights.
6a488035 418 *
c490a46a 419 * @param int $opGroupId
6a0b768e
TO
420 * @param array $opWeights
421 * Options value , weight pair.
6a488035
TO
422 *
423 * @return void
6a488035 424 */
00be9182 425 public static function updateOptionWeights($opGroupId, $opWeights) {
6a488035
TO
426 if (!is_array($opWeights) || empty($opWeights)) {
427 return;
428 }
429
430 foreach ($opWeights as $opValue => $opWeight) {
431 $optionValue = new CRM_Core_DAO_OptionValue();
432 $optionValue->option_group_id = $opGroupId;
433 $optionValue->value = $opValue;
434 if ($optionValue->find(TRUE)) {
435 $optionValue->weight = $opWeight;
436 $optionValue->save();
437 }
438 $optionValue->free();
439 }
440 }
441
442 /**
443 * Get the values of all option values given an option group ID. Store in system cache
444 * Does not take any filtering arguments. The object is to avoid hitting the DB and retrieve
445 * from memory
446 *
6a0b768e
TO
447 * @param int $optionGroupID
448 * The option group for which we want the values from.
6a488035 449 *
a6c01b45
CW
450 * @return array
451 * an array of array of values for this option group
6a488035 452 */
00be9182 453 public static function getOptionValuesArray($optionGroupID) {
6a488035 454 // check if we can get the field values from the system cache
353ffa53
TO
455 $cacheKey = "CRM_Core_BAO_OptionValue_OptionGroupID_{$optionGroupID}";
456 $cache = CRM_Utils_Cache::singleton();
6a488035
TO
457 $optionValues = $cache->get($cacheKey);
458 if (empty($optionValues)) {
459 $dao = new CRM_Core_DAO_OptionValue();
460 $dao->option_group_id = $optionGroupID;
461 $dao->orderBy('weight ASC, label ASC');
462 $dao->find();
463
464 $optionValues = array();
465 while ($dao->fetch()) {
466 $optionValues[$dao->id] = array();
467 CRM_Core_DAO::storeValues($dao, $optionValues[$dao->id]);
468 }
469
470 $cache->set($cacheKey, $optionValues);
471 }
472
473 return $optionValues;
474 }
475
476 /**
477 * Get the values of all option values given an option group ID as a key => value pair
478 * Use above cached function to make it super efficient
479 *
6a0b768e
TO
480 * @param int $optionGroupID
481 * The option group for which we want the values from.
6a488035 482 *
a6c01b45
CW
483 * @return array
484 * an associative array of label, value pairs
6a488035 485 */
00be9182 486 public static function getOptionValuesAssocArray($optionGroupID) {
6a488035
TO
487 $optionValues = self::getOptionValuesArray($optionGroupID);
488
489 $options = array();
490 foreach ($optionValues as $id => $value) {
491 $options[$value['value']] = $value['label'];
492 }
493 return $options;
494 }
353ffa53 495
6a488035
TO
496 /**
497 * Get the values of all option values given an option group Name as a key => value pair
498 * Use above cached function to make it super efficient
499 *
6a0b768e
TO
500 * @param string $optionGroupName
501 * The option group name for which we want the values from.
6a488035 502 *
a6c01b45
CW
503 * @return array
504 * an associative array of label, value pairs
6a488035 505 */
00be9182 506 public static function getOptionValuesAssocArrayFromName($optionGroupName) {
6a488035
TO
507 $dao = new CRM_Core_DAO_OptionGroup();
508 $dao->name = $optionGroupName;
509 $dao->selectAdd();
510 $dao->selectAdd('id');
511 $dao->find(TRUE);
512 $optionValues = self::getOptionValuesArray($dao->id);
513
514 $options = array();
515 foreach ($optionValues as $id => $value) {
516 $options[$value['value']] = $value['label'];
517 }
518 return $options;
519 }
520
521}