Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
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 | |
6b83d5bd | 32 | * @copyright CiviCRM LLC (c) 2004-2019 |
6a488035 TO |
33 | */ |
34 | ||
35 | /** | |
36 | * This page displays the list of extensions registered in the system. | |
37 | */ | |
38 | class 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 | 44 | */ |
62d3ee27 | 45 | public static $_links = NULL; |
6a488035 TO |
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 | 78 | if (!(self::$_links)) { |
be2fb01f CW |
79 | self::$_links = [ |
80 | CRM_Core_Action::ADD => [ | |
6a488035 TO |
81 | 'name' => ts('Install'), |
82 | 'url' => 'civicrm/admin/extensions', | |
83 | 'qs' => 'action=add&id=%%id%%&key=%%key%%', | |
84 | 'title' => ts('Install'), | |
be2fb01f CW |
85 | ], |
86 | CRM_Core_Action::ENABLE => [ | |
6a488035 TO |
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'), | |
be2fb01f CW |
92 | ], |
93 | CRM_Core_Action::DISABLE => [ | |
6a488035 TO |
94 | 'name' => ts('Disable'), |
95 | 'url' => 'civicrm/admin/extensions', | |
96 | 'qs' => 'action=disable&id=%%id%%&key=%%key%%', | |
6a488035 | 97 | 'title' => ts('Disable'), |
be2fb01f CW |
98 | ], |
99 | CRM_Core_Action::DELETE => [ | |
6a488035 TO |
100 | 'name' => ts('Uninstall'), |
101 | 'url' => 'civicrm/admin/extensions', | |
102 | 'qs' => 'action=delete&id=%%id%%&key=%%key%%', | |
103 | 'title' => ts('Uninstall Extension'), | |
be2fb01f CW |
104 | ], |
105 | CRM_Core_Action::UPDATE => [ | |
6a488035 TO |
106 | 'name' => ts('Download'), |
107 | 'url' => 'civicrm/admin/extensions', | |
108 | 'qs' => 'action=update&id=%%id%%&key=%%key%%', | |
109 | 'title' => ts('Download Extension'), | |
be2fb01f CW |
110 | ], |
111 | ]; | |
6a488035 TO |
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 | |
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 | ||
616aa7a1 TO |
146 | $localExtensionRows = $this->formatLocalExtensionRows(); |
147 | $this->assign('localExtensionRows', $localExtensionRows); | |
148 | ||
149 | $remoteExtensionRows = $this->formatRemoteExtensionRows($localExtensionRows); | |
150 | $this->assign('remoteExtensionRows', $remoteExtensionRows); | |
151 | } | |
152 | ||
153 | /** | |
154 | * Get the list of local extensions and format them as a table with | |
155 | * status and action data. | |
156 | * | |
157 | * @return array | |
158 | */ | |
159 | public function formatLocalExtensionRows() { | |
160 | $mapper = CRM_Extension_System::singleton()->getMapper(); | |
161 | $manager = CRM_Extension_System::singleton()->getManager(); | |
162 | ||
5d4fcf54 TO |
163 | // array($pseudo_id => extended_CRM_Extension_Info) |
164 | $localExtensionRows = []; | |
6a488035 TO |
165 | $keys = array_keys($manager->getStatuses()); |
166 | sort($keys); | |
22e263ad | 167 | foreach ($keys as $key) { |
6a488035 TO |
168 | try { |
169 | $obj = $mapper->keyToInfo($key); | |
0db6c3e1 TO |
170 | } |
171 | catch (CRM_Extension_Exception $ex) { | |
be2fb01f | 172 | CRM_Core_Session::setStatus(ts('Failed to read extension (%1). Please refresh the extension list.', [1 => $key])); |
6a488035 TO |
173 | continue; |
174 | } | |
175 | ||
176 | $row = self::createExtendedInfo($obj); | |
177 | $row['id'] = $obj->key; | |
25fcba46 | 178 | $row['action'] = ''; |
6a488035 TO |
179 | |
180 | // assign actions | |
181 | $action = 0; | |
182 | switch ($row['status']) { | |
183 | case CRM_Extension_Manager::STATUS_UNINSTALLED: | |
25fcba46 CW |
184 | if (!$manager->isIncompatible($row['id'])) { |
185 | $action += CRM_Core_Action::ADD; | |
186 | } | |
6a488035 | 187 | break; |
02fc859b | 188 | |
6a488035 | 189 | case CRM_Extension_Manager::STATUS_DISABLED: |
25fcba46 CW |
190 | if (!$manager->isIncompatible($row['id'])) { |
191 | $action += CRM_Core_Action::ENABLE; | |
192 | } | |
6a488035 TO |
193 | $action += CRM_Core_Action::DELETE; |
194 | break; | |
02fc859b | 195 | |
6a488035 TO |
196 | case CRM_Extension_Manager::STATUS_DISABLED_MISSING: |
197 | $action += CRM_Core_Action::DELETE; | |
198 | break; | |
02fc859b | 199 | |
6a488035 TO |
200 | case CRM_Extension_Manager::STATUS_INSTALLED: |
201 | case CRM_Extension_Manager::STATUS_INSTALLED_MISSING: | |
202 | $action += CRM_Core_Action::DISABLE; | |
203 | break; | |
02fc859b | 204 | |
6a488035 TO |
205 | default: |
206 | } | |
207 | // TODO if extbrowser is enabled and extbrowser has newer version than extcontainer, | |
208 | // then $action += CRM_Core_Action::UPDATE | |
25fcba46 CW |
209 | if ($action) { |
210 | $row['action'] = CRM_Core_Action::formLink(self::links(), | |
211 | $action, | |
212 | ['id' => $row['id'], 'key' => $obj->key], | |
213 | ts('more'), | |
214 | FALSE, | |
215 | 'extension.local.action', | |
216 | 'Extension', | |
217 | $row['id'] | |
218 | ); | |
219 | } | |
87dab4a4 AH |
220 | // Key would be better to send, but it's not an integer. Moreover, sending the |
221 | // values to hook_civicrm_links means that you can still get at the key | |
6a488035 TO |
222 | |
223 | $localExtensionRows[$row['id']] = $row; | |
224 | } | |
616aa7a1 TO |
225 | return $localExtensionRows; |
226 | } | |
6a488035 | 227 | |
616aa7a1 | 228 | /** |
e4c4f267 | 229 | * Get the list of remote extensions and format them as a table with |
616aa7a1 TO |
230 | * status and action data. |
231 | * | |
232 | * @param array $localExtensionRows | |
233 | * @return array | |
234 | */ | |
235 | public function formatRemoteExtensionRows($localExtensionRows) { | |
b769826b CW |
236 | try { |
237 | $remoteExtensions = CRM_Extension_System::singleton()->getBrowser()->getExtensions(); | |
238 | } | |
239 | catch (CRM_Extension_Exception $e) { | |
be2fb01f | 240 | $remoteExtensions = []; |
b769826b CW |
241 | CRM_Core_Session::setStatus($e->getMessage(), ts('Extension download error'), 'error'); |
242 | } | |
243 | ||
b44e3f84 | 244 | // build list of available downloads |
be2fb01f | 245 | $remoteExtensionRows = []; |
e4c4f267 CW |
246 | $compat = CRM_Extension_System::getCompatibilityInfo(); |
247 | ||
b769826b | 248 | foreach ($remoteExtensions as $info) { |
e4c4f267 CW |
249 | if (!empty($compat[$info->key]['obsolete'])) { |
250 | continue; | |
251 | } | |
6a488035 TO |
252 | $row = (array) $info; |
253 | $row['id'] = $info->key; | |
254 | $action = CRM_Core_Action::UPDATE; | |
255 | $row['action'] = CRM_Core_Action::formLink(self::links(), | |
256 | $action, | |
be2fb01f | 257 | [ |
6a488035 TO |
258 | 'id' => $row['id'], |
259 | 'key' => $row['key'], | |
be2fb01f | 260 | ], |
87dab4a4 AH |
261 | ts('more'), |
262 | FALSE, | |
263 | 'extension.remote.action', | |
264 | 'Extension', | |
265 | $row['id'] | |
6a488035 | 266 | ); |
46693baf | 267 | if (isset($localExtensionRows[$info->key])) { |
518d3e48 JK |
268 | if (array_key_exists('version', $localExtensionRows[$info->key])) { |
269 | if (version_compare($localExtensionRows[$info->key]['version'], $info->version, '<')) { | |
270 | $row['is_upgradeable'] = TRUE; | |
271 | } | |
46693baf TO |
272 | } |
273 | } | |
6a488035 TO |
274 | $remoteExtensionRows[$row['id']] = $row; |
275 | } | |
616aa7a1 TO |
276 | |
277 | return $remoteExtensionRows; | |
6a488035 TO |
278 | } |
279 | ||
280 | /** | |
eceb18cc | 281 | * Get name of edit form. |
6a488035 | 282 | * |
a6c01b45 CW |
283 | * @return string |
284 | * Classname of edit form. | |
6a488035 | 285 | */ |
00be9182 | 286 | public function editForm() { |
6a488035 TO |
287 | return 'CRM_Admin_Form_Extensions'; |
288 | } | |
289 | ||
290 | /** | |
eceb18cc | 291 | * Get edit form name. |
6a488035 | 292 | * |
a6c01b45 CW |
293 | * @return string |
294 | * name of this page. | |
6a488035 | 295 | */ |
00be9182 | 296 | public function editName() { |
6a488035 TO |
297 | return 'CRM_Admin_Form_Extensions'; |
298 | } | |
299 | ||
300 | /** | |
301 | * Get user context. | |
302 | * | |
fd31fa4c EM |
303 | * @param null $mode |
304 | * | |
a6c01b45 CW |
305 | * @return string |
306 | * user context. | |
6a488035 | 307 | */ |
00be9182 | 308 | public function userContext($mode = NULL) { |
6a488035 TO |
309 | return 'civicrm/admin/extensions'; |
310 | } | |
311 | ||
312 | /** | |
eceb18cc | 313 | * Get userContext params. |
6a488035 | 314 | * |
5173bd95 TO |
315 | * @param int $mode |
316 | * Mode that we are in. | |
6a488035 TO |
317 | * |
318 | * @return string | |
6a488035 | 319 | */ |
00be9182 | 320 | public function userContextParams($mode = NULL) { |
6a488035 TO |
321 | return 'reset=1&action=browse'; |
322 | } | |
323 | ||
324 | /** | |
325 | * Take an extension's raw XML info and add information about the | |
326 | * extension's status on the local system. | |
327 | * | |
328 | * The result format resembles the old CRM_Core_Extensions_Extension. | |
329 | * | |
da6b46f4 EM |
330 | * @param CRM_Extension_Info $obj |
331 | * | |
6a488035 TO |
332 | * @return array |
333 | */ | |
334 | public static function createExtendedInfo(CRM_Extension_Info $obj) { | |
9a5dc864 | 335 | return CRM_Extension_System::createExtendedInfo($obj); |
6a488035 | 336 | } |
96025800 | 337 | |
6a488035 | 338 | } |