Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
8c9251b3 | 6 | | Copyright CiviCRM LLC (c) 2004-2018 | |
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 | |
8c9251b3 | 31 | * @copyright CiviCRM LLC (c) 2004-2018 |
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 | * |
8a4fede3 | 154 | * @return bool |
155 | * true if we found and updated the object, else false | |
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 | |
d0488daf | 167 | * deprecated Reference array contains the id. |
6a488035 | 168 | * |
d0488daf | 169 | * @return \CRM_Core_DAO_OptionValue |
170 | * @throws \CRM_Core_Exception | |
6a488035 | 171 | */ |
0be95659 | 172 | public static function add(&$params, $ids = array()) { |
d0488daf | 173 | $id = CRM_Utils_Array::value('id', $params, CRM_Utils_Array::value('optionValue', $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 | |
d0488daf | 179 | if (!$id) { |
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 | } | |
58eaa092 CW |
185 | // Update custom field data to reflect the new value |
186 | elseif (isset($params['value'])) { | |
d0488daf | 187 | CRM_Core_BAO_CustomOption::updateValue($id, $params['value']); |
58eaa092 | 188 | } |
6a488035 | 189 | |
d0488daf | 190 | // We need to have option_group_id populated for validation so load if necessary. |
191 | if (empty($params['option_group_id'])) { | |
192 | $params['option_group_id'] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', | |
193 | $id, 'option_group_id', 'id' | |
194 | ); | |
195 | } | |
196 | $groupName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', | |
197 | $params['option_group_id'], 'name', 'id' | |
198 | ); | |
199 | ||
6a488035 TO |
200 | // action is taken depending upon the mode |
201 | $optionValue = new CRM_Core_DAO_OptionValue(); | |
202 | $optionValue->copyValues($params); | |
203 | ||
a7488080 | 204 | if (!empty($params['is_default'])) { |
6a488035 TO |
205 | $query = 'UPDATE civicrm_option_value SET is_default = 0 WHERE option_group_id = %1'; |
206 | ||
207 | // tweak default reset, and allow multiple default within group. | |
208 | if ($resetDefaultFor = CRM_Utils_Array::value('reset_default_for', $params)) { | |
209 | if (is_array($resetDefaultFor)) { | |
210 | $colName = key($resetDefaultFor); | |
211 | $colVal = $resetDefaultFor[$colName]; | |
212 | $query .= " AND ( $colName IN ( $colVal ) )"; | |
213 | } | |
214 | } | |
215 | ||
216 | $p = array(1 => array($params['option_group_id'], 'Integer')); | |
217 | CRM_Core_DAO::executeQuery($query, $p); | |
218 | } | |
89ab5601 | 219 | |
d0488daf | 220 | if (empty($params['domain_id']) && in_array($groupName, CRM_Core_OptionGroup::$_domainIDGroups)) { |
221 | $optionValue->domain_id = CRM_Core_Config::domainID(); | |
89ab5601 PJ |
222 | } |
223 | ||
d0488daf | 224 | $groupsSupportingDuplicateValues = ['languages']; |
225 | if (!$id && !empty($params['value'])) { | |
e5720c45 | 226 | $dao = new CRM_Core_DAO_OptionValue(); |
d0488daf | 227 | if (!in_array($groupName, $groupsSupportingDuplicateValues)) { |
228 | $dao->value = $params['value']; | |
229 | } | |
230 | else { | |
231 | // CRM-21737 languages option group does not use unique values but unique names. | |
232 | $dao->name = $params['name']; | |
233 | } | |
234 | if (in_array($groupName, CRM_Core_OptionGroup::$_domainIDGroups)) { | |
235 | $dao->domain_id = $optionValue->domain_id; | |
236 | } | |
237 | $dao->option_group_id = $params['option_group_id']; | |
e5720c45 SL |
238 | if ($dao->find(TRUE)) { |
239 | throw new CRM_Core_Exception('Value already exists in the database'); | |
240 | } | |
241 | } | |
6a488035 | 242 | |
d0488daf | 243 | $optionValue->id = $id; |
6a488035 | 244 | $optionValue->save(); |
a4a33486 | 245 | CRM_Core_PseudoConstant::flush(); |
a900072c PN |
246 | |
247 | // Create relationship for payment intrument options | |
248 | if (!empty($params['financial_account_id'])) { | |
249 | $optionName = civicrm_api3('OptionGroup', 'getvalue', [ | |
250 | 'return' => 'name', | |
251 | 'id' => $params['option_group_id'], | |
252 | ]); | |
253 | // Only create relationship for payment intrument options | |
254 | if ($optionName == 'payment_instrument') { | |
255 | $relationTypeId = civicrm_api3('OptionValue', 'getvalue', [ | |
256 | 'return' => 'value', | |
257 | 'option_group_id' => 'account_relationship', | |
258 | 'name' => 'Asset Account is', | |
259 | ]); | |
260 | $params = [ | |
261 | 'entity_table' => 'civicrm_option_value', | |
262 | 'entity_id' => $optionValue->id, | |
263 | 'account_relationship' => $relationTypeId, | |
264 | 'financial_account_id' => $params['financial_account_id'], | |
265 | ]; | |
266 | CRM_Financial_BAO_FinancialTypeAccount::add($params); | |
267 | } | |
268 | } | |
6a488035 TO |
269 | return $optionValue; |
270 | } | |
271 | ||
272 | /** | |
fe482240 | 273 | * Delete Option Value. |
6a488035 | 274 | * |
c490a46a | 275 | * @param int $optionValueId |
6a488035 | 276 | * |
5c766a0b | 277 | * @return bool |
6a488035 | 278 | * |
6a488035 | 279 | */ |
00be9182 | 280 | public static function del($optionValueId) { |
6a488035 TO |
281 | $optionValue = new CRM_Core_DAO_OptionValue(); |
282 | $optionValue->id = $optionValueId; | |
a60c0bc8 SL |
283 | if (!$optionValue->find()) { |
284 | return FALSE; | |
285 | } | |
6a488035 | 286 | if (self::updateRecords($optionValueId, CRM_Core_Action::DELETE)) { |
a4a33486 | 287 | CRM_Core_PseudoConstant::flush(); |
a60c0bc8 SL |
288 | $optionValue->delete(); |
289 | return TRUE; | |
6a488035 TO |
290 | } |
291 | return FALSE; | |
292 | } | |
293 | ||
294 | /** | |
fe482240 | 295 | * Retrieve activity type label and description. |
6a488035 | 296 | * |
6a0b768e TO |
297 | * @param int $activityTypeId |
298 | * Activity type id. | |
6a488035 | 299 | * |
a6c01b45 CW |
300 | * @return array |
301 | * label and description | |
6a488035 | 302 | */ |
00be9182 | 303 | public static function getActivityTypeDetails($activityTypeId) { |
6a488035 TO |
304 | $query = "SELECT civicrm_option_value.label, civicrm_option_value.description |
305 | FROM civicrm_option_value | |
306 | LEFT JOIN civicrm_option_group ON ( civicrm_option_value.option_group_id = civicrm_option_group.id ) | |
307 | WHERE civicrm_option_group.name = 'activity_type' | |
308 | AND civicrm_option_value.value = {$activityTypeId} "; | |
309 | ||
310 | $dao = CRM_Core_DAO::executeQuery($query); | |
311 | ||
312 | $dao->fetch(); | |
313 | ||
314 | return array($dao->label, $dao->description); | |
315 | } | |
316 | ||
317 | /** | |
318 | * Get the Option Value title. | |
319 | * | |
6a0b768e TO |
320 | * @param int $id |
321 | * Id of Option Value. | |
6a488035 | 322 | * |
a6c01b45 CW |
323 | * @return string |
324 | * title | |
6a488035 | 325 | * |
6a488035 TO |
326 | */ |
327 | public static function getTitle($id) { | |
328 | return CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $id, 'label'); | |
329 | } | |
330 | ||
331 | /** | |
100fef9d | 332 | * Updates contacts affected by the option value passed. |
6a488035 | 333 | * |
6a0b768e TO |
334 | * @param int $optionValueId |
335 | * The option value id. | |
336 | * @param int $action | |
337 | * The action describing whether prefix/suffix was UPDATED or DELETED. | |
6a488035 | 338 | * |
7a6073fd | 339 | * @return bool |
6a488035 | 340 | */ |
00be9182 | 341 | public static function updateRecords(&$optionValueId, $action) { |
6a488035 TO |
342 | //finding group name |
343 | $optionValue = new CRM_Core_DAO_OptionValue(); | |
344 | $optionValue->id = $optionValueId; | |
345 | $optionValue->find(TRUE); | |
346 | ||
347 | $optionGroup = new CRM_Core_DAO_OptionGroup(); | |
348 | $optionGroup->id = $optionValue->option_group_id; | |
349 | $optionGroup->find(TRUE); | |
350 | ||
351 | // group name | |
352 | $gName = $optionGroup->name; | |
353 | // value | |
354 | $value = $optionValue->value; | |
355 | ||
356 | // get the proper group name & affected field name | |
67744c4e | 357 | // todo: this may no longer be needed for individuals - check inputs |
6a488035 TO |
358 | $individuals = array( |
359 | 'gender' => 'gender_id', | |
360 | 'individual_prefix' => 'prefix_id', | |
361 | 'individual_suffix' => 'suffix_id', | |
353ffa53 TO |
362 | 'communication_style' => 'communication_style_id', |
363 | // Not only Individuals -- but the code seems to be generic for all contact types, despite the naming... | |
6a488035 TO |
364 | ); |
365 | $contributions = array('payment_instrument' => 'payment_instrument_id'); | |
353ffa53 TO |
366 | $activities = array('activity_type' => 'activity_type_id'); |
367 | $participant = array('participant_role' => 'role_id'); | |
368 | $eventType = array('event_type' => 'event_type_id'); | |
369 | $aclRole = array('acl_role' => 'acl_role_id'); | |
6a488035 TO |
370 | |
371 | $all = array_merge($individuals, $contributions, $activities, $participant, $eventType, $aclRole); | |
372 | $fieldName = ''; | |
373 | ||
374 | foreach ($all as $name => $id) { | |
375 | if ($gName == $name) { | |
376 | $fieldName = $id; | |
377 | } | |
378 | } | |
379 | if ($fieldName == '') { | |
380 | return TRUE; | |
381 | } | |
382 | ||
383 | if (array_key_exists($gName, $individuals)) { | |
384 | $contactDAO = new CRM_Contact_DAO_Contact(); | |
385 | ||
386 | $contactDAO->$fieldName = $value; | |
387 | $contactDAO->find(); | |
388 | ||
389 | while ($contactDAO->fetch()) { | |
390 | if ($action == CRM_Core_Action::DELETE) { | |
391 | $contact = new CRM_Contact_DAO_Contact(); | |
392 | $contact->id = $contactDAO->id; | |
393 | $contact->find(TRUE); | |
394 | ||
395 | // make sure dates doesn't get reset | |
396 | $contact->birth_date = CRM_Utils_Date::isoToMysql($contact->birth_date); | |
397 | $contact->deceased_date = CRM_Utils_Date::isoToMysql($contact->deceased_date); | |
398 | $contact->$fieldName = 'NULL'; | |
399 | $contact->save(); | |
400 | } | |
401 | } | |
402 | ||
403 | return TRUE; | |
404 | } | |
405 | ||
406 | if (array_key_exists($gName, $contributions)) { | |
407 | $contribution = new CRM_Contribute_DAO_Contribution(); | |
408 | $contribution->$fieldName = $value; | |
409 | $contribution->find(); | |
410 | while ($contribution->fetch()) { | |
411 | if ($action == CRM_Core_Action::DELETE) { | |
412 | $contribution->$fieldName = 'NULL'; | |
413 | $contribution->save(); | |
414 | } | |
415 | } | |
416 | return TRUE; | |
417 | } | |
418 | ||
419 | if (array_key_exists($gName, $activities)) { | |
420 | $activity = new CRM_Activity_DAO_Activity(); | |
421 | $activity->$fieldName = $value; | |
422 | $activity->find(); | |
423 | while ($activity->fetch()) { | |
424 | $activity->delete(); | |
425 | } | |
426 | return TRUE; | |
427 | } | |
428 | ||
429 | //delete participant role, type and event type option value | |
430 | if (array_key_exists($gName, $participant)) { | |
431 | $participantValue = new CRM_Event_DAO_Participant(); | |
432 | $participantValue->$fieldName = $value; | |
433 | if ($participantValue->find(TRUE)) { | |
434 | return FALSE; | |
435 | } | |
436 | return TRUE; | |
437 | } | |
438 | ||
439 | //delete event type option value | |
440 | if (array_key_exists($gName, $eventType)) { | |
441 | $event = new CRM_Event_DAO_Event(); | |
442 | $event->$fieldName = $value; | |
443 | if ($event->find(TRUE)) { | |
444 | return FALSE; | |
445 | } | |
446 | return TRUE; | |
447 | } | |
448 | ||
449 | //delete acl_role option value | |
450 | if (array_key_exists($gName, $aclRole)) { | |
451 | $entityRole = new CRM_ACL_DAO_EntityRole(); | |
452 | $entityRole->$fieldName = $value; | |
453 | ||
454 | $aclDAO = new CRM_ACL_DAO_ACL(); | |
455 | $aclDAO->entity_id = $value; | |
456 | if ($entityRole->find(TRUE) || $aclDAO->find(TRUE)) { | |
457 | return FALSE; | |
458 | } | |
459 | return TRUE; | |
460 | } | |
461 | } | |
462 | ||
463 | /** | |
100fef9d | 464 | * Updates options values weights. |
6a488035 | 465 | * |
c490a46a | 466 | * @param int $opGroupId |
6a0b768e TO |
467 | * @param array $opWeights |
468 | * Options value , weight pair. | |
6a488035 | 469 | */ |
00be9182 | 470 | public static function updateOptionWeights($opGroupId, $opWeights) { |
6a488035 TO |
471 | if (!is_array($opWeights) || empty($opWeights)) { |
472 | return; | |
473 | } | |
474 | ||
475 | foreach ($opWeights as $opValue => $opWeight) { | |
476 | $optionValue = new CRM_Core_DAO_OptionValue(); | |
477 | $optionValue->option_group_id = $opGroupId; | |
478 | $optionValue->value = $opValue; | |
479 | if ($optionValue->find(TRUE)) { | |
480 | $optionValue->weight = $opWeight; | |
481 | $optionValue->save(); | |
482 | } | |
483 | $optionValue->free(); | |
484 | } | |
485 | } | |
486 | ||
487 | /** | |
488 | * Get the values of all option values given an option group ID. Store in system cache | |
489 | * Does not take any filtering arguments. The object is to avoid hitting the DB and retrieve | |
490 | * from memory | |
491 | * | |
6a0b768e TO |
492 | * @param int $optionGroupID |
493 | * The option group for which we want the values from. | |
6a488035 | 494 | * |
a6c01b45 CW |
495 | * @return array |
496 | * an array of array of values for this option group | |
6a488035 | 497 | */ |
00be9182 | 498 | public static function getOptionValuesArray($optionGroupID) { |
6a488035 | 499 | // check if we can get the field values from the system cache |
353ffa53 TO |
500 | $cacheKey = "CRM_Core_BAO_OptionValue_OptionGroupID_{$optionGroupID}"; |
501 | $cache = CRM_Utils_Cache::singleton(); | |
6a488035 TO |
502 | $optionValues = $cache->get($cacheKey); |
503 | if (empty($optionValues)) { | |
504 | $dao = new CRM_Core_DAO_OptionValue(); | |
505 | $dao->option_group_id = $optionGroupID; | |
506 | $dao->orderBy('weight ASC, label ASC'); | |
507 | $dao->find(); | |
508 | ||
509 | $optionValues = array(); | |
510 | while ($dao->fetch()) { | |
511 | $optionValues[$dao->id] = array(); | |
512 | CRM_Core_DAO::storeValues($dao, $optionValues[$dao->id]); | |
513 | } | |
514 | ||
515 | $cache->set($cacheKey, $optionValues); | |
516 | } | |
517 | ||
518 | return $optionValues; | |
519 | } | |
520 | ||
521 | /** | |
522 | * Get the values of all option values given an option group ID as a key => value pair | |
523 | * Use above cached function to make it super efficient | |
524 | * | |
6a0b768e TO |
525 | * @param int $optionGroupID |
526 | * The option group for which we want the values from. | |
6a488035 | 527 | * |
a6c01b45 CW |
528 | * @return array |
529 | * an associative array of label, value pairs | |
6a488035 | 530 | */ |
00be9182 | 531 | public static function getOptionValuesAssocArray($optionGroupID) { |
6a488035 TO |
532 | $optionValues = self::getOptionValuesArray($optionGroupID); |
533 | ||
534 | $options = array(); | |
535 | foreach ($optionValues as $id => $value) { | |
536 | $options[$value['value']] = $value['label']; | |
537 | } | |
538 | return $options; | |
539 | } | |
353ffa53 | 540 | |
6a488035 TO |
541 | /** |
542 | * Get the values of all option values given an option group Name as a key => value pair | |
543 | * Use above cached function to make it super efficient | |
544 | * | |
6a0b768e TO |
545 | * @param string $optionGroupName |
546 | * The option group name for which we want the values from. | |
6a488035 | 547 | * |
a6c01b45 CW |
548 | * @return array |
549 | * an associative array of label, value pairs | |
6a488035 | 550 | */ |
00be9182 | 551 | public static function getOptionValuesAssocArrayFromName($optionGroupName) { |
6a488035 TO |
552 | $dao = new CRM_Core_DAO_OptionGroup(); |
553 | $dao->name = $optionGroupName; | |
554 | $dao->selectAdd(); | |
555 | $dao->selectAdd('id'); | |
556 | $dao->find(TRUE); | |
557 | $optionValues = self::getOptionValuesArray($dao->id); | |
558 | ||
559 | $options = array(); | |
560 | foreach ($optionValues as $id => $value) { | |
561 | $options[$value['value']] = $value['label']; | |
562 | } | |
563 | return $options; | |
564 | } | |
565 | ||
6f408d71 | 566 | /** |
567 | * Ensure an option value exists. | |
568 | * | |
569 | * This function is intended to be called from the upgrade script to ensure | |
570 | * that an option value exists, without hitting an error if it already exists. | |
571 | * | |
572 | * This is sympathetic to sites who might pre-add it. | |
ad8d1ce3 RO |
573 | * |
574 | * @param array $params the option value attributes. | |
575 | * @return array the option value attributes. | |
6f408d71 | 576 | */ |
577 | public static function ensureOptionValueExists($params) { | |
ad8d1ce3 | 578 | $result = civicrm_api3('OptionValue', 'get', array( |
bf87703e | 579 | 'option_group_id' => $params['option_group_id'], |
6f408d71 | 580 | 'name' => $params['name'], |
ad8d1ce3 RO |
581 | 'return' => ['id', 'value'], |
582 | 'sequential' => 1, | |
6f408d71 | 583 | )); |
ad8d1ce3 RO |
584 | |
585 | if (!$result['count']) { | |
586 | $result = civicrm_api3('OptionValue', 'create', $params); | |
6f408d71 | 587 | } |
ad8d1ce3 RO |
588 | |
589 | return CRM_Utils_Array::first($result['values']); | |
6f408d71 | 590 | } |
591 | ||
6a488035 | 592 | } |