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');
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
);
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);
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.'));
76 $info = CRM_Extension_System
::singleton()->getBrowser()->getExtension($this->_key
);
77 $extInfo = CRM_Admin_Page_Extensions
::createExtendedInfo($info);
78 $this->assign('extension', $extInfo);
82 CRM_Core_Error
::statusBounce(ts('Unsupported action'));
88 * Set default values for the form.
90 public function setDefaultValues() {
96 * Build the form object.
98 public function buildQuickForm() {
99 switch ($this->_action
) {
100 case CRM_Core_Action
::ADD
:
101 $buttonName = ts('Install');
102 $title = ts('Install "%1"?', [
107 case CRM_Core_Action
::UPDATE
:
108 $buttonName = ts('Download and Install');
109 $title = ts('Download and Install "%1"?', [
114 case CRM_Core_Action
::DELETE
:
115 $buttonName = ts('Uninstall');
116 $title = ts('Uninstall "%1"?', [
121 case CRM_Core_Action
::ENABLE
:
122 $buttonName = ts('Enable');
123 $title = ts('Enable "%1"?', [
128 case CRM_Core_Action
::DISABLE
:
129 $buttonName = ts('Disable');
130 $title = ts('Disable "%1"?', [
136 $this->assign('title', $title);
140 'name' => $buttonName,
145 'name' => ts('Cancel'),
153 * @param array $fields
154 * The input form values.
155 * @param array $files
156 * The uploaded files if any.
161 * true if no errors, else an array of errors
163 public static function formRule($fields, $files, $self) {
166 return empty($errors) ?
TRUE : $errors;
170 * Process the form submission.
172 public function postProcess() {
173 CRM_Utils_System
::flushCache();
175 if ($this->_action
& CRM_Core_Action
::DELETE
) {
177 CRM_Extension_System
::singleton()->getManager()->uninstall([$this->_key
]);
178 CRM_Core_Session
::setStatus("", ts('Extension Uninstalled'), "success");
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');
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");
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");
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");
201 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
202 $result = civicrm_api('Extension', 'download', [
204 'key' => $this->_key
,
206 if (empty($result['is_error'])) {
207 CRM_Core_Session
::setStatus("", ts('Extension Upgraded'), "success");
210 CRM_Core_Session
::setStatus($result['error_message'], ts('Extension Upgrade Failed'), "error");
214 CRM_Utils_System
::redirect(
215 CRM_Utils_System
::url(
216 'civicrm/admin/extensions',
217 'reset=1&action=browse'