Convert final settings on the Display form
[civicrm-core.git] / CRM / Admin / Form / SettingTrait.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2018
32 */
33
34 /**
35 * This trait allows us to consolidate Preferences & Settings forms.
36 *
37 * It is intended mostly as part of a refactoring process to get rid of having 2.
38 */
39 trait CRM_Admin_Form_SettingTrait {
40
41 /**
42 * @var array
43 */
44 protected $settingsMetadata;
45
46 /**
47 * Get default entity.
48 *
49 * @return string
50 */
51 public function getDefaultEntity() {
52 return 'Setting';
53 }
54
55 /**
56 * Get the metadata relating to the settings on the form, ordered by the keys in $this->_settings.
57 *
58 * @return array
59 */
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);
66 }
67 return $this->settingsMetadata;
68 }
69
70 /**
71 * Get the settings which can be stored based on metadata.
72 *
73 * @param array $params
74 * @return array
75 */
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];
84 }
85 }
86 return $setValues;
87 }
88
89 /**
90 * @param $params
91 */
92 protected function filterParamsSetByMetadata(&$params) {
93 foreach ($this->getSettingsToSetByMetadata($params) as $setting => $settingGroup) {
94 //@todo array_diff this
95 unset($params[$setting]);
96 }
97 }
98
99 /**
100 * Get the metadata for a particular field.
101 *
102 * @param $setting
103 * @return mixed
104 */
105 protected function getSettingMetadata($setting) {
106 return $this->getSettingsMetaData()[$setting];
107 }
108
109 /**
110 * Get the metadata for a particular field for a particular item.
111 *
112 * e.g get 'serialize' key, if exists, for a field.
113 *
114 * @param $setting
115 * @return mixed
116 */
117 protected function getSettingMetadataItem($setting, $item) {
118 return CRM_Utils_Array::value($item, $this->getSettingsMetaData()[$setting]);
119 }
120
121 /**
122 * Add fields in the metadata to the template.
123 */
124 protected function addFieldsDefinedInSettingsMetadata() {
125 $settingMetaData = $this->getSettingsMetaData();
126 $descriptions = [];
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', [
133 'field' => $setting,
134 ])['values'];
135 }
136 if ($props['type'] === 'Boolean') {
137 $options = [$props['title'] => $props['name']];
138 }
139
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;
144 }
145
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']);
149
150 }
151 $add = 'add' . $quickFormType;
152 if ($add == 'addElement') {
153 $this->$add(
154 $props['html_type'],
155 $setting,
156 ts($props['title']),
157 ($options !== NULL) ? $options : CRM_Utils_Array::value('html_attributes', $props, []),
158 ($options !== NULL) ? CRM_Utils_Array::value('html_attributes', $props, []) : NULL
159 );
160 }
161 elseif ($add == 'addSelect') {
162 $this->addElement('select', $setting, ts($props['title']), $options, CRM_Utils_Array::value('html_attributes', $props));
163 }
164 elseif ($add == 'addCheckBox') {
165 $this->addCheckBox($setting, ts($props['title']), $options, NULL, CRM_Utils_Array::value('html_attributes', $props), NULL, NULL, ['&nbsp;&nbsp;']);
166 }
167 elseif ($add == 'addCheckBoxes') {
168 $options = array_flip($options);
169 $newOptions = [];
170 foreach ($options as $key => $val) {
171 $newOptions[$key] = $val;
172 }
173 $this->addCheckBox($setting,
174 $props['title'],
175 $newOptions,
176 NULL, NULL, NULL, NULL,
177 ['&nbsp;&nbsp;', '&nbsp;&nbsp;', '<br/>']
178 );
179 }
180 elseif ($add == 'addChainSelect') {
181 $this->addChainSelect($setting, [
182 'label' => ts($props['title']),
183 ]);
184 }
185 elseif ($add == 'addMonthDay') {
186 $this->add('date', $setting, ts($props['title']), CRM_Core_SelectValues::date(NULL, 'M d'));
187 }
188 elseif ($add === 'addEntityRef') {
189 $this->$add($setting, ts($props['title']), $props['entity_reference_options']);
190 }
191 elseif ($add === 'addYesNo' && ($props['type'] === 'Boolean')) {
192 $this->addRadio($setting, ts($props['title']), array(1 => 'Yes', 0 => 'No'), NULL, '&nbsp;&nbsp;');
193 }
194 else {
195 $this->$add($setting, ts($props['title']), $options);
196 }
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');
203 }
204 if ($setting == 'maxFileSize') {
205 //temp hack
206 $this->addRule('maxFileSize', ts('Value should be a positive number'), 'positiveInteger');
207 }
208
209 }
210 }
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);
215 }
216
217 /**
218 * Get the quickform type for the given html type.
219 *
220 * @param array $spec
221 *
222 * @return string
223 */
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'];
228 }
229 $mapping = [
230 'checkboxes' => 'CheckBoxes',
231 'checkbox' => 'CheckBox',
232 'radio' => 'Radio',
233 'select' => 'Select',
234 'textarea' => 'Element',
235 'text' => 'Element',
236 'entity_reference' => 'EntityRef',
237 ];
238 return $mapping[$spec['html_type']];
239 }
240 /**
241 * Get the defaults for all fields defined in the metadata.
242 *
243 * All others are pending conversion.
244 */
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']);
252 }
253 if ($this->getQuickFormType($spec) === 'CheckBoxes') {
254 $this->_defaults[$setting] = array_fill_keys($this->_defaults[$setting], 1);
255 }
256 if ($this->getQuickFormType($spec) === 'CheckBox') {
257 $this->_defaults[$setting] = [$setting => $this->_defaults[$setting]];
258 }
259 }
260 }
261
262 /**
263 * @param $params
264 *
265 */
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);
271 }
272 if ($this->getQuickFormType($this->getSettingMetadata($setting)) === 'CheckBox') {
273 // This will be an array with one value.
274 $settings[$setting] = (int) reset($settings[$setting]);
275 }
276 }
277 civicrm_api3('setting', 'create', $settings);
278 }
279
280 }