3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class generates form components for Extensions.
21 class CRM_Admin_Form_Extensions
extends CRM_Admin_Form
{
34 * Form pre-processing.
36 public function preProcess() {
39 $mainPage = new CRM_Admin_Page_Extensions();
40 $localExtensionRows = $mainPage->formatLocalExtensionRows();
41 $this->assign('localExtensionRows', $localExtensionRows);
43 $remoteExtensionRows = $mainPage->formatRemoteExtensionRows($localExtensionRows);
44 $this->assign('remoteExtensionRows', $remoteExtensionRows);
46 $this->_key
= CRM_Utils_Request
::retrieve('key', 'String',
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');
52 $this->label
= $remoteExtensionRows[$this->_key
]['label'] ??
$this->_key
;
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
);
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);
69 case CRM_Core_Action
::UPDATE
:
70 if (!CRM_Extension_System
::singleton()->getBrowser()->isEnabled()) {
71 CRM_Core_Error
::statusBounce(ts('The system administrator has disabled this feature.'));
73 $info = CRM_Extension_System
::singleton()->getBrowser()->getExtension($this->_key
);
74 $extInfo = CRM_Admin_Page_Extensions
::createExtendedInfo($info);
75 $this->assign('extension', $extInfo);
79 CRM_Core_Error
::statusBounce(ts('Unsupported action'));
85 * Set default values for the form.
87 public function setDefaultValues() {
93 * Build the form object.
95 public function buildQuickForm() {
96 switch ($this->_action
) {
97 case CRM_Core_Action
::ADD
:
98 $buttonName = ts('Install');
99 $title = ts('Install "%1"?', [
104 case CRM_Core_Action
::UPDATE
:
105 $buttonName = ts('Download and Install');
106 $title = ts('Download and Install "%1"?', [
111 case CRM_Core_Action
::DELETE
:
112 $buttonName = ts('Uninstall');
113 $title = ts('Uninstall "%1"?', [
118 case CRM_Core_Action
::ENABLE
:
119 $buttonName = ts('Enable');
120 $title = ts('Enable "%1"?', [
125 case CRM_Core_Action
::DISABLE
:
126 $buttonName = ts('Disable');
127 $title = ts('Disable "%1"?', [
133 $this->assign('title', $title);
137 'name' => $buttonName,
142 'name' => ts('Cancel'),
150 * @param array $fields
151 * The input form values.
152 * @param array $files
153 * The uploaded files if any.
158 * true if no errors, else an array of errors
160 public static function formRule($fields, $files, $self) {
163 return empty($errors) ?
TRUE : $errors;
167 * Process the form submission.
169 public function postProcess() {
170 CRM_Utils_System
::flushCache();
172 if ($this->_action
& CRM_Core_Action
::DELETE
) {
174 CRM_Extension_System
::singleton()->getManager()->uninstall([$this->_key
]);
175 CRM_Core_Session
::setStatus("", ts('Extension Uninstalled'), "success");
177 catch (CRM_Extension_Exception_DependencyException
$e) {
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');
183 if ($this->_action
& CRM_Core_Action
::ADD
) {
184 civicrm_api3('Extension', 'install', ['keys' => $this->_key
]);
185 CRM_Core_Session
::setStatus("", ts('Extension Installed'), "success");
188 if ($this->_action
& CRM_Core_Action
::ENABLE
) {
189 civicrm_api3('Extension', 'enable', ['keys' => $this->_key
]);
190 CRM_Core_Session
::setStatus("", ts('Extension Enabled'), "success");
193 if ($this->_action
& CRM_Core_Action
::DISABLE
) {
194 CRM_Extension_System
::singleton()->getManager()->disable([$this->_key
]);
195 CRM_Core_Session
::setStatus("", ts('Extension Disabled'), "success");
198 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
199 $result = civicrm_api('Extension', 'download', [
201 'key' => $this->_key
,
203 if (empty($result['is_error'])) {
204 CRM_Core_Session
::setStatus("", ts('Extension Upgraded'), "success");
207 CRM_Core_Session
::setStatus($result['error_message'], ts('Extension Upgrade Failed'), "error");
211 CRM_Utils_System
::redirect(
212 CRM_Utils_System
::url(
213 'civicrm/admin/extensions',
214 'reset=1&action=browse'