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 | } | |
b78bc9e4 | 52 | $this->label = $remoteExtensionRows[$this->_key]['label'] ?? $this->_key; |
6a488035 TO |
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 | ||
6a488035 TO |
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: | |
353ffa53 | 70 | if (!CRM_Extension_System::singleton()->getBrowser()->isEnabled()) { |
fbdcf459 | 71 | CRM_Core_Error::statusBounce(ts('The system administrator has disabled this feature.')); |
6a488035 TO |
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: | |
fbdcf459 | 79 | CRM_Core_Error::statusBounce(ts('Unsupported action')); |
6a488035 TO |
80 | } |
81 | ||
82 | } | |
83 | ||
84 | /** | |
c490a46a | 85 | * Set default values for the form. |
6a488035 | 86 | */ |
00be9182 | 87 | public function setDefaultValues() { |
be2fb01f | 88 | $defaults = []; |
6a488035 TO |
89 | return $defaults; |
90 | } | |
91 | ||
92 | /** | |
eceb18cc | 93 | * Build the form object. |
6a488035 TO |
94 | */ |
95 | public function buildQuickForm() { | |
6a488035 TO |
96 | switch ($this->_action) { |
97 | case CRM_Core_Action::ADD: | |
98 | $buttonName = ts('Install'); | |
be2fb01f | 99 | $title = ts('Install "%1"?', [ |
b78bc9e4 | 100 | 1 => $this->label, |
be2fb01f | 101 | ]); |
6a488035 TO |
102 | break; |
103 | ||
104 | case CRM_Core_Action::UPDATE: | |
105 | $buttonName = ts('Download and Install'); | |
be2fb01f | 106 | $title = ts('Download and Install "%1"?', [ |
b78bc9e4 | 107 | 1 => $this->label, |
be2fb01f | 108 | ]); |
6a488035 TO |
109 | break; |
110 | ||
111 | case CRM_Core_Action::DELETE: | |
112 | $buttonName = ts('Uninstall'); | |
be2fb01f | 113 | $title = ts('Uninstall "%1"?', [ |
b78bc9e4 | 114 | 1 => $this->label, |
be2fb01f | 115 | ]); |
6a488035 TO |
116 | break; |
117 | ||
118 | case CRM_Core_Action::ENABLE: | |
119 | $buttonName = ts('Enable'); | |
be2fb01f | 120 | $title = ts('Enable "%1"?', [ |
b78bc9e4 | 121 | 1 => $this->label, |
be2fb01f | 122 | ]); |
6a488035 TO |
123 | break; |
124 | ||
125 | case CRM_Core_Action::DISABLE: | |
affc7312 | 126 | $buttonName = ts('Disable'); |
be2fb01f | 127 | $title = ts('Disable "%1"?', [ |
b78bc9e4 | 128 | 1 => $this->label, |
be2fb01f | 129 | ]); |
6a488035 TO |
130 | break; |
131 | } | |
132 | ||
133 | $this->assign('title', $title); | |
be2fb01f | 134 | $this->addButtons([ |
0d48f1cc TO |
135 | [ |
136 | 'type' => 'next', | |
137 | 'name' => $buttonName, | |
138 | 'isDefault' => TRUE, | |
139 | ], | |
140 | [ | |
141 | 'type' => 'cancel', | |
142 | 'name' => ts('Cancel'), | |
143 | ], | |
144 | ]); | |
6a488035 TO |
145 | } |
146 | ||
147 | /** | |
eceb18cc | 148 | * Global form rule. |
6a488035 | 149 | * |
5173bd95 TO |
150 | * @param array $fields |
151 | * The input form values. | |
152 | * @param array $files | |
153 | * The uploaded files if any. | |
154 | * @param array $self | |
155 | * This object. | |
6a488035 | 156 | * |
72b3a70c CW |
157 | * @return bool|array |
158 | * true if no errors, else an array of errors | |
6a488035 | 159 | */ |
00be9182 | 160 | public static function formRule($fields, $files, $self) { |
be2fb01f | 161 | $errors = []; |
6a488035 TO |
162 | |
163 | return empty($errors) ? TRUE : $errors; | |
164 | } | |
165 | ||
166 | /** | |
eceb18cc | 167 | * Process the form submission. |
6a488035 TO |
168 | */ |
169 | public function postProcess() { | |
170 | CRM_Utils_System::flushCache(); | |
171 | ||
172 | if ($this->_action & CRM_Core_Action::DELETE) { | |
173 | try { | |
be2fb01f | 174 | CRM_Extension_System::singleton()->getManager()->uninstall([$this->_key]); |
6a488035 | 175 | CRM_Core_Session::setStatus("", ts('Extension Uninstalled'), "success"); |
0db6c3e1 TO |
176 | } |
177 | catch (CRM_Extension_Exception_DependencyException $e) { | |
6a488035 TO |
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) { | |
be2fb01f | 184 | civicrm_api3('Extension', 'install', ['keys' => $this->_key]); |
6a488035 TO |
185 | CRM_Core_Session::setStatus("", ts('Extension Installed'), "success"); |
186 | } | |
187 | ||
188 | if ($this->_action & CRM_Core_Action::ENABLE) { | |
be2fb01f | 189 | civicrm_api3('Extension', 'enable', ['keys' => $this->_key]); |
6a488035 TO |
190 | CRM_Core_Session::setStatus("", ts('Extension Enabled'), "success"); |
191 | } | |
192 | ||
193 | if ($this->_action & CRM_Core_Action::DISABLE) { | |
be2fb01f | 194 | CRM_Extension_System::singleton()->getManager()->disable([$this->_key]); |
6a488035 TO |
195 | CRM_Core_Session::setStatus("", ts('Extension Disabled'), "success"); |
196 | } | |
197 | ||
198 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
be2fb01f | 199 | $result = civicrm_api('Extension', 'download', [ |
6a488035 TO |
200 | 'version' => 3, |
201 | 'key' => $this->_key, | |
be2fb01f | 202 | ]); |
de6c59ca | 203 | if (empty($result['is_error'])) { |
6a488035 | 204 | CRM_Core_Session::setStatus("", ts('Extension Upgraded'), "success"); |
0db6c3e1 TO |
205 | } |
206 | else { | |
6a488035 TO |
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 | } | |
96025800 | 218 | |
6a488035 | 219 | } |