civicrm_api3_setting_getvalue - Skip $config. Use settings.
[civicrm-core.git] / api / v3 / Setting.php
index a8343612a2f924f4afe7d2bd8a4636f5bf70de97..09e46fb6639d666f69395562efb16d9fc44e149d 100644 (file)
@@ -1,9 +1,9 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.6                                                |
+ | CiviCRM version 4.7                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2014                                |
+ | Copyright CiviCRM LLC (c) 2004-2015                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -28,8 +28,7 @@
 /**
  * This api exposes CiviCRM configuration settings.
  *
- * @package CiviCRM_APIv3_Core
- * @subpackage API_Settings
+ * @package CiviCRM_APIv3
  */
 
 /**
@@ -52,7 +51,7 @@ function civicrm_api3_setting_getfields($params) {
         'description' => 'Settings Group. This is required if the setting is not stored in config',
         'type' => CRM_Utils_Type::T_STRING),
     );
-    return civicrm_api3_create_success($result, $params, 'setting', 'getfields');
+    return civicrm_api3_create_success($result, $params, 'Setting', 'getfields');
   }
   if (!empty($params['name'])) {
     //am of two minds about special handling for 'name' as opposed to other filters - but is does make most common
@@ -72,7 +71,7 @@ function civicrm_api3_setting_getfields($params) {
       $specFunction($result);
     }
   }
-  return civicrm_api3_create_success($result, $params, 'setting', 'getfields');
+  return civicrm_api3_create_success($result, $params, 'Setting', 'getfields');
 }
 
 /**
@@ -81,9 +80,18 @@ function civicrm_api3_setting_getfields($params) {
  * @param array $params
  */
 function _civicrm_api3_setting_getfields_spec(&$params) {
-  $params['filters'] = array('title' => 'Fields you wish to filter by e.g. array("group_name" => "CiviCRM Preferences")');
-  $params['component_id'] = array('title' => 'id of relevant component');
-  $params['profile'] = array('title' => 'profile is passed through to hooks & added to cachestring');
+  $params['filters'] = array(
+    'title' => 'Filters',
+    'description' => 'Fields you wish to filter by e.g. array("group_name" => "CiviCRM Preferences")',
+  );
+  $params['component_id'] = array(
+    'title' => 'Component ID',
+    'description' => 'ID of relevant component',
+  );
+  $params['profile'] = array(
+    'title' => 'Profile',
+    'description' => 'Profile is passed through to hooks & added to cachestring',
+  );
 }
 
 /**
@@ -100,7 +108,7 @@ function _civicrm_api3_setting_getfields_spec(&$params) {
  * @throws \Exception
  */
 function civicrm_api3_setting_getdefaults(&$params) {
-  $settings = civicrm_api3('setting', 'getfields', $params);
+  $settings = civicrm_api3('Setting', 'getfields', $params);
   $domains = _civicrm_api3_setting_getDomainArray($params);
   $defaults = array();
   foreach ($domains as $domainID) {
@@ -119,10 +127,10 @@ function civicrm_api3_setting_getdefaults(&$params) {
       print_r($noDefaults);
     }
   }
-  return civicrm_api3_create_success($defaults, $params, 'setting', 'getfields');
+  return civicrm_api3_create_success($defaults, $params, 'Setting', 'getfields');
 }
 /**
- * Metadata for setting create function.
+ * Metadata for Setting create function.
  *
  * @param array $params
  *   Parameters as passed to the API.
@@ -136,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.
  *
@@ -145,8 +173,8 @@ function _civicrm_api3_setting_getdefaults_spec(&$params) {
  * @throws \Exception
  */
 function civicrm_api3_setting_revert(&$params) {
-  $defaults = civicrm_api('setting', 'getdefaults', $params);
-  $fields = civicrm_api('setting', 'getfields', $params);
+  $defaults = civicrm_api('Setting', 'getdefaults', $params);
+  $fields = civicrm_api('Setting', 'getfields', $params);
   $fields = $fields['values'];
   $domains = _civicrm_api3_setting_getDomainArray($params);
   $result = array();
@@ -156,11 +184,11 @@ function civicrm_api3_setting_revert(&$params) {
       $valuesToRevert['version'] = $params['version'];
       $valuesToRevert['domain_id'] = $domainID;
       // note that I haven't looked at how the result would appear with multiple domains in play
-      $result = array_merge($result, civicrm_api('setting', 'create', $valuesToRevert));
+      $result = array_merge($result, civicrm_api('Setting', 'create', $valuesToRevert));
     }
   }
 
-  return civicrm_api3_create_success($result, $params, 'setting', 'revert');
+  return civicrm_api3_create_success($result, $params, 'Setting', 'revert');
 }
 
 /**
@@ -169,8 +197,14 @@ function civicrm_api3_setting_revert(&$params) {
  * @param array $params
  */
 function _civicrm_api3_setting_revert_spec(&$params) {
-  $params['name'] = array('title' => 'Setting Name belongs to');
-  $params['component_id'] = array('title' => 'id of relevant component');
+  $params['name'] = array(
+    'title' => 'Name',
+    'description' => 'Setting Name belongs to',
+  );
+  $params['component_id'] = array(
+    'title' => 'Component ID',
+    'description' => 'ID of relevant component',
+  );
   $params['domain_id'] = array(
     'api.default' => 'current_domain',
     'description' => 'Defaults may differ by domain - if you do not pass in a domain id this will default to the current domain'
@@ -189,7 +223,7 @@ function _civicrm_api3_setting_revert_spec(&$params) {
  * @throws \Exception
  */
 function civicrm_api3_setting_fill(&$params) {
-  $defaults = civicrm_api3('setting', 'getdefaults', $params);
+  $defaults = civicrm_api3('Setting', 'getdefaults', $params);
   $domains = _civicrm_api3_setting_getDomainArray($params);
   $result = array();
   foreach ($domains as $domainID) {
@@ -197,13 +231,13 @@ function civicrm_api3_setting_fill(&$params) {
       'version' => $params['version'],
       'domain_id' => $domainID,
     );
-    $existing = civicrm_api3('setting', 'get', $apiArray);
+    $existing = civicrm_api3('Setting', 'get', $apiArray);
     $valuesToFill = array_diff_key($defaults['values'][$domainID], $existing['values'][$domainID]);
     if (!empty($valuesToFill)) {
-      $result = array_merge($result, civicrm_api('setting', 'create', $valuesToFill + $apiArray));
+      $result = array_merge($result, civicrm_api('Setting', 'create', $valuesToFill + $apiArray));
     }
   }
-  return civicrm_api3_create_success($result, $params, 'setting', 'fill');
+  return civicrm_api3_create_success($result, $params, 'Setting', 'fill');
 }
 
 /**
@@ -212,8 +246,14 @@ function civicrm_api3_setting_fill(&$params) {
  * @param array $params
  */
 function _civicrm_api3_setting_fill_spec(&$params) {
-  $params['name'] = array('title' => 'Setting Name belongs to');
-  $params['component_id'] = array('title' => 'id of relevant component');
+  $params['name'] = array(
+    'title' => 'Name',
+    'description' => 'Setting Name belongs to',
+  );
+  $params['component_id'] = array(
+    'title' => 'Component ID',
+    'description' => 'ID of relevant component',
+  );
   $params['domain_id'] = array(
     'api.default' => 'current_domain',
     'title' => 'Setting Domain',
@@ -234,7 +274,7 @@ function _civicrm_api3_setting_fill_spec(&$params) {
 function civicrm_api3_setting_create($params) {
   $domains = _civicrm_api3_setting_getDomainArray($params);
   $result = CRM_Core_BAO_Setting::setItems($params, $domains);
-  return civicrm_api3_create_success($result, $params, 'setting', 'create');
+  return civicrm_api3_create_success($result, $params, 'Setting', 'create');
 }
 
 /**
@@ -268,7 +308,7 @@ function _civicrm_api3_setting_create_spec(&$params) {
 function civicrm_api3_setting_get($params) {
   $domains = _civicrm_api3_setting_getDomainArray($params);
   $result = $result = CRM_Core_BAO_Setting::getItems($params, $domains, CRM_Utils_Array::value('return', $params, array()));
-  return civicrm_api3_create_success($result, $params, 'setting', 'get');
+  return civicrm_api3_create_success($result, $params, 'Setting', 'get');
 }
 /**
  * Metadata for setting create function.
@@ -301,10 +341,10 @@ function _civicrm_api3_setting_get_spec(&$params) {
  *   API result array.
  */
 function civicrm_api3_setting_getvalue($params) {
-  $config = CRM_Core_Config::singleton();
-  if (isset($config->$params['name'])) {
-    return $config->$params['name'];
-  }
+  //$config = CRM_Core_Config::singleton();
+  //if (isset($config->$params['name'])) {
+  //  return $config->$params['name'];
+  //}
   return CRM_Core_BAO_Setting::getItem(
     $params['group'],
     CRM_Utils_Array::value('name', $params),