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 return array_intersect_key($params, $this->_settings
);
83 protected function filterParamsSetByMetadata(&$params) {
84 foreach ($this->getSettingsToSetByMetadata($params) as $setting => $settingGroup) {
85 //@todo array_diff this
86 unset($params[$setting]);
91 * Get the metadata for a particular field.
96 protected function getSettingMetadata($setting) {
97 return $this->getSettingsMetaData()[$setting];
101 * Get the metadata for a particular field for a particular item.
103 * e.g get 'serialize' key, if exists, for a field.
108 protected function getSettingMetadataItem($setting, $item) {
109 return CRM_Utils_Array
::value($item, $this->getSettingsMetaData()[$setting]);
113 * Add fields in the metadata to the template.
115 protected function addFieldsDefinedInSettingsMetadata() {
116 $settingMetaData = $this->getSettingsMetaData();
118 foreach ($settingMetaData as $setting => $props) {
119 $quickFormType = $this->getQuickFormType($props);
120 if (isset($quickFormType)) {
122 if (isset($props['pseudoconstant'])) {
123 $options = civicrm_api3('Setting', 'getoptions', [
127 if ($props['type'] === 'Boolean') {
128 $options = [$props['title'] => $props['name']];
131 //Load input as readonly whose values are overridden in civicrm.settings.php.
132 if (Civi
::settings()->getMandatory($setting)) {
133 $props['html_attributes']['readonly'] = TRUE;
134 $this->includesReadOnlyFields
= TRUE;
137 $add = 'add' . $quickFormType;
138 if ($add == 'addElement') {
143 ($options !== NULL) ?
$options : CRM_Utils_Array
::value('html_attributes', $props, []),
144 ($options !== NULL) ? CRM_Utils_Array
::value('html_attributes', $props, []) : NULL
147 elseif ($add == 'addSelect') {
148 $this->addElement('select', $setting, ts($props['title']), $options, CRM_Utils_Array
::value('html_attributes', $props));
150 elseif ($add == 'addCheckBox') {
151 $this->addCheckBox($setting, ts($props['title']), $options, NULL, CRM_Utils_Array
::value('html_attributes', $props), NULL, NULL, [' ']);
153 elseif ($add == 'addCheckBoxes') {
154 $options = array_flip($options);
156 foreach ($options as $key => $val) {
157 $newOptions[$key] = $val;
159 $this->addCheckBox($setting,
162 NULL, NULL, NULL, NULL,
163 [' ', ' ', '<br/>']
166 elseif ($add == 'addChainSelect') {
167 $this->addChainSelect($setting, [
168 'label' => ts($props['title']),
171 elseif ($add == 'addMonthDay') {
172 $this->add('date', $setting, ts($props['title']), CRM_Core_SelectValues
::date(NULL, 'M d'));
175 $this->$add($setting, ts($props['title']), $options);
177 // Migrate to using an array as easier in smart...
178 $descriptions[$setting] = ts($props['description']);
179 $this->assign("{$setting}_description", ts($props['description']));
180 if ($setting == 'max_attachments') {
181 //temp hack @todo fix to get from metadata
182 $this->addRule('max_attachments', ts('Value should be a positive number'), 'positiveInteger');
184 if ($setting == 'maxFileSize') {
186 $this->addRule('maxFileSize', ts('Value should be a positive number'), 'positiveInteger');
191 // setting_description should be deprecated - see Mail.tpl for metadata based tpl.
192 $this->assign('setting_descriptions', $descriptions);
193 $this->assign('settings_fields', $settingMetaData);
194 $this->assign('fields', $settingMetaData);
198 * Get the quickform type for the given html type.
204 protected function getQuickFormType($spec) {
205 if (isset($spec['quick_form_type'])) {
206 return $spec['quick_form_type'];
209 'checkboxes' => 'CheckBoxes',
210 'checkbox' => 'CheckBox',
212 'select' => 'Select',
214 return $mapping[$spec['html_type']];
217 * Get the defaults for all fields defined in the metadata.
219 * All others are pending conversion.
221 protected function setDefaultsForMetadataDefinedFields() {
222 CRM_Core_BAO_ConfigSetting
::retrieve($this->_defaults
);
223 foreach (array_keys($this->_settings
) as $setting) {
224 $this->_defaults
[$setting] = civicrm_api3('setting', 'getvalue', ['name' => $setting]);
225 $spec = $this->getSettingsMetadata()[$setting];
226 if (!empty($spec['serialize'])) {
227 $this->_defaults
[$setting] = CRM_Core_DAO
::unSerializeField($this->_defaults
[$setting], $spec['serialize']);
229 if ($this->getQuickFormType($spec) === 'CheckBoxes') {
230 $this->_defaults
[$setting] = array_fill_keys($this->_defaults
[$setting], 1);
239 protected function saveMetadataDefinedSettings($params) {
240 $settings = $this->getSettingsToSetByMetadata($params);
241 foreach ($settings as $setting => $settingValue) {
242 if ($this->getQuickFormType($this->getSettingMetadata($setting)) === 'CheckBoxes') {
243 $settings[$setting] = array_keys($settingValue);
246 civicrm_api3('setting', 'create', $settings);