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