3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
35 class CRM_Core_BAO_OptionValue
extends CRM_Core_DAO_OptionValue
{
40 public function __construct() {
41 parent
::__construct();
45 * Create option value - note that the create function calls 'add' but
46 * has more business logic
48 * @param array $params
53 public static function create($params) {
54 if (empty($params['id'])) {
55 self
::setDefaults($params);
58 if (!empty($params['id'])) {
59 $ids = array('optionValue' => $params['id']);
61 return CRM_Core_BAO_OptionValue
::add($params, $ids);
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)
69 * - weight & value will be set to their respective option groups next values
70 * if nothing is passed in.
72 * Note this function does not check for presence of $params['id'] so should only be called
73 * if 'id' is not present
75 * @param array $params
77 public static function setDefaults(&$params) {
78 if (CRM_Utils_Array
::value('label', $params, NULL) === NULL) {
79 $params['label'] = $params['name'];
81 if (CRM_Utils_Array
::value('name', $params, NULL) === NULL) {
82 $params['name'] = $params['label'];
84 if (CRM_Utils_Array
::value('weight', $params, NULL) === NULL) {
85 $params['weight'] = self
::getDefaultWeight($params);
87 if (CRM_Utils_Array
::value('value', $params, NULL) === NULL) {
88 $params['value'] = self
::getDefaultValue($params);
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
98 * @param array $params
102 public static function getDefaultWeight($params) {
103 return (int) CRM_Utils_Weight
::getDefaultWeight('CRM_Core_DAO_OptionValue',
104 array('option_group_id' => $params['option_group_id']));
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
114 public static function getDefaultValue($params) {
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'];
121 $bao->whereAdd("value REGEXP '^[0-9]+$'");
122 $bao->selectAdd('(ROUND(COALESCE(MAX(CONVERT(value, UNSIGNED)),0)) +1) as nextvalue');
124 return $bao->nextvalue
;
128 * Fetch object based on array of properties
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.
135 * @return CRM_Core_BAO_OptionValue
138 public static function retrieve(&$params, &$defaults) {
139 $optionValue = new CRM_Core_DAO_OptionValue();
140 $optionValue->copyValues($params);
141 if ($optionValue->find(TRUE)) {
142 CRM_Core_DAO
::storeValues($optionValue, $defaults);
149 * Update the is_active flag in the db
152 * Id of the database record.
153 * @param bool $is_active
154 * Value we want to set the is_active field.
157 * DAO object on sucess, null otherwise
160 public static function setIsActive($id, $is_active) {
161 return CRM_Core_DAO
::setFieldValue('CRM_Core_DAO_OptionValue', $id, 'is_active', $is_active);
165 * Add an Option Value
167 * @param array $params
168 * Reference array contains the values submitted by the form.
170 * Reference array contains the id.
174 * @return CRM_Core_DAO_OptionValue
176 public static function add(&$params, &$ids) {
177 // CRM-10921: do not reset attributes to default if this is an update
178 //@todo consider if defaults are being set in the right place. 'dumb' defaults like
179 // these would be usefully set @ the api layer so they are visible to api users
180 // complex defaults like the domain id below would make sense in the setDefauls function
181 // but unclear what other ways this function is being used
182 if (empty($ids['optionValue'])) {
183 $params['is_active'] = CRM_Utils_Array
::value('is_active', $params, FALSE);
184 $params['is_default'] = CRM_Utils_Array
::value('is_default', $params, FALSE);
185 $params['is_optgroup'] = CRM_Utils_Array
::value('is_optgroup', $params, FALSE);
186 $params['filter'] = CRM_Utils_Array
::value('filter', $params, FALSE);
189 // action is taken depending upon the mode
190 $optionValue = new CRM_Core_DAO_OptionValue();
191 $optionValue->copyValues($params);
193 if (!empty($params['is_default'])) {
194 $query = 'UPDATE civicrm_option_value SET is_default = 0 WHERE option_group_id = %1';
196 // tweak default reset, and allow multiple default within group.
197 if ($resetDefaultFor = CRM_Utils_Array
::value('reset_default_for', $params)) {
198 if (is_array($resetDefaultFor)) {
199 $colName = key($resetDefaultFor);
200 $colVal = $resetDefaultFor[$colName];
201 $query .= " AND ( $colName IN ( $colVal ) )";
205 $p = array(1 => array($params['option_group_id'], 'Integer'));
206 CRM_Core_DAO
::executeQuery($query, $p);
209 // CRM-13814 : evalute option group id
210 if (!array_key_exists('option_group_id', $params) && !empty($ids['optionValue'])) {
211 $groupId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionValue',
212 $ids['optionValue'], 'option_group_id', 'id'
216 $groupId = $params['option_group_id'];
219 $groupName = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup',
220 $groupId, 'name', 'id'
222 if (in_array($groupName, CRM_Core_OptionGroup
::$_domainIDGroups)) {
223 $optionValue->domain_id
= CRM_Utils_Array
::value('domain_id', $params, CRM_Core_Config
::domainID());
226 $optionValue->id
= CRM_Utils_Array
::value('optionValue', $ids);
227 $optionValue->save();
228 CRM_Core_PseudoConstant
::flush();
233 * Delete Option Value
235 * @param int $optionValueId
241 public static function del($optionValueId) {
242 $optionValue = new CRM_Core_DAO_OptionValue();
243 $optionValue->id
= $optionValueId;
244 if (self
::updateRecords($optionValueId, CRM_Core_Action
::DELETE
)) {
245 CRM_Core_PseudoConstant
::flush();
246 return $optionValue->delete();
252 * Retrieve activity type label and description
254 * @param int $activityTypeId
258 * label and description
261 public static function getActivityTypeDetails($activityTypeId) {
262 $query = "SELECT civicrm_option_value.label, civicrm_option_value.description
263 FROM civicrm_option_value
264 LEFT JOIN civicrm_option_group ON ( civicrm_option_value.option_group_id = civicrm_option_group.id )
265 WHERE civicrm_option_group.name = 'activity_type'
266 AND civicrm_option_value.value = {$activityTypeId} ";
268 $dao = CRM_Core_DAO
::executeQuery($query);
272 return array($dao->label
, $dao->description
);
276 * Get the Option Value title.
279 * Id of Option Value.
286 public static function getTitle($id) {
287 return CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionValue', $id, 'label');
291 * Updates contacts affected by the option value passed.
293 * @param int $optionValueId
294 * The option value id.
296 * The action describing whether prefix/suffix was UPDATED or DELETED.
300 public static function updateRecords(&$optionValueId, $action) {
302 $optionValue = new CRM_Core_DAO_OptionValue();
303 $optionValue->id
= $optionValueId;
304 $optionValue->find(TRUE);
306 $optionGroup = new CRM_Core_DAO_OptionGroup();
307 $optionGroup->id
= $optionValue->option_group_id
;
308 $optionGroup->find(TRUE);
311 $gName = $optionGroup->name
;
313 $value = $optionValue->value
;
315 // get the proper group name & affected field name
316 // todo: this may no longer be needed for individuals - check inputs
317 $individuals = array(
318 'gender' => 'gender_id',
319 'individual_prefix' => 'prefix_id',
320 'individual_suffix' => 'suffix_id',
321 'communication_style' => 'communication_style_id',
322 // Not only Individuals -- but the code seems to be generic for all contact types, despite the naming...
324 $contributions = array('payment_instrument' => 'payment_instrument_id');
325 $activities = array('activity_type' => 'activity_type_id');
326 $participant = array('participant_role' => 'role_id');
327 $eventType = array('event_type' => 'event_type_id');
328 $aclRole = array('acl_role' => 'acl_role_id');
330 $all = array_merge($individuals, $contributions, $activities, $participant, $eventType, $aclRole);
333 foreach ($all as $name => $id) {
334 if ($gName == $name) {
338 if ($fieldName == '') {
342 if (array_key_exists($gName, $individuals)) {
343 $contactDAO = new CRM_Contact_DAO_Contact();
345 $contactDAO->$fieldName = $value;
348 while ($contactDAO->fetch()) {
349 if ($action == CRM_Core_Action
::DELETE
) {
350 $contact = new CRM_Contact_DAO_Contact();
351 $contact->id
= $contactDAO->id
;
352 $contact->find(TRUE);
354 // make sure dates doesn't get reset
355 $contact->birth_date
= CRM_Utils_Date
::isoToMysql($contact->birth_date
);
356 $contact->deceased_date
= CRM_Utils_Date
::isoToMysql($contact->deceased_date
);
357 $contact->$fieldName = 'NULL';
365 if (array_key_exists($gName, $contributions)) {
366 $contribution = new CRM_Contribute_DAO_Contribution();
367 $contribution->$fieldName = $value;
368 $contribution->find();
369 while ($contribution->fetch()) {
370 if ($action == CRM_Core_Action
::DELETE
) {
371 $contribution->$fieldName = 'NULL';
372 $contribution->save();
378 if (array_key_exists($gName, $activities)) {
379 $activity = new CRM_Activity_DAO_Activity();
380 $activity->$fieldName = $value;
382 while ($activity->fetch()) {
388 //delete participant role, type and event type option value
389 if (array_key_exists($gName, $participant)) {
390 $participantValue = new CRM_Event_DAO_Participant();
391 $participantValue->$fieldName = $value;
392 if ($participantValue->find(TRUE)) {
398 //delete event type option value
399 if (array_key_exists($gName, $eventType)) {
400 $event = new CRM_Event_DAO_Event();
401 $event->$fieldName = $value;
402 if ($event->find(TRUE)) {
408 //delete acl_role option value
409 if (array_key_exists($gName, $aclRole)) {
410 $entityRole = new CRM_ACL_DAO_EntityRole();
411 $entityRole->$fieldName = $value;
413 $aclDAO = new CRM_ACL_DAO_ACL();
414 $aclDAO->entity_id
= $value;
415 if ($entityRole->find(TRUE) ||
$aclDAO->find(TRUE)) {
423 * Updates options values weights.
425 * @param int $opGroupId
426 * @param array $opWeights
427 * Options value , weight pair.
432 public static function updateOptionWeights($opGroupId, $opWeights) {
433 if (!is_array($opWeights) ||
empty($opWeights)) {
437 foreach ($opWeights as $opValue => $opWeight) {
438 $optionValue = new CRM_Core_DAO_OptionValue();
439 $optionValue->option_group_id
= $opGroupId;
440 $optionValue->value
= $opValue;
441 if ($optionValue->find(TRUE)) {
442 $optionValue->weight
= $opWeight;
443 $optionValue->save();
445 $optionValue->free();
450 * Get the values of all option values given an option group ID. Store in system cache
451 * Does not take any filtering arguments. The object is to avoid hitting the DB and retrieve
454 * @param int $optionGroupID
455 * The option group for which we want the values from.
458 * an array of array of values for this option group
461 public static function getOptionValuesArray($optionGroupID) {
462 // check if we can get the field values from the system cache
463 $cacheKey = "CRM_Core_BAO_OptionValue_OptionGroupID_{$optionGroupID}";
464 $cache = CRM_Utils_Cache
::singleton();
465 $optionValues = $cache->get($cacheKey);
466 if (empty($optionValues)) {
467 $dao = new CRM_Core_DAO_OptionValue();
468 $dao->option_group_id
= $optionGroupID;
469 $dao->orderBy('weight ASC, label ASC');
472 $optionValues = array();
473 while ($dao->fetch()) {
474 $optionValues[$dao->id
] = array();
475 CRM_Core_DAO
::storeValues($dao, $optionValues[$dao->id
]);
478 $cache->set($cacheKey, $optionValues);
481 return $optionValues;
485 * Get the values of all option values given an option group ID as a key => value pair
486 * Use above cached function to make it super efficient
488 * @param int $optionGroupID
489 * The option group for which we want the values from.
492 * an associative array of label, value pairs
495 public static function getOptionValuesAssocArray($optionGroupID) {
496 $optionValues = self
::getOptionValuesArray($optionGroupID);
499 foreach ($optionValues as $id => $value) {
500 $options[$value['value']] = $value['label'];
506 * Get the values of all option values given an option group Name as a key => value pair
507 * Use above cached function to make it super efficient
509 * @param string $optionGroupName
510 * The option group name for which we want the values from.
513 * an associative array of label, value pairs
516 public static function getOptionValuesAssocArrayFromName($optionGroupName) {
517 $dao = new CRM_Core_DAO_OptionGroup();
518 $dao->name
= $optionGroupName;
520 $dao->selectAdd('id');
522 $optionValues = self
::getOptionValuesArray($dao->id
);
525 foreach ($optionValues as $id => $value) {
526 $options[$value['value']] = $value['label'];