Merge pull request #16077 from civicrm/5.21
[civicrm-core.git] / CRM / Admin / Form / Extensions.php
CommitLineData
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 */
21class CRM_Admin_Form_Extensions extends CRM_Admin_Form {
22
23 /**
b6c94f42 24 * Form pre-processing.
6a488035
TO
25 */
26 public function preProcess() {
27 parent::preProcess();
28
0927bbe8
TO
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
6a488035
TO
36 $this->_key = CRM_Utils_Request::retrieve('key', 'String',
37 $this, FALSE, 0
38 );
000e682a 39 if (!CRM_Utils_Type::validate($this->_key, 'ExtensionKey') && !empty($this->_key)) {
4b685cdf
SL
40 throw new CRM_Core_Exception('Extension Key does not match expected standard');
41 }
6a488035
TO
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
6a488035
TO
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:
353ffa53 59 if (!CRM_Extension_System::singleton()->getBrowser()->isEnabled()) {
6a488035
TO
60 CRM_Core_Error::fatal(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::fatal(ts('Unsupported action'));
69 }
70
71 }
72
73 /**
c490a46a 74 * Set default values for the form.
6a488035 75 */
00be9182 76 public function setDefaultValues() {
be2fb01f 77 $defaults = [];
6a488035
TO
78 return $defaults;
79 }
80
81 /**
eceb18cc 82 * Build the form object.
6a488035
TO
83 */
84 public function buildQuickForm() {
6a488035
TO
85 switch ($this->_action) {
86 case CRM_Core_Action::ADD:
87 $buttonName = ts('Install');
be2fb01f 88 $title = ts('Install "%1"?', [
f640e46c 89 1 => $this->_key,
be2fb01f 90 ]);
6a488035
TO
91 break;
92
93 case CRM_Core_Action::UPDATE:
94 $buttonName = ts('Download and Install');
be2fb01f 95 $title = ts('Download and Install "%1"?', [
f640e46c 96 1 => $this->_key,
be2fb01f 97 ]);
6a488035
TO
98 break;
99
100 case CRM_Core_Action::DELETE:
101 $buttonName = ts('Uninstall');
be2fb01f 102 $title = ts('Uninstall "%1"?', [
f640e46c 103 1 => $this->_key,
be2fb01f 104 ]);
6a488035
TO
105 break;
106
107 case CRM_Core_Action::ENABLE:
108 $buttonName = ts('Enable');
be2fb01f 109 $title = ts('Enable "%1"?', [
f640e46c 110 1 => $this->_key,
be2fb01f 111 ]);
6a488035
TO
112 break;
113
114 case CRM_Core_Action::DISABLE:
affc7312 115 $buttonName = ts('Disable');
be2fb01f 116 $title = ts('Disable "%1"?', [
f640e46c 117 1 => $this->_key,
be2fb01f 118 ]);
6a488035
TO
119 break;
120 }
121
122 $this->assign('title', $title);
be2fb01f 123 $this->addButtons([
0d48f1cc
TO
124 [
125 'type' => 'next',
126 'name' => $buttonName,
127 'isDefault' => TRUE,
128 ],
129 [
130 'type' => 'cancel',
131 'name' => ts('Cancel'),
132 ],
133 ]);
6a488035
TO
134 }
135
136 /**
eceb18cc 137 * Global form rule.
6a488035 138 *
5173bd95
TO
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.
6a488035 145 *
72b3a70c
CW
146 * @return bool|array
147 * true if no errors, else an array of errors
6a488035 148 */
00be9182 149 public static function formRule($fields, $files, $self) {
be2fb01f 150 $errors = [];
6a488035
TO
151
152 return empty($errors) ? TRUE : $errors;
153 }
154
155 /**
eceb18cc 156 * Process the form submission.
6a488035
TO
157 */
158 public function postProcess() {
159 CRM_Utils_System::flushCache();
160
161 if ($this->_action & CRM_Core_Action::DELETE) {
162 try {
be2fb01f 163 CRM_Extension_System::singleton()->getManager()->uninstall([$this->_key]);
6a488035 164 CRM_Core_Session::setStatus("", ts('Extension Uninstalled'), "success");
0db6c3e1
TO
165 }
166 catch (CRM_Extension_Exception_DependencyException $e) {
6a488035
TO
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) {
be2fb01f 173 civicrm_api3('Extension', 'install', ['keys' => $this->_key]);
6a488035
TO
174 CRM_Core_Session::setStatus("", ts('Extension Installed'), "success");
175 }
176
177 if ($this->_action & CRM_Core_Action::ENABLE) {
be2fb01f 178 civicrm_api3('Extension', 'enable', ['keys' => $this->_key]);
6a488035
TO
179 CRM_Core_Session::setStatus("", ts('Extension Enabled'), "success");
180 }
181
182 if ($this->_action & CRM_Core_Action::DISABLE) {
be2fb01f 183 CRM_Extension_System::singleton()->getManager()->disable([$this->_key]);
6a488035
TO
184 CRM_Core_Session::setStatus("", ts('Extension Disabled'), "success");
185 }
186
187 if ($this->_action & CRM_Core_Action::UPDATE) {
be2fb01f 188 $result = civicrm_api('Extension', 'download', [
6a488035
TO
189 'version' => 3,
190 'key' => $this->_key,
be2fb01f 191 ]);
de6c59ca 192 if (empty($result['is_error'])) {
6a488035 193 CRM_Core_Session::setStatus("", ts('Extension Upgraded'), "success");
0db6c3e1
TO
194 }
195 else {
6a488035
TO
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 }
96025800 207
6a488035 208}