Merge pull request #15808 from civicrm/5.20
[civicrm-core.git] / CRM / Admin / Form / Generic.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2020 |
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-2020
32 */
33
34 /**
35 * Generic metadata based settings form.
36 *
37 * The form filter will determine the settings displayed.
38 */
39 class CRM_Admin_Form_Generic extends CRM_Core_Form {
40 use CRM_Admin_Form_SettingTrait;
41
42 protected $_settings = [];
43 protected $includesReadOnlyFields = FALSE;
44 public $_defaults = [];
45
46 /**
47 * Get the tpl file name.
48 *
49 * @return string
50 */
51 public function getTemplateFileName() {
52 return 'CRM/Form/basicForm.tpl';
53 }
54
55 /**
56 * Set default values for the form.
57 *
58 * Default values are retrieved from the database.
59 */
60 public function setDefaultValues() {
61 $this->setDefaultsForMetadataDefinedFields();
62 return $this->_defaults;
63 }
64
65 /**
66 * Build the form object.
67 */
68 public function buildQuickForm() {
69 $filter = $this->getSettingPageFilter();
70 $settings = civicrm_api3('Setting', 'getfields', [])['values'];
71 foreach ($settings as $key => $setting) {
72 if (isset($setting['settings_pages'][$filter])) {
73 $this->_settings[$key] = $setting;
74 }
75 }
76
77 $this->addFieldsDefinedInSettingsMetadata();
78
79 // @todo look at sharing the code below in the settings trait.
80 if ($this->includesReadOnlyFields) {
81 CRM_Core_Session::setStatus(ts("Some fields are loaded as 'readonly' as they have been set (overridden) in civicrm.settings.php."), '', 'info', ['expires' => 0]);
82 }
83
84 // @todo - do we still like this redirect?
85 CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url('civicrm/admin', 'reset=1'));
86 $this->addButtons([
87 [
88 'type' => 'next',
89 'name' => ts('Save'),
90 'isDefault' => TRUE,
91 ],
92 [
93 'type' => 'cancel',
94 'name' => ts('Cancel'),
95 ],
96 ]);
97 }
98
99 /**
100 * Process the form submission.
101 */
102 public function postProcess() {
103 $params = $this->controller->exportValues($this->_name);
104 try {
105 $this->saveMetadataDefinedSettings($params);
106 }
107 catch (CiviCRM_API3_Exception $e) {
108 CRM_Core_Session::setStatus($e->getMessage(), ts('Save Failed'), 'error');
109 }
110 }
111
112 }