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