3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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 * @copyright CiviCRM LLC (c) 2004-2018
35 * This trait allows us to consolidate Preferences & Settings forms.
37 * It is intended mostly as part of a refactoring process to get rid of having 2.
39 trait CRM_Admin_Form_SettingTrait
{
44 protected $settingsMetadata;
51 public function getDefaultEntity() {
56 * Get the metadata relating to the settings on the form, ordered by the keys in $this->_settings.
60 protected function getSettingsMetaData() {
61 if (empty($this->settingsMetadata
)) {
62 $allSettingMetaData = civicrm_api3('setting', 'getfields', []);
63 $this->settingsMetadata
= array_intersect_key($allSettingMetaData['values'], $this->_settings
);
64 // This array_merge re-orders to the key order of $this->_settings.
65 $this->settingsMetadata
= array_merge($this->_settings
, $this->settingsMetadata
);
67 return $this->settingsMetadata
;
71 * Get the settings which can be stored based on metadata.
73 * @param array $params
76 protected function getSettingsToSetByMetadata($params) {
77 $setValues = array_intersect_key($params, $this->_settings
);
78 // Checkboxes will be unset rather than empty so we need to add them back in.
79 // Handle quickform hateability just once, right here right now.
80 $unsetValues = array_diff_key($this->_settings
, $params);
81 foreach ($unsetValues as $key => $unsetValue) {
82 if ($this->getQuickFormType($this->getSettingMetadata($key)) === 'CheckBox') {
83 $setValues[$key] = [$key => 0];
92 protected function filterParamsSetByMetadata(&$params) {
93 foreach ($this->getSettingsToSetByMetadata($params) as $setting => $settingGroup) {
94 //@todo array_diff this
95 unset($params[$setting]);
100 * Get the metadata for a particular field.
105 protected function getSettingMetadata($setting) {
106 return $this->getSettingsMetaData()[$setting];
110 * Get the metadata for a particular field for a particular item.
112 * e.g get 'serialize' key, if exists, for a field.
117 protected function getSettingMetadataItem($setting, $item) {
118 return CRM_Utils_Array
::value($item, $this->getSettingsMetaData()[$setting]);
122 * Add fields in the metadata to the template.
124 protected function addFieldsDefinedInSettingsMetadata() {
125 $settingMetaData = $this->getSettingsMetaData();
127 foreach ($settingMetaData as $setting => $props) {
128 $quickFormType = $this->getQuickFormType($props);
129 if (isset($quickFormType)) {
130 $options = CRM_Utils_Array
::value('options', $props);
131 if (isset($props['pseudoconstant'])) {
132 $options = civicrm_api3('Setting', 'getoptions', [
136 if ($props['type'] === 'Boolean') {
137 $options = [$props['title'] => $props['name']];
140 //Load input as readonly whose values are overridden in civicrm.settings.php.
141 if (Civi
::settings()->getMandatory($setting)) {
142 $props['html_attributes']['readonly'] = TRUE;
143 $this->includesReadOnlyFields
= TRUE;
146 $add = 'add' . $quickFormType;
147 if ($add == 'addElement') {
152 ($options !== NULL) ?
$options : CRM_Utils_Array
::value('html_attributes', $props, []),
153 ($options !== NULL) ? CRM_Utils_Array
::value('html_attributes', $props, []) : NULL
156 elseif ($add == 'addSelect') {
157 $this->addElement('select', $setting, ts($props['title']), $options, CRM_Utils_Array
::value('html_attributes', $props));
159 elseif ($add == 'addCheckBox') {
160 $this->addCheckBox($setting, '', $options, NULL, CRM_Utils_Array
::value('html_attributes', $props), NULL, NULL, [' ']);
162 elseif ($add == 'addCheckBoxes') {
163 $options = array_flip($options);
165 foreach ($options as $key => $val) {
166 $newOptions[$key] = $val;
168 $this->addCheckBox($setting,
171 NULL, NULL, NULL, NULL,
172 [' ', ' ', '<br/>']
175 elseif ($add == 'addChainSelect') {
176 $this->addChainSelect($setting, [
177 'label' => ts($props['title']),
180 elseif ($add == 'addMonthDay') {
181 $this->add('date', $setting, ts($props['title']), CRM_Core_SelectValues
::date(NULL, 'M d'));
183 elseif ($add === 'addEntityRef') {
184 $this->$add($setting, ts($props['title']), $props['entity_reference_options']);
186 elseif ($add === 'addYesNo' && ($props['type'] === 'Boolean')) {
187 $this->addRadio($setting, ts($props['title']), array(1 => 'Yes', 0 => 'No'), NULL, ' ');
190 $this->$add($setting, ts($props['title']), $options);
192 // Migrate to using an array as easier in smart...
193 $description = CRM_Utils_Array
::value('description', $props);
194 $descriptions[$setting] = $description;
195 $this->assign("{$setting}_description", $description);
196 if ($setting == 'max_attachments') {
197 //temp hack @todo fix to get from metadata
198 $this->addRule('max_attachments', ts('Value should be a positive number'), 'positiveInteger');
200 if ($setting == 'maxFileSize') {
202 $this->addRule('maxFileSize', ts('Value should be a positive number'), 'positiveInteger');
207 // setting_description should be deprecated - see Mail.tpl for metadata based tpl.
208 $this->assign('setting_descriptions', $descriptions);
209 $this->assign('settings_fields', $settingMetaData);
210 $this->assign('fields', $settingMetaData);
214 * Get the quickform type for the given html type.
220 protected function getQuickFormType($spec) {
221 if (isset($spec['quick_form_type']) &&
222 !($spec['quick_form_type'] === 'Element' && !empty($spec['html_type']))) {
223 // This is kinda transitional
224 return $spec['quick_form_type'];
227 // The spec for settings has been updated for consistency - we provide deprecation notices for sites that have
228 // not made this change.
229 $htmlType = $spec['html_type'];
230 if ($htmlType !== strtolower($htmlType)) {
231 CRM_Core_Error
::deprecatedFunctionWarning(ts('Settings fields html_type should be lower case - see https://docs.civicrm.org/dev/en/latest/framework/setting/ - this needs to be fixed for ' . $spec['name']));
232 $htmlType = strtolower($spec['html_type']);
235 'checkboxes' => 'CheckBoxes',
236 'checkbox' => 'CheckBox',
238 'select' => 'Select',
239 'textarea' => 'Element',
241 'entity_reference' => 'EntityRef',
242 'advmultiselect' => 'Element',
244 return $mapping[$htmlType];
247 * Get the defaults for all fields defined in the metadata.
249 * All others are pending conversion.
251 protected function setDefaultsForMetadataDefinedFields() {
252 CRM_Core_BAO_ConfigSetting
::retrieve($this->_defaults
);
253 foreach (array_keys($this->_settings
) as $setting) {
254 $this->_defaults
[$setting] = civicrm_api3('setting', 'getvalue', ['name' => $setting]);
255 $spec = $this->getSettingsMetadata()[$setting];
256 if (!empty($spec['serialize'])) {
257 $this->_defaults
[$setting] = CRM_Core_DAO
::unSerializeField($this->_defaults
[$setting], $spec['serialize']);
259 if ($this->getQuickFormType($spec) === 'CheckBoxes') {
260 $this->_defaults
[$setting] = array_fill_keys($this->_defaults
[$setting], 1);
262 if ($this->getQuickFormType($spec) === 'CheckBox') {
263 $this->_defaults
[$setting] = [$setting => $this->_defaults
[$setting]];
272 protected function saveMetadataDefinedSettings($params) {
273 $settings = $this->getSettingsToSetByMetadata($params);
274 foreach ($settings as $setting => $settingValue) {
275 if ($this->getQuickFormType($this->getSettingMetadata($setting)) === 'CheckBoxes') {
276 $settings[$setting] = array_keys($settingValue);
278 if ($this->getQuickFormType($this->getSettingMetadata($setting)) === 'CheckBox') {
279 // This will be an array with one value.
280 $settings[$setting] = (int) reset($settings[$setting]);
283 civicrm_api3('setting', 'create', $settings);