Update copyright date for 2020
[civicrm-core.git] / CRM / Admin / Form / Extensions.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
f299f7db 6 | Copyright CiviCRM LLC (c) 2004-2020 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
f299f7db 31 * @copyright CiviCRM LLC (c) 2004-2020
6a488035
TO
32 */
33
34/**
b6c94f42 35 * This class generates form components for Extensions.
6a488035
TO
36 */
37class CRM_Admin_Form_Extensions extends CRM_Admin_Form {
38
39 /**
b6c94f42 40 * Form pre-processing.
6a488035
TO
41 */
42 public function preProcess() {
43 parent::preProcess();
44
0927bbe8
TO
45 $mainPage = new CRM_Admin_Page_Extensions();
46 $localExtensionRows = $mainPage->formatLocalExtensionRows();
47 $this->assign('localExtensionRows', $localExtensionRows);
48
49 $remoteExtensionRows = $mainPage->formatRemoteExtensionRows($localExtensionRows);
50 $this->assign('remoteExtensionRows', $remoteExtensionRows);
51
6a488035
TO
52 $this->_key = CRM_Utils_Request::retrieve('key', 'String',
53 $this, FALSE, 0
54 );
000e682a 55 if (!CRM_Utils_Type::validate($this->_key, 'ExtensionKey') && !empty($this->_key)) {
4b685cdf
SL
56 throw new CRM_Core_Exception('Extension Key does not match expected standard');
57 }
6a488035
TO
58 $session = CRM_Core_Session::singleton();
59 $url = CRM_Utils_System::url('civicrm/admin/extensions', 'reset=1&action=browse');
60 $session->pushUserContext($url);
61 $this->assign('id', $this->_id);
62 $this->assign('key', $this->_key);
63
6a488035
TO
64 switch ($this->_action) {
65 case CRM_Core_Action::ADD:
66 case CRM_Core_Action::DELETE:
67 case CRM_Core_Action::ENABLE:
68 case CRM_Core_Action::DISABLE:
69 $info = CRM_Extension_System::singleton()->getMapper()->keyToInfo($this->_key);
70 $extInfo = CRM_Admin_Page_Extensions::createExtendedInfo($info);
71 $this->assign('extension', $extInfo);
72 break;
73
74 case CRM_Core_Action::UPDATE:
353ffa53 75 if (!CRM_Extension_System::singleton()->getBrowser()->isEnabled()) {
6a488035
TO
76 CRM_Core_Error::fatal(ts('The system administrator has disabled this feature.'));
77 }
78 $info = CRM_Extension_System::singleton()->getBrowser()->getExtension($this->_key);
79 $extInfo = CRM_Admin_Page_Extensions::createExtendedInfo($info);
80 $this->assign('extension', $extInfo);
81 break;
82
83 default:
84 CRM_Core_Error::fatal(ts('Unsupported action'));
85 }
86
87 }
88
89 /**
c490a46a 90 * Set default values for the form.
6a488035 91 */
00be9182 92 public function setDefaultValues() {
be2fb01f 93 $defaults = [];
6a488035
TO
94 return $defaults;
95 }
96
97 /**
eceb18cc 98 * Build the form object.
6a488035
TO
99 */
100 public function buildQuickForm() {
6a488035
TO
101 switch ($this->_action) {
102 case CRM_Core_Action::ADD:
103 $buttonName = ts('Install');
be2fb01f 104 $title = ts('Install "%1"?', [
f640e46c 105 1 => $this->_key,
be2fb01f 106 ]);
6a488035
TO
107 break;
108
109 case CRM_Core_Action::UPDATE:
110 $buttonName = ts('Download and Install');
be2fb01f 111 $title = ts('Download and Install "%1"?', [
f640e46c 112 1 => $this->_key,
be2fb01f 113 ]);
6a488035
TO
114 break;
115
116 case CRM_Core_Action::DELETE:
117 $buttonName = ts('Uninstall');
be2fb01f 118 $title = ts('Uninstall "%1"?', [
f640e46c 119 1 => $this->_key,
be2fb01f 120 ]);
6a488035
TO
121 break;
122
123 case CRM_Core_Action::ENABLE:
124 $buttonName = ts('Enable');
be2fb01f 125 $title = ts('Enable "%1"?', [
f640e46c 126 1 => $this->_key,
be2fb01f 127 ]);
6a488035
TO
128 break;
129
130 case CRM_Core_Action::DISABLE:
affc7312 131 $buttonName = ts('Disable');
be2fb01f 132 $title = ts('Disable "%1"?', [
f640e46c 133 1 => $this->_key,
be2fb01f 134 ]);
6a488035
TO
135 break;
136 }
137
138 $this->assign('title', $title);
be2fb01f 139 $this->addButtons([
0d48f1cc
TO
140 [
141 'type' => 'next',
142 'name' => $buttonName,
143 'isDefault' => TRUE,
144 ],
145 [
146 'type' => 'cancel',
147 'name' => ts('Cancel'),
148 ],
149 ]);
6a488035
TO
150 }
151
152 /**
eceb18cc 153 * Global form rule.
6a488035 154 *
5173bd95
TO
155 * @param array $fields
156 * The input form values.
157 * @param array $files
158 * The uploaded files if any.
159 * @param array $self
160 * This object.
6a488035 161 *
72b3a70c
CW
162 * @return bool|array
163 * true if no errors, else an array of errors
6a488035 164 */
00be9182 165 public static function formRule($fields, $files, $self) {
be2fb01f 166 $errors = [];
6a488035
TO
167
168 return empty($errors) ? TRUE : $errors;
169 }
170
171 /**
eceb18cc 172 * Process the form submission.
6a488035
TO
173 */
174 public function postProcess() {
175 CRM_Utils_System::flushCache();
176
177 if ($this->_action & CRM_Core_Action::DELETE) {
178 try {
be2fb01f 179 CRM_Extension_System::singleton()->getManager()->uninstall([$this->_key]);
6a488035 180 CRM_Core_Session::setStatus("", ts('Extension Uninstalled'), "success");
0db6c3e1
TO
181 }
182 catch (CRM_Extension_Exception_DependencyException $e) {
6a488035
TO
183 // currently only thrown for payment-processor dependencies
184 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');
185 }
186 }
187
188 if ($this->_action & CRM_Core_Action::ADD) {
be2fb01f 189 civicrm_api3('Extension', 'install', ['keys' => $this->_key]);
6a488035
TO
190 CRM_Core_Session::setStatus("", ts('Extension Installed'), "success");
191 }
192
193 if ($this->_action & CRM_Core_Action::ENABLE) {
be2fb01f 194 civicrm_api3('Extension', 'enable', ['keys' => $this->_key]);
6a488035
TO
195 CRM_Core_Session::setStatus("", ts('Extension Enabled'), "success");
196 }
197
198 if ($this->_action & CRM_Core_Action::DISABLE) {
be2fb01f 199 CRM_Extension_System::singleton()->getManager()->disable([$this->_key]);
6a488035
TO
200 CRM_Core_Session::setStatus("", ts('Extension Disabled'), "success");
201 }
202
203 if ($this->_action & CRM_Core_Action::UPDATE) {
be2fb01f 204 $result = civicrm_api('Extension', 'download', [
6a488035
TO
205 'version' => 3,
206 'key' => $this->_key,
be2fb01f 207 ]);
de6c59ca 208 if (empty($result['is_error'])) {
6a488035 209 CRM_Core_Session::setStatus("", ts('Extension Upgraded'), "success");
0db6c3e1
TO
210 }
211 else {
6a488035
TO
212 CRM_Core_Session::setStatus($result['error_message'], ts('Extension Upgrade Failed'), "error");
213 }
214 }
215
216 CRM_Utils_System::redirect(
217 CRM_Utils_System::url(
218 'civicrm/admin/extensions',
219 'reset=1&action=browse'
220 )
221 );
222 }
96025800 223
6a488035 224}