734eccc83098fd3eaa1df04efde53e0c6f7e3b11
[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 return array_intersect_key($params, $this->_settings);
78 }
79
80 /**
81 * @param $params
82 */
83 protected function filterParamsSetByMetadata(&$params) {
84 foreach ($this->getSettingsToSetByMetadata($params) as $setting => $settingGroup) {
85 //@todo array_diff this
86 unset($params[$setting]);
87 }
88 }
89
90 /**
91 * Add fields in the metadata to the template.
92 */
93 protected function addFieldsDefinedInSettingsMetadata() {
94 $settingMetaData = $this->getSettingsMetaData();
95 $descriptions = [];
96 foreach ($settingMetaData as $setting => $props) {
97 if (isset($props['quick_form_type'])) {
98 if (isset($props['pseudoconstant'])) {
99 $options = civicrm_api3('Setting', 'getoptions', [
100 'field' => $setting,
101 ]);
102 }
103 else {
104 $options = NULL;
105 }
106 //Load input as readonly whose values are overridden in civicrm.settings.php.
107 if (Civi::settings()->getMandatory($setting)) {
108 $props['html_attributes']['readonly'] = TRUE;
109 $this->includesReadOnlyFields = TRUE;
110 }
111
112 $add = 'add' . $props['quick_form_type'];
113 if ($add == 'addElement') {
114 $this->$add(
115 $props['html_type'],
116 $setting,
117 ts($props['title']),
118 ($options !== NULL) ? $options['values'] : CRM_Utils_Array::value('html_attributes', $props, []),
119 ($options !== NULL) ? CRM_Utils_Array::value('html_attributes', $props, []) : NULL
120 );
121 }
122 elseif ($add == 'addSelect') {
123 $this->addElement('select', $setting, ts($props['title']), $options['values'], CRM_Utils_Array::value('html_attributes', $props));
124 }
125 elseif ($add == 'addCheckBox') {
126 $this->addCheckBox($setting, ts($props['title']), $options['values'], NULL, CRM_Utils_Array::value('html_attributes', $props), NULL, NULL, ['&nbsp;&nbsp;']);
127 }
128 elseif ($add == 'addChainSelect') {
129 $this->addChainSelect($setting, [
130 'label' => ts($props['title']),
131 ]);
132 }
133 elseif ($add == 'addMonthDay') {
134 $this->add('date', $setting, ts($props['title']), CRM_Core_SelectValues::date(NULL, 'M d'));
135 }
136 else {
137 $this->$add($setting, ts($props['title']));
138 }
139 // Migrate to using an array as easier in smart...
140 $descriptions[$setting] = ts($props['description']);
141 $this->assign("{$setting}_description", ts($props['description']));
142 if ($setting == 'max_attachments') {
143 //temp hack @todo fix to get from metadata
144 $this->addRule('max_attachments', ts('Value should be a positive number'), 'positiveInteger');
145 }
146 if ($setting == 'maxFileSize') {
147 //temp hack
148 $this->addRule('maxFileSize', ts('Value should be a positive number'), 'positiveInteger');
149 }
150
151 }
152 }
153 // setting_description should be deprecated - see Mail.tpl for metadata based tpl.
154 $this->assign('setting_descriptions', $descriptions);
155 $this->assign('settings_fields', $settingMetaData);
156 }
157
158 }