Merge pull request #115 from dlobo/CRM-12088
[civicrm-core.git] / CRM / Core / BAO / OptionValue.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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
31 * @copyright CiviCRM LLC (c) 2004-2013
32 * $Id$
33 *
34 */
35 class CRM_Core_BAO_OptionValue extends CRM_Core_DAO_OptionValue {
36
37 /**
38 * class constructor
39 */
40 function __construct() {
41 parent::__construct();
42 }
43
44 /**
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
50 *
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
53 *
54 * @return object CRM_Core_BAO_OptionValue object
55 * @access public
56 * @static
57 */
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);
63 return $optionValue;
64 }
65 return NULL;
66 }
67
68 /**
69 * update the is_active flag in the db
70 *
71 * @param int $id id of the database record
72 * @param boolean $is_active value we want to set the is_active field
73 *
74 * @return Object DAO object on sucess, null otherwise
75 * @static
76 */
77 static function setIsActive($id, $is_active) {
78 return CRM_Core_DAO::setFieldValue('CRM_Core_DAO_OptionValue', $id, 'is_active', $is_active);
79 }
80
81 /**
82 * Function to add an Option Value
83 *
84 * @param array $params reference array contains the values submitted by the form
85 * @param array $ids reference array contains the id
86 *
87 * @access public
88 * @static
89 *
90 * @return object
91 */
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);
99 }
100
101 // action is taken depending upon the mode
102 $optionValue = new CRM_Core_DAO_OptionValue();
103 $optionValue->copyValues($params);
104
105 if (CRM_Utils_Array::value('is_default', $params)) {
106 $query = 'UPDATE civicrm_option_value SET is_default = 0 WHERE option_group_id = %1';
107
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 ) )";
114 }
115 }
116
117 $p = array(1 => array($params['option_group_id'], 'Integer'));
118 CRM_Core_DAO::executeQuery($query, $p);
119 }
120
121 $groupName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup',
122 $params['option_group_id'], 'name', 'id'
123 );
124 if (in_array($groupName, CRM_Core_OptionGroup::$_domainIDGroups)) {
125 $optionValue->domain_id = CRM_Utils_Array::value('domain_id', $params, CRM_Core_Config::domainID());
126 }
127
128 $optionValue->id = CRM_Utils_Array::value('optionValue', $ids);
129 $optionValue->save();
130 return $optionValue;
131 }
132
133 /**
134 * Function to delete Option Value
135 *
136 * @param int $optionGroupId Id of the Option Group to be deleted.
137 *
138 * @return boolean
139 *
140 * @access public
141 * @static
142 */
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();
148 }
149 return FALSE;
150 }
151
152 /**
153 * Function to retrieve activity type label and description
154 *
155 * @param int $activityTypeId activity type id
156 *
157 * @return array label and description
158 * @static
159 * @access public
160 */
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} ";
167
168 $dao = CRM_Core_DAO::executeQuery($query);
169
170 $dao->fetch();
171
172 return array($dao->label, $dao->description);
173 }
174
175 /**
176 * Get the Option Value title.
177 *
178 * @param int $id id of Option Value
179 *
180 * @return string title
181 *
182 * @access public
183 * @static
184 *
185 */
186 public static function getTitle($id) {
187 return CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $id, 'label');
188 }
189
190 /**
191 * updates contacts affected by the option value passed.
192 *
193 * @param Integer $optionValueId the option value id.
194 * @param int $action the action describing whether prefix/suffix was UPDATED or DELETED
195 *
196 * @return void
197 */
198 static function updateRecords(&$optionValueId, $action) {
199 //finding group name
200 $optionValue = new CRM_Core_DAO_OptionValue();
201 $optionValue->id = $optionValueId;
202 $optionValue->find(TRUE);
203
204 $optionGroup = new CRM_Core_DAO_OptionGroup();
205 $optionGroup->id = $optionValue->option_group_id;
206 $optionGroup->find(TRUE);
207
208 // group name
209 $gName = $optionGroup->name;
210 // value
211 $value = $optionValue->value;
212
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',
218 );
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');
224
225 $all = array_merge($individuals, $contributions, $activities, $participant, $eventType, $aclRole);
226 $fieldName = '';
227
228 foreach ($all as $name => $id) {
229 if ($gName == $name) {
230 $fieldName = $id;
231 }
232 }
233 if ($fieldName == '') {
234 return TRUE;
235 }
236
237 if (array_key_exists($gName, $individuals)) {
238 $contactDAO = new CRM_Contact_DAO_Contact();
239
240 $contactDAO->$fieldName = $value;
241 $contactDAO->find();
242
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);
248
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';
253 $contact->save();
254 }
255 }
256
257 return TRUE;
258 }
259
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();
268 }
269 }
270 return TRUE;
271 }
272
273 if (array_key_exists($gName, $activities)) {
274 $activity = new CRM_Activity_DAO_Activity();
275 $activity->$fieldName = $value;
276 $activity->find();
277 while ($activity->fetch()) {
278 $activity->delete();
279 }
280 return TRUE;
281 }
282
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)) {
288 return FALSE;
289 }
290 return TRUE;
291 }
292
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)) {
298 return FALSE;
299 }
300 return TRUE;
301 }
302
303 //delete acl_role option value
304 if (array_key_exists($gName, $aclRole)) {
305 $entityRole = new CRM_ACL_DAO_EntityRole();
306 $entityRole->$fieldName = $value;
307
308 $aclDAO = new CRM_ACL_DAO_ACL();
309 $aclDAO->entity_id = $value;
310 if ($entityRole->find(TRUE) || $aclDAO->find(TRUE)) {
311 return FALSE;
312 }
313 return TRUE;
314 }
315 }
316
317 /**
318 * updates options values weights.
319 *
320 * @param int $opGroupIde option group id.
321 * @param array $opWeights options value , weight pair
322 *
323 * @return void
324 * @access public
325 * @static
326 */
327 static function updateOptionWeights($opGroupId, $opWeights) {
328 if (!is_array($opWeights) || empty($opWeights)) {
329 return;
330 }
331
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();
339 }
340 $optionValue->free();
341 }
342 }
343
344 /**
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
347 * from memory
348 *
349 * @param int $optionGroupID the option group for which we want the values from
350 *
351 * @return array an array of array of values for this option group
352 * @static
353 * @public
354 */
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');
364 $dao->find();
365
366 $optionValues = array();
367 while ($dao->fetch()) {
368 $optionValues[$dao->id] = array();
369 CRM_Core_DAO::storeValues($dao, $optionValues[$dao->id]);
370 }
371
372 $cache->set($cacheKey, $optionValues);
373 }
374
375 return $optionValues;
376 }
377
378 /**
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
381 *
382 * @param int $optionGroupID the option group for which we want the values from
383 *
384 * @return array an associative array of label, value pairs
385 * @static
386 * @public
387 */
388 static function getOptionValuesAssocArray($optionGroupID) {
389 $optionValues = self::getOptionValuesArray($optionGroupID);
390
391 $options = array();
392 foreach ($optionValues as $id => $value) {
393 $options[$value['value']] = $value['label'];
394 }
395 return $options;
396 }
397 /**
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
400 *
401 * @param string $optionGroupName the option group name for which we want the values from
402 *
403 * @return array an associative array of label, value pairs
404 * @static
405 * @public
406 */
407 static function getOptionValuesAssocArrayFromName($optionGroupName) {
408 $dao = new CRM_Core_DAO_OptionGroup();
409 $dao->name = $optionGroupName;
410 $dao->selectAdd();
411 $dao->selectAdd('id');
412 $dao->find(TRUE);
413 $optionValues = self::getOptionValuesArray($dao->id);
414
415 $options = array();
416 foreach ($optionValues as $id => $value) {
417 $options[$value['value']] = $value['label'];
418 }
419 return $options;
420 }
421
422 }
423