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