Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
fa938177 | 6 | | Copyright CiviCRM LLC (c) 2004-2016 | |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
fa938177 | 31 | * @copyright CiviCRM LLC (c) 2004-2016 |
6a488035 TO |
32 | */ |
33 | class CRM_Core_BAO_OptionValue extends CRM_Core_DAO_OptionValue { | |
34 | ||
35 | /** | |
fe482240 | 36 | * Class constructor. |
6a488035 | 37 | */ |
00be9182 | 38 | public function __construct() { |
6a488035 TO |
39 | parent::__construct(); |
40 | } | |
77b97be7 | 41 | |
9fe6051a | 42 | /** |
8eedd10a | 43 | * Create option value. |
44 | * | |
45 | * Note that the create function calls 'add' but has more business logic. | |
77b97be7 | 46 | * |
6a0b768e TO |
47 | * @param array $params |
48 | * Input parameters. | |
77b97be7 EM |
49 | * |
50 | * @return object | |
51 | */ | |
00be9182 | 52 | public static function create($params) { |
9b873358 | 53 | if (empty($params['id'])) { |
c87bbced | 54 | self::setDefaults($params); |
55 | } | |
56 | $ids = array(); | |
a7488080 | 57 | if (!empty($params['id'])) { |
c87bbced | 58 | $ids = array('optionValue' => $params['id']); |
59 | } | |
d3e86119 | 60 | return CRM_Core_BAO_OptionValue::add($params, $ids); |
c87bbced | 61 | } |
353ffa53 | 62 | |
c87bbced | 63 | /** |
fe482240 | 64 | * Set default Parameters. |
c87bbced | 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 | /** |
fe482240 | 92 | * Get next available value. |
9fe6051a | 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', |
353ffa53 | 103 | array('option_group_id' => $params['option_group_id'])); |
1c6b7d30 | 104 | } |
105 | ||
106 | /** | |
0880a9d0 | 107 | * Get next available value. |
1c6b7d30 | 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) { |
353ffa53 TO |
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]+$'"); | |
121 | $bao->selectAdd('(ROUND(COALESCE(MAX(CONVERT(value, UNSIGNED)),0)) +1) as nextvalue'); | |
122 | $bao->find(TRUE); | |
123 | return $bao->nextvalue; | |
9fe6051a | 124 | } |
353ffa53 | 125 | |
6a488035 | 126 | /** |
fe482240 | 127 | * Fetch object based on array of properties. |
6a488035 | 128 | * |
6a0b768e TO |
129 | * @param array $params |
130 | * (reference ) an assoc array of name/value pairs. | |
131 | * @param array $defaults | |
132 | * (reference ) an assoc array to hold the flattened values. | |
6a488035 | 133 | * |
16b10e64 | 134 | * @return CRM_Core_BAO_OptionValue |
6a488035 | 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 | /** | |
fe482240 | 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 | 153 | * |
a6c01b45 | 154 | * @return Object |
b44e3f84 | 155 | * DAO object on success, null otherwise |
6a488035 | 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 | /** | |
fe482240 | 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 | 169 | * |
7c285037 | 170 | * @return CRM_Core_DAO_OptionValue |
6a488035 | 171 | */ |
0be95659 | 172 | public static function add(&$params, $ids = array()) { |
6a488035 | 173 | // CRM-10921: do not reset attributes to default if this is an update |
9fe6051a | 174 | //@todo consider if defaults are being set in the right place. 'dumb' defaults like |
175 | // these would be usefully set @ the api layer so they are visible to api users | |
176 | // complex defaults like the domain id below would make sense in the setDefauls function | |
177 | // but unclear what other ways this function is being used | |
a7488080 | 178 | if (empty($ids['optionValue'])) { |
6a488035 TO |
179 | $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE); |
180 | $params['is_default'] = CRM_Utils_Array::value('is_default', $params, FALSE); | |
181 | $params['is_optgroup'] = CRM_Utils_Array::value('is_optgroup', $params, FALSE); | |
182 | $params['filter'] = CRM_Utils_Array::value('filter', $params, FALSE); | |
183 | } | |
58eaa092 CW |
184 | // Update custom field data to reflect the new value |
185 | elseif (isset($params['value'])) { | |
186 | CRM_Core_BAO_CustomOption::updateValue($ids['optionValue'], $params['value']); | |
187 | } | |
6a488035 TO |
188 | |
189 | // action is taken depending upon the mode | |
190 | $optionValue = new CRM_Core_DAO_OptionValue(); | |
191 | $optionValue->copyValues($params); | |
192 | ||
a7488080 | 193 | if (!empty($params['is_default'])) { |
6a488035 TO |
194 | $query = 'UPDATE civicrm_option_value SET is_default = 0 WHERE option_group_id = %1'; |
195 | ||
196 | // tweak default reset, and allow multiple default within group. | |
197 | if ($resetDefaultFor = CRM_Utils_Array::value('reset_default_for', $params)) { | |
198 | if (is_array($resetDefaultFor)) { | |
199 | $colName = key($resetDefaultFor); | |
200 | $colVal = $resetDefaultFor[$colName]; | |
201 | $query .= " AND ( $colName IN ( $colVal ) )"; | |
202 | } | |
203 | } | |
204 | ||
205 | $p = array(1 => array($params['option_group_id'], 'Integer')); | |
206 | CRM_Core_DAO::executeQuery($query, $p); | |
207 | } | |
89ab5601 PJ |
208 | |
209 | // CRM-13814 : evalute option group id | |
210 | if (!array_key_exists('option_group_id', $params) && !empty($ids['optionValue'])) { | |
211 | $groupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', | |
212 | $ids['optionValue'], 'option_group_id', 'id' | |
213 | ); | |
214 | } | |
215 | else { | |
216 | $groupId = $params['option_group_id']; | |
217 | } | |
218 | ||
6a488035 | 219 | $groupName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', |
89ab5601 | 220 | $groupId, 'name', 'id' |
6a488035 TO |
221 | ); |
222 | if (in_array($groupName, CRM_Core_OptionGroup::$_domainIDGroups)) { | |
223 | $optionValue->domain_id = CRM_Utils_Array::value('domain_id', $params, CRM_Core_Config::domainID()); | |
224 | } | |
225 | ||
226 | $optionValue->id = CRM_Utils_Array::value('optionValue', $ids); | |
227 | $optionValue->save(); | |
a4a33486 | 228 | CRM_Core_PseudoConstant::flush(); |
6a488035 TO |
229 | return $optionValue; |
230 | } | |
231 | ||
232 | /** | |
fe482240 | 233 | * Delete Option Value. |
6a488035 | 234 | * |
c490a46a | 235 | * @param int $optionValueId |
6a488035 | 236 | * |
5c766a0b | 237 | * @return bool |
6a488035 | 238 | * |
6a488035 | 239 | */ |
00be9182 | 240 | public static function del($optionValueId) { |
6a488035 TO |
241 | $optionValue = new CRM_Core_DAO_OptionValue(); |
242 | $optionValue->id = $optionValueId; | |
a60c0bc8 SL |
243 | if (!$optionValue->find()) { |
244 | return FALSE; | |
245 | } | |
6a488035 | 246 | if (self::updateRecords($optionValueId, CRM_Core_Action::DELETE)) { |
a4a33486 | 247 | CRM_Core_PseudoConstant::flush(); |
a60c0bc8 SL |
248 | $optionValue->delete(); |
249 | return TRUE; | |
6a488035 TO |
250 | } |
251 | return FALSE; | |
252 | } | |
253 | ||
254 | /** | |
fe482240 | 255 | * Retrieve activity type label and description. |
6a488035 | 256 | * |
6a0b768e TO |
257 | * @param int $activityTypeId |
258 | * Activity type id. | |
6a488035 | 259 | * |
a6c01b45 CW |
260 | * @return array |
261 | * label and description | |
6a488035 | 262 | */ |
00be9182 | 263 | public static function getActivityTypeDetails($activityTypeId) { |
6a488035 TO |
264 | $query = "SELECT civicrm_option_value.label, civicrm_option_value.description |
265 | FROM civicrm_option_value | |
266 | LEFT JOIN civicrm_option_group ON ( civicrm_option_value.option_group_id = civicrm_option_group.id ) | |
267 | WHERE civicrm_option_group.name = 'activity_type' | |
268 | AND civicrm_option_value.value = {$activityTypeId} "; | |
269 | ||
270 | $dao = CRM_Core_DAO::executeQuery($query); | |
271 | ||
272 | $dao->fetch(); | |
273 | ||
274 | return array($dao->label, $dao->description); | |
275 | } | |
276 | ||
277 | /** | |
278 | * Get the Option Value title. | |
279 | * | |
6a0b768e TO |
280 | * @param int $id |
281 | * Id of Option Value. | |
6a488035 | 282 | * |
a6c01b45 CW |
283 | * @return string |
284 | * title | |
6a488035 | 285 | * |
6a488035 TO |
286 | */ |
287 | public static function getTitle($id) { | |
288 | return CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $id, 'label'); | |
289 | } | |
290 | ||
291 | /** | |
100fef9d | 292 | * Updates contacts affected by the option value passed. |
6a488035 | 293 | * |
6a0b768e TO |
294 | * @param int $optionValueId |
295 | * The option value id. | |
296 | * @param int $action | |
297 | * The action describing whether prefix/suffix was UPDATED or DELETED. | |
6a488035 | 298 | * |
7a6073fd | 299 | * @return bool |
6a488035 | 300 | */ |
00be9182 | 301 | public static function updateRecords(&$optionValueId, $action) { |
6a488035 TO |
302 | //finding group name |
303 | $optionValue = new CRM_Core_DAO_OptionValue(); | |
304 | $optionValue->id = $optionValueId; | |
305 | $optionValue->find(TRUE); | |
306 | ||
307 | $optionGroup = new CRM_Core_DAO_OptionGroup(); | |
308 | $optionGroup->id = $optionValue->option_group_id; | |
309 | $optionGroup->find(TRUE); | |
310 | ||
311 | // group name | |
312 | $gName = $optionGroup->name; | |
313 | // value | |
314 | $value = $optionValue->value; | |
315 | ||
316 | // get the proper group name & affected field name | |
67744c4e | 317 | // todo: this may no longer be needed for individuals - check inputs |
6a488035 TO |
318 | $individuals = array( |
319 | 'gender' => 'gender_id', | |
320 | 'individual_prefix' => 'prefix_id', | |
321 | 'individual_suffix' => 'suffix_id', | |
353ffa53 TO |
322 | 'communication_style' => 'communication_style_id', |
323 | // Not only Individuals -- but the code seems to be generic for all contact types, despite the naming... | |
6a488035 TO |
324 | ); |
325 | $contributions = array('payment_instrument' => 'payment_instrument_id'); | |
353ffa53 TO |
326 | $activities = array('activity_type' => 'activity_type_id'); |
327 | $participant = array('participant_role' => 'role_id'); | |
328 | $eventType = array('event_type' => 'event_type_id'); | |
329 | $aclRole = array('acl_role' => 'acl_role_id'); | |
6a488035 TO |
330 | |
331 | $all = array_merge($individuals, $contributions, $activities, $participant, $eventType, $aclRole); | |
332 | $fieldName = ''; | |
333 | ||
334 | foreach ($all as $name => $id) { | |
335 | if ($gName == $name) { | |
336 | $fieldName = $id; | |
337 | } | |
338 | } | |
339 | if ($fieldName == '') { | |
340 | return TRUE; | |
341 | } | |
342 | ||
343 | if (array_key_exists($gName, $individuals)) { | |
344 | $contactDAO = new CRM_Contact_DAO_Contact(); | |
345 | ||
346 | $contactDAO->$fieldName = $value; | |
347 | $contactDAO->find(); | |
348 | ||
349 | while ($contactDAO->fetch()) { | |
350 | if ($action == CRM_Core_Action::DELETE) { | |
351 | $contact = new CRM_Contact_DAO_Contact(); | |
352 | $contact->id = $contactDAO->id; | |
353 | $contact->find(TRUE); | |
354 | ||
355 | // make sure dates doesn't get reset | |
356 | $contact->birth_date = CRM_Utils_Date::isoToMysql($contact->birth_date); | |
357 | $contact->deceased_date = CRM_Utils_Date::isoToMysql($contact->deceased_date); | |
358 | $contact->$fieldName = 'NULL'; | |
359 | $contact->save(); | |
360 | } | |
361 | } | |
362 | ||
363 | return TRUE; | |
364 | } | |
365 | ||
366 | if (array_key_exists($gName, $contributions)) { | |
367 | $contribution = new CRM_Contribute_DAO_Contribution(); | |
368 | $contribution->$fieldName = $value; | |
369 | $contribution->find(); | |
370 | while ($contribution->fetch()) { | |
371 | if ($action == CRM_Core_Action::DELETE) { | |
372 | $contribution->$fieldName = 'NULL'; | |
373 | $contribution->save(); | |
374 | } | |
375 | } | |
376 | return TRUE; | |
377 | } | |
378 | ||
379 | if (array_key_exists($gName, $activities)) { | |
380 | $activity = new CRM_Activity_DAO_Activity(); | |
381 | $activity->$fieldName = $value; | |
382 | $activity->find(); | |
383 | while ($activity->fetch()) { | |
384 | $activity->delete(); | |
385 | } | |
386 | return TRUE; | |
387 | } | |
388 | ||
389 | //delete participant role, type and event type option value | |
390 | if (array_key_exists($gName, $participant)) { | |
391 | $participantValue = new CRM_Event_DAO_Participant(); | |
392 | $participantValue->$fieldName = $value; | |
393 | if ($participantValue->find(TRUE)) { | |
394 | return FALSE; | |
395 | } | |
396 | return TRUE; | |
397 | } | |
398 | ||
399 | //delete event type option value | |
400 | if (array_key_exists($gName, $eventType)) { | |
401 | $event = new CRM_Event_DAO_Event(); | |
402 | $event->$fieldName = $value; | |
403 | if ($event->find(TRUE)) { | |
404 | return FALSE; | |
405 | } | |
406 | return TRUE; | |
407 | } | |
408 | ||
409 | //delete acl_role option value | |
410 | if (array_key_exists($gName, $aclRole)) { | |
411 | $entityRole = new CRM_ACL_DAO_EntityRole(); | |
412 | $entityRole->$fieldName = $value; | |
413 | ||
414 | $aclDAO = new CRM_ACL_DAO_ACL(); | |
415 | $aclDAO->entity_id = $value; | |
416 | if ($entityRole->find(TRUE) || $aclDAO->find(TRUE)) { | |
417 | return FALSE; | |
418 | } | |
419 | return TRUE; | |
420 | } | |
421 | } | |
422 | ||
423 | /** | |
100fef9d | 424 | * Updates options values weights. |
6a488035 | 425 | * |
c490a46a | 426 | * @param int $opGroupId |
6a0b768e TO |
427 | * @param array $opWeights |
428 | * Options value , weight pair. | |
6a488035 | 429 | */ |
00be9182 | 430 | public static function updateOptionWeights($opGroupId, $opWeights) { |
6a488035 TO |
431 | if (!is_array($opWeights) || empty($opWeights)) { |
432 | return; | |
433 | } | |
434 | ||
435 | foreach ($opWeights as $opValue => $opWeight) { | |
436 | $optionValue = new CRM_Core_DAO_OptionValue(); | |
437 | $optionValue->option_group_id = $opGroupId; | |
438 | $optionValue->value = $opValue; | |
439 | if ($optionValue->find(TRUE)) { | |
440 | $optionValue->weight = $opWeight; | |
441 | $optionValue->save(); | |
442 | } | |
443 | $optionValue->free(); | |
444 | } | |
445 | } | |
446 | ||
447 | /** | |
448 | * Get the values of all option values given an option group ID. Store in system cache | |
449 | * Does not take any filtering arguments. The object is to avoid hitting the DB and retrieve | |
450 | * from memory | |
451 | * | |
6a0b768e TO |
452 | * @param int $optionGroupID |
453 | * The option group for which we want the values from. | |
6a488035 | 454 | * |
a6c01b45 CW |
455 | * @return array |
456 | * an array of array of values for this option group | |
6a488035 | 457 | */ |
00be9182 | 458 | public static function getOptionValuesArray($optionGroupID) { |
6a488035 | 459 | // check if we can get the field values from the system cache |
353ffa53 TO |
460 | $cacheKey = "CRM_Core_BAO_OptionValue_OptionGroupID_{$optionGroupID}"; |
461 | $cache = CRM_Utils_Cache::singleton(); | |
6a488035 TO |
462 | $optionValues = $cache->get($cacheKey); |
463 | if (empty($optionValues)) { | |
464 | $dao = new CRM_Core_DAO_OptionValue(); | |
465 | $dao->option_group_id = $optionGroupID; | |
466 | $dao->orderBy('weight ASC, label ASC'); | |
467 | $dao->find(); | |
468 | ||
469 | $optionValues = array(); | |
470 | while ($dao->fetch()) { | |
471 | $optionValues[$dao->id] = array(); | |
472 | CRM_Core_DAO::storeValues($dao, $optionValues[$dao->id]); | |
473 | } | |
474 | ||
475 | $cache->set($cacheKey, $optionValues); | |
476 | } | |
477 | ||
478 | return $optionValues; | |
479 | } | |
480 | ||
481 | /** | |
482 | * Get the values of all option values given an option group ID as a key => value pair | |
483 | * Use above cached function to make it super efficient | |
484 | * | |
6a0b768e TO |
485 | * @param int $optionGroupID |
486 | * The option group for which we want the values from. | |
6a488035 | 487 | * |
a6c01b45 CW |
488 | * @return array |
489 | * an associative array of label, value pairs | |
6a488035 | 490 | */ |
00be9182 | 491 | public static function getOptionValuesAssocArray($optionGroupID) { |
6a488035 TO |
492 | $optionValues = self::getOptionValuesArray($optionGroupID); |
493 | ||
494 | $options = array(); | |
495 | foreach ($optionValues as $id => $value) { | |
496 | $options[$value['value']] = $value['label']; | |
497 | } | |
498 | return $options; | |
499 | } | |
353ffa53 | 500 | |
6a488035 TO |
501 | /** |
502 | * Get the values of all option values given an option group Name as a key => value pair | |
503 | * Use above cached function to make it super efficient | |
504 | * | |
6a0b768e TO |
505 | * @param string $optionGroupName |
506 | * The option group name for which we want the values from. | |
6a488035 | 507 | * |
a6c01b45 CW |
508 | * @return array |
509 | * an associative array of label, value pairs | |
6a488035 | 510 | */ |
00be9182 | 511 | public static function getOptionValuesAssocArrayFromName($optionGroupName) { |
6a488035 TO |
512 | $dao = new CRM_Core_DAO_OptionGroup(); |
513 | $dao->name = $optionGroupName; | |
514 | $dao->selectAdd(); | |
515 | $dao->selectAdd('id'); | |
516 | $dao->find(TRUE); | |
517 | $optionValues = self::getOptionValuesArray($dao->id); | |
518 | ||
519 | $options = array(); | |
520 | foreach ($optionValues as $id => $value) { | |
521 | $options[$value['value']] = $value['label']; | |
522 | } | |
523 | return $options; | |
524 | } | |
525 | ||
6f408d71 | 526 | /** |
527 | * Ensure an option value exists. | |
528 | * | |
529 | * This function is intended to be called from the upgrade script to ensure | |
530 | * that an option value exists, without hitting an error if it already exists. | |
531 | * | |
532 | * This is sympathetic to sites who might pre-add it. | |
533 | */ | |
534 | public static function ensureOptionValueExists($params) { | |
535 | $existingValues = civicrm_api3('OptionValue', 'get', array( | |
bf87703e | 536 | 'option_group_id' => $params['option_group_id'], |
6f408d71 | 537 | 'name' => $params['name'], |
34273b2a | 538 | 'return' => 'id', |
6f408d71 | 539 | )); |
540 | if (!$existingValues['count']) { | |
541 | civicrm_api3('OptionValue', 'create', $params); | |
542 | } | |
543 | } | |
544 | ||
6a488035 | 545 | } |