Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
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 | 39 | */ |
00be9182 | 40 | public function __construct() { |
6a488035 TO |
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 | * | |
6a0b768e TO |
48 | * @param array $params |
49 | * Input parameters. | |
77b97be7 EM |
50 | * |
51 | * @return object | |
52 | */ | |
00be9182 | 53 | public static function create($params) { |
9b873358 | 54 | if (empty($params['id'])) { |
c87bbced | 55 | self::setDefaults($params); |
56 | } | |
57 | $ids = array(); | |
a7488080 | 58 | if (!empty($params['id'])) { |
c87bbced | 59 | $ids = array('optionValue' => $params['id']); |
60 | } | |
d3e86119 | 61 | return CRM_Core_BAO_OptionValue::add($params, $ids); |
c87bbced | 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 | */ |
9b873358 TO |
76 | public static function setDefaults(&$params) { |
77 | if (CRM_Utils_Array::value('label', $params, NULL) === NULL) { | |
c87bbced | 78 | $params['label'] = $params['name']; |
79 | } | |
9b873358 | 80 | if (CRM_Utils_Array::value('name', $params, NULL) === NULL) { |
c87bbced | 81 | $params['name'] = $params['label']; |
82 | } | |
9b873358 | 83 | if (CRM_Utils_Array::value('weight', $params, NULL) === NULL) { |
983e888c | 84 | $params['weight'] = self::getDefaultWeight($params); |
c87bbced | 85 | } |
9b873358 | 86 | if (CRM_Utils_Array::value('value', $params, NULL) === NULL) { |
1c6b7d30 | 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 | */ |
9b873358 | 101 | public static function getDefaultWeight($params) { |
1c6b7d30 | 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 | */ | |
9b873358 | 113 | public static function getDefaultValue($params) { |
9fe6051a | 114 | $bao = new CRM_Core_BAO_OptionValue(); |
115 | $bao->option_group_id = $params['option_group_id']; | |
9b873358 | 116 | if (isset($params['domain_id'])) { |
9fe6051a | 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 | 125 | /** |
c490a46a | 126 | * Fetch object based on array of properties |
6a488035 | 127 | * |
6a0b768e TO |
128 | * @param array $params |
129 | * (reference ) an assoc array of name/value pairs. | |
130 | * @param array $defaults | |
131 | * (reference ) an assoc array to hold the flattened values. | |
6a488035 | 132 | * |
c490a46a | 133 | * @return CRM_Core_BAO_OptionValue object |
6a488035 TO |
134 | * @static |
135 | */ | |
00be9182 | 136 | public static function retrieve(&$params, &$defaults) { |
6a488035 TO |
137 | $optionValue = new CRM_Core_DAO_OptionValue(); |
138 | $optionValue->copyValues($params); | |
139 | if ($optionValue->find(TRUE)) { | |
140 | CRM_Core_DAO::storeValues($optionValue, $defaults); | |
141 | return $optionValue; | |
142 | } | |
143 | return NULL; | |
144 | } | |
145 | ||
146 | /** | |
100fef9d | 147 | * Update the is_active flag in the db |
6a488035 | 148 | * |
6a0b768e TO |
149 | * @param int $id |
150 | * Id of the database record. | |
151 | * @param bool $is_active | |
152 | * Value we want to set the is_active field. | |
6a488035 TO |
153 | * |
154 | * @return Object DAO object on sucess, null otherwise | |
155 | * @static | |
156 | */ | |
00be9182 | 157 | public static function setIsActive($id, $is_active) { |
6a488035 TO |
158 | return CRM_Core_DAO::setFieldValue('CRM_Core_DAO_OptionValue', $id, 'is_active', $is_active); |
159 | } | |
160 | ||
161 | /** | |
100fef9d | 162 | * Add an Option Value |
6a488035 | 163 | * |
6a0b768e TO |
164 | * @param array $params |
165 | * Reference array contains the values submitted by the form. | |
166 | * @param array $ids | |
167 | * Reference array contains the id. | |
6a488035 | 168 | * |
6a488035 TO |
169 | * @static |
170 | * | |
7c285037 | 171 | * @return CRM_Core_DAO_OptionValue |
6a488035 | 172 | */ |
00be9182 | 173 | public static function add(&$params, &$ids) { |
6a488035 | 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 | /** | |
100fef9d | 230 | * Delete Option Value |
6a488035 | 231 | * |
c490a46a | 232 | * @param int $optionValueId |
6a488035 TO |
233 | * |
234 | * @return boolean | |
235 | * | |
6a488035 TO |
236 | * @static |
237 | */ | |
00be9182 | 238 | public static function del($optionValueId) { |
6a488035 TO |
239 | $optionValue = new CRM_Core_DAO_OptionValue(); |
240 | $optionValue->id = $optionValueId; | |
241 | if (self::updateRecords($optionValueId, CRM_Core_Action::DELETE)) { | |
a4a33486 | 242 | CRM_Core_PseudoConstant::flush(); |
6a488035 TO |
243 | return $optionValue->delete(); |
244 | } | |
245 | return FALSE; | |
246 | } | |
247 | ||
248 | /** | |
100fef9d | 249 | * Retrieve activity type label and description |
6a488035 | 250 | * |
6a0b768e TO |
251 | * @param int $activityTypeId |
252 | * Activity type id. | |
6a488035 TO |
253 | * |
254 | * @return array label and description | |
255 | * @static | |
6a488035 | 256 | */ |
00be9182 | 257 | public static function getActivityTypeDetails($activityTypeId) { |
6a488035 TO |
258 | $query = "SELECT civicrm_option_value.label, civicrm_option_value.description |
259 | FROM civicrm_option_value | |
260 | LEFT JOIN civicrm_option_group ON ( civicrm_option_value.option_group_id = civicrm_option_group.id ) | |
261 | WHERE civicrm_option_group.name = 'activity_type' | |
262 | AND civicrm_option_value.value = {$activityTypeId} "; | |
263 | ||
264 | $dao = CRM_Core_DAO::executeQuery($query); | |
265 | ||
266 | $dao->fetch(); | |
267 | ||
268 | return array($dao->label, $dao->description); | |
269 | } | |
270 | ||
271 | /** | |
272 | * Get the Option Value title. | |
273 | * | |
6a0b768e TO |
274 | * @param int $id |
275 | * Id of Option Value. | |
6a488035 TO |
276 | * |
277 | * @return string title | |
278 | * | |
6a488035 TO |
279 | * @static |
280 | * | |
281 | */ | |
282 | public static function getTitle($id) { | |
283 | return CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $id, 'label'); | |
284 | } | |
285 | ||
286 | /** | |
100fef9d | 287 | * Updates contacts affected by the option value passed. |
6a488035 | 288 | * |
6a0b768e TO |
289 | * @param int $optionValueId |
290 | * The option value id. | |
291 | * @param int $action | |
292 | * The action describing whether prefix/suffix was UPDATED or DELETED. | |
6a488035 | 293 | * |
7a6073fd | 294 | * @return bool |
6a488035 | 295 | */ |
00be9182 | 296 | public static function updateRecords(&$optionValueId, $action) { |
6a488035 TO |
297 | //finding group name |
298 | $optionValue = new CRM_Core_DAO_OptionValue(); | |
299 | $optionValue->id = $optionValueId; | |
300 | $optionValue->find(TRUE); | |
301 | ||
302 | $optionGroup = new CRM_Core_DAO_OptionGroup(); | |
303 | $optionGroup->id = $optionValue->option_group_id; | |
304 | $optionGroup->find(TRUE); | |
305 | ||
306 | // group name | |
307 | $gName = $optionGroup->name; | |
308 | // value | |
309 | $value = $optionValue->value; | |
310 | ||
311 | // get the proper group name & affected field name | |
67744c4e | 312 | // todo: this may no longer be needed for individuals - check inputs |
6a488035 TO |
313 | $individuals = array( |
314 | 'gender' => 'gender_id', | |
315 | 'individual_prefix' => 'prefix_id', | |
316 | 'individual_suffix' => 'suffix_id', | |
aa62b355 | 317 | 'communication_style' => 'communication_style_id', // Not only Individuals -- but the code seems to be generic for all contact types, despite the naming... |
6a488035 TO |
318 | ); |
319 | $contributions = array('payment_instrument' => 'payment_instrument_id'); | |
320 | $activities = array('activity_type' => 'activity_type_id'); | |
321 | $participant = array('participant_role' => 'role_id'); | |
322 | $eventType = array('event_type' => 'event_type_id'); | |
323 | $aclRole = array('acl_role' => 'acl_role_id'); | |
324 | ||
325 | $all = array_merge($individuals, $contributions, $activities, $participant, $eventType, $aclRole); | |
326 | $fieldName = ''; | |
327 | ||
328 | foreach ($all as $name => $id) { | |
329 | if ($gName == $name) { | |
330 | $fieldName = $id; | |
331 | } | |
332 | } | |
333 | if ($fieldName == '') { | |
334 | return TRUE; | |
335 | } | |
336 | ||
337 | if (array_key_exists($gName, $individuals)) { | |
338 | $contactDAO = new CRM_Contact_DAO_Contact(); | |
339 | ||
340 | $contactDAO->$fieldName = $value; | |
341 | $contactDAO->find(); | |
342 | ||
343 | while ($contactDAO->fetch()) { | |
344 | if ($action == CRM_Core_Action::DELETE) { | |
345 | $contact = new CRM_Contact_DAO_Contact(); | |
346 | $contact->id = $contactDAO->id; | |
347 | $contact->find(TRUE); | |
348 | ||
349 | // make sure dates doesn't get reset | |
350 | $contact->birth_date = CRM_Utils_Date::isoToMysql($contact->birth_date); | |
351 | $contact->deceased_date = CRM_Utils_Date::isoToMysql($contact->deceased_date); | |
352 | $contact->$fieldName = 'NULL'; | |
353 | $contact->save(); | |
354 | } | |
355 | } | |
356 | ||
357 | return TRUE; | |
358 | } | |
359 | ||
360 | if (array_key_exists($gName, $contributions)) { | |
361 | $contribution = new CRM_Contribute_DAO_Contribution(); | |
362 | $contribution->$fieldName = $value; | |
363 | $contribution->find(); | |
364 | while ($contribution->fetch()) { | |
365 | if ($action == CRM_Core_Action::DELETE) { | |
366 | $contribution->$fieldName = 'NULL'; | |
367 | $contribution->save(); | |
368 | } | |
369 | } | |
370 | return TRUE; | |
371 | } | |
372 | ||
373 | if (array_key_exists($gName, $activities)) { | |
374 | $activity = new CRM_Activity_DAO_Activity(); | |
375 | $activity->$fieldName = $value; | |
376 | $activity->find(); | |
377 | while ($activity->fetch()) { | |
378 | $activity->delete(); | |
379 | } | |
380 | return TRUE; | |
381 | } | |
382 | ||
383 | //delete participant role, type and event type option value | |
384 | if (array_key_exists($gName, $participant)) { | |
385 | $participantValue = new CRM_Event_DAO_Participant(); | |
386 | $participantValue->$fieldName = $value; | |
387 | if ($participantValue->find(TRUE)) { | |
388 | return FALSE; | |
389 | } | |
390 | return TRUE; | |
391 | } | |
392 | ||
393 | //delete event type option value | |
394 | if (array_key_exists($gName, $eventType)) { | |
395 | $event = new CRM_Event_DAO_Event(); | |
396 | $event->$fieldName = $value; | |
397 | if ($event->find(TRUE)) { | |
398 | return FALSE; | |
399 | } | |
400 | return TRUE; | |
401 | } | |
402 | ||
403 | //delete acl_role option value | |
404 | if (array_key_exists($gName, $aclRole)) { | |
405 | $entityRole = new CRM_ACL_DAO_EntityRole(); | |
406 | $entityRole->$fieldName = $value; | |
407 | ||
408 | $aclDAO = new CRM_ACL_DAO_ACL(); | |
409 | $aclDAO->entity_id = $value; | |
410 | if ($entityRole->find(TRUE) || $aclDAO->find(TRUE)) { | |
411 | return FALSE; | |
412 | } | |
413 | return TRUE; | |
414 | } | |
415 | } | |
416 | ||
417 | /** | |
100fef9d | 418 | * Updates options values weights. |
6a488035 | 419 | * |
c490a46a | 420 | * @param int $opGroupId |
6a0b768e TO |
421 | * @param array $opWeights |
422 | * Options value , weight pair. | |
6a488035 TO |
423 | * |
424 | * @return void | |
6a488035 TO |
425 | * @static |
426 | */ | |
00be9182 | 427 | public static function updateOptionWeights($opGroupId, $opWeights) { |
6a488035 TO |
428 | if (!is_array($opWeights) || empty($opWeights)) { |
429 | return; | |
430 | } | |
431 | ||
432 | foreach ($opWeights as $opValue => $opWeight) { | |
433 | $optionValue = new CRM_Core_DAO_OptionValue(); | |
434 | $optionValue->option_group_id = $opGroupId; | |
435 | $optionValue->value = $opValue; | |
436 | if ($optionValue->find(TRUE)) { | |
437 | $optionValue->weight = $opWeight; | |
438 | $optionValue->save(); | |
439 | } | |
440 | $optionValue->free(); | |
441 | } | |
442 | } | |
443 | ||
444 | /** | |
445 | * Get the values of all option values given an option group ID. Store in system cache | |
446 | * Does not take any filtering arguments. The object is to avoid hitting the DB and retrieve | |
447 | * from memory | |
448 | * | |
6a0b768e TO |
449 | * @param int $optionGroupID |
450 | * The option group for which we want the values from. | |
6a488035 TO |
451 | * |
452 | * @return array an array of array of values for this option group | |
453 | * @static | |
6a488035 | 454 | */ |
00be9182 | 455 | public static function getOptionValuesArray($optionGroupID) { |
6a488035 TO |
456 | // check if we can get the field values from the system cache |
457 | $cacheKey = "CRM_Core_BAO_OptionValue_OptionGroupID_{$optionGroupID}"; | |
458 | $cache = CRM_Utils_Cache::singleton(); | |
459 | $optionValues = $cache->get($cacheKey); | |
460 | if (empty($optionValues)) { | |
461 | $dao = new CRM_Core_DAO_OptionValue(); | |
462 | $dao->option_group_id = $optionGroupID; | |
463 | $dao->orderBy('weight ASC, label ASC'); | |
464 | $dao->find(); | |
465 | ||
466 | $optionValues = array(); | |
467 | while ($dao->fetch()) { | |
468 | $optionValues[$dao->id] = array(); | |
469 | CRM_Core_DAO::storeValues($dao, $optionValues[$dao->id]); | |
470 | } | |
471 | ||
472 | $cache->set($cacheKey, $optionValues); | |
473 | } | |
474 | ||
475 | return $optionValues; | |
476 | } | |
477 | ||
478 | /** | |
479 | * Get the values of all option values given an option group ID as a key => value pair | |
480 | * Use above cached function to make it super efficient | |
481 | * | |
6a0b768e TO |
482 | * @param int $optionGroupID |
483 | * The option group for which we want the values from. | |
6a488035 TO |
484 | * |
485 | * @return array an associative array of label, value pairs | |
486 | * @static | |
6a488035 | 487 | */ |
00be9182 | 488 | public static function getOptionValuesAssocArray($optionGroupID) { |
6a488035 TO |
489 | $optionValues = self::getOptionValuesArray($optionGroupID); |
490 | ||
491 | $options = array(); | |
492 | foreach ($optionValues as $id => $value) { | |
493 | $options[$value['value']] = $value['label']; | |
494 | } | |
495 | return $options; | |
496 | } | |
497 | /** | |
498 | * Get the values of all option values given an option group Name as a key => value pair | |
499 | * Use above cached function to make it super efficient | |
500 | * | |
6a0b768e TO |
501 | * @param string $optionGroupName |
502 | * The option group name for which we want the values from. | |
6a488035 TO |
503 | * |
504 | * @return array an associative array of label, value pairs | |
505 | * @static | |
6a488035 | 506 | */ |
00be9182 | 507 | public static function getOptionValuesAssocArrayFromName($optionGroupName) { |
6a488035 TO |
508 | $dao = new CRM_Core_DAO_OptionGroup(); |
509 | $dao->name = $optionGroupName; | |
510 | $dao->selectAdd(); | |
511 | $dao->selectAdd('id'); | |
512 | $dao->find(TRUE); | |
513 | $optionValues = self::getOptionValuesArray($dao->id); | |
514 | ||
515 | $options = array(); | |
516 | foreach ($optionValues as $id => $value) { | |
517 | $options[$value['value']] = $value['label']; | |
518 | } | |
519 | return $options; | |
520 | } | |
521 | ||
522 | } |