Merge pull request #15523 from jamie-tillman/patch-1
[civicrm-core.git] / CRM / Admin / Form / Generic.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * Generic metadata based settings form.
20 *
21 * The form filter will determine the settings displayed.
22 */
23 class CRM_Admin_Form_Generic extends CRM_Core_Form {
24 use CRM_Admin_Form_SettingTrait;
25
26 protected $_settings = [];
27 protected $includesReadOnlyFields = FALSE;
28 public $_defaults = [];
29
30 /**
31 * Get the tpl file name.
32 *
33 * @return string
34 */
35 public function getTemplateFileName() {
36 return 'CRM/Form/basicForm.tpl';
37 }
38
39 /**
40 * Set default values for the form.
41 *
42 * Default values are retrieved from the database.
43 */
44 public function setDefaultValues() {
45 $this->setDefaultsForMetadataDefinedFields();
46 return $this->_defaults;
47 }
48
49 /**
50 * Build the form object.
51 */
52 public function buildQuickForm() {
53 $filter = $this->getSettingPageFilter();
54 $settings = civicrm_api3('Setting', 'getfields', [])['values'];
55 foreach ($settings as $key => $setting) {
56 if (isset($setting['settings_pages'][$filter])) {
57 $this->_settings[$key] = $setting;
58 }
59 }
60
61 $this->addFieldsDefinedInSettingsMetadata();
62
63 // @todo look at sharing the code below in the settings trait.
64 if ($this->includesReadOnlyFields) {
65 CRM_Core_Session::setStatus(ts("Some fields are loaded as 'readonly' as they have been set (overridden) in civicrm.settings.php."), '', 'info', ['expires' => 0]);
66 }
67
68 // @todo - do we still like this redirect?
69 CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url('civicrm/admin', 'reset=1'));
70 $this->addButtons([
71 [
72 'type' => 'next',
73 'name' => ts('Save'),
74 'isDefault' => TRUE,
75 ],
76 [
77 'type' => 'cancel',
78 'name' => ts('Cancel'),
79 ],
80 ]);
81 }
82
83 /**
84 * Process the form submission.
85 */
86 public function postProcess() {
87 $params = $this->controller->exportValues($this->_name);
88 try {
89 $this->saveMetadataDefinedSettings($params);
90 }
91 catch (CiviCRM_API3_Exception $e) {
92 CRM_Core_Session::setStatus($e->getMessage(), ts('Save Failed'), 'error');
93 }
94 }
95
96 }