Merge pull request #3367 from kirkatcaat/empty-labels-on-profile-fields
[civicrm-core.git] / api / v3 / Extension.php
CommitLineData
6a488035 1<?php
6a488035
TO
2
3define('API_V3_EXTENSION_DELIMITER', ',');
4
5/*
6 +--------------------------------------------------------------------+
731a0992 7 | CiviCRM version 4.5 |
6a488035 8 +--------------------------------------------------------------------+
731a0992 9 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
10 +--------------------------------------------------------------------+
11 | This file is a part of CiviCRM. |
12 | |
13 | CiviCRM is free software; you can copy, modify, and distribute it |
14 | under the terms of the GNU Affero General Public License |
15 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
16 | |
17 | CiviCRM is distributed in the hope that it will be useful, but |
18 | WITHOUT ANY WARRANTY; without even the implied warranty of |
19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
20 | See the GNU Affero General Public License for more details. |
21 | |
22 | You should have received a copy of the GNU Affero General Public |
23 | License and the CiviCRM Licensing Exception along |
24 | with this program; if not, contact CiviCRM LLC |
25 | at info[AT]civicrm[DOT]org. If you have questions about the |
26 | GNU Affero General Public License or the licensing of CiviCRM, |
27 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
28 +--------------------------------------------------------------------+
29 */
30
31/**
32 * File for the CiviCRM APIv3 extension functions
33 *
34 * @package CiviCRM_APIv3
35 * @subpackage API_Extension
36 *
731a0992 37 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
38 * @version $Id$
39 *
40 */
41
42/**
43 * Install an extension
44 *
45 * @param array $params input parameters
46 * - key: string, eg "com.example.myextension"
47 * - keys: mixed; array of string, eg array("com.example.myextension1", "com.example.myextension2") or string with comma-delimited list
48 * using 'keys' should be more performant than making multiple API calls with 'key'
49 *
50 * @return array API result
51 * @static void
52 * @access public
53 * @example ExtensionInstall.php
54 *
55 */
56function civicrm_api3_extension_install($params) {
57 $keys = _civicrm_api3_getKeys($params);
58 if (count($keys) == 0) {
59 return civicrm_api3_create_success();
60 }
61
62 try {
63 CRM_Extension_System::singleton()->getManager()->install($keys);
64 } catch (CRM_Extension_Exception $e) {
65 return civicrm_api3_create_error($e->getMessage());
66 }
67
68 return civicrm_api3_create_success();
69}
70
71/**
72 * Enable an extension
73 *
74 * @param array $params input parameters
75 * - key: string, eg "com.example.myextension"
76 * - keys: mixed; array of string, eg array("com.example.myextension1", "com.example.myextension2") or string with comma-delimited list
77 * using 'keys' should be more performant than making multiple API calls with 'key'
78 *
79 * @return array API result
80 * @static void
81 * @access public
82 * @example ExtensionEnable.php
83 *
84 */
85function civicrm_api3_extension_enable($params) {
86 $keys = _civicrm_api3_getKeys($params);
87 if (count($keys) == 0) {
88 return civicrm_api3_create_success();
89 }
90
91 CRM_Extension_System::singleton()->getManager()->enable($keys);
92 return civicrm_api3_create_success();
93}
94
95/**
96 * Disable an extension
97 *
98 * @param array $params input parameters
99 * - key: string, eg "com.example.myextension"
100 * - keys: mixed; array of string, eg array("com.example.myextension1", "com.example.myextension2") or string with comma-delimited list
101 * using 'keys' should be more performant than making multiple API calls with 'key'
102 *
103 * @return array API result
104 * @static void
105 * @access public
106 * @example ExtensionDisable.php
107 *
108 */
109function civicrm_api3_extension_disable($params) {
110 $keys = _civicrm_api3_getKeys($params);
111 if (count($keys) == 0) {
112 return civicrm_api3_create_success();
113 }
114
115 CRM_Extension_System::singleton()->getManager()->disable($keys);
116 return civicrm_api3_create_success();
117}
118
119/**
120 * Uninstall an extension
121 *
122 * @param array $params input parameters
123 * - key: string, eg "com.example.myextension"
124 * - keys: array of string, eg array("com.example.myextension1", "com.example.myextension2")
125 * using 'keys' should be more performant than making multiple API calls with 'key'
126 * - removeFiles: bool, whether to remove source tree; default: FALSE
127 *
128 * @return array API result
129 * @static void
130 * @access public
131 * @example ExtensionUninstall.php
132 *
133 */
134function civicrm_api3_extension_uninstall($params) {
135 $keys = _civicrm_api3_getKeys($params);
136 if (count($keys) == 0) {
137 return civicrm_api3_create_success();
138 }
139
140 // TODO // $removeFiles = CRM_Utils_Array::value('removeFiles', $params, FALSE);
141 CRM_Extension_System::singleton()->getManager()->uninstall($keys);
142 return civicrm_api3_create_success();
143}
144
145/**
146 * Download and install an extension
147 *
77b97be7 148 * @param array $params input parameters
6a488035
TO
149 * - key: string, eg "com.example.myextension"
150 * - url: string eg "http://repo.com/myextension-1.0.zip"
151 *
77b97be7 152 * @throws API_Exception
6a488035
TO
153 * @return array API result
154 * @static void
155 * @access public
156 * @example ExtensionDownload.php
6a488035
TO
157 */
158function civicrm_api3_extension_download($params) {
159 if (! array_key_exists('key', $params)) {
160 throw new API_Exception('Missing required parameter: key');
161 }
162
163 if (! array_key_exists('url', $params)) {
164 if (! CRM_Extension_System::singleton()->getBrowser()->isEnabled()) {
165 throw new API_Exception('Automatic downloading is diabled. Try adding parameter "url"');
166 }
167 if ($reqs = CRM_Extension_System::singleton()->getBrowser()->checkRequirements()) {
168 $first = array_shift($reqs);
169 throw new API_Exception($first['message']);
170 }
171 if ($info = CRM_Extension_System::singleton()->getBrowser()->getExtension($params['key'])) {
172 if ($info->downloadUrl) {
173 $params['url'] = $info->downloadUrl;
174 }
175 }
176 }
177
178 if (! array_key_exists('url', $params)) {
179 throw new API_Exception('Cannot resolve download url for extension. Try adding parameter "url"');
180 }
181
182 foreach (CRM_Extension_System::singleton()->getDownloader()->checkRequirements() as $requirement) {
183 return civicrm_api3_create_error($requirement['message']);
184 }
185
186 if (! CRM_Extension_System::singleton()->getDownloader()->download($params['key'], $params['url'])) {
187 return civicrm_api3_create_error('Download failed - ZIP file is unavailable or malformed');
188 }
189 CRM_Extension_System::singleton()->getCache()->flush();
190 CRM_Extension_System::singleton(TRUE);
191 CRM_Extension_System::singleton()->getManager()->install(array($params['key']));
192
193 return civicrm_api3_create_success();
194}
195
196/**
197 * Download and install an extension
198 *
199 * @param array $params input parameters
200 * - local: bool, whether to rescan local filesystem (default: TRUE)
201 * - remote: bool, whether to rescan remote repository (default: TRUE)
202 *
203 * @return array API result
204 * @static void
205 * @access public
206 * @example ExtensionRefresh.php
207 *
208 */
209function civicrm_api3_extension_refresh($params) {
210 $defaults = array('local' => TRUE, 'remote' => TRUE);
211 $params = array_merge($defaults, $params);
212
213 $system = CRM_Extension_System::singleton(TRUE);
214
215 if ($params['local']) {
216 $system->getManager()->refresh();
217 $system->getManager()->getStatuses(); // force immediate scan
218 }
219
220 if ($params['remote']) {
221 if ($system->getBrowser()->isEnabled() && empty($system->getBrowser()->checkRequirements)) {
222 $system->getBrowser()->refresh();
223 $system->getBrowser()->getExtensions(); // force immediate download
224 }
225 }
226
227 return civicrm_api3_create_success();
228}
229
230/**
231 * Get a list of available extensions
232 *
77b97be7
EM
233 * @param $params
234 *
6a488035
TO
235 * @return array API result
236 * @static void
237 * @access public
238 * @example ExtensionGet.php
6a488035
TO
239 */
240function civicrm_api3_extension_get($params) {
241 $statuses = CRM_Extension_System::singleton()->getManager()->getStatuses();
242 $mapper = CRM_Extension_System::singleton()->getMapper();
243 $result = array();
244 foreach ($statuses as $key => $status) {
245 //try {
246 // $info = (array) $mapper->keyToInfo($key);
247 //} catch (CRM_Extension_Exception $e) {
248 $info = array();
249 $info['key'] = $key;
250 //}
251 $info['status'] = $status;
252 $result[] = $info;
253 }
254 return civicrm_api3_create_success($result);
255}
256
257/**
258 * Determine the list of extension keys
259 *
260 * @param array $params API request params with 'key' or 'keys'
261 * @return array of extension keys
262 * @throws API_Exception
263 */
264function _civicrm_api3_getKeys($params) {
265 if (array_key_exists('keys', $params) && is_array($params['keys'])) {
266 return $params['keys'];
267 } elseif (array_key_exists('keys', $params) && is_string($params['keys'])) {
268 if ($params['keys'] == '') {
269 return array();
270 } else {
271 return explode(API_V3_EXTENSION_DELIMITER, $params['keys']);
272 }
273 } elseif (array_key_exists('key', $params)) {
274 return array($params['key']);
275 } else {
276 throw new API_Exception('Missing required parameter: key or keys');
277 }
278}