Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
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 | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | class CRM_Core_BAO_OptionValue extends CRM_Core_DAO_OptionValue { | |
36 | ||
37 | /** | |
100fef9d | 38 | * Class constructor |
6a488035 TO |
39 | */ |
40 | function __construct() { | |
41 | parent::__construct(); | |
42 | } | |
77b97be7 | 43 | |
9fe6051a | 44 | /** |
e99c9805 | 45 | * Create option value - note that the create function calls 'add' but |
77b97be7 EM |
46 | * has more business logic |
47 | * | |
48 | * @param array $params input parameters | |
49 | * | |
50 | * @return object | |
51 | */ | |
c87bbced | 52 | static function create($params) { |
53 | if (empty($params['id'])){ | |
54 | self::setDefaults($params); | |
55 | } | |
56 | $ids = array(); | |
a7488080 | 57 | if (!empty($params['id'])) { |
c87bbced | 58 | $ids = array('optionValue' => $params['id']); |
59 | } | |
60 | return CRM_Core_BAO_OptionValue::add($params, $ids); | |
c87bbced | 61 | } |
62 | /** | |
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) | |
9fe6051a | 67 | * - weight & value will be set to their respective option groups next values |
68 | * if nothing is passed in. | |
69 | * | |
70 | * Note this function does not check for presence of $params['id'] so should only be called | |
71 | * if 'id' is not present | |
c87bbced | 72 | * |
9fe6051a | 73 | * @param array $params |
c87bbced | 74 | */ |
75 | static function setDefaults(&$params){ | |
1c6b7d30 | 76 | if(CRM_Utils_Array::value('label', $params, NULL) === NULL){ |
c87bbced | 77 | $params['label'] = $params['name']; |
78 | } | |
1c6b7d30 | 79 | if(CRM_Utils_Array::value('name', $params, NULL) === NULL){ |
c87bbced | 80 | $params['name'] = $params['label']; |
81 | } | |
1c6b7d30 | 82 | if(CRM_Utils_Array::value('weight', $params, NULL) === NULL){ |
983e888c | 83 | $params['weight'] = self::getDefaultWeight($params); |
c87bbced | 84 | } |
1c6b7d30 | 85 | if (CRM_Utils_Array::value('value', $params, NULL) === NULL){ |
86 | $params['value'] = self::getDefaultValue($params); | |
c87bbced | 87 | } |
88 | } | |
77b97be7 | 89 | |
9fe6051a | 90 | /** |
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 | |
77b97be7 | 95 | * |
9fe6051a | 96 | * @param array $params |
77b97be7 EM |
97 | * |
98 | * @return int | |
9fe6051a | 99 | */ |
1c6b7d30 | 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'])); | |
103 | } | |
104 | ||
105 | /** | |
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 | |
111 | */ | |
112 | static function getDefaultValue($params){ | |
9fe6051a | 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']; | |
117 | } | |
118 | $bao->selectAdd(); | |
119 | $bao->whereAdd("value REGEXP '^[0-9]+$'"); | |
0e9de3f5 | 120 | $bao->selectAdd('(ROUND(COALESCE(MAX(CONVERT(value, UNSIGNED)),0)) +1) as nextvalue'); |
9fe6051a | 121 | $bao->find(TRUE); |
122 | return $bao->nextvalue; | |
123 | } | |
6a488035 | 124 | /** |
c490a46a | 125 | * Fetch object based on array of properties |
6a488035 TO |
126 | * |
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 | |
129 | * | |
c490a46a | 130 | * @return CRM_Core_BAO_OptionValue object |
6a488035 TO |
131 | * @access public |
132 | * @static | |
133 | */ | |
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); | |
139 | return $optionValue; | |
140 | } | |
141 | return NULL; | |
142 | } | |
143 | ||
144 | /** | |
100fef9d | 145 | * Update the is_active flag in the db |
6a488035 TO |
146 | * |
147 | * @param int $id id of the database record | |
148 | * @param boolean $is_active value we want to set the is_active field | |
149 | * | |
150 | * @return Object DAO object on sucess, null otherwise | |
151 | * @static | |
152 | */ | |
153 | static function setIsActive($id, $is_active) { | |
154 | return CRM_Core_DAO::setFieldValue('CRM_Core_DAO_OptionValue', $id, 'is_active', $is_active); | |
155 | } | |
156 | ||
157 | /** | |
100fef9d | 158 | * Add an Option Value |
6a488035 TO |
159 | * |
160 | * @param array $params reference array contains the values submitted by the form | |
161 | * @param array $ids reference array contains the id | |
162 | * | |
163 | * @access public | |
164 | * @static | |
165 | * | |
7c285037 | 166 | * @return CRM_Core_DAO_OptionValue |
6a488035 TO |
167 | */ |
168 | static function add(&$params, &$ids) { | |
169 | // CRM-10921: do not reset attributes to default if this is an update | |
9fe6051a | 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 | |
a7488080 | 174 | if (empty($ids['optionValue'])) { |
6a488035 TO |
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); | |
179 | } | |
180 | ||
181 | // action is taken depending upon the mode | |
182 | $optionValue = new CRM_Core_DAO_OptionValue(); | |
183 | $optionValue->copyValues($params); | |
184 | ||
a7488080 | 185 | if (!empty($params['is_default'])) { |
6a488035 TO |
186 | $query = 'UPDATE civicrm_option_value SET is_default = 0 WHERE option_group_id = %1'; |
187 | ||
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 ) )"; | |
194 | } | |
195 | } | |
196 | ||
197 | $p = array(1 => array($params['option_group_id'], 'Integer')); | |
198 | CRM_Core_DAO::executeQuery($query, $p); | |
199 | } | |
89ab5601 PJ |
200 | |
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' | |
205 | ); | |
206 | } | |
207 | else { | |
208 | $groupId = $params['option_group_id']; | |
209 | } | |
210 | ||
6a488035 | 211 | $groupName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', |
89ab5601 | 212 | $groupId, 'name', 'id' |
6a488035 TO |
213 | ); |
214 | if (in_array($groupName, CRM_Core_OptionGroup::$_domainIDGroups)) { | |
215 | $optionValue->domain_id = CRM_Utils_Array::value('domain_id', $params, CRM_Core_Config::domainID()); | |
216 | } | |
217 | ||
218 | $optionValue->id = CRM_Utils_Array::value('optionValue', $ids); | |
219 | $optionValue->save(); | |
a4a33486 | 220 | CRM_Core_PseudoConstant::flush(); |
6a488035 TO |
221 | return $optionValue; |
222 | } | |
223 | ||
224 | /** | |
100fef9d | 225 | * Delete Option Value |
6a488035 | 226 | * |
c490a46a | 227 | * @param int $optionValueId |
6a488035 TO |
228 | * |
229 | * @return boolean | |
230 | * | |
231 | * @access public | |
232 | * @static | |
233 | */ | |
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)) { | |
a4a33486 | 238 | CRM_Core_PseudoConstant::flush(); |
6a488035 TO |
239 | return $optionValue->delete(); |
240 | } | |
241 | return FALSE; | |
242 | } | |
243 | ||
244 | /** | |
100fef9d | 245 | * Retrieve activity type label and description |
6a488035 TO |
246 | * |
247 | * @param int $activityTypeId activity type id | |
248 | * | |
249 | * @return array label and description | |
250 | * @static | |
251 | * @access public | |
252 | */ | |
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} "; | |
259 | ||
260 | $dao = CRM_Core_DAO::executeQuery($query); | |
261 | ||
262 | $dao->fetch(); | |
263 | ||
264 | return array($dao->label, $dao->description); | |
265 | } | |
266 | ||
267 | /** | |
268 | * Get the Option Value title. | |
269 | * | |
270 | * @param int $id id of Option Value | |
271 | * | |
272 | * @return string title | |
273 | * | |
274 | * @access public | |
275 | * @static | |
276 | * | |
277 | */ | |
278 | public static function getTitle($id) { | |
279 | return CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $id, 'label'); | |
280 | } | |
281 | ||
282 | /** | |
100fef9d | 283 | * Updates contacts affected by the option value passed. |
6a488035 TO |
284 | * |
285 | * @param Integer $optionValueId the option value id. | |
286 | * @param int $action the action describing whether prefix/suffix was UPDATED or DELETED | |
287 | * | |
7a6073fd | 288 | * @return bool |
6a488035 TO |
289 | */ |
290 | static function updateRecords(&$optionValueId, $action) { | |
291 | //finding group name | |
292 | $optionValue = new CRM_Core_DAO_OptionValue(); | |
293 | $optionValue->id = $optionValueId; | |
294 | $optionValue->find(TRUE); | |
295 | ||
296 | $optionGroup = new CRM_Core_DAO_OptionGroup(); | |
297 | $optionGroup->id = $optionValue->option_group_id; | |
298 | $optionGroup->find(TRUE); | |
299 | ||
300 | // group name | |
301 | $gName = $optionGroup->name; | |
302 | // value | |
303 | $value = $optionValue->value; | |
304 | ||
305 | // get the proper group name & affected field name | |
67744c4e | 306 | // todo: this may no longer be needed for individuals - check inputs |
6a488035 TO |
307 | $individuals = array( |
308 | 'gender' => 'gender_id', | |
309 | 'individual_prefix' => 'prefix_id', | |
310 | 'individual_suffix' => 'suffix_id', | |
aa62b355 | 311 | 'communication_style' => 'communication_style_id', // Not only Individuals -- but the code seems to be generic for all contact types, despite the naming... |
6a488035 TO |
312 | ); |
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'); | |
318 | ||
319 | $all = array_merge($individuals, $contributions, $activities, $participant, $eventType, $aclRole); | |
320 | $fieldName = ''; | |
321 | ||
322 | foreach ($all as $name => $id) { | |
323 | if ($gName == $name) { | |
324 | $fieldName = $id; | |
325 | } | |
326 | } | |
327 | if ($fieldName == '') { | |
328 | return TRUE; | |
329 | } | |
330 | ||
331 | if (array_key_exists($gName, $individuals)) { | |
332 | $contactDAO = new CRM_Contact_DAO_Contact(); | |
333 | ||
334 | $contactDAO->$fieldName = $value; | |
335 | $contactDAO->find(); | |
336 | ||
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); | |
342 | ||
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'; | |
347 | $contact->save(); | |
348 | } | |
349 | } | |
350 | ||
351 | return TRUE; | |
352 | } | |
353 | ||
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(); | |
362 | } | |
363 | } | |
364 | return TRUE; | |
365 | } | |
366 | ||
367 | if (array_key_exists($gName, $activities)) { | |
368 | $activity = new CRM_Activity_DAO_Activity(); | |
369 | $activity->$fieldName = $value; | |
370 | $activity->find(); | |
371 | while ($activity->fetch()) { | |
372 | $activity->delete(); | |
373 | } | |
374 | return TRUE; | |
375 | } | |
376 | ||
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)) { | |
382 | return FALSE; | |
383 | } | |
384 | return TRUE; | |
385 | } | |
386 | ||
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)) { | |
392 | return FALSE; | |
393 | } | |
394 | return TRUE; | |
395 | } | |
396 | ||
397 | //delete acl_role option value | |
398 | if (array_key_exists($gName, $aclRole)) { | |
399 | $entityRole = new CRM_ACL_DAO_EntityRole(); | |
400 | $entityRole->$fieldName = $value; | |
401 | ||
402 | $aclDAO = new CRM_ACL_DAO_ACL(); | |
403 | $aclDAO->entity_id = $value; | |
404 | if ($entityRole->find(TRUE) || $aclDAO->find(TRUE)) { | |
405 | return FALSE; | |
406 | } | |
407 | return TRUE; | |
408 | } | |
409 | } | |
410 | ||
411 | /** | |
100fef9d | 412 | * Updates options values weights. |
6a488035 | 413 | * |
c490a46a | 414 | * @param int $opGroupId |
2a6da8d7 | 415 | * @param array $opWeights options value , weight pair |
6a488035 TO |
416 | * |
417 | * @return void | |
418 | * @access public | |
419 | * @static | |
420 | */ | |
421 | static function updateOptionWeights($opGroupId, $opWeights) { | |
422 | if (!is_array($opWeights) || empty($opWeights)) { | |
423 | return; | |
424 | } | |
425 | ||
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(); | |
433 | } | |
434 | $optionValue->free(); | |
435 | } | |
436 | } | |
437 | ||
438 | /** | |
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 | |
441 | * from memory | |
442 | * | |
443 | * @param int $optionGroupID the option group for which we want the values from | |
444 | * | |
445 | * @return array an array of array of values for this option group | |
446 | * @static | |
447 | * @public | |
448 | */ | |
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'); | |
458 | $dao->find(); | |
459 | ||
460 | $optionValues = array(); | |
461 | while ($dao->fetch()) { | |
462 | $optionValues[$dao->id] = array(); | |
463 | CRM_Core_DAO::storeValues($dao, $optionValues[$dao->id]); | |
464 | } | |
465 | ||
466 | $cache->set($cacheKey, $optionValues); | |
467 | } | |
468 | ||
469 | return $optionValues; | |
470 | } | |
471 | ||
472 | /** | |
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 | |
475 | * | |
476 | * @param int $optionGroupID the option group for which we want the values from | |
477 | * | |
478 | * @return array an associative array of label, value pairs | |
479 | * @static | |
480 | * @public | |
481 | */ | |
482 | static function getOptionValuesAssocArray($optionGroupID) { | |
483 | $optionValues = self::getOptionValuesArray($optionGroupID); | |
484 | ||
485 | $options = array(); | |
486 | foreach ($optionValues as $id => $value) { | |
487 | $options[$value['value']] = $value['label']; | |
488 | } | |
489 | return $options; | |
490 | } | |
491 | /** | |
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 | |
494 | * | |
495 | * @param string $optionGroupName the option group name for which we want the values from | |
496 | * | |
497 | * @return array an associative array of label, value pairs | |
498 | * @static | |
499 | * @public | |
500 | */ | |
501 | static function getOptionValuesAssocArrayFromName($optionGroupName) { | |
502 | $dao = new CRM_Core_DAO_OptionGroup(); | |
503 | $dao->name = $optionGroupName; | |
504 | $dao->selectAdd(); | |
505 | $dao->selectAdd('id'); | |
506 | $dao->find(TRUE); | |
507 | $optionValues = self::getOptionValuesArray($dao->id); | |
508 | ||
509 | $options = array(); | |
510 | foreach ($optionValues as $id => $value) { | |
511 | $options[$value['value']] = $value['label']; | |
512 | } | |
513 | return $options; | |
514 | } | |
515 | ||
516 | } | |
517 |