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