3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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 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 input parameters
52 static function create($params) {
53 if (empty($params['id'])){
54 self
::setDefaults($params);
57 if (!empty($params['id'])) {
58 $ids = array('optionValue' => $params['id']);
60 return CRM_Core_BAO_OptionValue
::add($params, $ids);
63 * Set default Parameters
64 * This functions sets default parameters if not set:
65 * - name & label are set to each other as required (it might make more sense for one
66 * to be required but this would mean a change to the api level)
67 * - weight & value will be set to their respective option groups next values
68 * if nothing is passed in.
70 * Note this function does not check for presence of $params['id'] so should only be called
71 * if 'id' is not present
73 * @param array $params
75 static function setDefaults(&$params){
76 if(CRM_Utils_Array
::value('label', $params, NULL) === NULL){
77 $params['label'] = $params['name'];
79 if(CRM_Utils_Array
::value('name', $params, NULL) === NULL){
80 $params['name'] = $params['label'];
82 if(CRM_Utils_Array
::value('weight', $params, NULL) === NULL){
83 $params['weight'] = self
::getDefaultWeight($params);
85 if (CRM_Utils_Array
::value('value', $params, NULL) === NULL){
86 $params['value'] = self
::getDefaultValue($params);
91 * Get next available value
92 * We will take the highest numeric value (or 0 if no numeric values exist)
93 * and add one. The calling function is responsible for any
94 * more complex decision making
96 * @param array $params
100 static function getDefaultWeight($params){
101 return (int) CRM_Utils_Weight
::getDefaultWeight('CRM_Core_DAO_OptionValue',
102 array('option_group_id' => $params['option_group_id']));
106 * Get next available value
107 * We will take the highest numeric value (or 0 if no numeric values exist)
108 * and add one. The calling function is responsible for any
109 * more complex decision making
110 * @param array $params
112 static function getDefaultValue($params){
113 $bao = new CRM_Core_BAO_OptionValue();
114 $bao->option_group_id
= $params['option_group_id'];
115 if(isset($params['domain_id'])){
116 $bao->domain_id
= $params['domain_id'];
119 $bao->whereAdd("value REGEXP '^[0-9]+$'");
120 $bao->selectAdd('(ROUND(COALESCE(MAX(CONVERT(value, UNSIGNED)),0)) +1) as nextvalue');
122 return $bao->nextvalue
;
125 * Fetch object based on array of properties
127 * @param array $params (reference ) an assoc array of name/value pairs
128 * @param array $defaults (reference ) an assoc array to hold the flattened values
130 * @return CRM_Core_BAO_OptionValue object
134 static function retrieve(&$params, &$defaults) {
135 $optionValue = new CRM_Core_DAO_OptionValue();
136 $optionValue->copyValues($params);
137 if ($optionValue->find(TRUE)) {
138 CRM_Core_DAO
::storeValues($optionValue, $defaults);
145 * Update the is_active flag in the db
147 * @param int $id id of the database record
148 * @param boolean $is_active value we want to set the is_active field
150 * @return Object DAO object on sucess, null otherwise
153 static function setIsActive($id, $is_active) {
154 return CRM_Core_DAO
::setFieldValue('CRM_Core_DAO_OptionValue', $id, 'is_active', $is_active);
158 * Add an Option Value
160 * @param array $params reference array contains the values submitted by the form
161 * @param array $ids reference array contains the id
166 * @return CRM_Core_DAO_OptionValue
168 static function add(&$params, &$ids) {
169 // CRM-10921: do not reset attributes to default if this is an update
170 //@todo consider if defaults are being set in the right place. 'dumb' defaults like
171 // these would be usefully set @ the api layer so they are visible to api users
172 // complex defaults like the domain id below would make sense in the setDefauls function
173 // but unclear what other ways this function is being used
174 if (empty($ids['optionValue'])) {
175 $params['is_active'] = CRM_Utils_Array
::value('is_active', $params, FALSE);
176 $params['is_default'] = CRM_Utils_Array
::value('is_default', $params, FALSE);
177 $params['is_optgroup'] = CRM_Utils_Array
::value('is_optgroup', $params, FALSE);
178 $params['filter'] = CRM_Utils_Array
::value('filter', $params, FALSE);
181 // action is taken depending upon the mode
182 $optionValue = new CRM_Core_DAO_OptionValue();
183 $optionValue->copyValues($params);
185 if (!empty($params['is_default'])) {
186 $query = 'UPDATE civicrm_option_value SET is_default = 0 WHERE option_group_id = %1';
188 // tweak default reset, and allow multiple default within group.
189 if ($resetDefaultFor = CRM_Utils_Array
::value('reset_default_for', $params)) {
190 if (is_array($resetDefaultFor)) {
191 $colName = key($resetDefaultFor);
192 $colVal = $resetDefaultFor[$colName];
193 $query .= " AND ( $colName IN ( $colVal ) )";
197 $p = array(1 => array($params['option_group_id'], 'Integer'));
198 CRM_Core_DAO
::executeQuery($query, $p);
201 // CRM-13814 : evalute option group id
202 if (!array_key_exists('option_group_id', $params) && !empty($ids['optionValue'])) {
203 $groupId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionValue',
204 $ids['optionValue'], 'option_group_id', 'id'
208 $groupId = $params['option_group_id'];
211 $groupName = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup',
212 $groupId, 'name', 'id'
214 if (in_array($groupName, CRM_Core_OptionGroup
::$_domainIDGroups)) {
215 $optionValue->domain_id
= CRM_Utils_Array
::value('domain_id', $params, CRM_Core_Config
::domainID());
218 $optionValue->id
= CRM_Utils_Array
::value('optionValue', $ids);
219 $optionValue->save();
220 CRM_Core_PseudoConstant
::flush();
225 * Delete Option Value
227 * @param int $optionValueId
234 static function del($optionValueId) {
235 $optionValue = new CRM_Core_DAO_OptionValue();
236 $optionValue->id
= $optionValueId;
237 if (self
::updateRecords($optionValueId, CRM_Core_Action
::DELETE
)) {
238 CRM_Core_PseudoConstant
::flush();
239 return $optionValue->delete();
245 * Retrieve activity type label and description
247 * @param int $activityTypeId activity type id
249 * @return array label and description
253 static function getActivityTypeDetails($activityTypeId) {
254 $query = "SELECT civicrm_option_value.label, civicrm_option_value.description
255 FROM civicrm_option_value
256 LEFT JOIN civicrm_option_group ON ( civicrm_option_value.option_group_id = civicrm_option_group.id )
257 WHERE civicrm_option_group.name = 'activity_type'
258 AND civicrm_option_value.value = {$activityTypeId} ";
260 $dao = CRM_Core_DAO
::executeQuery($query);
264 return array($dao->label
, $dao->description
);
268 * Get the Option Value title.
270 * @param int $id id of Option Value
272 * @return string title
278 public static function getTitle($id) {
279 return CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionValue', $id, 'label');
283 * Updates contacts affected by the option value passed.
285 * @param Integer $optionValueId the option value id.
286 * @param int $action the action describing whether prefix/suffix was UPDATED or DELETED
290 static function updateRecords(&$optionValueId, $action) {
292 $optionValue = new CRM_Core_DAO_OptionValue();
293 $optionValue->id
= $optionValueId;
294 $optionValue->find(TRUE);
296 $optionGroup = new CRM_Core_DAO_OptionGroup();
297 $optionGroup->id
= $optionValue->option_group_id
;
298 $optionGroup->find(TRUE);
301 $gName = $optionGroup->name
;
303 $value = $optionValue->value
;
305 // get the proper group name & affected field name
306 // todo: this may no longer be needed for individuals - check inputs
307 $individuals = array(
308 'gender' => 'gender_id',
309 'individual_prefix' => 'prefix_id',
310 'individual_suffix' => 'suffix_id',
311 'communication_style' => 'communication_style_id', // Not only Individuals -- but the code seems to be generic for all contact types, despite the naming...
313 $contributions = array('payment_instrument' => 'payment_instrument_id');
314 $activities = array('activity_type' => 'activity_type_id');
315 $participant = array('participant_role' => 'role_id');
316 $eventType = array('event_type' => 'event_type_id');
317 $aclRole = array('acl_role' => 'acl_role_id');
319 $all = array_merge($individuals, $contributions, $activities, $participant, $eventType, $aclRole);
322 foreach ($all as $name => $id) {
323 if ($gName == $name) {
327 if ($fieldName == '') {
331 if (array_key_exists($gName, $individuals)) {
332 $contactDAO = new CRM_Contact_DAO_Contact();
334 $contactDAO->$fieldName = $value;
337 while ($contactDAO->fetch()) {
338 if ($action == CRM_Core_Action
::DELETE
) {
339 $contact = new CRM_Contact_DAO_Contact();
340 $contact->id
= $contactDAO->id
;
341 $contact->find(TRUE);
343 // make sure dates doesn't get reset
344 $contact->birth_date
= CRM_Utils_Date
::isoToMysql($contact->birth_date
);
345 $contact->deceased_date
= CRM_Utils_Date
::isoToMysql($contact->deceased_date
);
346 $contact->$fieldName = 'NULL';
354 if (array_key_exists($gName, $contributions)) {
355 $contribution = new CRM_Contribute_DAO_Contribution();
356 $contribution->$fieldName = $value;
357 $contribution->find();
358 while ($contribution->fetch()) {
359 if ($action == CRM_Core_Action
::DELETE
) {
360 $contribution->$fieldName = 'NULL';
361 $contribution->save();
367 if (array_key_exists($gName, $activities)) {
368 $activity = new CRM_Activity_DAO_Activity();
369 $activity->$fieldName = $value;
371 while ($activity->fetch()) {
377 //delete participant role, type and event type option value
378 if (array_key_exists($gName, $participant)) {
379 $participantValue = new CRM_Event_DAO_Participant();
380 $participantValue->$fieldName = $value;
381 if ($participantValue->find(TRUE)) {
387 //delete event type option value
388 if (array_key_exists($gName, $eventType)) {
389 $event = new CRM_Event_DAO_Event();
390 $event->$fieldName = $value;
391 if ($event->find(TRUE)) {
397 //delete acl_role option value
398 if (array_key_exists($gName, $aclRole)) {
399 $entityRole = new CRM_ACL_DAO_EntityRole();
400 $entityRole->$fieldName = $value;
402 $aclDAO = new CRM_ACL_DAO_ACL();
403 $aclDAO->entity_id
= $value;
404 if ($entityRole->find(TRUE) ||
$aclDAO->find(TRUE)) {
412 * Updates options values weights.
414 * @param int $opGroupId
415 * @param array $opWeights options value , weight pair
421 static function updateOptionWeights($opGroupId, $opWeights) {
422 if (!is_array($opWeights) ||
empty($opWeights)) {
426 foreach ($opWeights as $opValue => $opWeight) {
427 $optionValue = new CRM_Core_DAO_OptionValue();
428 $optionValue->option_group_id
= $opGroupId;
429 $optionValue->value
= $opValue;
430 if ($optionValue->find(TRUE)) {
431 $optionValue->weight
= $opWeight;
432 $optionValue->save();
434 $optionValue->free();
439 * Get the values of all option values given an option group ID. Store in system cache
440 * Does not take any filtering arguments. The object is to avoid hitting the DB and retrieve
443 * @param int $optionGroupID the option group for which we want the values from
445 * @return array an array of array of values for this option group
449 static function getOptionValuesArray($optionGroupID) {
450 // check if we can get the field values from the system cache
451 $cacheKey = "CRM_Core_BAO_OptionValue_OptionGroupID_{$optionGroupID}";
452 $cache = CRM_Utils_Cache
::singleton();
453 $optionValues = $cache->get($cacheKey);
454 if (empty($optionValues)) {
455 $dao = new CRM_Core_DAO_OptionValue();
456 $dao->option_group_id
= $optionGroupID;
457 $dao->orderBy('weight ASC, label ASC');
460 $optionValues = array();
461 while ($dao->fetch()) {
462 $optionValues[$dao->id
] = array();
463 CRM_Core_DAO
::storeValues($dao, $optionValues[$dao->id
]);
466 $cache->set($cacheKey, $optionValues);
469 return $optionValues;
473 * Get the values of all option values given an option group ID as a key => value pair
474 * Use above cached function to make it super efficient
476 * @param int $optionGroupID the option group for which we want the values from
478 * @return array an associative array of label, value pairs
482 static function getOptionValuesAssocArray($optionGroupID) {
483 $optionValues = self
::getOptionValuesArray($optionGroupID);
486 foreach ($optionValues as $id => $value) {
487 $options[$value['value']] = $value['label'];
492 * Get the values of all option values given an option group Name as a key => value pair
493 * Use above cached function to make it super efficient
495 * @param string $optionGroupName the option group name for which we want the values from
497 * @return array an associative array of label, value pairs
501 static function getOptionValuesAssocArrayFromName($optionGroupName) {
502 $dao = new CRM_Core_DAO_OptionGroup();
503 $dao->name
= $optionGroupName;
505 $dao->selectAdd('id');
507 $optionValues = self
::getOptionValuesArray($dao->id
);
510 foreach ($optionValues as $id => $value) {
511 $options[$value['value']] = $value['label'];