3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
35 class CRM_Core_BAO_OptionValue
extends CRM_Core_DAO_OptionValue
{
40 function __construct() {
41 parent
::__construct();
45 * Takes a bunch of params that are needed to match certain criteria and
46 * retrieves the relevant objects. Typically the valid params are only
47 * contact_id. We'll tweak this function to be more full featured over a period
48 * of time. This is the inverse function of create. It also stores all the retrieved
49 * values in the default array
51 * @param array $params (reference ) an assoc array of name/value pairs
52 * @param array $defaults (reference ) an assoc array to hold the flattened values
54 * @return object CRM_Core_BAO_OptionValue object
58 static function retrieve(&$params, &$defaults) {
59 $optionValue = new CRM_Core_DAO_OptionValue();
60 $optionValue->copyValues($params);
61 if ($optionValue->find(TRUE)) {
62 CRM_Core_DAO
::storeValues($optionValue, $defaults);
69 * update the is_active flag in the db
71 * @param int $id id of the database record
72 * @param boolean $is_active value we want to set the is_active field
74 * @return Object DAO object on sucess, null otherwise
77 static function setIsActive($id, $is_active) {
78 return CRM_Core_DAO
::setFieldValue('CRM_Core_DAO_OptionValue', $id, 'is_active', $is_active);
82 * Function to add an Option Value
84 * @param array $params reference array contains the values submitted by the form
85 * @param array $ids reference array contains the id
92 static function add(&$params, &$ids) {
93 // CRM-10921: do not reset attributes to default if this is an update
94 if (!CRM_Utils_Array
::value('optionValue', $ids)) {
95 $params['is_active'] = CRM_Utils_Array
::value('is_active', $params, FALSE);
96 $params['is_default'] = CRM_Utils_Array
::value('is_default', $params, FALSE);
97 $params['is_optgroup'] = CRM_Utils_Array
::value('is_optgroup', $params, FALSE);
98 $params['filter'] = CRM_Utils_Array
::value('filter', $params, FALSE);
101 // action is taken depending upon the mode
102 $optionValue = new CRM_Core_DAO_OptionValue();
103 $optionValue->copyValues($params);
105 if (CRM_Utils_Array
::value('is_default', $params)) {
106 $query = 'UPDATE civicrm_option_value SET is_default = 0 WHERE option_group_id = %1';
108 // tweak default reset, and allow multiple default within group.
109 if ($resetDefaultFor = CRM_Utils_Array
::value('reset_default_for', $params)) {
110 if (is_array($resetDefaultFor)) {
111 $colName = key($resetDefaultFor);
112 $colVal = $resetDefaultFor[$colName];
113 $query .= " AND ( $colName IN ( $colVal ) )";
117 $p = array(1 => array($params['option_group_id'], 'Integer'));
118 CRM_Core_DAO
::executeQuery($query, $p);
121 $groupName = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup',
122 $params['option_group_id'], 'name', 'id'
124 if (in_array($groupName, CRM_Core_OptionGroup
::$_domainIDGroups)) {
125 $optionValue->domain_id
= CRM_Utils_Array
::value('domain_id', $params, CRM_Core_Config
::domainID());
128 $optionValue->id
= CRM_Utils_Array
::value('optionValue', $ids);
129 $optionValue->save();
134 * Function to delete Option Value
136 * @param int $optionGroupId Id of the Option Group to be deleted.
143 static function del($optionValueId) {
144 $optionValue = new CRM_Core_DAO_OptionValue();
145 $optionValue->id
= $optionValueId;
146 if (self
::updateRecords($optionValueId, CRM_Core_Action
::DELETE
)) {
147 return $optionValue->delete();
153 * Function to retrieve activity type label and description
155 * @param int $activityTypeId activity type id
157 * @return array label and description
161 static function getActivityTypeDetails($activityTypeId) {
162 $query = "SELECT civicrm_option_value.label, civicrm_option_value.description
163 FROM civicrm_option_value
164 LEFT JOIN civicrm_option_group ON ( civicrm_option_value.option_group_id = civicrm_option_group.id )
165 WHERE civicrm_option_group.name = 'activity_type'
166 AND civicrm_option_value.value = {$activityTypeId} ";
168 $dao = CRM_Core_DAO
::executeQuery($query);
172 return array($dao->label
, $dao->description
);
176 * Get the Option Value title.
178 * @param int $id id of Option Value
180 * @return string title
186 public static function getTitle($id) {
187 return CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionValue', $id, 'label');
191 * updates contacts affected by the option value passed.
193 * @param Integer $optionValueId the option value id.
194 * @param int $action the action describing whether prefix/suffix was UPDATED or DELETED
198 static function updateRecords(&$optionValueId, $action) {
200 $optionValue = new CRM_Core_DAO_OptionValue();
201 $optionValue->id
= $optionValueId;
202 $optionValue->find(TRUE);
204 $optionGroup = new CRM_Core_DAO_OptionGroup();
205 $optionGroup->id
= $optionValue->option_group_id
;
206 $optionGroup->find(TRUE);
209 $gName = $optionGroup->name
;
211 $value = $optionValue->value
;
213 // get the proper group name & affected field name
214 $individuals = array(
215 'gender' => 'gender_id',
216 'individual_prefix' => 'prefix_id',
217 'individual_suffix' => 'suffix_id',
219 $contributions = array('payment_instrument' => 'payment_instrument_id');
220 $activities = array('activity_type' => 'activity_type_id');
221 $participant = array('participant_role' => 'role_id');
222 $eventType = array('event_type' => 'event_type_id');
223 $aclRole = array('acl_role' => 'acl_role_id');
225 $all = array_merge($individuals, $contributions, $activities, $participant, $eventType, $aclRole);
228 foreach ($all as $name => $id) {
229 if ($gName == $name) {
233 if ($fieldName == '') {
237 if (array_key_exists($gName, $individuals)) {
238 $contactDAO = new CRM_Contact_DAO_Contact();
240 $contactDAO->$fieldName = $value;
243 while ($contactDAO->fetch()) {
244 if ($action == CRM_Core_Action
::DELETE
) {
245 $contact = new CRM_Contact_DAO_Contact();
246 $contact->id
= $contactDAO->id
;
247 $contact->find(TRUE);
249 // make sure dates doesn't get reset
250 $contact->birth_date
= CRM_Utils_Date
::isoToMysql($contact->birth_date
);
251 $contact->deceased_date
= CRM_Utils_Date
::isoToMysql($contact->deceased_date
);
252 $contact->$fieldName = 'NULL';
260 if (array_key_exists($gName, $contributions)) {
261 $contribution = new CRM_Contribute_DAO_Contribution();
262 $contribution->$fieldName = $value;
263 $contribution->find();
264 while ($contribution->fetch()) {
265 if ($action == CRM_Core_Action
::DELETE
) {
266 $contribution->$fieldName = 'NULL';
267 $contribution->save();
273 if (array_key_exists($gName, $activities)) {
274 $activity = new CRM_Activity_DAO_Activity();
275 $activity->$fieldName = $value;
277 while ($activity->fetch()) {
283 //delete participant role, type and event type option value
284 if (array_key_exists($gName, $participant)) {
285 $participantValue = new CRM_Event_DAO_Participant();
286 $participantValue->$fieldName = $value;
287 if ($participantValue->find(TRUE)) {
293 //delete event type option value
294 if (array_key_exists($gName, $eventType)) {
295 $event = new CRM_Event_DAO_Event();
296 $event->$fieldName = $value;
297 if ($event->find(TRUE)) {
303 //delete acl_role option value
304 if (array_key_exists($gName, $aclRole)) {
305 $entityRole = new CRM_ACL_DAO_EntityRole();
306 $entityRole->$fieldName = $value;
308 $aclDAO = new CRM_ACL_DAO_ACL();
309 $aclDAO->entity_id
= $value;
310 if ($entityRole->find(TRUE) ||
$aclDAO->find(TRUE)) {
318 * updates options values weights.
320 * @param int $opGroupIde option group id.
321 * @param array $opWeights options value , weight pair
327 static function updateOptionWeights($opGroupId, $opWeights) {
328 if (!is_array($opWeights) ||
empty($opWeights)) {
332 foreach ($opWeights as $opValue => $opWeight) {
333 $optionValue = new CRM_Core_DAO_OptionValue();
334 $optionValue->option_group_id
= $opGroupId;
335 $optionValue->value
= $opValue;
336 if ($optionValue->find(TRUE)) {
337 $optionValue->weight
= $opWeight;
338 $optionValue->save();
340 $optionValue->free();
345 * Get the values of all option values given an option group ID. Store in system cache
346 * Does not take any filtering arguments. The object is to avoid hitting the DB and retrieve
349 * @param int $optionGroupID the option group for which we want the values from
351 * @return array an array of array of values for this option group
355 static function getOptionValuesArray($optionGroupID) {
356 // check if we can get the field values from the system cache
357 $cacheKey = "CRM_Core_BAO_OptionValue_OptionGroupID_{$optionGroupID}";
358 $cache = CRM_Utils_Cache
::singleton();
359 $optionValues = $cache->get($cacheKey);
360 if (empty($optionValues)) {
361 $dao = new CRM_Core_DAO_OptionValue();
362 $dao->option_group_id
= $optionGroupID;
363 $dao->orderBy('weight ASC, label ASC');
366 $optionValues = array();
367 while ($dao->fetch()) {
368 $optionValues[$dao->id
] = array();
369 CRM_Core_DAO
::storeValues($dao, $optionValues[$dao->id
]);
372 $cache->set($cacheKey, $optionValues);
375 return $optionValues;
379 * Get the values of all option values given an option group ID as a key => value pair
380 * Use above cached function to make it super efficient
382 * @param int $optionGroupID the option group for which we want the values from
384 * @return array an associative array of label, value pairs
388 static function getOptionValuesAssocArray($optionGroupID) {
389 $optionValues = self
::getOptionValuesArray($optionGroupID);
392 foreach ($optionValues as $id => $value) {
393 $options[$value['value']] = $value['label'];
398 * Get the values of all option values given an option group Name as a key => value pair
399 * Use above cached function to make it super efficient
401 * @param string $optionGroupName the option group name for which we want the values from
403 * @return array an associative array of label, value pairs
407 static function getOptionValuesAssocArrayFromName($optionGroupName) {
408 $dao = new CRM_Core_DAO_OptionGroup();
409 $dao->name
= $optionGroupName;
411 $dao->selectAdd('id');
413 $optionValues = self
::getOptionValuesArray($dao->id
);
416 foreach ($optionValues as $id => $value) {
417 $options[$value['value']] = $value['label'];