3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * Class SettingsMetadata
34 class SettingsMetadata
{
39 * WARNING: This interface may change.
41 * This provides information about the setting - similar to the fields concept for DAO information.
42 * As the setting is serialized code creating validation setting input needs to know the data type
43 * This also helps move information out of the form layer into the data layer where people can interact with
44 * it via the API or other mechanisms. In order to keep this consistent it is important the form layer
47 * Note that this function should never be called when using the runtime getvalue function. Caching works
48 * around the expectation it will be called during setting administration
50 * Function is intended for configuration rather than runtime access to settings
52 * The following params will filter the result. If none are passed all settings will be returns
54 * @param array $filters
55 * @param int $domainID
58 * the following information as appropriate for each setting
62 * - add (CiviCRM version added)
68 public static function getMetadata($filters = array(), $domainID = NULL) {
69 if ($domainID === NULL) {
70 $domainID = \CRM_Core_Config
::domainID();
73 $cache = \Civi
::cache('settings');
74 $cacheString = 'settingsMetadata_' . $domainID . '_';
75 // the caching into 'All' seems to be a duplicate of caching to
76 // settingsMetadata__ - I think the reason was to cache all settings as defined & then those altered by a hook
77 $settingsMetadata = $cache->get($cacheString);
78 $cached = is_array($settingsMetadata);
81 $settingsMetadata = $cache->get(self
::ALL
);
82 if (empty($settingsMetadata)) {
84 $metaDataFolders = array($civicrm_root . '/settings');
85 \CRM_Utils_Hook
::alterSettingsFolders($metaDataFolders);
86 $settingsMetadata = self
::loadSettingsMetaDataFolders($metaDataFolders);
87 $cache->set(self
::ALL
, $settingsMetadata);
91 \CRM_Utils_Hook
::alterSettingsMetaData($settingsMetadata, $domainID, NULL);
94 $cache->set($cacheString, $settingsMetadata);
97 self
::_filterSettingsSpecification($filters, $settingsMetadata);
99 return $settingsMetadata;
103 * Load the settings files defined in a series of folders.
104 * @param array $metaDataFolders
105 * List of folder paths.
108 protected static function loadSettingsMetaDataFolders($metaDataFolders) {
109 $settingsMetadata = array();
110 $loadedFolders = array();
111 foreach ($metaDataFolders as $metaDataFolder) {
112 $realFolder = realpath($metaDataFolder);
113 if (is_dir($realFolder) && !isset($loadedFolders[$realFolder])) {
114 $loadedFolders[$realFolder] = TRUE;
115 $settingsMetadata = $settingsMetadata + self
::loadSettingsMetaData($metaDataFolder);
118 return $settingsMetadata;
122 * Load up settings metadata from files.
124 * @param array $metaDataFolder
128 protected static function loadSettingsMetadata($metaDataFolder) {
129 $settingMetaData = array();
130 $settingsFiles = \CRM_Utils_File
::findFiles($metaDataFolder, '*.setting.php');
131 foreach ($settingsFiles as $file) {
132 $settings = include $file;
133 $settingMetaData = array_merge($settingMetaData, $settings);
135 return $settingMetaData;
139 * Filter the settings metadata according to filters passed in. This is a convenience filter
140 * and allows selective reverting / filling of settings
142 * @param array $filters
143 * Filters to match against data.
144 * @param array $settingSpec
145 * Metadata to filter.
147 protected static function _filterSettingsSpecification($filters, &$settingSpec) {
148 if (empty($filters)) {
151 elseif (array_keys($filters) == array('name')) {
152 $settingSpec = array($filters['name'] => \CRM_Utils_Array
::value($filters['name'], $settingSpec, ''));
156 foreach ($settingSpec as $field => $fieldValues) {
157 if (array_intersect_assoc($fieldValues, $filters) != $filters) {
158 unset($settingSpec[$field]);