946389fb |
1 | <?php |
2 | /* |
3 | +--------------------------------------------------------------------+ |
4 | | CiviCRM version 5 | |
5 | +--------------------------------------------------------------------+ |
6b83d5bd |
6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
946389fb |
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 |
6b83d5bd |
31 | * @copyright CiviCRM LLC (c) 2004-2019 |
946389fb |
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) { |
74f89a9f |
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; |
946389fb |
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 | |
6821aa1d |
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 | |
e894ae15 |
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) { |
c5af8245 |
128 | $quickFormType = $this->getQuickFormType($props); |
129 | if (isset($quickFormType)) { |
719eda4a |
130 | $options = CRM_Utils_Array::value('options', $props); |
e894ae15 |
131 | if (isset($props['pseudoconstant'])) { |
132 | $options = civicrm_api3('Setting', 'getoptions', [ |
133 | 'field' => $setting, |
c89a43b3 |
134 | ])['values']; |
e894ae15 |
135 | } |
c89a43b3 |
136 | if ($props['type'] === 'Boolean') { |
137 | $options = [$props['title'] => $props['name']]; |
e894ae15 |
138 | } |
c89a43b3 |
139 | |
e894ae15 |
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 | |
c5af8245 |
146 | $add = 'add' . $quickFormType; |
e894ae15 |
147 | if ($add == 'addElement') { |
148 | $this->$add( |
149 | $props['html_type'], |
150 | $setting, |
151 | ts($props['title']), |
c89a43b3 |
152 | ($options !== NULL) ? $options : CRM_Utils_Array::value('html_attributes', $props, []), |
e894ae15 |
153 | ($options !== NULL) ? CRM_Utils_Array::value('html_attributes', $props, []) : NULL |
154 | ); |
155 | } |
156 | elseif ($add == 'addSelect') { |
c89a43b3 |
157 | $this->addElement('select', $setting, ts($props['title']), $options, CRM_Utils_Array::value('html_attributes', $props)); |
e894ae15 |
158 | } |
159 | elseif ($add == 'addCheckBox') { |
969afb18 |
160 | $this->addCheckBox($setting, '', $options, NULL, CRM_Utils_Array::value('html_attributes', $props), NULL, NULL, [' ']); |
e894ae15 |
161 | } |
a55c9b35 |
162 | elseif ($add == 'addCheckBoxes') { |
c89a43b3 |
163 | $options = array_flip($options); |
a55c9b35 |
164 | $newOptions = []; |
165 | foreach ($options as $key => $val) { |
166 | $newOptions[$key] = $val; |
167 | } |
168 | $this->addCheckBox($setting, |
169 | $props['title'], |
170 | $newOptions, |
171 | NULL, NULL, NULL, NULL, |
172 | [' ', ' ', '<br/>'] |
173 | ); |
174 | } |
e894ae15 |
175 | elseif ($add == 'addChainSelect') { |
176 | $this->addChainSelect($setting, [ |
177 | 'label' => ts($props['title']), |
178 | ]); |
179 | } |
180 | elseif ($add == 'addMonthDay') { |
181 | $this->add('date', $setting, ts($props['title']), CRM_Core_SelectValues::date(NULL, 'M d')); |
182 | } |
eba92929 |
183 | elseif ($add === 'addEntityRef') { |
184 | $this->$add($setting, ts($props['title']), $props['entity_reference_options']); |
185 | } |
c7cd4e2c |
186 | elseif ($add === 'addYesNo' && ($props['type'] === 'Boolean')) { |
187 | $this->addRadio($setting, ts($props['title']), array(1 => 'Yes', 0 => 'No'), NULL, ' '); |
188 | } |
e894ae15 |
189 | else { |
c89a43b3 |
190 | $this->$add($setting, ts($props['title']), $options); |
e894ae15 |
191 | } |
192 | // Migrate to using an array as easier in smart... |
f8857611 |
193 | $description = CRM_Utils_Array::value('description', $props); |
194 | $descriptions[$setting] = $description; |
195 | $this->assign("{$setting}_description", $description); |
e894ae15 |
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'); |
199 | } |
200 | if ($setting == 'maxFileSize') { |
201 | //temp hack |
202 | $this->addRule('maxFileSize', ts('Value should be a positive number'), 'positiveInteger'); |
203 | } |
204 | |
205 | } |
206 | } |
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); |
b70c6629 |
210 | $this->assign('fields', $settingMetaData); |
e894ae15 |
211 | } |
212 | |
c5af8245 |
213 | /** |
214 | * Get the quickform type for the given html type. |
215 | * |
216 | * @param array $spec |
217 | * |
218 | * @return string |
219 | */ |
220 | protected function getQuickFormType($spec) { |
0e700ee7 |
221 | if (isset($spec['quick_form_type']) && |
222 | !($spec['quick_form_type'] === 'Element' && !empty($spec['html_type']))) { |
5c33bd6b |
223 | // This is kinda transitional |
c5af8245 |
224 | return $spec['quick_form_type']; |
225 | } |
5c33bd6b |
226 | |
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']); |
233 | } |
c5af8245 |
234 | $mapping = [ |
235 | 'checkboxes' => 'CheckBoxes', |
c89a43b3 |
236 | 'checkbox' => 'CheckBox', |
c5af8245 |
237 | 'radio' => 'Radio', |
b70c6629 |
238 | 'select' => 'Select', |
7399a0a6 |
239 | 'textarea' => 'Element', |
a7e15692 |
240 | 'text' => 'Element', |
eba92929 |
241 | 'entity_reference' => 'EntityRef', |
5c33bd6b |
242 | 'advmultiselect' => 'Element', |
c5af8245 |
243 | ]; |
5c33bd6b |
244 | return $mapping[$htmlType]; |
c5af8245 |
245 | } |
601361a3 |
246 | /** |
247 | * Get the defaults for all fields defined in the metadata. |
248 | * |
249 | * All others are pending conversion. |
250 | */ |
251 | protected function setDefaultsForMetadataDefinedFields() { |
252 | CRM_Core_BAO_ConfigSetting::retrieve($this->_defaults); |
6821aa1d |
253 | foreach (array_keys($this->_settings) as $setting) { |
601361a3 |
254 | $this->_defaults[$setting] = civicrm_api3('setting', 'getvalue', ['name' => $setting]); |
6821aa1d |
255 | $spec = $this->getSettingsMetadata()[$setting]; |
256 | if (!empty($spec['serialize'])) { |
257 | $this->_defaults[$setting] = CRM_Core_DAO::unSerializeField($this->_defaults[$setting], $spec['serialize']); |
258 | } |
c5af8245 |
259 | if ($this->getQuickFormType($spec) === 'CheckBoxes') { |
6821aa1d |
260 | $this->_defaults[$setting] = array_fill_keys($this->_defaults[$setting], 1); |
261 | } |
74f89a9f |
262 | if ($this->getQuickFormType($spec) === 'CheckBox') { |
263 | $this->_defaults[$setting] = [$setting => $this->_defaults[$setting]]; |
264 | } |
6821aa1d |
265 | } |
266 | } |
267 | |
268 | /** |
269 | * @param $params |
270 | * |
271 | */ |
272 | protected function saveMetadataDefinedSettings($params) { |
273 | $settings = $this->getSettingsToSetByMetadata($params); |
274 | foreach ($settings as $setting => $settingValue) { |
2e94f477 |
275 | if ($this->getQuickFormType($this->getSettingMetadata($setting)) === 'CheckBoxes') { |
6821aa1d |
276 | $settings[$setting] = array_keys($settingValue); |
277 | } |
74f89a9f |
278 | if ($this->getQuickFormType($this->getSettingMetadata($setting)) === 'CheckBox') { |
279 | // This will be an array with one value. |
280 | $settings[$setting] = (int) reset($settings[$setting]); |
281 | } |
601361a3 |
282 | } |
6821aa1d |
283 | civicrm_api3('setting', 'create', $settings); |
601361a3 |
284 | } |
285 | |
946389fb |
286 | } |