Set version to 5.51.alpha1
[civicrm-core.git] / CRM / Admin / Form / Preferences.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
18/**
ce064e4f 19 * Base class for settings forms.
6a488035
TO
20 */
21class CRM_Admin_Form_Preferences extends CRM_Core_Form {
a55c9b35 22
23 use CRM_Admin_Form_SettingTrait;
24
6a488035
TO
25 protected $_system = FALSE;
26 protected $_contactID = NULL;
3a936dab 27 public $_action = NULL;
6a488035 28
6a488035
TO
29 protected $_params = NULL;
30
3fbe15d9 31 /**
32 * Preprocess form.
33 *
34 * @throws \CRM_Core_Exception
35 */
00be9182 36 public function preProcess() {
3fbe15d9 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.
6a488035
TO
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 );
6a488035
TO
49
50 if ($this->_system) {
51 if (CRM_Core_Permission::check('administer CiviCRM')) {
52 $this->_contactID = NULL;
53 }
54 else {
3fbe15d9 55 throw new CRM_Core_Exception('You do not have permission to edit preferences');
6a488035 56 }
6a488035
TO
57 }
58 else {
59 if (!$this->_contactID) {
3fbe15d9 60 $this->_contactID = CRM_Core_Session::getLoggedInContactID();
6a488035 61 if (!$this->_contactID) {
3fbe15d9 62 throw new CRM_Core_Exception('Could not retrieve contact id');
6a488035
TO
63 }
64 $this->set('cid', $this->_contactID);
65 }
6a488035
TO
66 }
67
a55c9b35 68 $this->addFieldsDefinedInSettingsMetadata();
3fbe15d9 69 CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url('civicrm/admin', 'reset=1'));
6a488035
TO
70 }
71
e0ef6999
EM
72 /**
73 * @return array
74 */
00be9182 75 public function setDefaultValues() {
be2fb01f 76 $this->_defaults = [];
601361a3 77 $this->setDefaultsForMetadataDefinedFields();
601361a3 78 return $this->_defaults;
6a488035
TO
79 }
80
6a488035 81 /**
eceb18cc 82 * Build the form object.
6a488035
TO
83 */
84 public function buildQuickForm() {
85 parent::buildQuickForm();
86
be2fb01f 87 $this->addButtons([
0d48f1cc
TO
88 [
89 'type' => 'next',
90 'name' => ts('Save'),
91 'isDefault' => TRUE,
92 ],
93 [
94 'type' => 'cancel',
95 'name' => ts('Cancel'),
96 ],
97 ]);
6a488035
TO
98
99 if ($this->_action == CRM_Core_Action::VIEW) {
100 $this->freeze();
101 }
102 }
103
104 /**
eceb18cc 105 * Process the form submission.
6a488035
TO
106 */
107 public function postProcess() {
6a488035
TO
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 }
6a488035
TO
116
117 /**
eceb18cc 118 * Process the form submission.
6a488035
TO
119 */
120 public function postProcessCommon() {
6821aa1d 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
6a488035
TO
129 CRM_Core_Session::setStatus(ts('Your changes have been saved.'), ts('Saved'), 'success');
130 }
131
132}