Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
6a488035 TO |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
6a488035 TO |
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 | /** | |
100fef9d | 43 | * For pre-processing |
6a488035 | 44 | * |
355ba699 | 45 | * @return void |
6a488035 TO |
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 | ||
6a488035 TO |
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: | |
353ffa53 | 71 | if (!CRM_Extension_System::singleton()->getBrowser()->isEnabled()) { |
6a488035 TO |
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 | /** | |
c490a46a | 86 | * Set default values for the form. |
6a488035 TO |
87 | * the default values are retrieved from the database |
88 | * | |
6a488035 | 89 | * |
355ba699 | 90 | * @return void |
6a488035 | 91 | */ |
00be9182 | 92 | public function setDefaultValues() { |
6a488035 TO |
93 | $defaults = array(); |
94 | return $defaults; | |
95 | } | |
96 | ||
97 | /** | |
eceb18cc | 98 | * Build the form object. |
6a488035 | 99 | * |
355ba699 | 100 | * @return void |
6a488035 TO |
101 | */ |
102 | public function buildQuickForm() { | |
6a488035 TO |
103 | switch ($this->_action) { |
104 | case CRM_Core_Action::ADD: | |
105 | $buttonName = ts('Install'); | |
f640e46c TO |
106 | $title = ts('Install "%1"?', array( |
107 | 1 => $this->_key, | |
108 | )); | |
6a488035 TO |
109 | break; |
110 | ||
111 | case CRM_Core_Action::UPDATE: | |
112 | $buttonName = ts('Download and Install'); | |
f640e46c TO |
113 | $title = ts('Download and Install "%1"?', array( |
114 | 1 => $this->_key, | |
115 | )); | |
6a488035 TO |
116 | break; |
117 | ||
118 | case CRM_Core_Action::DELETE: | |
119 | $buttonName = ts('Uninstall'); | |
f640e46c TO |
120 | $title = ts('Uninstall "%1"?', array( |
121 | 1 => $this->_key, | |
122 | )); | |
6a488035 TO |
123 | break; |
124 | ||
125 | case CRM_Core_Action::ENABLE: | |
126 | $buttonName = ts('Enable'); | |
f640e46c TO |
127 | $title = ts('Enable "%1"?', array( |
128 | 1 => $this->_key, | |
129 | )); | |
6a488035 TO |
130 | break; |
131 | ||
132 | case CRM_Core_Action::DISABLE: | |
affc7312 | 133 | $buttonName = ts('Disable'); |
f640e46c TO |
134 | $title = ts('Disable "%1"?', array( |
135 | 1 => $this->_key, | |
136 | )); | |
6a488035 TO |
137 | break; |
138 | } | |
139 | ||
140 | $this->assign('title', $title); | |
141 | $this->addButtons(array( | |
142 | array( | |
143 | 'type' => 'next', | |
144 | 'name' => $buttonName, | |
145 | 'isDefault' => TRUE, | |
146 | ), | |
147 | array( | |
148 | 'type' => 'cancel', | |
149 | 'name' => ts('Cancel'), | |
150 | ), | |
151 | ) | |
152 | ); | |
153 | } | |
154 | ||
155 | /** | |
eceb18cc | 156 | * Global form rule. |
6a488035 | 157 | * |
5173bd95 TO |
158 | * @param array $fields |
159 | * The input form values. | |
160 | * @param array $files | |
161 | * The uploaded files if any. | |
162 | * @param array $self | |
163 | * This object. | |
6a488035 | 164 | * |
72b3a70c CW |
165 | * @return bool|array |
166 | * true if no errors, else an array of errors | |
6a488035 | 167 | */ |
00be9182 | 168 | public static function formRule($fields, $files, $self) { |
6a488035 TO |
169 | $errors = array(); |
170 | ||
171 | return empty($errors) ? TRUE : $errors; | |
172 | } | |
173 | ||
174 | /** | |
eceb18cc | 175 | * Process the form submission. |
6a488035 | 176 | * |
6a488035 | 177 | * |
355ba699 | 178 | * @return void |
6a488035 TO |
179 | */ |
180 | public function postProcess() { | |
181 | CRM_Utils_System::flushCache(); | |
182 | ||
183 | if ($this->_action & CRM_Core_Action::DELETE) { | |
184 | try { | |
185 | CRM_Extension_System::singleton()->getManager()->uninstall(array($this->_key)); | |
186 | CRM_Core_Session::setStatus("", ts('Extension Uninstalled'), "success"); | |
0db6c3e1 TO |
187 | } |
188 | catch (CRM_Extension_Exception_DependencyException $e) { | |
6a488035 TO |
189 | // currently only thrown for payment-processor dependencies |
190 | 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'); | |
191 | } | |
192 | } | |
193 | ||
194 | if ($this->_action & CRM_Core_Action::ADD) { | |
195 | CRM_Extension_System::singleton()->getManager()->install(array($this->_key)); | |
196 | CRM_Core_Session::setStatus("", ts('Extension Installed'), "success"); | |
197 | } | |
198 | ||
199 | if ($this->_action & CRM_Core_Action::ENABLE) { | |
200 | CRM_Extension_System::singleton()->getManager()->enable(array($this->_key)); | |
201 | CRM_Core_Session::setStatus("", ts('Extension Enabled'), "success"); | |
202 | } | |
203 | ||
204 | if ($this->_action & CRM_Core_Action::DISABLE) { | |
205 | CRM_Extension_System::singleton()->getManager()->disable(array($this->_key)); | |
206 | CRM_Core_Session::setStatus("", ts('Extension Disabled'), "success"); | |
207 | } | |
208 | ||
209 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
210 | $result = civicrm_api('Extension', 'download', array( | |
211 | 'version' => 3, | |
212 | 'key' => $this->_key, | |
213 | )); | |
353ffa53 | 214 | if (!CRM_Utils_Array::value('is_error', $result, FALSE)) { |
6a488035 | 215 | CRM_Core_Session::setStatus("", ts('Extension Upgraded'), "success"); |
0db6c3e1 TO |
216 | } |
217 | else { | |
6a488035 TO |
218 | CRM_Core_Session::setStatus($result['error_message'], ts('Extension Upgrade Failed'), "error"); |
219 | } | |
220 | } | |
221 | ||
222 | CRM_Utils_System::redirect( | |
223 | CRM_Utils_System::url( | |
224 | 'civicrm/admin/extensions', | |
225 | 'reset=1&action=browse' | |
226 | ) | |
227 | ); | |
228 | } | |
96025800 | 229 | |
6a488035 | 230 | } |