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