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