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