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