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