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 | /** | |
38 | * class constructor | |
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); | |
61 | ; | |
62 | } | |
63 | /** | |
64 | * Set default Parameters | |
65 | * This functions sets default parameters if not set: | |
66 | * - name & label are set to each other as required (it might make more sense for one | |
67 | * to be required but this would mean a change to the api level) | |
9fe6051a | 68 | * - weight & value will be set to their respective option groups next values |
69 | * if nothing is passed in. | |
70 | * | |
71 | * Note this function does not check for presence of $params['id'] so should only be called | |
72 | * if 'id' is not present | |
c87bbced | 73 | * |
9fe6051a | 74 | * @param array $params |
c87bbced | 75 | */ |
76 | static function setDefaults(&$params){ | |
1c6b7d30 | 77 | if(CRM_Utils_Array::value('label', $params, NULL) === NULL){ |
c87bbced | 78 | $params['label'] = $params['name']; |
79 | } | |
1c6b7d30 | 80 | if(CRM_Utils_Array::value('name', $params, NULL) === NULL){ |
c87bbced | 81 | $params['name'] = $params['label']; |
82 | } | |
1c6b7d30 | 83 | if(CRM_Utils_Array::value('weight', $params, NULL) === NULL){ |
983e888c | 84 | $params['weight'] = self::getDefaultWeight($params); |
c87bbced | 85 | } |
1c6b7d30 | 86 | if (CRM_Utils_Array::value('value', $params, NULL) === NULL){ |
87 | $params['value'] = self::getDefaultValue($params); | |
c87bbced | 88 | } |
89 | } | |
77b97be7 | 90 | |
9fe6051a | 91 | /** |
92 | * Get next available value | |
93 | * We will take the highest numeric value (or 0 if no numeric values exist) | |
94 | * and add one. The calling function is responsible for any | |
95 | * more complex decision making | |
77b97be7 | 96 | * |
9fe6051a | 97 | * @param array $params |
77b97be7 EM |
98 | * |
99 | * @return int | |
9fe6051a | 100 | */ |
1c6b7d30 | 101 | static function getDefaultWeight($params){ |
102 | return (int) CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_OptionValue', | |
103 | array('option_group_id' => $params['option_group_id'])); | |
104 | } | |
105 | ||
106 | /** | |
107 | * Get next available value | |
108 | * We will take the highest numeric value (or 0 if no numeric values exist) | |
109 | * and add one. The calling function is responsible for any | |
110 | * more complex decision making | |
111 | * @param array $params | |
112 | */ | |
113 | static function getDefaultValue($params){ | |
9fe6051a | 114 | $bao = new CRM_Core_BAO_OptionValue(); |
115 | $bao->option_group_id = $params['option_group_id']; | |
116 | if(isset($params['domain_id'])){ | |
117 | $bao->domain_id = $params['domain_id']; | |
118 | } | |
119 | $bao->selectAdd(); | |
120 | $bao->whereAdd("value REGEXP '^[0-9]+$'"); | |
0e9de3f5 | 121 | $bao->selectAdd('(ROUND(COALESCE(MAX(CONVERT(value, UNSIGNED)),0)) +1) as nextvalue'); |
9fe6051a | 122 | $bao->find(TRUE); |
123 | return $bao->nextvalue; | |
124 | } | |
6a488035 TO |
125 | /** |
126 | * Takes a bunch of params that are needed to match certain criteria and | |
127 | * retrieves the relevant objects. Typically the valid params are only | |
128 | * contact_id. We'll tweak this function to be more full featured over a period | |
129 | * of time. This is the inverse function of create. It also stores all the retrieved | |
130 | * values in the default array | |
131 | * | |
132 | * @param array $params (reference ) an assoc array of name/value pairs | |
133 | * @param array $defaults (reference ) an assoc array to hold the flattened values | |
134 | * | |
135 | * @return object CRM_Core_BAO_OptionValue object | |
136 | * @access public | |
137 | * @static | |
138 | */ | |
139 | static function retrieve(&$params, &$defaults) { | |
140 | $optionValue = new CRM_Core_DAO_OptionValue(); | |
141 | $optionValue->copyValues($params); | |
142 | if ($optionValue->find(TRUE)) { | |
143 | CRM_Core_DAO::storeValues($optionValue, $defaults); | |
144 | return $optionValue; | |
145 | } | |
146 | return NULL; | |
147 | } | |
148 | ||
149 | /** | |
150 | * update the is_active flag in the db | |
151 | * | |
152 | * @param int $id id of the database record | |
153 | * @param boolean $is_active value we want to set the is_active field | |
154 | * | |
155 | * @return Object DAO object on sucess, null otherwise | |
156 | * @static | |
157 | */ | |
158 | static function setIsActive($id, $is_active) { | |
159 | return CRM_Core_DAO::setFieldValue('CRM_Core_DAO_OptionValue', $id, 'is_active', $is_active); | |
160 | } | |
161 | ||
162 | /** | |
163 | * Function to add an Option Value | |
164 | * | |
165 | * @param array $params reference array contains the values submitted by the form | |
166 | * @param array $ids reference array contains the id | |
167 | * | |
168 | * @access public | |
169 | * @static | |
170 | * | |
7c285037 | 171 | * @return CRM_Core_DAO_OptionValue |
6a488035 TO |
172 | */ |
173 | static function add(&$params, &$ids) { | |
174 | // CRM-10921: do not reset attributes to default if this is an update | |
9fe6051a | 175 | //@todo consider if defaults are being set in the right place. 'dumb' defaults like |
176 | // these would be usefully set @ the api layer so they are visible to api users | |
177 | // complex defaults like the domain id below would make sense in the setDefauls function | |
178 | // but unclear what other ways this function is being used | |
a7488080 | 179 | if (empty($ids['optionValue'])) { |
6a488035 TO |
180 | $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE); |
181 | $params['is_default'] = CRM_Utils_Array::value('is_default', $params, FALSE); | |
182 | $params['is_optgroup'] = CRM_Utils_Array::value('is_optgroup', $params, FALSE); | |
183 | $params['filter'] = CRM_Utils_Array::value('filter', $params, FALSE); | |
184 | } | |
185 | ||
186 | // action is taken depending upon the mode | |
187 | $optionValue = new CRM_Core_DAO_OptionValue(); | |
188 | $optionValue->copyValues($params); | |
189 | ||
a7488080 | 190 | if (!empty($params['is_default'])) { |
6a488035 TO |
191 | $query = 'UPDATE civicrm_option_value SET is_default = 0 WHERE option_group_id = %1'; |
192 | ||
193 | // tweak default reset, and allow multiple default within group. | |
194 | if ($resetDefaultFor = CRM_Utils_Array::value('reset_default_for', $params)) { | |
195 | if (is_array($resetDefaultFor)) { | |
196 | $colName = key($resetDefaultFor); | |
197 | $colVal = $resetDefaultFor[$colName]; | |
198 | $query .= " AND ( $colName IN ( $colVal ) )"; | |
199 | } | |
200 | } | |
201 | ||
202 | $p = array(1 => array($params['option_group_id'], 'Integer')); | |
203 | CRM_Core_DAO::executeQuery($query, $p); | |
204 | } | |
89ab5601 PJ |
205 | |
206 | // CRM-13814 : evalute option group id | |
207 | if (!array_key_exists('option_group_id', $params) && !empty($ids['optionValue'])) { | |
208 | $groupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', | |
209 | $ids['optionValue'], 'option_group_id', 'id' | |
210 | ); | |
211 | } | |
212 | else { | |
213 | $groupId = $params['option_group_id']; | |
214 | } | |
215 | ||
6a488035 | 216 | $groupName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', |
89ab5601 | 217 | $groupId, 'name', 'id' |
6a488035 TO |
218 | ); |
219 | if (in_array($groupName, CRM_Core_OptionGroup::$_domainIDGroups)) { | |
220 | $optionValue->domain_id = CRM_Utils_Array::value('domain_id', $params, CRM_Core_Config::domainID()); | |
221 | } | |
222 | ||
223 | $optionValue->id = CRM_Utils_Array::value('optionValue', $ids); | |
224 | $optionValue->save(); | |
a4a33486 | 225 | CRM_Core_PseudoConstant::flush(); |
6a488035 TO |
226 | return $optionValue; |
227 | } | |
228 | ||
229 | /** | |
230 | * Function to delete Option Value | |
231 | * | |
dd244018 EM |
232 | * @param $optionValueId |
233 | * | |
234 | * @internal param int $optionGroupId Id of the Option Group to be deleted. | |
6a488035 TO |
235 | * |
236 | * @return boolean | |
237 | * | |
238 | * @access public | |
239 | * @static | |
240 | */ | |
241 | static function del($optionValueId) { | |
242 | $optionValue = new CRM_Core_DAO_OptionValue(); | |
243 | $optionValue->id = $optionValueId; | |
244 | if (self::updateRecords($optionValueId, CRM_Core_Action::DELETE)) { | |
a4a33486 | 245 | CRM_Core_PseudoConstant::flush(); |
6a488035 TO |
246 | return $optionValue->delete(); |
247 | } | |
248 | return FALSE; | |
249 | } | |
250 | ||
251 | /** | |
252 | * Function to retrieve activity type label and description | |
253 | * | |
254 | * @param int $activityTypeId activity type id | |
255 | * | |
256 | * @return array label and description | |
257 | * @static | |
258 | * @access public | |
259 | */ | |
260 | static function getActivityTypeDetails($activityTypeId) { | |
261 | $query = "SELECT civicrm_option_value.label, civicrm_option_value.description | |
262 | FROM civicrm_option_value | |
263 | LEFT JOIN civicrm_option_group ON ( civicrm_option_value.option_group_id = civicrm_option_group.id ) | |
264 | WHERE civicrm_option_group.name = 'activity_type' | |
265 | AND civicrm_option_value.value = {$activityTypeId} "; | |
266 | ||
267 | $dao = CRM_Core_DAO::executeQuery($query); | |
268 | ||
269 | $dao->fetch(); | |
270 | ||
271 | return array($dao->label, $dao->description); | |
272 | } | |
273 | ||
274 | /** | |
275 | * Get the Option Value title. | |
276 | * | |
277 | * @param int $id id of Option Value | |
278 | * | |
279 | * @return string title | |
280 | * | |
281 | * @access public | |
282 | * @static | |
283 | * | |
284 | */ | |
285 | public static function getTitle($id) { | |
286 | return CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $id, 'label'); | |
287 | } | |
288 | ||
289 | /** | |
290 | * updates contacts affected by the option value passed. | |
291 | * | |
292 | * @param Integer $optionValueId the option value id. | |
293 | * @param int $action the action describing whether prefix/suffix was UPDATED or DELETED | |
294 | * | |
295 | * @return void | |
296 | */ | |
297 | static function updateRecords(&$optionValueId, $action) { | |
298 | //finding group name | |
299 | $optionValue = new CRM_Core_DAO_OptionValue(); | |
300 | $optionValue->id = $optionValueId; | |
301 | $optionValue->find(TRUE); | |
302 | ||
303 | $optionGroup = new CRM_Core_DAO_OptionGroup(); | |
304 | $optionGroup->id = $optionValue->option_group_id; | |
305 | $optionGroup->find(TRUE); | |
306 | ||
307 | // group name | |
308 | $gName = $optionGroup->name; | |
309 | // value | |
310 | $value = $optionValue->value; | |
311 | ||
312 | // get the proper group name & affected field name | |
67744c4e | 313 | // todo: this may no longer be needed for individuals - check inputs |
6a488035 TO |
314 | $individuals = array( |
315 | 'gender' => 'gender_id', | |
316 | 'individual_prefix' => 'prefix_id', | |
317 | 'individual_suffix' => 'suffix_id', | |
aa62b355 | 318 | 'communication_style' => 'communication_style_id', // Not only Individuals -- but the code seems to be generic for all contact types, despite the naming... |
6a488035 TO |
319 | ); |
320 | $contributions = array('payment_instrument' => 'payment_instrument_id'); | |
321 | $activities = array('activity_type' => 'activity_type_id'); | |
322 | $participant = array('participant_role' => 'role_id'); | |
323 | $eventType = array('event_type' => 'event_type_id'); | |
324 | $aclRole = array('acl_role' => 'acl_role_id'); | |
325 | ||
326 | $all = array_merge($individuals, $contributions, $activities, $participant, $eventType, $aclRole); | |
327 | $fieldName = ''; | |
328 | ||
329 | foreach ($all as $name => $id) { | |
330 | if ($gName == $name) { | |
331 | $fieldName = $id; | |
332 | } | |
333 | } | |
334 | if ($fieldName == '') { | |
335 | return TRUE; | |
336 | } | |
337 | ||
338 | if (array_key_exists($gName, $individuals)) { | |
339 | $contactDAO = new CRM_Contact_DAO_Contact(); | |
340 | ||
341 | $contactDAO->$fieldName = $value; | |
342 | $contactDAO->find(); | |
343 | ||
344 | while ($contactDAO->fetch()) { | |
345 | if ($action == CRM_Core_Action::DELETE) { | |
346 | $contact = new CRM_Contact_DAO_Contact(); | |
347 | $contact->id = $contactDAO->id; | |
348 | $contact->find(TRUE); | |
349 | ||
350 | // make sure dates doesn't get reset | |
351 | $contact->birth_date = CRM_Utils_Date::isoToMysql($contact->birth_date); | |
352 | $contact->deceased_date = CRM_Utils_Date::isoToMysql($contact->deceased_date); | |
353 | $contact->$fieldName = 'NULL'; | |
354 | $contact->save(); | |
355 | } | |
356 | } | |
357 | ||
358 | return TRUE; | |
359 | } | |
360 | ||
361 | if (array_key_exists($gName, $contributions)) { | |
362 | $contribution = new CRM_Contribute_DAO_Contribution(); | |
363 | $contribution->$fieldName = $value; | |
364 | $contribution->find(); | |
365 | while ($contribution->fetch()) { | |
366 | if ($action == CRM_Core_Action::DELETE) { | |
367 | $contribution->$fieldName = 'NULL'; | |
368 | $contribution->save(); | |
369 | } | |
370 | } | |
371 | return TRUE; | |
372 | } | |
373 | ||
374 | if (array_key_exists($gName, $activities)) { | |
375 | $activity = new CRM_Activity_DAO_Activity(); | |
376 | $activity->$fieldName = $value; | |
377 | $activity->find(); | |
378 | while ($activity->fetch()) { | |
379 | $activity->delete(); | |
380 | } | |
381 | return TRUE; | |
382 | } | |
383 | ||
384 | //delete participant role, type and event type option value | |
385 | if (array_key_exists($gName, $participant)) { | |
386 | $participantValue = new CRM_Event_DAO_Participant(); | |
387 | $participantValue->$fieldName = $value; | |
388 | if ($participantValue->find(TRUE)) { | |
389 | return FALSE; | |
390 | } | |
391 | return TRUE; | |
392 | } | |
393 | ||
394 | //delete event type option value | |
395 | if (array_key_exists($gName, $eventType)) { | |
396 | $event = new CRM_Event_DAO_Event(); | |
397 | $event->$fieldName = $value; | |
398 | if ($event->find(TRUE)) { | |
399 | return FALSE; | |
400 | } | |
401 | return TRUE; | |
402 | } | |
403 | ||
404 | //delete acl_role option value | |
405 | if (array_key_exists($gName, $aclRole)) { | |
406 | $entityRole = new CRM_ACL_DAO_EntityRole(); | |
407 | $entityRole->$fieldName = $value; | |
408 | ||
409 | $aclDAO = new CRM_ACL_DAO_ACL(); | |
410 | $aclDAO->entity_id = $value; | |
411 | if ($entityRole->find(TRUE) || $aclDAO->find(TRUE)) { | |
412 | return FALSE; | |
413 | } | |
414 | return TRUE; | |
415 | } | |
416 | } | |
417 | ||
418 | /** | |
419 | * updates options values weights. | |
420 | * | |
2a6da8d7 EM |
421 | * @param $opGroupId |
422 | * @param array $opWeights options value , weight pair | |
6a488035 | 423 | * |
2a6da8d7 | 424 | * @internal param int $opGroupIde option group id. |
6a488035 TO |
425 | * @return void |
426 | * @access public | |
427 | * @static | |
428 | */ | |
429 | static function updateOptionWeights($opGroupId, $opWeights) { | |
430 | if (!is_array($opWeights) || empty($opWeights)) { | |
431 | return; | |
432 | } | |
433 | ||
434 | foreach ($opWeights as $opValue => $opWeight) { | |
435 | $optionValue = new CRM_Core_DAO_OptionValue(); | |
436 | $optionValue->option_group_id = $opGroupId; | |
437 | $optionValue->value = $opValue; | |
438 | if ($optionValue->find(TRUE)) { | |
439 | $optionValue->weight = $opWeight; | |
440 | $optionValue->save(); | |
441 | } | |
442 | $optionValue->free(); | |
443 | } | |
444 | } | |
445 | ||
446 | /** | |
447 | * Get the values of all option values given an option group ID. Store in system cache | |
448 | * Does not take any filtering arguments. The object is to avoid hitting the DB and retrieve | |
449 | * from memory | |
450 | * | |
451 | * @param int $optionGroupID the option group for which we want the values from | |
452 | * | |
453 | * @return array an array of array of values for this option group | |
454 | * @static | |
455 | * @public | |
456 | */ | |
457 | static function getOptionValuesArray($optionGroupID) { | |
458 | // check if we can get the field values from the system cache | |
459 | $cacheKey = "CRM_Core_BAO_OptionValue_OptionGroupID_{$optionGroupID}"; | |
460 | $cache = CRM_Utils_Cache::singleton(); | |
461 | $optionValues = $cache->get($cacheKey); | |
462 | if (empty($optionValues)) { | |
463 | $dao = new CRM_Core_DAO_OptionValue(); | |
464 | $dao->option_group_id = $optionGroupID; | |
465 | $dao->orderBy('weight ASC, label ASC'); | |
466 | $dao->find(); | |
467 | ||
468 | $optionValues = array(); | |
469 | while ($dao->fetch()) { | |
470 | $optionValues[$dao->id] = array(); | |
471 | CRM_Core_DAO::storeValues($dao, $optionValues[$dao->id]); | |
472 | } | |
473 | ||
474 | $cache->set($cacheKey, $optionValues); | |
475 | } | |
476 | ||
477 | return $optionValues; | |
478 | } | |
479 | ||
480 | /** | |
481 | * Get the values of all option values given an option group ID as a key => value pair | |
482 | * Use above cached function to make it super efficient | |
483 | * | |
484 | * @param int $optionGroupID the option group for which we want the values from | |
485 | * | |
486 | * @return array an associative array of label, value pairs | |
487 | * @static | |
488 | * @public | |
489 | */ | |
490 | static function getOptionValuesAssocArray($optionGroupID) { | |
491 | $optionValues = self::getOptionValuesArray($optionGroupID); | |
492 | ||
493 | $options = array(); | |
494 | foreach ($optionValues as $id => $value) { | |
495 | $options[$value['value']] = $value['label']; | |
496 | } | |
497 | return $options; | |
498 | } | |
499 | /** | |
500 | * Get the values of all option values given an option group Name as a key => value pair | |
501 | * Use above cached function to make it super efficient | |
502 | * | |
503 | * @param string $optionGroupName the option group name for which we want the values from | |
504 | * | |
505 | * @return array an associative array of label, value pairs | |
506 | * @static | |
507 | * @public | |
508 | */ | |
509 | static function getOptionValuesAssocArrayFromName($optionGroupName) { | |
510 | $dao = new CRM_Core_DAO_OptionGroup(); | |
511 | $dao->name = $optionGroupName; | |
512 | $dao->selectAdd(); | |
513 | $dao->selectAdd('id'); | |
514 | $dao->find(TRUE); | |
515 | $optionValues = self::getOptionValuesArray($dao->id); | |
516 | ||
517 | $options = array(); | |
518 | foreach ($optionValues as $id => $value) { | |
519 | $options[$value['value']] = $value['label']; | |
520 | } | |
521 | return $options; | |
522 | } | |
523 | ||
524 | } | |
525 |