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