(NFC) Update CRM/Activity CRM/Admin and CRM/Batch folders to be the future coder...
[civicrm-core.git] / CRM / Admin / Page / Extensions.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * This is a part of CiviCRM extension management functionality.
30 *
31 * @package CRM
32 * @copyright CiviCRM LLC (c) 2004-2019
33 */
34
35 /**
36 * This page displays the list of extensions registered in the system.
37 */
38 class CRM_Admin_Page_Extensions extends CRM_Core_Page_Basic {
39
40 /**
41 * The action links that we need to display for the browse screen.
42 *
43 * @var array
44 */
45 public static $_links = NULL;
46
47 /**
48 * Obtains the group name from url and sets the title.
49 */
50 public function preProcess() {
51 Civi::resources()->addStyleFile('civicrm', 'css/admin.css');
52
53 CRM_Utils_System::setTitle(ts('CiviCRM Extensions'));
54 $destination = CRM_Utils_System::url('civicrm/admin/extensions',
55 'reset=1');
56
57 $destination = urlencode($destination);
58 $this->assign('destination', $destination);
59 }
60
61 /**
62 * Get BAO Name.
63 *
64 * @return string
65 * Classname of BAO.
66 */
67 public function getBAOName() {
68 return 'CRM_Core_BAO_Extension';
69 }
70
71 /**
72 * Get action Links.
73 *
74 * @return array
75 * (reference) of action links
76 */
77 public function &links() {
78 if (!(self::$_links)) {
79 self::$_links = [
80 CRM_Core_Action::ADD => [
81 'name' => ts('Install'),
82 'url' => 'civicrm/admin/extensions',
83 'qs' => 'action=add&id=%%id%%&key=%%key%%',
84 'title' => ts('Install'),
85 ],
86 CRM_Core_Action::ENABLE => [
87 'name' => ts('Enable'),
88 'url' => 'civicrm/admin/extensions',
89 'qs' => 'action=enable&id=%%id%%&key=%%key%%',
90 'ref' => 'enable-action',
91 'title' => ts('Enable'),
92 ],
93 CRM_Core_Action::DISABLE => [
94 'name' => ts('Disable'),
95 'url' => 'civicrm/admin/extensions',
96 'qs' => 'action=disable&id=%%id%%&key=%%key%%',
97 'title' => ts('Disable'),
98 ],
99 CRM_Core_Action::DELETE => [
100 'name' => ts('Uninstall'),
101 'url' => 'civicrm/admin/extensions',
102 'qs' => 'action=delete&id=%%id%%&key=%%key%%',
103 'title' => ts('Uninstall Extension'),
104 ],
105 CRM_Core_Action::UPDATE => [
106 'name' => ts('Download'),
107 'url' => 'civicrm/admin/extensions',
108 'qs' => 'action=update&id=%%id%%&key=%%key%%',
109 'title' => ts('Download Extension'),
110 ],
111 ];
112 }
113 return self::$_links;
114 }
115
116 /**
117 * Run the basic page (run essentially starts execution for that page).
118 */
119 public function run() {
120 $this->preProcess();
121 return parent::run();
122 }
123
124 /**
125 * Browse all options.
126 */
127 public function browse() {
128
129 // build announcements at the top of the page
130 $this->assign('extAddNewEnabled', CRM_Extension_System::singleton()->getBrowser()->isEnabled());
131 $reqs = CRM_Extension_System::singleton()->getDownloader()->checkRequirements();
132 if (empty($reqs)) {
133 $reqs = CRM_Extension_System::singleton()->getBrowser()->checkRequirements();
134 }
135 if (empty($reqs)) {
136 $reqs = CRM_Extension_System::singleton()->getDefaultContainer()->checkRequirements();
137 }
138 $this->assign('extAddNewReqs', $reqs);
139
140 $this->assign('extDbUpgrades', CRM_Extension_Upgrades::hasPending());
141 $this->assign('extDbUpgradeUrl', CRM_Utils_System::url('civicrm/admin/extensions/upgrade', 'reset=1'));
142
143 // TODO: Debate whether to immediately detect changes in underlying source tree
144 // $manager->refresh();
145
146 $localExtensionRows = $this->formatLocalExtensionRows();
147 $this->assign('localExtensionRows', $localExtensionRows);
148
149 $remoteExtensionRows = $this->formatRemoteExtensionRows($localExtensionRows);
150 $this->assign('remoteExtensionRows', $remoteExtensionRows);
151 }
152
153 /**
154 * Get the list of local extensions and format them as a table with
155 * status and action data.
156 *
157 * @return array
158 */
159 public function formatLocalExtensionRows() {
160 $mapper = CRM_Extension_System::singleton()->getMapper();
161 $manager = CRM_Extension_System::singleton()->getManager();
162
163 // array($pseudo_id => extended_CRM_Extension_Info)
164 $localExtensionRows = [];
165 $keys = array_keys($manager->getStatuses());
166 sort($keys);
167 foreach ($keys as $key) {
168 try {
169 $obj = $mapper->keyToInfo($key);
170 }
171 catch (CRM_Extension_Exception $ex) {
172 CRM_Core_Session::setStatus(ts('Failed to read extension (%1). Please refresh the extension list.', [1 => $key]));
173 continue;
174 }
175
176 $row = self::createExtendedInfo($obj);
177 $row['id'] = $obj->key;
178
179 // assign actions
180 $action = 0;
181 switch ($row['status']) {
182 case CRM_Extension_Manager::STATUS_UNINSTALLED:
183 $action += CRM_Core_Action::ADD;
184 break;
185
186 case CRM_Extension_Manager::STATUS_DISABLED:
187 $action += CRM_Core_Action::ENABLE;
188 $action += CRM_Core_Action::DELETE;
189 break;
190
191 case CRM_Extension_Manager::STATUS_DISABLED_MISSING:
192 $action += CRM_Core_Action::DELETE;
193 break;
194
195 case CRM_Extension_Manager::STATUS_INSTALLED:
196 case CRM_Extension_Manager::STATUS_INSTALLED_MISSING:
197 $action += CRM_Core_Action::DISABLE;
198 break;
199
200 default:
201 }
202 // TODO if extbrowser is enabled and extbrowser has newer version than extcontainer,
203 // then $action += CRM_Core_Action::UPDATE
204 $row['action'] = CRM_Core_Action::formLink(self::links(),
205 $action,
206 [
207 'id' => $row['id'],
208 'key' => $obj->key,
209 ],
210 ts('more'),
211 FALSE,
212 'extension.local.action',
213 'Extension',
214 $row['id']
215 );
216 // Key would be better to send, but it's not an integer. Moreover, sending the
217 // values to hook_civicrm_links means that you can still get at the key
218
219 $localExtensionRows[$row['id']] = $row;
220 }
221 return $localExtensionRows;
222 }
223
224 /**
225 * Get the list of remote extensions and format them as a table with
226 * status and action data.
227 *
228 * @param array $localExtensionRows
229 * @return array
230 */
231 public function formatRemoteExtensionRows($localExtensionRows) {
232 try {
233 $remoteExtensions = CRM_Extension_System::singleton()->getBrowser()->getExtensions();
234 }
235 catch (CRM_Extension_Exception $e) {
236 $remoteExtensions = [];
237 CRM_Core_Session::setStatus($e->getMessage(), ts('Extension download error'), 'error');
238 }
239
240 // build list of available downloads
241 $remoteExtensionRows = [];
242 $compat = CRM_Extension_System::getCompatibilityInfo();
243
244 foreach ($remoteExtensions as $info) {
245 if (!empty($compat[$info->key]['obsolete'])) {
246 continue;
247 }
248 $row = (array) $info;
249 $row['id'] = $info->key;
250 $action = CRM_Core_Action::UPDATE;
251 $row['action'] = CRM_Core_Action::formLink(self::links(),
252 $action,
253 [
254 'id' => $row['id'],
255 'key' => $row['key'],
256 ],
257 ts('more'),
258 FALSE,
259 'extension.remote.action',
260 'Extension',
261 $row['id']
262 );
263 if (isset($localExtensionRows[$info->key])) {
264 if (array_key_exists('version', $localExtensionRows[$info->key])) {
265 if (version_compare($localExtensionRows[$info->key]['version'], $info->version, '<')) {
266 $row['is_upgradeable'] = TRUE;
267 }
268 }
269 }
270 $remoteExtensionRows[$row['id']] = $row;
271 }
272
273 return $remoteExtensionRows;
274 }
275
276 /**
277 * Get name of edit form.
278 *
279 * @return string
280 * Classname of edit form.
281 */
282 public function editForm() {
283 return 'CRM_Admin_Form_Extensions';
284 }
285
286 /**
287 * Get edit form name.
288 *
289 * @return string
290 * name of this page.
291 */
292 public function editName() {
293 return 'CRM_Admin_Form_Extensions';
294 }
295
296 /**
297 * Get user context.
298 *
299 * @param null $mode
300 *
301 * @return string
302 * user context.
303 */
304 public function userContext($mode = NULL) {
305 return 'civicrm/admin/extensions';
306 }
307
308 /**
309 * Get userContext params.
310 *
311 * @param int $mode
312 * Mode that we are in.
313 *
314 * @return string
315 */
316 public function userContextParams($mode = NULL) {
317 return 'reset=1&action=browse';
318 }
319
320 /**
321 * Take an extension's raw XML info and add information about the
322 * extension's status on the local system.
323 *
324 * The result format resembles the old CRM_Core_Extensions_Extension.
325 *
326 * @param CRM_Extension_Info $obj
327 *
328 * @return array
329 */
330 public static function createExtendedInfo(CRM_Extension_Info $obj) {
331 return CRM_Extension_System::createExtendedInfo($obj);
332 }
333
334 }