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