Merge pull request #18506 from totten/master-bundle-array
[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 $this->addFieldsDefinedInSettingsMetadata();
54
55 // @todo look at sharing the code below in the settings trait.
56 if ($this->includesReadOnlyFields) {
57 CRM_Core_Session::setStatus(ts("Some fields are loaded as 'readonly' as they have been set (overridden) in civicrm.settings.php."), '', 'info', ['expires' => 0]);
58 }
59
60 // @todo - do we still like this redirect?
61 CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url('civicrm/admin', 'reset=1'));
62 $this->addButtons([
63 [
64 'type' => 'next',
65 'name' => ts('Save'),
66 'isDefault' => TRUE,
67 ],
68 [
69 'type' => 'cancel',
70 'name' => ts('Cancel'),
71 ],
72 ]);
73 }
74
75 /**
76 * Process the form submission.
77 */
78 public function postProcess() {
79 $params = $this->controller->exportValues($this->_name);
80 try {
81 $this->saveMetadataDefinedSettings($params);
82 }
83 catch (CiviCRM_API3_Exception $e) {
84 CRM_Core_Session::setStatus($e->getMessage(), ts('Save Failed'), 'error');
85 }
86 }
87
88 }