f167c7a9 |
1 | <?php |
2 | /* |
3 | +--------------------------------------------------------------------+ |
4 | | CiviCRM version 5 | |
5 | +--------------------------------------------------------------------+ |
6b83d5bd |
6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
f167c7a9 |
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 |
f167c7a9 |
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 | * Build the form object. |
66 | */ |
67 | public function buildQuickForm() { |
68 | $filter = array_pop($this->urlPath); |
69 | $settings = civicrm_api3('Setting', 'getfields', [])['values']; |
70 | foreach ($settings as $key => $setting) { |
71 | if (isset($setting['settings_pages'][$filter])) { |
72 | $this->_settings[$key] = $setting; |
73 | } |
74 | } |
75 | // @todo sort settings by weight. |
76 | $this->addFieldsDefinedInSettingsMetadata(); |
77 | |
78 | // @todo look at sharing the code below in the settings trait. |
79 | if ($this->includesReadOnlyFields) { |
80 | CRM_Core_Session::setStatus(ts("Some fields are loaded as 'readonly' as they have been set (overridden) in civicrm.settings.php."), '', 'info', array('expires' => 0)); |
81 | } |
82 | |
83 | // @todo - do we still like this redirect? |
84 | CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url('civicrm/admin', 'reset=1')); |
85 | $this->addButtons(array( |
86 | array( |
87 | 'type' => 'next', |
88 | 'name' => ts('Save'), |
89 | 'isDefault' => TRUE, |
90 | ), |
91 | array( |
92 | 'type' => 'cancel', |
93 | 'name' => ts('Cancel'), |
94 | ), |
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 | } |