Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
232624b1 | 4 | | CiviCRM version 4.4 | |
6a488035 TO |
5 | +--------------------------------------------------------------------+ |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
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-2013 | |
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 | * Function to for pre-processing | |
44 | * | |
45 | * @return None | |
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 | * This function sets the default values for the form. | |
89 | * the default values are retrieved from the database | |
90 | * | |
91 | * @access public | |
92 | * | |
93 | * @return None | |
94 | */ | |
95 | function setDefaultValues() { | |
96 | $defaults = array(); | |
97 | return $defaults; | |
98 | } | |
99 | ||
100 | /** | |
101 | * Function to build the form | |
102 | * | |
103 | * @return None | |
104 | * @access public | |
105 | */ | |
106 | public function buildQuickForm() { | |
107 | ||
108 | switch ($this->_action) { | |
109 | case CRM_Core_Action::ADD: | |
110 | $buttonName = ts('Install'); | |
111 | $title = ts('Install Extension'); | |
112 | break; | |
113 | ||
114 | case CRM_Core_Action::UPDATE: | |
115 | $buttonName = ts('Download and Install'); | |
116 | $title = ts('Download and Install Extension'); | |
117 | break; | |
118 | ||
119 | case CRM_Core_Action::DELETE: | |
120 | $buttonName = ts('Uninstall'); | |
121 | $title = ts('Uninstall Extension'); | |
122 | break; | |
123 | ||
124 | case CRM_Core_Action::ENABLE: | |
125 | $buttonName = ts('Enable'); | |
126 | $title = ts('Enable Extension'); | |
127 | break; | |
128 | ||
129 | case CRM_Core_Action::DISABLE: | |
130 | $buttonName = 'Disable'; | |
131 | $title = ts('Disable Extension'); | |
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 the input form values | |
154 | * @param array $files the uploaded files if any | |
155 | * @param array $self this object. | |
156 | * | |
157 | * @return true if no errors, else an array of errors | |
158 | * @access public | |
159 | * @static | |
160 | */ | |
161 | static function formRule($fields, $files, $self) { | |
162 | $errors = array(); | |
163 | ||
164 | return empty($errors) ? TRUE : $errors; | |
165 | } | |
166 | ||
167 | /** | |
168 | * Function to process the form | |
169 | * | |
170 | * @access public | |
171 | * | |
172 | * @return None | |
173 | */ | |
174 | public function postProcess() { | |
175 | CRM_Utils_System::flushCache(); | |
176 | ||
177 | if ($this->_action & CRM_Core_Action::DELETE) { | |
178 | try { | |
179 | CRM_Extension_System::singleton()->getManager()->uninstall(array($this->_key)); | |
180 | CRM_Core_Session::setStatus("", ts('Extension Uninstalled'), "success"); | |
181 | } catch (CRM_Extension_Exception_DependencyException $e) { | |
182 | // currently only thrown for payment-processor dependencies | |
183 | 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'); | |
184 | } | |
185 | } | |
186 | ||
187 | if ($this->_action & CRM_Core_Action::ADD) { | |
188 | CRM_Extension_System::singleton()->getManager()->install(array($this->_key)); | |
189 | CRM_Core_Session::setStatus("", ts('Extension Installed'), "success"); | |
190 | } | |
191 | ||
192 | if ($this->_action & CRM_Core_Action::ENABLE) { | |
193 | CRM_Extension_System::singleton()->getManager()->enable(array($this->_key)); | |
194 | CRM_Core_Session::setStatus("", ts('Extension Enabled'), "success"); | |
195 | } | |
196 | ||
197 | if ($this->_action & CRM_Core_Action::DISABLE) { | |
198 | CRM_Extension_System::singleton()->getManager()->disable(array($this->_key)); | |
199 | CRM_Core_Session::setStatus("", ts('Extension Disabled'), "success"); | |
200 | } | |
201 | ||
202 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
203 | $result = civicrm_api('Extension', 'download', array( | |
204 | 'version' => 3, | |
205 | 'key' => $this->_key, | |
206 | )); | |
207 | if (! CRM_Utils_Array::value('is_error', $result, FALSE)) { | |
208 | CRM_Core_Session::setStatus("", ts('Extension Upgraded'), "success"); | |
209 | } else { | |
210 | CRM_Core_Session::setStatus($result['error_message'], ts('Extension Upgrade Failed'), "error"); | |
211 | } | |
212 | } | |
213 | ||
214 | CRM_Utils_System::redirect( | |
215 | CRM_Utils_System::url( | |
216 | 'civicrm/admin/extensions', | |
217 | 'reset=1&action=browse' | |
218 | ) | |
219 | ); | |
220 | } | |
221 | } | |
222 |