Merge pull request #16424 from eileenmcnaughton/cont_setting_handling
[civicrm-core.git] / CRM / Admin / Form / Preferences.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 * Base class for settings forms.
20 */
21 class CRM_Admin_Form_Preferences extends CRM_Core_Form {
22
23 use CRM_Admin_Form_SettingTrait;
24
25 protected $_system = FALSE;
26 protected $_contactID = NULL;
27 public $_action = NULL;
28
29 protected $_params = NULL;
30
31 /**
32 * Preprocess form.
33 *
34 * @throws \CRM_Core_Exception
35 */
36 public function preProcess() {
37 // @todo - it's likely the only 'current' code in this function is the line
38 // $this->addFieldsDefinedInSettingsMetadata(); and this class is no different to CRM_Admin_Form_Setting
39 // in any meaningful way.
40 $this->_contactID = CRM_Utils_Request::retrieve('cid', 'Positive',
41 $this, FALSE
42 );
43 $this->_system = CRM_Utils_Request::retrieve('system', 'Boolean',
44 $this, FALSE, TRUE
45 );
46 $this->_action = CRM_Utils_Request::retrieve('action', 'String',
47 $this, FALSE, 'update'
48 );
49
50 if ($this->_system) {
51 if (CRM_Core_Permission::check('administer CiviCRM')) {
52 $this->_contactID = NULL;
53 }
54 else {
55 throw new CRM_Core_Exception('You do not have permission to edit preferences');
56 }
57 }
58 else {
59 if (!$this->_contactID) {
60 $this->_contactID = CRM_Core_Session::getLoggedInContactID();
61 if (!$this->_contactID) {
62 throw new CRM_Core_Exception('Could not retrieve contact id');
63 }
64 $this->set('cid', $this->_contactID);
65 }
66 }
67
68 $this->addFieldsDefinedInSettingsMetadata();
69 CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url('civicrm/admin', 'reset=1'));
70 }
71
72 /**
73 * @return array
74 */
75 public function setDefaultValues() {
76 $this->_defaults = [];
77 $this->setDefaultsForMetadataDefinedFields();
78 return $this->_defaults;
79 }
80
81 /**
82 * Build the form object.
83 */
84 public function buildQuickForm() {
85 parent::buildQuickForm();
86
87 $this->addButtons([
88 [
89 'type' => 'next',
90 'name' => ts('Save'),
91 'isDefault' => TRUE,
92 ],
93 [
94 'type' => 'cancel',
95 'name' => ts('Cancel'),
96 ],
97 ]);
98
99 if ($this->_action == CRM_Core_Action::VIEW) {
100 $this->freeze();
101 }
102 }
103
104 /**
105 * Process the form submission.
106 */
107 public function postProcess() {
108 if ($this->_action == CRM_Core_Action::VIEW) {
109 return;
110 }
111
112 $this->_params = $this->controller->exportValues($this->_name);
113
114 $this->postProcessCommon();
115 }
116
117 /**
118 * Process the form submission.
119 */
120 public function postProcessCommon() {
121 try {
122 $this->saveMetadataDefinedSettings($this->_params);
123 $this->filterParamsSetByMetadata($this->_params);
124 }
125 catch (CiviCRM_API3_Exception $e) {
126 CRM_Core_Session::setStatus($e->getMessage(), ts('Save Failed'), 'error');
127 }
128
129 // Update any settings stored in dynamic js
130 CRM_Core_Resources::singleton()->resetCacheCode();
131
132 CRM_Core_Session::setStatus(ts('Your changes have been saved.'), ts('Saved'), 'success');
133 }
134
135 }