Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | class CRM_Core_BAO_OptionValue extends CRM_Core_DAO_OptionValue { | |
18 | ||
19 | /** | |
fe482240 | 20 | * Class constructor. |
6a488035 | 21 | */ |
00be9182 | 22 | public function __construct() { |
6a488035 TO |
23 | parent::__construct(); |
24 | } | |
77b97be7 | 25 | |
9fe6051a | 26 | /** |
8eedd10a | 27 | * Create option value. |
28 | * | |
29 | * Note that the create function calls 'add' but has more business logic. | |
77b97be7 | 30 | * |
6a0b768e TO |
31 | * @param array $params |
32 | * Input parameters. | |
77b97be7 EM |
33 | * |
34 | * @return object | |
35 | */ | |
00be9182 | 36 | public static function create($params) { |
9b873358 | 37 | if (empty($params['id'])) { |
c87bbced | 38 | self::setDefaults($params); |
39 | } | |
be2fb01f | 40 | $ids = []; |
a7488080 | 41 | if (!empty($params['id'])) { |
be2fb01f | 42 | $ids = ['optionValue' => $params['id']]; |
c87bbced | 43 | } |
d3e86119 | 44 | return CRM_Core_BAO_OptionValue::add($params, $ids); |
c87bbced | 45 | } |
353ffa53 | 46 | |
c87bbced | 47 | /** |
fe482240 | 48 | * Set default Parameters. |
c87bbced | 49 | * This functions sets default parameters if not set: |
50 | * - name & label are set to each other as required (it might make more sense for one | |
51 | * to be required but this would mean a change to the api level) | |
9fe6051a | 52 | * - weight & value will be set to their respective option groups next values |
53 | * if nothing is passed in. | |
54 | * | |
55 | * Note this function does not check for presence of $params['id'] so should only be called | |
56 | * if 'id' is not present | |
c87bbced | 57 | * |
9fe6051a | 58 | * @param array $params |
c87bbced | 59 | */ |
9b873358 TO |
60 | public static function setDefaults(&$params) { |
61 | if (CRM_Utils_Array::value('label', $params, NULL) === NULL) { | |
c87bbced | 62 | $params['label'] = $params['name']; |
63 | } | |
9b873358 | 64 | if (CRM_Utils_Array::value('name', $params, NULL) === NULL) { |
c87bbced | 65 | $params['name'] = $params['label']; |
66 | } | |
9b873358 | 67 | if (CRM_Utils_Array::value('weight', $params, NULL) === NULL) { |
983e888c | 68 | $params['weight'] = self::getDefaultWeight($params); |
c87bbced | 69 | } |
9b873358 | 70 | if (CRM_Utils_Array::value('value', $params, NULL) === NULL) { |
1c6b7d30 | 71 | $params['value'] = self::getDefaultValue($params); |
c87bbced | 72 | } |
73 | } | |
77b97be7 | 74 | |
9fe6051a | 75 | /** |
fe482240 | 76 | * Get next available value. |
9fe6051a | 77 | * We will take the highest numeric value (or 0 if no numeric values exist) |
78 | * and add one. The calling function is responsible for any | |
79 | * more complex decision making | |
77b97be7 | 80 | * |
9fe6051a | 81 | * @param array $params |
77b97be7 EM |
82 | * |
83 | * @return int | |
9fe6051a | 84 | */ |
9b873358 | 85 | public static function getDefaultWeight($params) { |
1c6b7d30 | 86 | return (int) CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_OptionValue', |
be2fb01f | 87 | ['option_group_id' => $params['option_group_id']]); |
1c6b7d30 | 88 | } |
89 | ||
90 | /** | |
0880a9d0 | 91 | * Get next available value. |
1c6b7d30 | 92 | * We will take the highest numeric value (or 0 if no numeric values exist) |
93 | * and add one. The calling function is responsible for any | |
94 | * more complex decision making | |
95 | * @param array $params | |
96 | */ | |
9b873358 | 97 | public static function getDefaultValue($params) { |
353ffa53 TO |
98 | $bao = new CRM_Core_BAO_OptionValue(); |
99 | $bao->option_group_id = $params['option_group_id']; | |
100 | if (isset($params['domain_id'])) { | |
101 | $bao->domain_id = $params['domain_id']; | |
102 | } | |
103 | $bao->selectAdd(); | |
104 | $bao->whereAdd("value REGEXP '^[0-9]+$'"); | |
105 | $bao->selectAdd('(ROUND(COALESCE(MAX(CONVERT(value, UNSIGNED)),0)) +1) as nextvalue'); | |
106 | $bao->find(TRUE); | |
107 | return $bao->nextvalue; | |
9fe6051a | 108 | } |
353ffa53 | 109 | |
6a488035 | 110 | /** |
fe482240 | 111 | * Fetch object based on array of properties. |
6a488035 | 112 | * |
6a0b768e TO |
113 | * @param array $params |
114 | * (reference ) an assoc array of name/value pairs. | |
115 | * @param array $defaults | |
116 | * (reference ) an assoc array to hold the flattened values. | |
6a488035 | 117 | * |
16b10e64 | 118 | * @return CRM_Core_BAO_OptionValue |
6a488035 | 119 | */ |
00be9182 | 120 | public static function retrieve(&$params, &$defaults) { |
6a488035 TO |
121 | $optionValue = new CRM_Core_DAO_OptionValue(); |
122 | $optionValue->copyValues($params); | |
123 | if ($optionValue->find(TRUE)) { | |
124 | CRM_Core_DAO::storeValues($optionValue, $defaults); | |
125 | return $optionValue; | |
126 | } | |
127 | return NULL; | |
128 | } | |
129 | ||
130 | /** | |
fe482240 | 131 | * Update the is_active flag in the db. |
6a488035 | 132 | * |
6a0b768e TO |
133 | * @param int $id |
134 | * Id of the database record. | |
135 | * @param bool $is_active | |
136 | * Value we want to set the is_active field. | |
6a488035 | 137 | * |
8a4fede3 | 138 | * @return bool |
139 | * true if we found and updated the object, else false | |
6a488035 | 140 | */ |
00be9182 | 141 | public static function setIsActive($id, $is_active) { |
6a488035 TO |
142 | return CRM_Core_DAO::setFieldValue('CRM_Core_DAO_OptionValue', $id, 'is_active', $is_active); |
143 | } | |
144 | ||
145 | /** | |
fe482240 | 146 | * Add an Option Value. |
6a488035 | 147 | * |
6a0b768e TO |
148 | * @param array $params |
149 | * Reference array contains the values submitted by the form. | |
150 | * @param array $ids | |
d0488daf | 151 | * deprecated Reference array contains the id. |
6a488035 | 152 | * |
d0488daf | 153 | * @return \CRM_Core_DAO_OptionValue |
154 | * @throws \CRM_Core_Exception | |
6a488035 | 155 | */ |
be2fb01f | 156 | public static function add(&$params, $ids = []) { |
d0488daf | 157 | $id = CRM_Utils_Array::value('id', $params, CRM_Utils_Array::value('optionValue', $ids)); |
6a488035 | 158 | // CRM-10921: do not reset attributes to default if this is an update |
9fe6051a | 159 | //@todo consider if defaults are being set in the right place. 'dumb' defaults like |
160 | // these would be usefully set @ the api layer so they are visible to api users | |
161 | // complex defaults like the domain id below would make sense in the setDefauls function | |
162 | // but unclear what other ways this function is being used | |
d0488daf | 163 | if (!$id) { |
6a488035 TO |
164 | $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE); |
165 | $params['is_default'] = CRM_Utils_Array::value('is_default', $params, FALSE); | |
166 | $params['is_optgroup'] = CRM_Utils_Array::value('is_optgroup', $params, FALSE); | |
167 | $params['filter'] = CRM_Utils_Array::value('filter', $params, FALSE); | |
168 | } | |
58eaa092 CW |
169 | // Update custom field data to reflect the new value |
170 | elseif (isset($params['value'])) { | |
d0488daf | 171 | CRM_Core_BAO_CustomOption::updateValue($id, $params['value']); |
58eaa092 | 172 | } |
6a488035 | 173 | |
d0488daf | 174 | // We need to have option_group_id populated for validation so load if necessary. |
175 | if (empty($params['option_group_id'])) { | |
176 | $params['option_group_id'] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', | |
177 | $id, 'option_group_id', 'id' | |
178 | ); | |
179 | } | |
180 | $groupName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', | |
181 | $params['option_group_id'], 'name', 'id' | |
182 | ); | |
183 | ||
6a488035 TO |
184 | // action is taken depending upon the mode |
185 | $optionValue = new CRM_Core_DAO_OptionValue(); | |
186 | $optionValue->copyValues($params); | |
187 | ||
a7488080 | 188 | if (!empty($params['is_default'])) { |
6a488035 TO |
189 | $query = 'UPDATE civicrm_option_value SET is_default = 0 WHERE option_group_id = %1'; |
190 | ||
191 | // tweak default reset, and allow multiple default within group. | |
192 | if ($resetDefaultFor = CRM_Utils_Array::value('reset_default_for', $params)) { | |
193 | if (is_array($resetDefaultFor)) { | |
194 | $colName = key($resetDefaultFor); | |
195 | $colVal = $resetDefaultFor[$colName]; | |
196 | $query .= " AND ( $colName IN ( $colVal ) )"; | |
197 | } | |
198 | } | |
199 | ||
be2fb01f | 200 | $p = [1 => [$params['option_group_id'], 'Integer']]; |
6a488035 TO |
201 | CRM_Core_DAO::executeQuery($query, $p); |
202 | } | |
89ab5601 | 203 | |
d0488daf | 204 | if (empty($params['domain_id']) && in_array($groupName, CRM_Core_OptionGroup::$_domainIDGroups)) { |
205 | $optionValue->domain_id = CRM_Core_Config::domainID(); | |
89ab5601 PJ |
206 | } |
207 | ||
d0488daf | 208 | $groupsSupportingDuplicateValues = ['languages']; |
209 | if (!$id && !empty($params['value'])) { | |
e5720c45 | 210 | $dao = new CRM_Core_DAO_OptionValue(); |
d0488daf | 211 | if (!in_array($groupName, $groupsSupportingDuplicateValues)) { |
212 | $dao->value = $params['value']; | |
213 | } | |
214 | else { | |
215 | // CRM-21737 languages option group does not use unique values but unique names. | |
216 | $dao->name = $params['name']; | |
217 | } | |
218 | if (in_array($groupName, CRM_Core_OptionGroup::$_domainIDGroups)) { | |
219 | $dao->domain_id = $optionValue->domain_id; | |
220 | } | |
221 | $dao->option_group_id = $params['option_group_id']; | |
e5720c45 SL |
222 | if ($dao->find(TRUE)) { |
223 | throw new CRM_Core_Exception('Value already exists in the database'); | |
224 | } | |
225 | } | |
6a488035 | 226 | |
d0488daf | 227 | $optionValue->id = $id; |
6a488035 | 228 | $optionValue->save(); |
a4a33486 | 229 | CRM_Core_PseudoConstant::flush(); |
a900072c | 230 | |
49bade89 | 231 | // Create relationship for payment instrument options |
a900072c PN |
232 | if (!empty($params['financial_account_id'])) { |
233 | $optionName = civicrm_api3('OptionGroup', 'getvalue', [ | |
234 | 'return' => 'name', | |
235 | 'id' => $params['option_group_id'], | |
236 | ]); | |
49bade89 | 237 | // Only create relationship for payment instrument options |
a900072c PN |
238 | if ($optionName == 'payment_instrument') { |
239 | $relationTypeId = civicrm_api3('OptionValue', 'getvalue', [ | |
240 | 'return' => 'value', | |
241 | 'option_group_id' => 'account_relationship', | |
242 | 'name' => 'Asset Account is', | |
243 | ]); | |
244 | $params = [ | |
245 | 'entity_table' => 'civicrm_option_value', | |
246 | 'entity_id' => $optionValue->id, | |
247 | 'account_relationship' => $relationTypeId, | |
248 | 'financial_account_id' => $params['financial_account_id'], | |
249 | ]; | |
250 | CRM_Financial_BAO_FinancialTypeAccount::add($params); | |
251 | } | |
252 | } | |
6a488035 TO |
253 | return $optionValue; |
254 | } | |
255 | ||
256 | /** | |
fe482240 | 257 | * Delete Option Value. |
6a488035 | 258 | * |
c490a46a | 259 | * @param int $optionValueId |
6a488035 | 260 | * |
5c766a0b | 261 | * @return bool |
6a488035 | 262 | * |
6a488035 | 263 | */ |
00be9182 | 264 | public static function del($optionValueId) { |
6a488035 TO |
265 | $optionValue = new CRM_Core_DAO_OptionValue(); |
266 | $optionValue->id = $optionValueId; | |
a60c0bc8 SL |
267 | if (!$optionValue->find()) { |
268 | return FALSE; | |
269 | } | |
6a488035 | 270 | if (self::updateRecords($optionValueId, CRM_Core_Action::DELETE)) { |
a4a33486 | 271 | CRM_Core_PseudoConstant::flush(); |
a60c0bc8 SL |
272 | $optionValue->delete(); |
273 | return TRUE; | |
6a488035 TO |
274 | } |
275 | return FALSE; | |
276 | } | |
277 | ||
278 | /** | |
fe482240 | 279 | * Retrieve activity type label and description. |
6a488035 | 280 | * |
6a0b768e TO |
281 | * @param int $activityTypeId |
282 | * Activity type id. | |
6a488035 | 283 | * |
a6c01b45 CW |
284 | * @return array |
285 | * label and description | |
6a488035 | 286 | */ |
00be9182 | 287 | public static function getActivityTypeDetails($activityTypeId) { |
6a488035 TO |
288 | $query = "SELECT civicrm_option_value.label, civicrm_option_value.description |
289 | FROM civicrm_option_value | |
290 | LEFT JOIN civicrm_option_group ON ( civicrm_option_value.option_group_id = civicrm_option_group.id ) | |
291 | WHERE civicrm_option_group.name = 'activity_type' | |
292 | AND civicrm_option_value.value = {$activityTypeId} "; | |
293 | ||
294 | $dao = CRM_Core_DAO::executeQuery($query); | |
295 | ||
296 | $dao->fetch(); | |
297 | ||
be2fb01f | 298 | return [$dao->label, $dao->description]; |
6a488035 TO |
299 | } |
300 | ||
301 | /** | |
302 | * Get the Option Value title. | |
303 | * | |
6a0b768e TO |
304 | * @param int $id |
305 | * Id of Option Value. | |
6a488035 | 306 | * |
a6c01b45 CW |
307 | * @return string |
308 | * title | |
6a488035 | 309 | * |
6a488035 TO |
310 | */ |
311 | public static function getTitle($id) { | |
312 | return CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $id, 'label'); | |
313 | } | |
314 | ||
315 | /** | |
100fef9d | 316 | * Updates contacts affected by the option value passed. |
6a488035 | 317 | * |
6a0b768e TO |
318 | * @param int $optionValueId |
319 | * The option value id. | |
320 | * @param int $action | |
321 | * The action describing whether prefix/suffix was UPDATED or DELETED. | |
6a488035 | 322 | * |
7a6073fd | 323 | * @return bool |
6a488035 | 324 | */ |
00be9182 | 325 | public static function updateRecords(&$optionValueId, $action) { |
6a488035 TO |
326 | //finding group name |
327 | $optionValue = new CRM_Core_DAO_OptionValue(); | |
328 | $optionValue->id = $optionValueId; | |
329 | $optionValue->find(TRUE); | |
330 | ||
331 | $optionGroup = new CRM_Core_DAO_OptionGroup(); | |
332 | $optionGroup->id = $optionValue->option_group_id; | |
333 | $optionGroup->find(TRUE); | |
334 | ||
335 | // group name | |
336 | $gName = $optionGroup->name; | |
337 | // value | |
338 | $value = $optionValue->value; | |
339 | ||
340 | // get the proper group name & affected field name | |
67744c4e | 341 | // todo: this may no longer be needed for individuals - check inputs |
be2fb01f | 342 | $individuals = [ |
6a488035 TO |
343 | 'gender' => 'gender_id', |
344 | 'individual_prefix' => 'prefix_id', | |
345 | 'individual_suffix' => 'suffix_id', | |
353ffa53 TO |
346 | 'communication_style' => 'communication_style_id', |
347 | // Not only Individuals -- but the code seems to be generic for all contact types, despite the naming... | |
be2fb01f CW |
348 | ]; |
349 | $contributions = ['payment_instrument' => 'payment_instrument_id']; | |
350 | $activities = ['activity_type' => 'activity_type_id']; | |
351 | $participant = ['participant_role' => 'role_id']; | |
352 | $eventType = ['event_type' => 'event_type_id']; | |
353 | $aclRole = ['acl_role' => 'acl_role_id']; | |
6a488035 TO |
354 | |
355 | $all = array_merge($individuals, $contributions, $activities, $participant, $eventType, $aclRole); | |
356 | $fieldName = ''; | |
357 | ||
358 | foreach ($all as $name => $id) { | |
359 | if ($gName == $name) { | |
360 | $fieldName = $id; | |
361 | } | |
362 | } | |
363 | if ($fieldName == '') { | |
364 | return TRUE; | |
365 | } | |
366 | ||
367 | if (array_key_exists($gName, $individuals)) { | |
368 | $contactDAO = new CRM_Contact_DAO_Contact(); | |
369 | ||
370 | $contactDAO->$fieldName = $value; | |
371 | $contactDAO->find(); | |
372 | ||
373 | while ($contactDAO->fetch()) { | |
374 | if ($action == CRM_Core_Action::DELETE) { | |
375 | $contact = new CRM_Contact_DAO_Contact(); | |
376 | $contact->id = $contactDAO->id; | |
377 | $contact->find(TRUE); | |
378 | ||
379 | // make sure dates doesn't get reset | |
380 | $contact->birth_date = CRM_Utils_Date::isoToMysql($contact->birth_date); | |
381 | $contact->deceased_date = CRM_Utils_Date::isoToMysql($contact->deceased_date); | |
382 | $contact->$fieldName = 'NULL'; | |
383 | $contact->save(); | |
384 | } | |
385 | } | |
386 | ||
387 | return TRUE; | |
388 | } | |
389 | ||
390 | if (array_key_exists($gName, $contributions)) { | |
391 | $contribution = new CRM_Contribute_DAO_Contribution(); | |
392 | $contribution->$fieldName = $value; | |
393 | $contribution->find(); | |
394 | while ($contribution->fetch()) { | |
395 | if ($action == CRM_Core_Action::DELETE) { | |
396 | $contribution->$fieldName = 'NULL'; | |
397 | $contribution->save(); | |
398 | } | |
399 | } | |
400 | return TRUE; | |
401 | } | |
402 | ||
403 | if (array_key_exists($gName, $activities)) { | |
404 | $activity = new CRM_Activity_DAO_Activity(); | |
405 | $activity->$fieldName = $value; | |
406 | $activity->find(); | |
407 | while ($activity->fetch()) { | |
408 | $activity->delete(); | |
409 | } | |
410 | return TRUE; | |
411 | } | |
412 | ||
413 | //delete participant role, type and event type option value | |
414 | if (array_key_exists($gName, $participant)) { | |
415 | $participantValue = new CRM_Event_DAO_Participant(); | |
416 | $participantValue->$fieldName = $value; | |
417 | if ($participantValue->find(TRUE)) { | |
418 | return FALSE; | |
419 | } | |
420 | return TRUE; | |
421 | } | |
422 | ||
423 | //delete event type option value | |
424 | if (array_key_exists($gName, $eventType)) { | |
425 | $event = new CRM_Event_DAO_Event(); | |
426 | $event->$fieldName = $value; | |
427 | if ($event->find(TRUE)) { | |
428 | return FALSE; | |
429 | } | |
430 | return TRUE; | |
431 | } | |
432 | ||
433 | //delete acl_role option value | |
434 | if (array_key_exists($gName, $aclRole)) { | |
435 | $entityRole = new CRM_ACL_DAO_EntityRole(); | |
436 | $entityRole->$fieldName = $value; | |
437 | ||
438 | $aclDAO = new CRM_ACL_DAO_ACL(); | |
439 | $aclDAO->entity_id = $value; | |
440 | if ($entityRole->find(TRUE) || $aclDAO->find(TRUE)) { | |
441 | return FALSE; | |
442 | } | |
443 | return TRUE; | |
444 | } | |
445 | } | |
446 | ||
447 | /** | |
100fef9d | 448 | * Updates options values weights. |
6a488035 | 449 | * |
c490a46a | 450 | * @param int $opGroupId |
6a0b768e TO |
451 | * @param array $opWeights |
452 | * Options value , weight pair. | |
6a488035 | 453 | */ |
00be9182 | 454 | public static function updateOptionWeights($opGroupId, $opWeights) { |
6a488035 TO |
455 | if (!is_array($opWeights) || empty($opWeights)) { |
456 | return; | |
457 | } | |
458 | ||
459 | foreach ($opWeights as $opValue => $opWeight) { | |
460 | $optionValue = new CRM_Core_DAO_OptionValue(); | |
461 | $optionValue->option_group_id = $opGroupId; | |
462 | $optionValue->value = $opValue; | |
463 | if ($optionValue->find(TRUE)) { | |
464 | $optionValue->weight = $opWeight; | |
465 | $optionValue->save(); | |
466 | } | |
6a488035 TO |
467 | } |
468 | } | |
469 | ||
470 | /** | |
471 | * Get the values of all option values given an option group ID. Store in system cache | |
472 | * Does not take any filtering arguments. The object is to avoid hitting the DB and retrieve | |
473 | * from memory | |
474 | * | |
6a0b768e TO |
475 | * @param int $optionGroupID |
476 | * The option group for which we want the values from. | |
6a488035 | 477 | * |
a6c01b45 CW |
478 | * @return array |
479 | * an array of array of values for this option group | |
6a488035 | 480 | */ |
00be9182 | 481 | public static function getOptionValuesArray($optionGroupID) { |
6a488035 | 482 | // check if we can get the field values from the system cache |
353ffa53 TO |
483 | $cacheKey = "CRM_Core_BAO_OptionValue_OptionGroupID_{$optionGroupID}"; |
484 | $cache = CRM_Utils_Cache::singleton(); | |
6a488035 TO |
485 | $optionValues = $cache->get($cacheKey); |
486 | if (empty($optionValues)) { | |
487 | $dao = new CRM_Core_DAO_OptionValue(); | |
488 | $dao->option_group_id = $optionGroupID; | |
489 | $dao->orderBy('weight ASC, label ASC'); | |
490 | $dao->find(); | |
491 | ||
be2fb01f | 492 | $optionValues = []; |
6a488035 | 493 | while ($dao->fetch()) { |
be2fb01f | 494 | $optionValues[$dao->id] = []; |
6a488035 TO |
495 | CRM_Core_DAO::storeValues($dao, $optionValues[$dao->id]); |
496 | } | |
497 | ||
498 | $cache->set($cacheKey, $optionValues); | |
499 | } | |
500 | ||
501 | return $optionValues; | |
502 | } | |
503 | ||
504 | /** | |
505 | * Get the values of all option values given an option group ID as a key => value pair | |
506 | * Use above cached function to make it super efficient | |
507 | * | |
6a0b768e TO |
508 | * @param int $optionGroupID |
509 | * The option group for which we want the values from. | |
6a488035 | 510 | * |
a6c01b45 CW |
511 | * @return array |
512 | * an associative array of label, value pairs | |
6a488035 | 513 | */ |
00be9182 | 514 | public static function getOptionValuesAssocArray($optionGroupID) { |
6a488035 TO |
515 | $optionValues = self::getOptionValuesArray($optionGroupID); |
516 | ||
be2fb01f | 517 | $options = []; |
6a488035 TO |
518 | foreach ($optionValues as $id => $value) { |
519 | $options[$value['value']] = $value['label']; | |
520 | } | |
521 | return $options; | |
522 | } | |
353ffa53 | 523 | |
6a488035 TO |
524 | /** |
525 | * Get the values of all option values given an option group Name as a key => value pair | |
526 | * Use above cached function to make it super efficient | |
527 | * | |
6a0b768e TO |
528 | * @param string $optionGroupName |
529 | * The option group name for which we want the values from. | |
6a488035 | 530 | * |
a6c01b45 CW |
531 | * @return array |
532 | * an associative array of label, value pairs | |
6a488035 | 533 | */ |
00be9182 | 534 | public static function getOptionValuesAssocArrayFromName($optionGroupName) { |
6a488035 TO |
535 | $dao = new CRM_Core_DAO_OptionGroup(); |
536 | $dao->name = $optionGroupName; | |
537 | $dao->selectAdd(); | |
538 | $dao->selectAdd('id'); | |
539 | $dao->find(TRUE); | |
540 | $optionValues = self::getOptionValuesArray($dao->id); | |
541 | ||
be2fb01f | 542 | $options = []; |
6a488035 TO |
543 | foreach ($optionValues as $id => $value) { |
544 | $options[$value['value']] = $value['label']; | |
545 | } | |
546 | return $options; | |
547 | } | |
548 | ||
6f408d71 | 549 | /** |
550 | * Ensure an option value exists. | |
551 | * | |
552 | * This function is intended to be called from the upgrade script to ensure | |
553 | * that an option value exists, without hitting an error if it already exists. | |
554 | * | |
555 | * This is sympathetic to sites who might pre-add it. | |
ad8d1ce3 RO |
556 | * |
557 | * @param array $params the option value attributes. | |
558 | * @return array the option value attributes. | |
6f408d71 | 559 | */ |
560 | public static function ensureOptionValueExists($params) { | |
be2fb01f | 561 | $result = civicrm_api3('OptionValue', 'get', [ |
bf87703e | 562 | 'option_group_id' => $params['option_group_id'], |
6f408d71 | 563 | 'name' => $params['name'], |
ad8d1ce3 RO |
564 | 'return' => ['id', 'value'], |
565 | 'sequential' => 1, | |
be2fb01f | 566 | ]); |
ad8d1ce3 RO |
567 | |
568 | if (!$result['count']) { | |
569 | $result = civicrm_api3('OptionValue', 'create', $params); | |
6f408d71 | 570 | } |
ad8d1ce3 RO |
571 | |
572 | return CRM_Utils_Array::first($result['values']); | |
6f408d71 | 573 | } |
574 | ||
6a488035 | 575 | } |