Merge remote-tracking branch 'upstream/4.5' into 4.5-master-2014-12-16-01-08-03
[civicrm-core.git] / CRM / Admin / Form / Extensions.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * This class generates form components for Extensions
38 *
39 */
40 class CRM_Admin_Form_Extensions extends CRM_Admin_Form {
41
42 /**
43 * For pre-processing
44 *
45 * @return void
46 * @access public
47 */
48 public function preProcess() {
49 parent::preProcess();
50
51 $this->_key = CRM_Utils_Request::retrieve('key', 'String',
52 $this, FALSE, 0
53 );
54
55 $session = CRM_Core_Session::singleton();
56 $url = CRM_Utils_System::url('civicrm/admin/extensions', 'reset=1&action=browse');
57 $session->pushUserContext($url);
58 $this->assign('id', $this->_id);
59 $this->assign('key', $this->_key);
60
61
62 switch ($this->_action) {
63 case CRM_Core_Action::ADD:
64 case CRM_Core_Action::DELETE:
65 case CRM_Core_Action::ENABLE:
66 case CRM_Core_Action::DISABLE:
67 $info = CRM_Extension_System::singleton()->getMapper()->keyToInfo($this->_key);
68 $extInfo = CRM_Admin_Page_Extensions::createExtendedInfo($info);
69 $this->assign('extension', $extInfo);
70 break;
71
72 case CRM_Core_Action::UPDATE:
73 if (! CRM_Extension_System::singleton()->getBrowser()->isEnabled()) {
74 CRM_Core_Error::fatal(ts('The system administrator has disabled this feature.'));
75 }
76 $info = CRM_Extension_System::singleton()->getBrowser()->getExtension($this->_key);
77 $extInfo = CRM_Admin_Page_Extensions::createExtendedInfo($info);
78 $this->assign('extension', $extInfo);
79 break;
80
81 default:
82 CRM_Core_Error::fatal(ts('Unsupported action'));
83 }
84
85 }
86
87 /**
88 * Set default values for the form.
89 * the default values are retrieved from the database
90 *
91 * @access public
92 *
93 * @return void
94 */
95 function setDefaultValues() {
96 $defaults = array();
97 return $defaults;
98 }
99
100 /**
101 * Build the form object
102 *
103 * @return void
104 * @access public
105 */
106 public function buildQuickForm() {
107
108 $info = CRM_Extension_System::singleton()->getMapper()->keyToInfo($this->_key);
109 $extInfo = CRM_Admin_Page_Extensions::createExtendedInfo($info);
110 $extName = $extInfo['name'];
111
112 switch ($this->_action) {
113 case CRM_Core_Action::ADD:
114 $buttonName = ts('Install');
115 $title = ts('Install ' . $extName . '?');
116 break;
117
118 case CRM_Core_Action::UPDATE:
119 $buttonName = ts('Download and Install');
120 $title = ts('Download and Install ' . $extName . '?');
121 break;
122
123 case CRM_Core_Action::DELETE:
124 $buttonName = ts('Uninstall');
125 $title = ts('Uninstall ' . $extName . '?');
126 break;
127
128 case CRM_Core_Action::ENABLE:
129 $buttonName = ts('Enable');
130 $title = ts('Enable ' . $extName . '?');
131 break;
132
133 case CRM_Core_Action::DISABLE:
134 $buttonName = ts('Disable');
135 $title = ts('Disable ' . $extName . '?');
136 break;
137 }
138
139 $this->assign('title', $title);
140 $this->addButtons(array(
141 array(
142 'type' => 'next',
143 'name' => $buttonName,
144 'isDefault' => TRUE,
145 ),
146 array(
147 'type' => 'cancel',
148 'name' => ts('Cancel'),
149 ),
150 )
151 );
152 }
153
154 /**
155 * Global form rule
156 *
157 * @param array $fields the input form values
158 * @param array $files the uploaded files if any
159 * @param array $self this object.
160 *
161 * @return true if no errors, else an array of errors
162 * @access public
163 * @static
164 */
165 static function formRule($fields, $files, $self) {
166 $errors = array();
167
168 return empty($errors) ? TRUE : $errors;
169 }
170
171 /**
172 * Process the form submission
173 *
174 * @access public
175 *
176 * @return void
177 */
178 public function postProcess() {
179 CRM_Utils_System::flushCache();
180
181 if ($this->_action & CRM_Core_Action::DELETE) {
182 try {
183 CRM_Extension_System::singleton()->getManager()->uninstall(array($this->_key));
184 CRM_Core_Session::setStatus("", ts('Extension Uninstalled'), "success");
185 } catch (CRM_Extension_Exception_DependencyException $e) {
186 // currently only thrown for payment-processor dependencies
187 CRM_Core_Session::setStatus(ts('Cannot uninstall this extension - there is at least one payment processor using the payment processor type provided by it.'), ts('Uninstall Error'), 'error');
188 }
189 }
190
191 if ($this->_action & CRM_Core_Action::ADD) {
192 CRM_Extension_System::singleton()->getManager()->install(array($this->_key));
193 CRM_Core_Session::setStatus("", ts('Extension Installed'), "success");
194 }
195
196 if ($this->_action & CRM_Core_Action::ENABLE) {
197 CRM_Extension_System::singleton()->getManager()->enable(array($this->_key));
198 CRM_Core_Session::setStatus("", ts('Extension Enabled'), "success");
199 }
200
201 if ($this->_action & CRM_Core_Action::DISABLE) {
202 CRM_Extension_System::singleton()->getManager()->disable(array($this->_key));
203 CRM_Core_Session::setStatus("", ts('Extension Disabled'), "success");
204 }
205
206 if ($this->_action & CRM_Core_Action::UPDATE) {
207 $result = civicrm_api('Extension', 'download', array(
208 'version' => 3,
209 'key' => $this->_key,
210 ));
211 if (! CRM_Utils_Array::value('is_error', $result, FALSE)) {
212 CRM_Core_Session::setStatus("", ts('Extension Upgraded'), "success");
213 } else {
214 CRM_Core_Session::setStatus($result['error_message'], ts('Extension Upgrade Failed'), "error");
215 }
216 }
217
218 CRM_Utils_System::redirect(
219 CRM_Utils_System::url(
220 'civicrm/admin/extensions',
221 'reset=1&action=browse'
222 )
223 );
224 }
225 }
226