| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.7 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
| 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-2015 |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | |
| 36 | /** |
| 37 | * Administer Personal Campaign Pages - Search form |
| 38 | */ |
| 39 | class CRM_PCP_Form_PCP extends CRM_Core_Form { |
| 40 | |
| 41 | public $_context; |
| 42 | |
| 43 | /** |
| 44 | * Set variables up before form is built. |
| 45 | */ |
| 46 | public function preProcess() { |
| 47 | if ($this->_action & CRM_Core_Action::DELETE) { |
| 48 | //check permission for action. |
| 49 | if (!CRM_Core_Permission::checkActionPermission('CiviEvent', $this->_action)) { |
| 50 | CRM_Core_Error::fatal(ts('You do not have permission to access this page.')); |
| 51 | } |
| 52 | |
| 53 | $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this); |
| 54 | $this->_title = CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP', $this->_id, 'title'); |
| 55 | $this->assign('title', $this->_title); |
| 56 | parent::preProcess(); |
| 57 | } |
| 58 | |
| 59 | if (!$this->_action) { |
| 60 | $this->_action = CRM_Utils_Array::value('action', $_GET); |
| 61 | $this->_id = CRM_Utils_Array::value('id', $_GET); |
| 62 | } |
| 63 | else { |
| 64 | $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this); |
| 65 | } |
| 66 | |
| 67 | //give the context. |
| 68 | if (!isset($this->_context)) { |
| 69 | $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this); |
| 70 | } |
| 71 | |
| 72 | $this->assign('context', $this->_context); |
| 73 | |
| 74 | $session = CRM_Core_Session::singleton(); |
| 75 | $context = $session->popUserContext(); |
| 76 | $userID = $session->get('userID'); |
| 77 | |
| 78 | //do not allow destructive actions without permissions |
| 79 | $permission = FALSE; |
| 80 | if (CRM_Core_Permission::check('administer CiviCRM') || |
| 81 | ($userID && (CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP', |
| 82 | $this->_id, |
| 83 | 'contact_id' |
| 84 | ) == $userID)) |
| 85 | ) { |
| 86 | $permission = TRUE; |
| 87 | } |
| 88 | if ($permission && $this->_id) { |
| 89 | |
| 90 | $this->_title = CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP', $this->_id, 'title'); |
| 91 | switch ($this->_action) { |
| 92 | case CRM_Core_Action::DELETE: |
| 93 | case 'delete': |
| 94 | CRM_PCP_BAO_PCP::deleteById($this->_id); |
| 95 | CRM_Core_Session::setStatus(ts("The Campaign Page '%1' has been deleted.", array(1 => $this->_title)), ts('Page Deleted'), 'success'); |
| 96 | break; |
| 97 | |
| 98 | case CRM_Core_Action::DISABLE: |
| 99 | case 'disable': |
| 100 | CRM_PCP_BAO_PCP::setDisable($this->_id, '0'); |
| 101 | CRM_Core_Session::setStatus(ts("The Campaign Page '%1' has been disabled.", array(1 => $this->_title)), ts('Page Disabled'), 'success'); |
| 102 | break; |
| 103 | |
| 104 | case CRM_Core_Action::ENABLE: |
| 105 | case 'enable': |
| 106 | CRM_PCP_BAO_PCP::setDisable($this->_id, '1'); |
| 107 | CRM_Core_Session::setStatus(ts("The Campaign Page '%1' has been enabled.", array(1 => $this->_title)), ts('Page Enabled'), 'success'); |
| 108 | break; |
| 109 | } |
| 110 | |
| 111 | if ($context) { |
| 112 | CRM_Utils_System::redirect($context); |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Set default values for the form. Note that in edit/view mode |
| 119 | * the default values are retrieved from the database |
| 120 | * |
| 121 | * @return array |
| 122 | * array of default values |
| 123 | */ |
| 124 | public function setDefaultValues() { |
| 125 | $defaults = array(); |
| 126 | |
| 127 | $pageType = CRM_Utils_Request::retrieve('page_type', 'String', $this); |
| 128 | $defaults['page_type'] = !empty($pageType) ? $pageType : ''; |
| 129 | |
| 130 | return $defaults; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Build the form object. |
| 135 | */ |
| 136 | public function buildQuickForm() { |
| 137 | if ($this->_action & CRM_Core_Action::DELETE) { |
| 138 | $this->addButtons(array( |
| 139 | array( |
| 140 | 'type' => 'next', |
| 141 | 'name' => ts('Delete Campaign'), |
| 142 | 'isDefault' => TRUE, |
| 143 | ), |
| 144 | array( |
| 145 | 'type' => 'cancel', |
| 146 | 'name' => ts('Cancel'), |
| 147 | ), |
| 148 | ) |
| 149 | ); |
| 150 | } |
| 151 | else { |
| 152 | |
| 153 | $status = array('' => ts('- select -')) + CRM_Core_OptionGroup::values("pcp_status"); |
| 154 | $types = array( |
| 155 | '' => ts('- select -'), |
| 156 | 'contribute' => ts('Contribution'), |
| 157 | 'event' => ts('Event'), |
| 158 | ); |
| 159 | $contribPages = array('' => ts('- select -')) + CRM_Contribute_PseudoConstant::contributionPage(); |
| 160 | $eventPages = array('' => ts('- select -')) + CRM_Event_PseudoConstant::event(NULL, FALSE, "( is_template IS NULL OR is_template != 1 )"); |
| 161 | |
| 162 | $this->addElement('select', 'status_id', ts('Status'), $status); |
| 163 | $this->addElement('select', 'page_type', ts('Source Type'), $types); |
| 164 | $this->addElement('select', 'page_id', ts('Contribution Page'), $contribPages); |
| 165 | $this->addElement('select', 'event_id', ts('Event Page'), $eventPages); |
| 166 | $this->addButtons(array( |
| 167 | array( |
| 168 | 'type' => 'refresh', |
| 169 | 'name' => ts('Search'), |
| 170 | 'spacing' => ' ', |
| 171 | 'isDefault' => TRUE, |
| 172 | ), |
| 173 | ) |
| 174 | ); |
| 175 | parent::buildQuickForm(); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Global validation rules for the form. |
| 181 | * |
| 182 | * @param array $fields |
| 183 | * Posted values of the form. |
| 184 | * @param array $files |
| 185 | * @param CRM_Core_Form $form |
| 186 | * |
| 187 | * @return array|NULL |
| 188 | * list of errors to be posted back to the form |
| 189 | */ |
| 190 | public static function formRule($fields, $files, $form) { |
| 191 | return NULL; |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Process the form. |
| 196 | */ |
| 197 | public function postProcess() { |
| 198 | if ($this->_action & CRM_Core_Action::DELETE) { |
| 199 | CRM_PCP_BAO_PCP::deleteById($this->_id); |
| 200 | CRM_Core_Session::setStatus(ts("The Campaign Page '%1' has been deleted.", array(1 => $this->_title)), ts('Page Deleted'), 'success'); |
| 201 | } |
| 202 | else { |
| 203 | $params = $this->controller->exportValues($this->_name); |
| 204 | $parent = $this->controller->getParent(); |
| 205 | |
| 206 | if (!empty($params)) { |
| 207 | $fields = array('status_id', 'page_id'); |
| 208 | foreach ($fields as $field) { |
| 209 | if (isset($params[$field]) && |
| 210 | !CRM_Utils_System::isNull($params[$field]) |
| 211 | ) { |
| 212 | $parent->set($field, $params[$field]); |
| 213 | } |
| 214 | else { |
| 215 | $parent->set($field, NULL); |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | } |