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