From 76068c6a8b75c66d7425ea23f33b403e95fb905a Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Sun, 16 Aug 2015 23:53:42 -0700 Subject: [PATCH] CRM-16373 - Setting.getoptions - Read/execute callbacks from *.settings.php --- CRM/Core/PseudoConstant.php | 5 +++++ api/v3/Setting.php | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/CRM/Core/PseudoConstant.php b/CRM/Core/PseudoConstant.php index 70ce51a8a8..e77ce3097a 100644 --- a/CRM/Core/PseudoConstant.php +++ b/CRM/Core/PseudoConstant.php @@ -192,6 +192,11 @@ class CRM_Core_PseudoConstant { * NOTE: Rather than calling this function directly use CRM_*_BAO_*::buildOptions() * @see http://wiki.civicrm.org/confluence/display/CRMDOC/Pseudoconstant+%28option+list%29+Reference * + * NOTE: If someone undertakes a refactoring of this, please consider the use-case of + * the Setting.getoptions API. There is no DAO/field, but it would be nice to use the + * same 'pseudoconstant' struct in *.settings.php. This means loosening the coupling + * between $field lookup and the $pseudoconstant evaluation. + * * @param string $daoName * @param string $fieldName * @param array $params diff --git a/api/v3/Setting.php b/api/v3/Setting.php index d2ff10ad18..f2b4f4b64c 100644 --- a/api/v3/Setting.php +++ b/api/v3/Setting.php @@ -144,6 +144,26 @@ function _civicrm_api3_setting_getdefaults_spec(&$params) { ); } +function civicrm_api3_setting_getoptions($params) { + $specs = CRM_Core_BAO_Setting::getSettingSpecification(); + + if (empty($specs[$params['field']]) || empty($specs[$params['field']]['pseudoconstant'])) { + throw new API_Exception("The field '" . $params['field'] . "' has no associated option list."); + } + + $pseudoconstant = $specs[$params['field']]['pseudoconstant']; + + // It would be nice if we could leverage CRM_Core_PseudoConstant::get() somehow, + // but it's tightly coupled to DAO/field. However, if you really need to support + // more pseudoconstant types, then probably best to refactor it. For now, KISS. + if (!empty($pseudoconstant['callback'])) { + $values = Civi\Core\Resolver::singleton()->call($pseudoconstant['callback'], array()); + return civicrm_api3_create_success($values, $params, 'Setting', 'getoptions'); + } + + throw new API_Exception("The field '" . $params['field'] . "' uses an unsupported option list."); +} + /** * Revert settings to defaults. * -- 2.25.1