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