Merge pull request #18939 from colemanw/serializeFix
[civicrm-core.git] / CRM / Admin / Page / Extensions.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * This is a part of CiviCRM extension management functionality.
14 *
15 * @package CRM
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
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 /**
25 * The action links that we need to display for the browse screen.
26 *
27 * @var array
28 */
29 public static $_links = NULL;
30
31 /**
32 * Obtains the group name from url and sets the title.
33 */
34 public function preProcess() {
35 Civi::resources()->addStyleFile('civicrm', 'css/admin.css');
36
37 CRM_Utils_System::setTitle(ts('CiviCRM Extensions'));
38 $destination = CRM_Utils_System::url('civicrm/admin/extensions',
39 'reset=1');
40
41 $destination = urlencode($destination);
42 $this->assign('destination', $destination);
43 }
44
45 /**
46 * Get BAO Name.
47 *
48 * @return string
49 * Classname of BAO.
50 */
51 public function getBAOName() {
52 return 'CRM_Core_BAO_Extension';
53 }
54
55 /**
56 * Get action Links.
57 *
58 * @return array
59 * (reference) of action links
60 */
61 public function &links() {
62 if (!(self::$_links)) {
63 self::$_links = [
64 CRM_Core_Action::ADD => [
65 'name' => ts('Install'),
66 'url' => 'civicrm/admin/extensions',
67 'qs' => 'action=add&id=%%id%%&key=%%key%%',
68 'title' => ts('Install'),
69 ],
70 CRM_Core_Action::ENABLE => [
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'),
76 ],
77 CRM_Core_Action::DISABLE => [
78 'name' => ts('Disable'),
79 'url' => 'civicrm/admin/extensions',
80 'qs' => 'action=disable&id=%%id%%&key=%%key%%',
81 'title' => ts('Disable'),
82 ],
83 CRM_Core_Action::DELETE => [
84 'name' => ts('Uninstall'),
85 'url' => 'civicrm/admin/extensions',
86 'qs' => 'action=delete&id=%%id%%&key=%%key%%',
87 'title' => ts('Uninstall Extension'),
88 ],
89 CRM_Core_Action::UPDATE => [
90 'name' => ts('Download'),
91 'url' => 'civicrm/admin/extensions',
92 'qs' => 'action=update&id=%%id%%&key=%%key%%',
93 'title' => ts('Download Extension'),
94 ],
95 ];
96 }
97 return self::$_links;
98 }
99
100 /**
101 * Run the basic page (run essentially starts execution for that page).
102 */
103 public function run() {
104 $this->preProcess();
105 return parent::run();
106 }
107
108 /**
109 * Browse all options.
110 */
111 public function browse() {
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 }
119 if (empty($reqs)) {
120 $reqs = CRM_Extension_System::singleton()->getDefaultContainer()->checkRequirements();
121 }
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
130 $localExtensionRows = $this->formatLocalExtensionRows();
131 $this->assign('localExtensionRows', $localExtensionRows);
132
133 $remoteExtensionRows = $this->formatRemoteExtensionRows($localExtensionRows);
134 $this->assign('remoteExtensionRows', $remoteExtensionRows);
135 }
136
137 /**
138 * Get the list of local extensions and format them as a table with
139 * status and action data.
140 *
141 * @return array
142 */
143 public function formatLocalExtensionRows() {
144 $mapper = CRM_Extension_System::singleton()->getMapper();
145 $manager = CRM_Extension_System::singleton()->getManager();
146
147 // array($pseudo_id => extended_CRM_Extension_Info)
148 $localExtensionRows = [];
149 $keys = array_keys($manager->getStatuses());
150 sort($keys);
151 $hiddenExtensions = $mapper->getKeysByTag('mgmt:hidden');
152 foreach ($keys as $key) {
153 if (in_array($key, $hiddenExtensions)) {
154 continue;
155 }
156 try {
157 $obj = $mapper->keyToInfo($key);
158 }
159 catch (CRM_Extension_Exception $ex) {
160 CRM_Core_Session::setStatus(ts('Failed to read extension (%1). Please refresh the extension list.', [1 => $key]));
161 continue;
162 }
163
164 $mapper = CRM_Extension_System::singleton()->getMapper();
165
166 $row = self::createExtendedInfo($obj);
167 $row['id'] = $obj->key;
168 $row['action'] = '';
169
170 // assign actions
171 $action = 0;
172 switch ($row['status']) {
173 case CRM_Extension_Manager::STATUS_UNINSTALLED:
174 if (!$manager->isIncompatible($row['id'])) {
175 $action += CRM_Core_Action::ADD;
176 }
177 break;
178
179 case CRM_Extension_Manager::STATUS_DISABLED:
180 if (!$manager->isIncompatible($row['id'])) {
181 $action += CRM_Core_Action::ENABLE;
182 }
183 $action += CRM_Core_Action::DELETE;
184 break;
185
186 case CRM_Extension_Manager::STATUS_DISABLED_MISSING:
187 $action += CRM_Core_Action::DELETE;
188 break;
189
190 case CRM_Extension_Manager::STATUS_INSTALLED:
191 case CRM_Extension_Manager::STATUS_INSTALLED_MISSING:
192 $action += CRM_Core_Action::DISABLE;
193 break;
194
195 default:
196 }
197 // TODO if extbrowser is enabled and extbrowser has newer version than extcontainer,
198 // then $action += CRM_Core_Action::UPDATE
199 if ($action) {
200 $row['action'] = CRM_Core_Action::formLink(self::links(),
201 $action,
202 ['id' => $row['id'], 'key' => $obj->key],
203 ts('more'),
204 FALSE,
205 'extension.local.action',
206 'Extension',
207 $row['id']
208 );
209 }
210 // Key would be better to send, but it's not an integer. Moreover, sending the
211 // values to hook_civicrm_links means that you can still get at the key
212
213 $localExtensionRows[$row['id']] = $row;
214 }
215 return $localExtensionRows;
216 }
217
218 /**
219 * Get the list of remote extensions and format them as a table with
220 * status and action data.
221 *
222 * @param array $localExtensionRows
223 * @return array
224 */
225 public function formatRemoteExtensionRows($localExtensionRows) {
226 try {
227 $remoteExtensions = CRM_Extension_System::singleton()->getBrowser()->getExtensions();
228 }
229 catch (CRM_Extension_Exception $e) {
230 $remoteExtensions = [];
231 CRM_Core_Session::setStatus($e->getMessage(), ts('Extension download error'), 'error');
232 }
233
234 // build list of available downloads
235 $remoteExtensionRows = [];
236 $compat = CRM_Extension_System::getCompatibilityInfo();
237 $mapper = CRM_Extension_System::singleton()->getMapper();
238
239 foreach ($remoteExtensions as $info) {
240 if (!empty($compat[$info->key]['obsolete'])) {
241 continue;
242 }
243 $row = (array) $info;
244 $row['id'] = $info->key;
245 $action = CRM_Core_Action::UPDATE;
246 $row['action'] = CRM_Core_Action::formLink(self::links(),
247 $action,
248 [
249 'id' => $row['id'],
250 'key' => $row['key'],
251 ],
252 ts('more'),
253 FALSE,
254 'extension.remote.action',
255 'Extension',
256 $row['id']
257 );
258 if (isset($localExtensionRows[$info->key])) {
259 if (array_key_exists('version', $localExtensionRows[$info->key])) {
260 if (version_compare($localExtensionRows[$info->key]['version'], $info->version, '<')) {
261 $row['upgradelink'] = $mapper->getUpgradeLink($remoteExtensions[$info->key], $localExtensionRows[$info->key]);
262 }
263 }
264 }
265 $remoteExtensionRows[$row['id']] = $row;
266 }
267
268 return $remoteExtensionRows;
269 }
270
271 /**
272 * Get name of edit form.
273 *
274 * @return string
275 * Classname of edit form.
276 */
277 public function editForm() {
278 return 'CRM_Admin_Form_Extensions';
279 }
280
281 /**
282 * Get edit form name.
283 *
284 * @return string
285 * name of this page.
286 */
287 public function editName() {
288 return 'CRM_Admin_Form_Extensions';
289 }
290
291 /**
292 * Get user context.
293 *
294 * @param null $mode
295 *
296 * @return string
297 * user context.
298 */
299 public function userContext($mode = NULL) {
300 return 'civicrm/admin/extensions';
301 }
302
303 /**
304 * Get userContext params.
305 *
306 * @param int $mode
307 * Mode that we are in.
308 *
309 * @return string
310 */
311 public function userContextParams($mode = NULL) {
312 return 'reset=1&action=browse';
313 }
314
315 /**
316 * Take an extension's raw XML info and add information about the
317 * extension's status on the local system.
318 *
319 * The result format resembles the old CRM_Core_Extensions_Extension.
320 *
321 * @param CRM_Extension_Info $obj
322 *
323 * @return array
324 */
325 public static function createExtendedInfo(CRM_Extension_Info $obj) {
326 return CRM_Extension_System::createExtendedInfo($obj);
327 }
328
329 }