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(); | |
54 | if (CRM_Utils_Array::value('id', $params)) { | |
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 | |
6a488035 TO |
172 | if (!CRM_Utils_Array::value('optionValue', $ids)) { |
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 | ||
183 | if (CRM_Utils_Array::value('is_default', $params)) { | |
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 | } | |
6a488035 TO |
198 | $groupName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', |
199 | $params['option_group_id'], 'name', 'id' | |
200 | ); | |
201 | if (in_array($groupName, CRM_Core_OptionGroup::$_domainIDGroups)) { | |
202 | $optionValue->domain_id = CRM_Utils_Array::value('domain_id', $params, CRM_Core_Config::domainID()); | |
203 | } | |
204 | ||
205 | $optionValue->id = CRM_Utils_Array::value('optionValue', $ids); | |
206 | $optionValue->save(); | |
a4a33486 | 207 | CRM_Core_PseudoConstant::flush(); |
6a488035 TO |
208 | return $optionValue; |
209 | } | |
210 | ||
211 | /** | |
212 | * Function to delete Option Value | |
213 | * | |
214 | * @param int $optionGroupId Id of the Option Group to be deleted. | |
215 | * | |
216 | * @return boolean | |
217 | * | |
218 | * @access public | |
219 | * @static | |
220 | */ | |
221 | static function del($optionValueId) { | |
222 | $optionValue = new CRM_Core_DAO_OptionValue(); | |
223 | $optionValue->id = $optionValueId; | |
224 | if (self::updateRecords($optionValueId, CRM_Core_Action::DELETE)) { | |
a4a33486 | 225 | CRM_Core_PseudoConstant::flush(); |
6a488035 TO |
226 | return $optionValue->delete(); |
227 | } | |
228 | return FALSE; | |
229 | } | |
230 | ||
231 | /** | |
232 | * Function to retrieve activity type label and description | |
233 | * | |
234 | * @param int $activityTypeId activity type id | |
235 | * | |
236 | * @return array label and description | |
237 | * @static | |
238 | * @access public | |
239 | */ | |
240 | static function getActivityTypeDetails($activityTypeId) { | |
241 | $query = "SELECT civicrm_option_value.label, civicrm_option_value.description | |
242 | FROM civicrm_option_value | |
243 | LEFT JOIN civicrm_option_group ON ( civicrm_option_value.option_group_id = civicrm_option_group.id ) | |
244 | WHERE civicrm_option_group.name = 'activity_type' | |
245 | AND civicrm_option_value.value = {$activityTypeId} "; | |
246 | ||
247 | $dao = CRM_Core_DAO::executeQuery($query); | |
248 | ||
249 | $dao->fetch(); | |
250 | ||
251 | return array($dao->label, $dao->description); | |
252 | } | |
253 | ||
254 | /** | |
255 | * Get the Option Value title. | |
256 | * | |
257 | * @param int $id id of Option Value | |
258 | * | |
259 | * @return string title | |
260 | * | |
261 | * @access public | |
262 | * @static | |
263 | * | |
264 | */ | |
265 | public static function getTitle($id) { | |
266 | return CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $id, 'label'); | |
267 | } | |
268 | ||
269 | /** | |
270 | * updates contacts affected by the option value passed. | |
271 | * | |
272 | * @param Integer $optionValueId the option value id. | |
273 | * @param int $action the action describing whether prefix/suffix was UPDATED or DELETED | |
274 | * | |
275 | * @return void | |
276 | */ | |
277 | static function updateRecords(&$optionValueId, $action) { | |
278 | //finding group name | |
279 | $optionValue = new CRM_Core_DAO_OptionValue(); | |
280 | $optionValue->id = $optionValueId; | |
281 | $optionValue->find(TRUE); | |
282 | ||
283 | $optionGroup = new CRM_Core_DAO_OptionGroup(); | |
284 | $optionGroup->id = $optionValue->option_group_id; | |
285 | $optionGroup->find(TRUE); | |
286 | ||
287 | // group name | |
288 | $gName = $optionGroup->name; | |
289 | // value | |
290 | $value = $optionValue->value; | |
291 | ||
292 | // get the proper group name & affected field name | |
67744c4e | 293 | // todo: this may no longer be needed for individuals - check inputs |
6a488035 TO |
294 | $individuals = array( |
295 | 'gender' => 'gender_id', | |
296 | 'individual_prefix' => 'prefix_id', | |
297 | 'individual_suffix' => 'suffix_id', | |
298 | ); | |
299 | $contributions = array('payment_instrument' => 'payment_instrument_id'); | |
300 | $activities = array('activity_type' => 'activity_type_id'); | |
301 | $participant = array('participant_role' => 'role_id'); | |
302 | $eventType = array('event_type' => 'event_type_id'); | |
303 | $aclRole = array('acl_role' => 'acl_role_id'); | |
304 | ||
305 | $all = array_merge($individuals, $contributions, $activities, $participant, $eventType, $aclRole); | |
306 | $fieldName = ''; | |
307 | ||
308 | foreach ($all as $name => $id) { | |
309 | if ($gName == $name) { | |
310 | $fieldName = $id; | |
311 | } | |
312 | } | |
313 | if ($fieldName == '') { | |
314 | return TRUE; | |
315 | } | |
316 | ||
317 | if (array_key_exists($gName, $individuals)) { | |
318 | $contactDAO = new CRM_Contact_DAO_Contact(); | |
319 | ||
320 | $contactDAO->$fieldName = $value; | |
321 | $contactDAO->find(); | |
322 | ||
323 | while ($contactDAO->fetch()) { | |
324 | if ($action == CRM_Core_Action::DELETE) { | |
325 | $contact = new CRM_Contact_DAO_Contact(); | |
326 | $contact->id = $contactDAO->id; | |
327 | $contact->find(TRUE); | |
328 | ||
329 | // make sure dates doesn't get reset | |
330 | $contact->birth_date = CRM_Utils_Date::isoToMysql($contact->birth_date); | |
331 | $contact->deceased_date = CRM_Utils_Date::isoToMysql($contact->deceased_date); | |
332 | $contact->$fieldName = 'NULL'; | |
333 | $contact->save(); | |
334 | } | |
335 | } | |
336 | ||
337 | return TRUE; | |
338 | } | |
339 | ||
340 | if (array_key_exists($gName, $contributions)) { | |
341 | $contribution = new CRM_Contribute_DAO_Contribution(); | |
342 | $contribution->$fieldName = $value; | |
343 | $contribution->find(); | |
344 | while ($contribution->fetch()) { | |
345 | if ($action == CRM_Core_Action::DELETE) { | |
346 | $contribution->$fieldName = 'NULL'; | |
347 | $contribution->save(); | |
348 | } | |
349 | } | |
350 | return TRUE; | |
351 | } | |
352 | ||
353 | if (array_key_exists($gName, $activities)) { | |
354 | $activity = new CRM_Activity_DAO_Activity(); | |
355 | $activity->$fieldName = $value; | |
356 | $activity->find(); | |
357 | while ($activity->fetch()) { | |
358 | $activity->delete(); | |
359 | } | |
360 | return TRUE; | |
361 | } | |
362 | ||
363 | //delete participant role, type and event type option value | |
364 | if (array_key_exists($gName, $participant)) { | |
365 | $participantValue = new CRM_Event_DAO_Participant(); | |
366 | $participantValue->$fieldName = $value; | |
367 | if ($participantValue->find(TRUE)) { | |
368 | return FALSE; | |
369 | } | |
370 | return TRUE; | |
371 | } | |
372 | ||
373 | //delete event type option value | |
374 | if (array_key_exists($gName, $eventType)) { | |
375 | $event = new CRM_Event_DAO_Event(); | |
376 | $event->$fieldName = $value; | |
377 | if ($event->find(TRUE)) { | |
378 | return FALSE; | |
379 | } | |
380 | return TRUE; | |
381 | } | |
382 | ||
383 | //delete acl_role option value | |
384 | if (array_key_exists($gName, $aclRole)) { | |
385 | $entityRole = new CRM_ACL_DAO_EntityRole(); | |
386 | $entityRole->$fieldName = $value; | |
387 | ||
388 | $aclDAO = new CRM_ACL_DAO_ACL(); | |
389 | $aclDAO->entity_id = $value; | |
390 | if ($entityRole->find(TRUE) || $aclDAO->find(TRUE)) { | |
391 | return FALSE; | |
392 | } | |
393 | return TRUE; | |
394 | } | |
395 | } | |
396 | ||
397 | /** | |
398 | * updates options values weights. | |
399 | * | |
400 | * @param int $opGroupIde option group id. | |
401 | * @param array $opWeights options value , weight pair | |
402 | * | |
403 | * @return void | |
404 | * @access public | |
405 | * @static | |
406 | */ | |
407 | static function updateOptionWeights($opGroupId, $opWeights) { | |
408 | if (!is_array($opWeights) || empty($opWeights)) { | |
409 | return; | |
410 | } | |
411 | ||
412 | foreach ($opWeights as $opValue => $opWeight) { | |
413 | $optionValue = new CRM_Core_DAO_OptionValue(); | |
414 | $optionValue->option_group_id = $opGroupId; | |
415 | $optionValue->value = $opValue; | |
416 | if ($optionValue->find(TRUE)) { | |
417 | $optionValue->weight = $opWeight; | |
418 | $optionValue->save(); | |
419 | } | |
420 | $optionValue->free(); | |
421 | } | |
422 | } | |
423 | ||
424 | /** | |
425 | * Get the values of all option values given an option group ID. Store in system cache | |
426 | * Does not take any filtering arguments. The object is to avoid hitting the DB and retrieve | |
427 | * from memory | |
428 | * | |
429 | * @param int $optionGroupID the option group for which we want the values from | |
430 | * | |
431 | * @return array an array of array of values for this option group | |
432 | * @static | |
433 | * @public | |
434 | */ | |
435 | static function getOptionValuesArray($optionGroupID) { | |
436 | // check if we can get the field values from the system cache | |
437 | $cacheKey = "CRM_Core_BAO_OptionValue_OptionGroupID_{$optionGroupID}"; | |
438 | $cache = CRM_Utils_Cache::singleton(); | |
439 | $optionValues = $cache->get($cacheKey); | |
440 | if (empty($optionValues)) { | |
441 | $dao = new CRM_Core_DAO_OptionValue(); | |
442 | $dao->option_group_id = $optionGroupID; | |
443 | $dao->orderBy('weight ASC, label ASC'); | |
444 | $dao->find(); | |
445 | ||
446 | $optionValues = array(); | |
447 | while ($dao->fetch()) { | |
448 | $optionValues[$dao->id] = array(); | |
449 | CRM_Core_DAO::storeValues($dao, $optionValues[$dao->id]); | |
450 | } | |
451 | ||
452 | $cache->set($cacheKey, $optionValues); | |
453 | } | |
454 | ||
455 | return $optionValues; | |
456 | } | |
457 | ||
458 | /** | |
459 | * Get the values of all option values given an option group ID as a key => value pair | |
460 | * Use above cached function to make it super efficient | |
461 | * | |
462 | * @param int $optionGroupID the option group for which we want the values from | |
463 | * | |
464 | * @return array an associative array of label, value pairs | |
465 | * @static | |
466 | * @public | |
467 | */ | |
468 | static function getOptionValuesAssocArray($optionGroupID) { | |
469 | $optionValues = self::getOptionValuesArray($optionGroupID); | |
470 | ||
471 | $options = array(); | |
472 | foreach ($optionValues as $id => $value) { | |
473 | $options[$value['value']] = $value['label']; | |
474 | } | |
475 | return $options; | |
476 | } | |
477 | /** | |
478 | * Get the values of all option values given an option group Name as a key => value pair | |
479 | * Use above cached function to make it super efficient | |
480 | * | |
481 | * @param string $optionGroupName the option group name for which we want the values from | |
482 | * | |
483 | * @return array an associative array of label, value pairs | |
484 | * @static | |
485 | * @public | |
486 | */ | |
487 | static function getOptionValuesAssocArrayFromName($optionGroupName) { | |
488 | $dao = new CRM_Core_DAO_OptionGroup(); | |
489 | $dao->name = $optionGroupName; | |
490 | $dao->selectAdd(); | |
491 | $dao->selectAdd('id'); | |
492 | $dao->find(TRUE); | |
493 | $optionValues = self::getOptionValuesArray($dao->id); | |
494 | ||
495 | $options = array(); | |
496 | foreach ($optionValues as $id => $value) { | |
497 | $options[$value['value']] = $value['label']; | |
498 | } | |
499 | return $options; | |
500 | } | |
501 | ||
502 | } | |
503 |