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 input parameters
52 public 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 public 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 public 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 public 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
133 public static function retrieve(&$params, &$defaults) {
134 $optionValue = new CRM_Core_DAO_OptionValue();
135 $optionValue->copyValues($params);
136 if ($optionValue->find(TRUE)) {
137 CRM_Core_DAO
::storeValues($optionValue, $defaults);
144 * Update the is_active flag in the db
146 * @param int $id id of the database record
147 * @param boolean $is_active value we want to set the is_active field
149 * @return Object DAO object on sucess, null otherwise
152 public static function setIsActive($id, $is_active) {
153 return CRM_Core_DAO
::setFieldValue('CRM_Core_DAO_OptionValue', $id, 'is_active', $is_active);
157 * Add an Option Value
159 * @param array $params reference array contains the values submitted by the form
160 * @param array $ids reference array contains the id
164 * @return CRM_Core_DAO_OptionValue
166 public static function add(&$params, &$ids) {
167 // CRM-10921: do not reset attributes to default if this is an update
168 //@todo consider if defaults are being set in the right place. 'dumb' defaults like
169 // these would be usefully set @ the api layer so they are visible to api users
170 // complex defaults like the domain id below would make sense in the setDefauls function
171 // but unclear what other ways this function is being used
172 if (empty($ids['optionValue'])) {
173 $params['is_active'] = CRM_Utils_Array
::value('is_active', $params, FALSE);
174 $params['is_default'] = CRM_Utils_Array
::value('is_default', $params, FALSE);
175 $params['is_optgroup'] = CRM_Utils_Array
::value('is_optgroup', $params, FALSE);
176 $params['filter'] = CRM_Utils_Array
::value('filter', $params, FALSE);
179 // action is taken depending upon the mode
180 $optionValue = new CRM_Core_DAO_OptionValue();
181 $optionValue->copyValues($params);
183 if (!empty($params['is_default'])) {
184 $query = 'UPDATE civicrm_option_value SET is_default = 0 WHERE option_group_id = %1';
186 // tweak default reset, and allow multiple default within group.
187 if ($resetDefaultFor = CRM_Utils_Array
::value('reset_default_for', $params)) {
188 if (is_array($resetDefaultFor)) {
189 $colName = key($resetDefaultFor);
190 $colVal = $resetDefaultFor[$colName];
191 $query .= " AND ( $colName IN ( $colVal ) )";
195 $p = array(1 => array($params['option_group_id'], 'Integer'));
196 CRM_Core_DAO
::executeQuery($query, $p);
199 // CRM-13814 : evalute option group id
200 if (!array_key_exists('option_group_id', $params) && !empty($ids['optionValue'])) {
201 $groupId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionValue',
202 $ids['optionValue'], 'option_group_id', 'id'
206 $groupId = $params['option_group_id'];
209 $groupName = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup',
210 $groupId, 'name', 'id'
212 if (in_array($groupName, CRM_Core_OptionGroup
::$_domainIDGroups)) {
213 $optionValue->domain_id
= CRM_Utils_Array
::value('domain_id', $params, CRM_Core_Config
::domainID());
216 $optionValue->id
= CRM_Utils_Array
::value('optionValue', $ids);
217 $optionValue->save();
218 CRM_Core_PseudoConstant
::flush();
223 * Delete Option Value
225 * @param int $optionValueId
231 public static function del($optionValueId) {
232 $optionValue = new CRM_Core_DAO_OptionValue();
233 $optionValue->id
= $optionValueId;
234 if (self
::updateRecords($optionValueId, CRM_Core_Action
::DELETE
)) {
235 CRM_Core_PseudoConstant
::flush();
236 return $optionValue->delete();
242 * Retrieve activity type label and description
244 * @param int $activityTypeId activity type id
246 * @return array label and description
249 public static function getActivityTypeDetails($activityTypeId) {
250 $query = "SELECT civicrm_option_value.label, civicrm_option_value.description
251 FROM civicrm_option_value
252 LEFT JOIN civicrm_option_group ON ( civicrm_option_value.option_group_id = civicrm_option_group.id )
253 WHERE civicrm_option_group.name = 'activity_type'
254 AND civicrm_option_value.value = {$activityTypeId} ";
256 $dao = CRM_Core_DAO
::executeQuery($query);
260 return array($dao->label
, $dao->description
);
264 * Get the Option Value title.
266 * @param int $id id of Option Value
268 * @return string title
273 public static function getTitle($id) {
274 return CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionValue', $id, 'label');
278 * Updates contacts affected by the option value passed.
280 * @param Integer $optionValueId the option value id.
281 * @param int $action the action describing whether prefix/suffix was UPDATED or DELETED
285 public static function updateRecords(&$optionValueId, $action) {
287 $optionValue = new CRM_Core_DAO_OptionValue();
288 $optionValue->id
= $optionValueId;
289 $optionValue->find(TRUE);
291 $optionGroup = new CRM_Core_DAO_OptionGroup();
292 $optionGroup->id
= $optionValue->option_group_id
;
293 $optionGroup->find(TRUE);
296 $gName = $optionGroup->name
;
298 $value = $optionValue->value
;
300 // get the proper group name & affected field name
301 // todo: this may no longer be needed for individuals - check inputs
302 $individuals = array(
303 'gender' => 'gender_id',
304 'individual_prefix' => 'prefix_id',
305 'individual_suffix' => 'suffix_id',
306 'communication_style' => 'communication_style_id', // Not only Individuals -- but the code seems to be generic for all contact types, despite the naming...
308 $contributions = array('payment_instrument' => 'payment_instrument_id');
309 $activities = array('activity_type' => 'activity_type_id');
310 $participant = array('participant_role' => 'role_id');
311 $eventType = array('event_type' => 'event_type_id');
312 $aclRole = array('acl_role' => 'acl_role_id');
314 $all = array_merge($individuals, $contributions, $activities, $participant, $eventType, $aclRole);
317 foreach ($all as $name => $id) {
318 if ($gName == $name) {
322 if ($fieldName == '') {
326 if (array_key_exists($gName, $individuals)) {
327 $contactDAO = new CRM_Contact_DAO_Contact();
329 $contactDAO->$fieldName = $value;
332 while ($contactDAO->fetch()) {
333 if ($action == CRM_Core_Action
::DELETE
) {
334 $contact = new CRM_Contact_DAO_Contact();
335 $contact->id
= $contactDAO->id
;
336 $contact->find(TRUE);
338 // make sure dates doesn't get reset
339 $contact->birth_date
= CRM_Utils_Date
::isoToMysql($contact->birth_date
);
340 $contact->deceased_date
= CRM_Utils_Date
::isoToMysql($contact->deceased_date
);
341 $contact->$fieldName = 'NULL';
349 if (array_key_exists($gName, $contributions)) {
350 $contribution = new CRM_Contribute_DAO_Contribution();
351 $contribution->$fieldName = $value;
352 $contribution->find();
353 while ($contribution->fetch()) {
354 if ($action == CRM_Core_Action
::DELETE
) {
355 $contribution->$fieldName = 'NULL';
356 $contribution->save();
362 if (array_key_exists($gName, $activities)) {
363 $activity = new CRM_Activity_DAO_Activity();
364 $activity->$fieldName = $value;
366 while ($activity->fetch()) {
372 //delete participant role, type and event type option value
373 if (array_key_exists($gName, $participant)) {
374 $participantValue = new CRM_Event_DAO_Participant();
375 $participantValue->$fieldName = $value;
376 if ($participantValue->find(TRUE)) {
382 //delete event type option value
383 if (array_key_exists($gName, $eventType)) {
384 $event = new CRM_Event_DAO_Event();
385 $event->$fieldName = $value;
386 if ($event->find(TRUE)) {
392 //delete acl_role option value
393 if (array_key_exists($gName, $aclRole)) {
394 $entityRole = new CRM_ACL_DAO_EntityRole();
395 $entityRole->$fieldName = $value;
397 $aclDAO = new CRM_ACL_DAO_ACL();
398 $aclDAO->entity_id
= $value;
399 if ($entityRole->find(TRUE) ||
$aclDAO->find(TRUE)) {
407 * Updates options values weights.
409 * @param int $opGroupId
410 * @param array $opWeights options value , weight pair
415 public static function updateOptionWeights($opGroupId, $opWeights) {
416 if (!is_array($opWeights) ||
empty($opWeights)) {
420 foreach ($opWeights as $opValue => $opWeight) {
421 $optionValue = new CRM_Core_DAO_OptionValue();
422 $optionValue->option_group_id
= $opGroupId;
423 $optionValue->value
= $opValue;
424 if ($optionValue->find(TRUE)) {
425 $optionValue->weight
= $opWeight;
426 $optionValue->save();
428 $optionValue->free();
433 * Get the values of all option values given an option group ID. Store in system cache
434 * Does not take any filtering arguments. The object is to avoid hitting the DB and retrieve
437 * @param int $optionGroupID the option group for which we want the values from
439 * @return array an array of array of values for this option group
442 public static function getOptionValuesArray($optionGroupID) {
443 // check if we can get the field values from the system cache
444 $cacheKey = "CRM_Core_BAO_OptionValue_OptionGroupID_{$optionGroupID}";
445 $cache = CRM_Utils_Cache
::singleton();
446 $optionValues = $cache->get($cacheKey);
447 if (empty($optionValues)) {
448 $dao = new CRM_Core_DAO_OptionValue();
449 $dao->option_group_id
= $optionGroupID;
450 $dao->orderBy('weight ASC, label ASC');
453 $optionValues = array();
454 while ($dao->fetch()) {
455 $optionValues[$dao->id
] = array();
456 CRM_Core_DAO
::storeValues($dao, $optionValues[$dao->id
]);
459 $cache->set($cacheKey, $optionValues);
462 return $optionValues;
466 * Get the values of all option values given an option group ID as a key => value pair
467 * Use above cached function to make it super efficient
469 * @param int $optionGroupID the option group for which we want the values from
471 * @return array an associative array of label, value pairs
474 public static function getOptionValuesAssocArray($optionGroupID) {
475 $optionValues = self
::getOptionValuesArray($optionGroupID);
478 foreach ($optionValues as $id => $value) {
479 $options[$value['value']] = $value['label'];
484 * Get the values of all option values given an option group Name as a key => value pair
485 * Use above cached function to make it super efficient
487 * @param string $optionGroupName the option group name for which we want the values from
489 * @return array an associative array of label, value pairs
492 public static function getOptionValuesAssocArrayFromName($optionGroupName) {
493 $dao = new CRM_Core_DAO_OptionGroup();
494 $dao->name
= $optionGroupName;
496 $dao->selectAdd('id');
498 $optionValues = self
::getOptionValuesArray($dao->id
);
501 foreach ($optionValues as $id => $value) {
502 $options[$value['value']] = $value['label'];