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 if (isset($props['help_link'])) {
147 // Set both the value in this loop & the outer value as we assign both to the template while we deprecate the $descriptions assignment.
148 $settingMetaData[$setting]['description'] = $props['description'] .= ' ' . CRM_Utils_System
::docURL2($props['help_link']['page'], NULL, NULL, NULL, NULL, $props['help_link']['resource']);
151 $add = 'add' . $quickFormType;
152 if ($add == 'addElement') {
157 ($options !== NULL) ?
$options : CRM_Utils_Array
::value('html_attributes', $props, []),
158 ($options !== NULL) ? CRM_Utils_Array
::value('html_attributes', $props, []) : NULL
161 elseif ($add == 'addSelect') {
162 $this->addElement('select', $setting, ts($props['title']), $options, CRM_Utils_Array
::value('html_attributes', $props));
164 elseif ($add == 'addCheckBox') {
165 $this->addCheckBox($setting, ts($props['title']), $options, NULL, CRM_Utils_Array
::value('html_attributes', $props), NULL, NULL, [' ']);
167 elseif ($add == 'addCheckBoxes') {
168 $options = array_flip($options);
170 foreach ($options as $key => $val) {
171 $newOptions[$key] = $val;
173 $this->addCheckBox($setting,
176 NULL, NULL, NULL, NULL,
177 [' ', ' ', '<br/>']
180 elseif ($add == 'addChainSelect') {
181 $this->addChainSelect($setting, [
182 'label' => ts($props['title']),
185 elseif ($add == 'addMonthDay') {
186 $this->add('date', $setting, ts($props['title']), CRM_Core_SelectValues
::date(NULL, 'M d'));
188 elseif ($add === 'addEntityRef') {
189 $this->$add($setting, ts($props['title']), $props['entity_reference_options']);
191 elseif ($add === 'addYesNo' && ($props['type'] === 'Boolean')) {
192 $this->addRadio($setting, ts($props['title']), array(1 => 'Yes', 0 => 'No'), NULL, ' ');
195 $this->$add($setting, ts($props['title']), $options);
197 // Migrate to using an array as easier in smart...
198 $descriptions[$setting] = ts($props['description']);
199 $this->assign("{$setting}_description", ts($props['description']));
200 if ($setting == 'max_attachments') {
201 //temp hack @todo fix to get from metadata
202 $this->addRule('max_attachments', ts('Value should be a positive number'), 'positiveInteger');
204 if ($setting == 'maxFileSize') {
206 $this->addRule('maxFileSize', ts('Value should be a positive number'), 'positiveInteger');
211 // setting_description should be deprecated - see Mail.tpl for metadata based tpl.
212 $this->assign('setting_descriptions', $descriptions);
213 $this->assign('settings_fields', $settingMetaData);
214 $this->assign('fields', $settingMetaData);
218 * Get the quickform type for the given html type.
224 protected function getQuickFormType($spec) {
225 if (isset($spec['quick_form_type']) &&
226 !($spec['quick_form_type'] === 'Element' && !empty($spec['html_type']))) {
227 return $spec['quick_form_type'];
230 'checkboxes' => 'CheckBoxes',
231 'checkbox' => 'CheckBox',
233 'select' => 'Select',
234 'textarea' => 'Element',
236 'entity_reference' => 'EntityRef',
238 return $mapping[$spec['html_type']];
241 * Get the defaults for all fields defined in the metadata.
243 * All others are pending conversion.
245 protected function setDefaultsForMetadataDefinedFields() {
246 CRM_Core_BAO_ConfigSetting
::retrieve($this->_defaults
);
247 foreach (array_keys($this->_settings
) as $setting) {
248 $this->_defaults
[$setting] = civicrm_api3('setting', 'getvalue', ['name' => $setting]);
249 $spec = $this->getSettingsMetadata()[$setting];
250 if (!empty($spec['serialize'])) {
251 $this->_defaults
[$setting] = CRM_Core_DAO
::unSerializeField($this->_defaults
[$setting], $spec['serialize']);
253 if ($this->getQuickFormType($spec) === 'CheckBoxes') {
254 $this->_defaults
[$setting] = array_fill_keys($this->_defaults
[$setting], 1);
256 if ($this->getQuickFormType($spec) === 'CheckBox') {
257 $this->_defaults
[$setting] = [$setting => $this->_defaults
[$setting]];
266 protected function saveMetadataDefinedSettings($params) {
267 $settings = $this->getSettingsToSetByMetadata($params);
268 foreach ($settings as $setting => $settingValue) {
269 if ($this->getQuickFormType($this->getSettingMetadata($setting)) === 'CheckBoxes') {
270 $settings[$setting] = array_keys($settingValue);
272 if ($this->getQuickFormType($this->getSettingMetadata($setting)) === 'CheckBox') {
273 // This will be an array with one value.
274 $settings[$setting] = (int) reset($settings[$setting]);
277 civicrm_api3('setting', 'create', $settings);