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