Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * This is a part of CiviCRM extension management functionality. | |
14 | * | |
15 | * @package CRM | |
ca5cec67 | 16 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
17 | */ |
18 | ||
19 | /** | |
20 | * This page displays the list of extensions registered in the system. | |
21 | */ | |
22 | class CRM_Admin_Page_Extensions extends CRM_Core_Page_Basic { | |
23 | ||
24 | /** | |
eceb18cc | 25 | * The action links that we need to display for the browse screen. |
6a488035 TO |
26 | * |
27 | * @var array | |
6a488035 | 28 | */ |
62d3ee27 | 29 | public static $_links = NULL; |
6a488035 TO |
30 | |
31 | /** | |
32 | * Obtains the group name from url and sets the title. | |
6a488035 | 33 | */ |
00be9182 | 34 | public function preProcess() { |
4ef8abc2 CW |
35 | Civi::resources()->addStyleFile('civicrm', 'css/admin.css'); |
36 | ||
6a488035 | 37 | CRM_Utils_System::setTitle(ts('CiviCRM Extensions')); |
481a74f4 | 38 | $destination = CRM_Utils_System::url('civicrm/admin/extensions', |
353ffa53 | 39 | 'reset=1'); |
8ef12e64 | 40 | |
481a74f4 TO |
41 | $destination = urlencode($destination); |
42 | $this->assign('destination', $destination); | |
6a488035 TO |
43 | } |
44 | ||
45 | /** | |
eceb18cc | 46 | * Get BAO Name. |
6a488035 | 47 | * |
a6c01b45 CW |
48 | * @return string |
49 | * Classname of BAO. | |
6a488035 | 50 | */ |
00be9182 | 51 | public function getBAOName() { |
6a488035 TO |
52 | return 'CRM_Core_BAO_Extension'; |
53 | } | |
54 | ||
55 | /** | |
eceb18cc | 56 | * Get action Links. |
6a488035 | 57 | * |
a6c01b45 CW |
58 | * @return array |
59 | * (reference) of action links | |
6a488035 | 60 | */ |
00be9182 | 61 | public function &links() { |
6a488035 | 62 | if (!(self::$_links)) { |
be2fb01f CW |
63 | self::$_links = [ |
64 | CRM_Core_Action::ADD => [ | |
6a488035 TO |
65 | 'name' => ts('Install'), |
66 | 'url' => 'civicrm/admin/extensions', | |
67 | 'qs' => 'action=add&id=%%id%%&key=%%key%%', | |
68 | 'title' => ts('Install'), | |
be2fb01f CW |
69 | ], |
70 | CRM_Core_Action::ENABLE => [ | |
6a488035 TO |
71 | 'name' => ts('Enable'), |
72 | 'url' => 'civicrm/admin/extensions', | |
73 | 'qs' => 'action=enable&id=%%id%%&key=%%key%%', | |
74 | 'ref' => 'enable-action', | |
75 | 'title' => ts('Enable'), | |
be2fb01f CW |
76 | ], |
77 | CRM_Core_Action::DISABLE => [ | |
6a488035 TO |
78 | 'name' => ts('Disable'), |
79 | 'url' => 'civicrm/admin/extensions', | |
80 | 'qs' => 'action=disable&id=%%id%%&key=%%key%%', | |
6a488035 | 81 | 'title' => ts('Disable'), |
be2fb01f CW |
82 | ], |
83 | CRM_Core_Action::DELETE => [ | |
6a488035 TO |
84 | 'name' => ts('Uninstall'), |
85 | 'url' => 'civicrm/admin/extensions', | |
86 | 'qs' => 'action=delete&id=%%id%%&key=%%key%%', | |
87 | 'title' => ts('Uninstall Extension'), | |
be2fb01f CW |
88 | ], |
89 | CRM_Core_Action::UPDATE => [ | |
6a488035 TO |
90 | 'name' => ts('Download'), |
91 | 'url' => 'civicrm/admin/extensions', | |
92 | 'qs' => 'action=update&id=%%id%%&key=%%key%%', | |
93 | 'title' => ts('Download Extension'), | |
be2fb01f CW |
94 | ], |
95 | ]; | |
6a488035 TO |
96 | } |
97 | return self::$_links; | |
98 | } | |
99 | ||
100 | /** | |
101 | * Run the basic page (run essentially starts execution for that page). | |
6a488035 | 102 | */ |
00be9182 | 103 | public function run() { |
6a488035 TO |
104 | $this->preProcess(); |
105 | return parent::run(); | |
106 | } | |
107 | ||
108 | /** | |
eceb18cc | 109 | * Browse all options. |
6a488035 | 110 | */ |
00be9182 | 111 | public function browse() { |
6a488035 TO |
112 | |
113 | // build announcements at the top of the page | |
114 | $this->assign('extAddNewEnabled', CRM_Extension_System::singleton()->getBrowser()->isEnabled()); | |
115 | $reqs = CRM_Extension_System::singleton()->getDownloader()->checkRequirements(); | |
116 | if (empty($reqs)) { | |
117 | $reqs = CRM_Extension_System::singleton()->getBrowser()->checkRequirements(); | |
118 | } | |
70d331a6 TO |
119 | if (empty($reqs)) { |
120 | $reqs = CRM_Extension_System::singleton()->getDefaultContainer()->checkRequirements(); | |
121 | } | |
6a488035 TO |
122 | $this->assign('extAddNewReqs', $reqs); |
123 | ||
124 | $this->assign('extDbUpgrades', CRM_Extension_Upgrades::hasPending()); | |
125 | $this->assign('extDbUpgradeUrl', CRM_Utils_System::url('civicrm/admin/extensions/upgrade', 'reset=1')); | |
126 | ||
127 | // TODO: Debate whether to immediately detect changes in underlying source tree | |
128 | // $manager->refresh(); | |
129 | ||
616aa7a1 TO |
130 | $localExtensionRows = $this->formatLocalExtensionRows(); |
131 | $this->assign('localExtensionRows', $localExtensionRows); | |
132 | ||
133 | $remoteExtensionRows = $this->formatRemoteExtensionRows($localExtensionRows); | |
134 | $this->assign('remoteExtensionRows', $remoteExtensionRows); | |
d3d5bc05 MW |
135 | |
136 | Civi::resources() | |
137 | ->addScriptFile('civicrm', 'templates/CRM/common/TabHeader.js', 1, 'html-header') | |
138 | ->addSetting([ | |
139 | 'tabSettings' => ['active' => $_GET['selectedChild'] ?? NULL], | |
140 | ]); | |
616aa7a1 TO |
141 | } |
142 | ||
143 | /** | |
144 | * Get the list of local extensions and format them as a table with | |
145 | * status and action data. | |
146 | * | |
147 | * @return array | |
148 | */ | |
149 | public function formatLocalExtensionRows() { | |
150 | $mapper = CRM_Extension_System::singleton()->getMapper(); | |
151 | $manager = CRM_Extension_System::singleton()->getManager(); | |
152 | ||
5d4fcf54 TO |
153 | // array($pseudo_id => extended_CRM_Extension_Info) |
154 | $localExtensionRows = []; | |
6a488035 TO |
155 | $keys = array_keys($manager->getStatuses()); |
156 | sort($keys); | |
8c24f621 | 157 | $hiddenExtensions = $mapper->getKeysByTag('mgmt:hidden'); |
22e263ad | 158 | foreach ($keys as $key) { |
38c87aa2 | 159 | if (in_array($key, $hiddenExtensions)) { |
160 | continue; | |
161 | } | |
6a488035 TO |
162 | try { |
163 | $obj = $mapper->keyToInfo($key); | |
0db6c3e1 TO |
164 | } |
165 | catch (CRM_Extension_Exception $ex) { | |
be2fb01f | 166 | CRM_Core_Session::setStatus(ts('Failed to read extension (%1). Please refresh the extension list.', [1 => $key])); |
6a488035 TO |
167 | continue; |
168 | } | |
169 | ||
38c87aa2 | 170 | $mapper = CRM_Extension_System::singleton()->getMapper(); |
171 | ||
6a488035 TO |
172 | $row = self::createExtendedInfo($obj); |
173 | $row['id'] = $obj->key; | |
25fcba46 | 174 | $row['action'] = ''; |
6a488035 TO |
175 | |
176 | // assign actions | |
177 | $action = 0; | |
178 | switch ($row['status']) { | |
179 | case CRM_Extension_Manager::STATUS_UNINSTALLED: | |
25fcba46 CW |
180 | if (!$manager->isIncompatible($row['id'])) { |
181 | $action += CRM_Core_Action::ADD; | |
182 | } | |
6a488035 | 183 | break; |
02fc859b | 184 | |
6a488035 | 185 | case CRM_Extension_Manager::STATUS_DISABLED: |
25fcba46 CW |
186 | if (!$manager->isIncompatible($row['id'])) { |
187 | $action += CRM_Core_Action::ENABLE; | |
188 | } | |
6a488035 TO |
189 | $action += CRM_Core_Action::DELETE; |
190 | break; | |
02fc859b | 191 | |
6a488035 TO |
192 | case CRM_Extension_Manager::STATUS_DISABLED_MISSING: |
193 | $action += CRM_Core_Action::DELETE; | |
194 | break; | |
02fc859b | 195 | |
6a488035 TO |
196 | case CRM_Extension_Manager::STATUS_INSTALLED: |
197 | case CRM_Extension_Manager::STATUS_INSTALLED_MISSING: | |
198 | $action += CRM_Core_Action::DISABLE; | |
199 | break; | |
02fc859b | 200 | |
6a488035 TO |
201 | default: |
202 | } | |
203 | // TODO if extbrowser is enabled and extbrowser has newer version than extcontainer, | |
204 | // then $action += CRM_Core_Action::UPDATE | |
25fcba46 CW |
205 | if ($action) { |
206 | $row['action'] = CRM_Core_Action::formLink(self::links(), | |
207 | $action, | |
208 | ['id' => $row['id'], 'key' => $obj->key], | |
209 | ts('more'), | |
210 | FALSE, | |
211 | 'extension.local.action', | |
212 | 'Extension', | |
213 | $row['id'] | |
214 | ); | |
215 | } | |
87dab4a4 AH |
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 | |
6a488035 TO |
218 | |
219 | $localExtensionRows[$row['id']] = $row; | |
220 | } | |
616aa7a1 TO |
221 | return $localExtensionRows; |
222 | } | |
6a488035 | 223 | |
616aa7a1 | 224 | /** |
e4c4f267 | 225 | * Get the list of remote extensions and format them as a table with |
616aa7a1 TO |
226 | * status and action data. |
227 | * | |
228 | * @param array $localExtensionRows | |
229 | * @return array | |
230 | */ | |
231 | public function formatRemoteExtensionRows($localExtensionRows) { | |
b769826b CW |
232 | try { |
233 | $remoteExtensions = CRM_Extension_System::singleton()->getBrowser()->getExtensions(); | |
234 | } | |
235 | catch (CRM_Extension_Exception $e) { | |
be2fb01f | 236 | $remoteExtensions = []; |
b769826b CW |
237 | CRM_Core_Session::setStatus($e->getMessage(), ts('Extension download error'), 'error'); |
238 | } | |
239 | ||
b44e3f84 | 240 | // build list of available downloads |
be2fb01f | 241 | $remoteExtensionRows = []; |
e4c4f267 | 242 | $compat = CRM_Extension_System::getCompatibilityInfo(); |
a304caa7 | 243 | $mapper = CRM_Extension_System::singleton()->getMapper(); |
e4c4f267 | 244 | |
b769826b | 245 | foreach ($remoteExtensions as $info) { |
e4c4f267 CW |
246 | if (!empty($compat[$info->key]['obsolete'])) { |
247 | continue; | |
248 | } | |
7b9346c0 | 249 | $row = self::fillMissingInfoKeys((array) $info); |
6a488035 | 250 | $row['id'] = $info->key; |
5d011bd0 | 251 | $row['upgradelink'] = ''; |
6a488035 TO |
252 | $action = CRM_Core_Action::UPDATE; |
253 | $row['action'] = CRM_Core_Action::formLink(self::links(), | |
254 | $action, | |
be2fb01f | 255 | [ |
6a488035 TO |
256 | 'id' => $row['id'], |
257 | 'key' => $row['key'], | |
be2fb01f | 258 | ], |
87dab4a4 AH |
259 | ts('more'), |
260 | FALSE, | |
261 | 'extension.remote.action', | |
262 | 'Extension', | |
263 | $row['id'] | |
6a488035 | 264 | ); |
46693baf | 265 | if (isset($localExtensionRows[$info->key])) { |
518d3e48 JK |
266 | if (array_key_exists('version', $localExtensionRows[$info->key])) { |
267 | if (version_compare($localExtensionRows[$info->key]['version'], $info->version, '<')) { | |
a304caa7 | 268 | $row['upgradelink'] = $mapper->getUpgradeLink($remoteExtensions[$info->key], $localExtensionRows[$info->key]); |
518d3e48 | 269 | } |
46693baf TO |
270 | } |
271 | } | |
6a488035 TO |
272 | $remoteExtensionRows[$row['id']] = $row; |
273 | } | |
616aa7a1 TO |
274 | |
275 | return $remoteExtensionRows; | |
6a488035 TO |
276 | } |
277 | ||
278 | /** | |
eceb18cc | 279 | * Get name of edit form. |
6a488035 | 280 | * |
a6c01b45 CW |
281 | * @return string |
282 | * Classname of edit form. | |
6a488035 | 283 | */ |
00be9182 | 284 | public function editForm() { |
6a488035 TO |
285 | return 'CRM_Admin_Form_Extensions'; |
286 | } | |
287 | ||
288 | /** | |
eceb18cc | 289 | * Get edit form name. |
6a488035 | 290 | * |
a6c01b45 CW |
291 | * @return string |
292 | * name of this page. | |
6a488035 | 293 | */ |
00be9182 | 294 | public function editName() { |
6a488035 TO |
295 | return 'CRM_Admin_Form_Extensions'; |
296 | } | |
297 | ||
298 | /** | |
299 | * Get user context. | |
300 | * | |
fd31fa4c EM |
301 | * @param null $mode |
302 | * | |
a6c01b45 CW |
303 | * @return string |
304 | * user context. | |
6a488035 | 305 | */ |
00be9182 | 306 | public function userContext($mode = NULL) { |
6a488035 TO |
307 | return 'civicrm/admin/extensions'; |
308 | } | |
309 | ||
310 | /** | |
eceb18cc | 311 | * Get userContext params. |
6a488035 | 312 | * |
5173bd95 TO |
313 | * @param int $mode |
314 | * Mode that we are in. | |
6a488035 TO |
315 | * |
316 | * @return string | |
6a488035 | 317 | */ |
00be9182 | 318 | public function userContextParams($mode = NULL) { |
6a488035 TO |
319 | return 'reset=1&action=browse'; |
320 | } | |
321 | ||
322 | /** | |
323 | * Take an extension's raw XML info and add information about the | |
324 | * extension's status on the local system. | |
325 | * | |
326 | * The result format resembles the old CRM_Core_Extensions_Extension. | |
327 | * | |
da6b46f4 EM |
328 | * @param CRM_Extension_Info $obj |
329 | * | |
6a488035 TO |
330 | * @return array |
331 | */ | |
332 | public static function createExtendedInfo(CRM_Extension_Info $obj) { | |
7b9346c0 BT |
333 | return self::fillMissingInfoKeys(CRM_Extension_System::createExtendedInfo($obj)); |
334 | } | |
335 | ||
336 | /** | |
337 | * Extension templates expect certain keys to always be set, but these might be missing from the relevant info.xml files | |
338 | * This ensures the expect keys are always set. | |
339 | * | |
340 | * @param array $info | |
341 | * @return array | |
342 | */ | |
343 | private static function fillMissingInfoKeys(array $info) { | |
344 | $defaultKeys = [ | |
345 | 'urls' => [], | |
346 | 'authors' => [], | |
347 | 'version' => '', | |
348 | 'description' => '', | |
349 | 'license' => '', | |
350 | 'releaseDate' => '', | |
351 | 'downloadUrl' => FALSE, | |
352 | 'compatibility' => FALSE, | |
353 | 'develStage' => FALSE, | |
354 | 'comments' => FALSE, | |
355 | ]; | |
356 | ||
357 | return array_merge($defaultKeys, $info); | |
6a488035 | 358 | } |
96025800 | 359 | |
6a488035 | 360 | } |