Merge pull request #20093 from larssandergreen/mailings-AB-test-improvements
[civicrm-core.git] / CRM / Admin / Form / Extensions.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 * This class generates form components for Extensions.
20 */
21 class CRM_Admin_Form_Extensions extends CRM_Admin_Form {
22
23 /**
24 * @var string
25 */
26 private $_key;
27
28 /**
29 * @var string
30 */
31 private $label;
32
33 /**
34 * Form pre-processing.
35 */
36 public function preProcess() {
37 parent::preProcess();
38
39 $mainPage = new CRM_Admin_Page_Extensions();
40 $localExtensionRows = $mainPage->formatLocalExtensionRows();
41 $this->assign('localExtensionRows', $localExtensionRows);
42
43 $remoteExtensionRows = $mainPage->formatRemoteExtensionRows($localExtensionRows);
44 $this->assign('remoteExtensionRows', $remoteExtensionRows);
45
46 $this->_key = CRM_Utils_Request::retrieve('key', 'String',
47 $this, FALSE, 0
48 );
49 if (!CRM_Utils_Type::validate($this->_key, 'ExtensionKey') && !empty($this->_key)) {
50 throw new CRM_Core_Exception('Extension Key does not match expected standard');
51 }
52 $this->label = $remoteExtensionRows[$this->_key]['label'] ?? $this->_key;
53 $session = CRM_Core_Session::singleton();
54 $url = CRM_Utils_System::url('civicrm/admin/extensions', 'reset=1&action=browse');
55 $session->pushUserContext($url);
56 $this->assign('id', $this->_id);
57 $this->assign('key', $this->_key);
58
59 switch ($this->_action) {
60 case CRM_Core_Action::ADD:
61 case CRM_Core_Action::DELETE:
62 case CRM_Core_Action::ENABLE:
63 case CRM_Core_Action::DISABLE:
64 $info = CRM_Extension_System::singleton()->getMapper()->keyToInfo($this->_key);
65 $extInfo = CRM_Admin_Page_Extensions::createExtendedInfo($info);
66 $this->assign('extension', $extInfo);
67 break;
68
69 case CRM_Core_Action::UPDATE:
70 if (!CRM_Extension_System::singleton()->getBrowser()->isEnabled()) {
71 CRM_Core_Error::statusBounce(ts('The system administrator has disabled this feature.'));
72 }
73 $info = CRM_Extension_System::singleton()->getBrowser()->getExtension($this->_key);
74 $extInfo = CRM_Admin_Page_Extensions::createExtendedInfo($info);
75 $this->assign('extension', $extInfo);
76 break;
77
78 default:
79 CRM_Core_Error::statusBounce(ts('Unsupported action'));
80 }
81
82 }
83
84 /**
85 * Set default values for the form.
86 */
87 public function setDefaultValues() {
88 $defaults = [];
89 return $defaults;
90 }
91
92 /**
93 * Build the form object.
94 */
95 public function buildQuickForm() {
96 switch ($this->_action) {
97 case CRM_Core_Action::ADD:
98 $buttonName = ts('Install');
99 $title = ts('Install "%1"?', [
100 1 => $this->label,
101 ]);
102 break;
103
104 case CRM_Core_Action::UPDATE:
105 $buttonName = ts('Download and Install');
106 $title = ts('Download and Install "%1"?', [
107 1 => $this->label,
108 ]);
109 break;
110
111 case CRM_Core_Action::DELETE:
112 $buttonName = ts('Uninstall');
113 $title = ts('Uninstall "%1"?', [
114 1 => $this->label,
115 ]);
116 break;
117
118 case CRM_Core_Action::ENABLE:
119 $buttonName = ts('Enable');
120 $title = ts('Enable "%1"?', [
121 1 => $this->label,
122 ]);
123 break;
124
125 case CRM_Core_Action::DISABLE:
126 $buttonName = ts('Disable');
127 $title = ts('Disable "%1"?', [
128 1 => $this->label,
129 ]);
130 break;
131 }
132
133 $this->assign('title', $title);
134 $this->addButtons([
135 [
136 'type' => 'next',
137 'name' => $buttonName,
138 'isDefault' => TRUE,
139 ],
140 [
141 'type' => 'cancel',
142 'name' => ts('Cancel'),
143 ],
144 ]);
145 }
146
147 /**
148 * Global form rule.
149 *
150 * @param array $fields
151 * The input form values.
152 * @param array $files
153 * The uploaded files if any.
154 * @param self $self
155 * This object.
156 *
157 * @return bool|array
158 * true if no errors, else an array of errors
159 */
160 public static function formRule($fields, $files, $self) {
161 $errors = [];
162
163 return empty($errors) ? TRUE : $errors;
164 }
165
166 /**
167 * Process the form submission.
168 */
169 public function postProcess() {
170 CRM_Utils_System::flushCache();
171
172 if ($this->_action & CRM_Core_Action::DELETE) {
173 try {
174 CRM_Extension_System::singleton()->getManager()->uninstall([$this->_key]);
175 CRM_Core_Session::setStatus("", ts('Extension Uninstalled'), "success");
176 }
177 catch (CRM_Extension_Exception_DependencyException $e) {
178 // currently only thrown for payment-processor dependencies
179 CRM_Core_Session::setStatus(ts('Cannot uninstall this extension - there is at least one payment processor using the payment processor type provided by it.'), ts('Uninstall Error'), 'error');
180 }
181 }
182
183 if ($this->_action & CRM_Core_Action::ADD) {
184 civicrm_api3('Extension', 'install', ['keys' => $this->_key]);
185 CRM_Core_Session::setStatus("", ts('Extension Installed'), "success");
186 }
187
188 if ($this->_action & CRM_Core_Action::ENABLE) {
189 civicrm_api3('Extension', 'enable', ['keys' => $this->_key]);
190 CRM_Core_Session::setStatus("", ts('Extension Enabled'), "success");
191 }
192
193 if ($this->_action & CRM_Core_Action::DISABLE) {
194 CRM_Extension_System::singleton()->getManager()->disable([$this->_key]);
195 CRM_Core_Session::setStatus("", ts('Extension Disabled'), "success");
196 }
197
198 if ($this->_action & CRM_Core_Action::UPDATE) {
199 $result = civicrm_api('Extension', 'download', [
200 'version' => 3,
201 'key' => $this->_key,
202 ]);
203 if (empty($result['is_error'])) {
204 CRM_Core_Session::setStatus("", ts('Extension Upgraded'), "success");
205 }
206 else {
207 CRM_Core_Session::setStatus($result['error_message'], ts('Extension Upgrade Failed'), "error");
208 }
209 }
210
211 CRM_Utils_System::redirect(
212 CRM_Utils_System::url(
213 'civicrm/admin/extensions',
214 'reset=1&action=browse'
215 )
216 );
217 }
218
219 }