Merge pull request #9599 from colemanw/CRM-19812
[civicrm-core.git] / CRM / Admin / Page / Extensions.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
fa938177 6 | Copyright CiviCRM LLC (c) 2004-2016 |
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
fa938177 32 * @copyright CiviCRM LLC (c) 2004-2016
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() {
4ef8abc2
CW
51 Civi::resources()->addStyleFile('civicrm', 'css/admin.css');
52
6a488035 53 CRM_Utils_System::setTitle(ts('CiviCRM Extensions'));
481a74f4 54 $destination = CRM_Utils_System::url('civicrm/admin/extensions',
353ffa53 55 'reset=1');
8ef12e64 56
481a74f4
TO
57 $destination = urlencode($destination);
58 $this->assign('destination', $destination);
6a488035
TO
59 }
60
61 /**
eceb18cc 62 * Get BAO Name.
6a488035 63 *
a6c01b45
CW
64 * @return string
65 * Classname of BAO.
6a488035 66 */
00be9182 67 public function getBAOName() {
6a488035
TO
68 return 'CRM_Core_BAO_Extension';
69 }
70
71 /**
eceb18cc 72 * Get action Links.
6a488035 73 *
a6c01b45
CW
74 * @return array
75 * (reference) of action links
6a488035 76 */
00be9182 77 public function &links() {
6a488035
TO
78 if (!(self::$_links)) {
79 self::$_links = array(
80 CRM_Core_Action::ADD => array(
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 => array(
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 => array(
94 'name' => ts('Disable'),
95 'url' => 'civicrm/admin/extensions',
96 'qs' => 'action=disable&id=%%id%%&key=%%key%%',
6a488035
TO
97 'title' => ts('Disable'),
98 ),
99 CRM_Core_Action::DELETE => array(
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 => array(
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).
6a488035 118 */
00be9182 119 public function run() {
6a488035
TO
120 $this->preProcess();
121 return parent::run();
122 }
123
124 /**
eceb18cc 125 * Browse all options.
6a488035 126 */
00be9182 127 public function browse() {
6a488035
TO
128 $mapper = CRM_Extension_System::singleton()->getMapper();
129 $manager = CRM_Extension_System::singleton()->getManager();
130
131 // build announcements at the top of the page
132 $this->assign('extAddNewEnabled', CRM_Extension_System::singleton()->getBrowser()->isEnabled());
133 $reqs = CRM_Extension_System::singleton()->getDownloader()->checkRequirements();
134 if (empty($reqs)) {
135 $reqs = CRM_Extension_System::singleton()->getBrowser()->checkRequirements();
136 }
70d331a6
TO
137 if (empty($reqs)) {
138 $reqs = CRM_Extension_System::singleton()->getDefaultContainer()->checkRequirements();
139 }
6a488035
TO
140 $this->assign('extAddNewReqs', $reqs);
141
142 $this->assign('extDbUpgrades', CRM_Extension_Upgrades::hasPending());
143 $this->assign('extDbUpgradeUrl', CRM_Utils_System::url('civicrm/admin/extensions/upgrade', 'reset=1'));
144
145 // TODO: Debate whether to immediately detect changes in underlying source tree
146 // $manager->refresh();
147
148 // build list of local extensions
149 $localExtensionRows = array(); // array($pseudo_id => extended_CRM_Extension_Info)
150 $keys = array_keys($manager->getStatuses());
151 sort($keys);
22e263ad 152 foreach ($keys as $key) {
6a488035
TO
153 try {
154 $obj = $mapper->keyToInfo($key);
0db6c3e1
TO
155 }
156 catch (CRM_Extension_Exception $ex) {
10a5be27 157 CRM_Core_Session::setStatus(ts('Failed to read extension (%1). Please refresh the extension list.', array(1 => $key)));
6a488035
TO
158 continue;
159 }
160
161 $row = self::createExtendedInfo($obj);
162 $row['id'] = $obj->key;
163
164 // assign actions
165 $action = 0;
166 switch ($row['status']) {
167 case CRM_Extension_Manager::STATUS_UNINSTALLED:
168 $action += CRM_Core_Action::ADD;
169 break;
02fc859b 170
6a488035
TO
171 case CRM_Extension_Manager::STATUS_DISABLED:
172 $action += CRM_Core_Action::ENABLE;
173 $action += CRM_Core_Action::DELETE;
174 break;
02fc859b 175
6a488035
TO
176 case CRM_Extension_Manager::STATUS_DISABLED_MISSING:
177 $action += CRM_Core_Action::DELETE;
178 break;
02fc859b 179
6a488035
TO
180 case CRM_Extension_Manager::STATUS_INSTALLED:
181 case CRM_Extension_Manager::STATUS_INSTALLED_MISSING:
182 $action += CRM_Core_Action::DISABLE;
183 break;
02fc859b 184
6a488035
TO
185 default:
186 }
187 // TODO if extbrowser is enabled and extbrowser has newer version than extcontainer,
188 // then $action += CRM_Core_Action::UPDATE
189 $row['action'] = CRM_Core_Action::formLink(self::links(),
190 $action,
191 array(
192 'id' => $row['id'],
193 'key' => $obj->key,
87dab4a4
AH
194 ),
195 ts('more'),
196 FALSE,
197 'extension.local.action',
198 'Extension',
199 $row['id']
6a488035 200 );
87dab4a4
AH
201 // Key would be better to send, but it's not an integer. Moreover, sending the
202 // values to hook_civicrm_links means that you can still get at the key
6a488035
TO
203
204 $localExtensionRows[$row['id']] = $row;
205 }
206 $this->assign('localExtensionRows', $localExtensionRows);
207
b769826b
CW
208 try {
209 $remoteExtensions = CRM_Extension_System::singleton()->getBrowser()->getExtensions();
210 }
211 catch (CRM_Extension_Exception $e) {
212 $remoteExtensions = array();
213 CRM_Core_Session::setStatus($e->getMessage(), ts('Extension download error'), 'error');
214 }
215
b44e3f84 216 // build list of available downloads
6a488035 217 $remoteExtensionRows = array();
b769826b 218 foreach ($remoteExtensions as $info) {
6a488035
TO
219 $row = (array) $info;
220 $row['id'] = $info->key;
221 $action = CRM_Core_Action::UPDATE;
222 $row['action'] = CRM_Core_Action::formLink(self::links(),
223 $action,
224 array(
225 'id' => $row['id'],
226 'key' => $row['key'],
87dab4a4
AH
227 ),
228 ts('more'),
229 FALSE,
230 'extension.remote.action',
231 'Extension',
232 $row['id']
6a488035 233 );
46693baf
TO
234 if (isset($localExtensionRows[$info->key])) {
235 if (version_compare($localExtensionRows[$info->key]['version'], $info->version, '<')) {
236 $row['is_upgradeable'] = TRUE;
237 }
238 }
6a488035
TO
239 $remoteExtensionRows[$row['id']] = $row;
240 }
241 $this->assign('remoteExtensionRows', $remoteExtensionRows);
242 }
243
244 /**
eceb18cc 245 * Get name of edit form.
6a488035 246 *
a6c01b45
CW
247 * @return string
248 * Classname of edit form.
6a488035 249 */
00be9182 250 public function editForm() {
6a488035
TO
251 return 'CRM_Admin_Form_Extensions';
252 }
253
254 /**
eceb18cc 255 * Get edit form name.
6a488035 256 *
a6c01b45
CW
257 * @return string
258 * name of this page.
6a488035 259 */
00be9182 260 public function editName() {
6a488035
TO
261 return 'CRM_Admin_Form_Extensions';
262 }
263
264 /**
265 * Get user context.
266 *
fd31fa4c
EM
267 * @param null $mode
268 *
a6c01b45
CW
269 * @return string
270 * user context.
6a488035 271 */
00be9182 272 public function userContext($mode = NULL) {
6a488035
TO
273 return 'civicrm/admin/extensions';
274 }
275
276 /**
eceb18cc 277 * Get userContext params.
6a488035 278 *
5173bd95
TO
279 * @param int $mode
280 * Mode that we are in.
6a488035
TO
281 *
282 * @return string
6a488035 283 */
00be9182 284 public function userContextParams($mode = NULL) {
6a488035
TO
285 return 'reset=1&action=browse';
286 }
287
288 /**
289 * Take an extension's raw XML info and add information about the
290 * extension's status on the local system.
291 *
292 * The result format resembles the old CRM_Core_Extensions_Extension.
293 *
da6b46f4
EM
294 * @param CRM_Extension_Info $obj
295 *
6a488035
TO
296 * @return array
297 */
298 public static function createExtendedInfo(CRM_Extension_Info $obj) {
9a5dc864 299 return CRM_Extension_System::createExtendedInfo($obj);
6a488035 300 }
96025800 301
6a488035 302}