| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | Copyright CiviCRM LLC. All rights reserved. | |
| 5 | | | |
| 6 | | This work is published under the GNU AGPLv3 license with some | |
| 7 | | permitted exceptions and without any warranty. For full license | |
| 8 | | and copyright information, see https://civicrm.org/licensing | |
| 9 | +--------------------------------------------------------------------+ |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * |
| 14 | * @package CRM |
| 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
| 16 | * $Id$ |
| 17 | * |
| 18 | */ |
| 19 | class CRM_Event_Page_Tab extends CRM_Core_Page { |
| 20 | |
| 21 | public $_permission = NULL; |
| 22 | public $_contactId = NULL; |
| 23 | |
| 24 | /** |
| 25 | * called when action is browse. |
| 26 | */ |
| 27 | public function browse() { |
| 28 | $controller = new CRM_Core_Controller_Simple( |
| 29 | 'CRM_Event_Form_Search', |
| 30 | ts('Events'), |
| 31 | $this->_action |
| 32 | ); |
| 33 | $controller->setEmbedded(TRUE); |
| 34 | $controller->reset(); |
| 35 | $controller->set('cid', $this->_contactId); |
| 36 | $controller->set('context', 'participant'); |
| 37 | $controller->process(); |
| 38 | $controller->run(); |
| 39 | |
| 40 | if ($this->_contactId) { |
| 41 | $displayName = CRM_Contact_BAO_Contact::displayName($this->_contactId); |
| 42 | $this->assign('displayName', $displayName); |
| 43 | $this->ajaxResponse['tabCount'] = CRM_Contact_BAO_Contact::getCountComponent('participant', $this->_contactId); |
| 44 | // Refresh other tabs with related data |
| 45 | $this->ajaxResponse['updateTabs'] = [ |
| 46 | '#tab_activity' => CRM_Contact_BAO_Contact::getCountComponent('activity', $this->_contactId), |
| 47 | ]; |
| 48 | if (CRM_Core_Permission::access('CiviContribute')) { |
| 49 | $this->ajaxResponse['updateTabs']['#tab_contribute'] = CRM_Contact_BAO_Contact::getCountComponent('contribution', $this->_contactId); |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * called when action is view. |
| 56 | * |
| 57 | * @return null |
| 58 | */ |
| 59 | public function view() { |
| 60 | // build associated contributions |
| 61 | $this->associatedContribution(); |
| 62 | |
| 63 | $controller = new CRM_Core_Controller_Simple( |
| 64 | 'CRM_Event_Form_ParticipantView', |
| 65 | ts('View Participant'), |
| 66 | $this->_action |
| 67 | ); |
| 68 | $controller->setEmbedded(TRUE); |
| 69 | $controller->set('id', $this->_id); |
| 70 | $controller->set('cid', $this->_contactId); |
| 71 | |
| 72 | return $controller->run(); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * called when action is update or new. |
| 77 | * |
| 78 | * @return null |
| 79 | */ |
| 80 | public function edit() { |
| 81 | // set https for offline cc transaction |
| 82 | $mode = CRM_Utils_Request::retrieve('mode', 'Alphanumeric', $this); |
| 83 | if ($mode == 'test' || $mode == 'live') { |
| 84 | CRM_Utils_System::redirectToSSL(); |
| 85 | } |
| 86 | |
| 87 | if ($this->_action != CRM_Core_Action::ADD) { |
| 88 | // get associated contributions only on edit/delete |
| 89 | $this->associatedContribution(); |
| 90 | } |
| 91 | |
| 92 | $controller = new CRM_Core_Controller_Simple( |
| 93 | 'CRM_Event_Form_Participant', |
| 94 | 'Create Participation', |
| 95 | $this->_action |
| 96 | ); |
| 97 | |
| 98 | $controller->setEmbedded(TRUE); |
| 99 | $controller->set('id', $this->_id); |
| 100 | $controller->set('cid', $this->_contactId); |
| 101 | |
| 102 | return $controller->run(); |
| 103 | } |
| 104 | |
| 105 | public function delete() { |
| 106 | $controller = new CRM_Core_Controller_Simple( |
| 107 | 'CRM_Event_Form_Participant', |
| 108 | ts('Delete Participant'), |
| 109 | $this->_action |
| 110 | ); |
| 111 | |
| 112 | $controller->setEmbedded(TRUE); |
| 113 | $controller->set('id', $this->_id); |
| 114 | $controller->set('cid', $this->_contactId); |
| 115 | $controller->run(); |
| 116 | } |
| 117 | |
| 118 | public function preProcess() { |
| 119 | $context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this); |
| 120 | $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'browse'); |
| 121 | $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this); |
| 122 | |
| 123 | if ($context == 'standalone') { |
| 124 | $this->_action = CRM_Core_Action::ADD; |
| 125 | } |
| 126 | else { |
| 127 | $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE); |
| 128 | $this->assign('contactId', $this->_contactId); |
| 129 | |
| 130 | // check logged in url permission |
| 131 | CRM_Contact_Page_View::checkUserPermission($this); |
| 132 | } |
| 133 | |
| 134 | $this->assign('action', $this->_action); |
| 135 | |
| 136 | if ($this->_permission == CRM_Core_Permission::EDIT && !CRM_Core_Permission::check('edit event participants')) { |
| 137 | // demote to view since user does not have edit event participants rights |
| 138 | $this->_permission = CRM_Core_Permission::VIEW; |
| 139 | $this->assign('permission', 'view'); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * the main function that is called when the page loads, it decides the which action has to be taken for the page. |
| 145 | * |
| 146 | * @return null |
| 147 | */ |
| 148 | public function run() { |
| 149 | $this->preProcess(); |
| 150 | |
| 151 | // check if we can process credit card registration |
| 152 | $this->assign('newCredit', CRM_Core_Config::isEnabledBackOfficeCreditCardPayments()); |
| 153 | |
| 154 | // Only show credit card registration button if user has CiviContribute permission |
| 155 | if (CRM_Core_Permission::access('CiviContribute')) { |
| 156 | $this->assign('accessContribution', TRUE); |
| 157 | } |
| 158 | else { |
| 159 | $this->assign('accessContribution', FALSE); |
| 160 | } |
| 161 | |
| 162 | $this->setContext(); |
| 163 | |
| 164 | if ($this->_action & CRM_Core_Action::VIEW) { |
| 165 | $this->view(); |
| 166 | } |
| 167 | elseif ($this->_action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD)) { |
| 168 | $this->edit(); |
| 169 | } |
| 170 | elseif ($this->_action & (CRM_Core_Action::DELETE | CRM_Core_Action::DETACH)) { |
| 171 | $this->delete(); |
| 172 | } |
| 173 | else { |
| 174 | $this->browse(); |
| 175 | } |
| 176 | |
| 177 | return parent::run(); |
| 178 | } |
| 179 | |
| 180 | public function setContext() { |
| 181 | $context = CRM_Utils_Request::retrieve('context', |
| 182 | 'String', $this, FALSE, 'search' |
| 183 | ); |
| 184 | $compContext = CRM_Utils_Request::retrieve('compContext', |
| 185 | 'String', $this |
| 186 | ); |
| 187 | |
| 188 | $searchContext = CRM_Utils_Request::retrieve('searchContext', 'String', $this); |
| 189 | |
| 190 | $qfKey = CRM_Utils_Request::retrieve('key', 'String', $this); |
| 191 | |
| 192 | //validate the qfKey |
| 193 | if (!CRM_Utils_Rule::qfKey($qfKey)) { |
| 194 | $qfKey = NULL; |
| 195 | } |
| 196 | |
| 197 | switch ($context) { |
| 198 | case 'dashboard': |
| 199 | $url = CRM_Utils_System::url('civicrm/event', 'reset=1'); |
| 200 | break; |
| 201 | |
| 202 | case 'search': |
| 203 | $urlParams = 'force=1'; |
| 204 | if ($qfKey) { |
| 205 | $urlParams .= "&qfKey=$qfKey"; |
| 206 | } |
| 207 | $this->assign('searchKey', $qfKey); |
| 208 | |
| 209 | if ($compContext == 'advanced') { |
| 210 | $url = CRM_Utils_System::url('civicrm/contact/search/advanced', $urlParams); |
| 211 | } |
| 212 | elseif ($searchContext) { |
| 213 | $url = CRM_Utils_System::url("civicrm/$searchContext/search", $urlParams); |
| 214 | } |
| 215 | else { |
| 216 | $url = CRM_Utils_System::url('civicrm/event/search', $urlParams); |
| 217 | } |
| 218 | break; |
| 219 | |
| 220 | case 'user': |
| 221 | $url = CRM_Utils_System::url('civicrm/user', 'reset=1'); |
| 222 | break; |
| 223 | |
| 224 | case 'participant': |
| 225 | $url = CRM_Utils_System::url('civicrm/contact/view', |
| 226 | "reset=1&force=1&cid={$this->_contactId}&selectedChild=participant" |
| 227 | ); |
| 228 | break; |
| 229 | |
| 230 | case 'home': |
| 231 | $url = CRM_Utils_System::url('civicrm/dashboard', 'force=1'); |
| 232 | break; |
| 233 | |
| 234 | case 'activity': |
| 235 | $url = CRM_Utils_System::url('civicrm/contact/view', |
| 236 | "reset=1&force=1&cid={$this->_contactId}&selectedChild=activity" |
| 237 | ); |
| 238 | break; |
| 239 | |
| 240 | case 'standalone': |
| 241 | $url = CRM_Utils_System::url('civicrm/dashboard', 'reset=1'); |
| 242 | break; |
| 243 | |
| 244 | case 'fulltext': |
| 245 | $keyName = '&qfKey'; |
| 246 | $urlParams = 'force=1'; |
| 247 | $urlString = 'civicrm/contact/search/custom'; |
| 248 | if ($this->_action == CRM_Core_Action::UPDATE) { |
| 249 | if ($this->_contactId) { |
| 250 | $urlParams .= '&cid=' . $this->_contactId; |
| 251 | } |
| 252 | $keyName = '&key'; |
| 253 | $urlParams .= '&context=fulltext&action=view'; |
| 254 | $urlString = 'civicrm/contact/view/participant'; |
| 255 | } |
| 256 | if ($qfKey) { |
| 257 | $urlParams .= "$keyName=$qfKey"; |
| 258 | } |
| 259 | $this->assign('searchKey', $qfKey); |
| 260 | $url = CRM_Utils_System::url($urlString, $urlParams); |
| 261 | break; |
| 262 | |
| 263 | default: |
| 264 | $cid = NULL; |
| 265 | if ($this->_contactId) { |
| 266 | $cid = '&cid=' . $this->_contactId; |
| 267 | } |
| 268 | $url = CRM_Utils_System::url('civicrm/event/search', |
| 269 | 'force=1' . $cid |
| 270 | ); |
| 271 | break; |
| 272 | } |
| 273 | $session = CRM_Core_Session::singleton(); |
| 274 | $session->pushUserContext($url); |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * used for the to show the associated |
| 279 | * contribution for the participant |
| 280 | */ |
| 281 | public function associatedContribution() { |
| 282 | if (CRM_Core_Permission::access('CiviContribute')) { |
| 283 | $this->assign('accessContribution', TRUE); |
| 284 | $controller = new CRM_Core_Controller_Simple( |
| 285 | 'CRM_Contribute_Form_Search', |
| 286 | ts('Contributions'), |
| 287 | NULL, |
| 288 | FALSE, FALSE, TRUE |
| 289 | ); |
| 290 | $controller->setEmbedded(TRUE); |
| 291 | $controller->set('force', 1); |
| 292 | $controller->set('cid', $this->_contactId); |
| 293 | $controller->set('participantId', $this->_id); |
| 294 | $controller->set('context', 'contribution'); |
| 295 | $controller->process(); |
| 296 | $controller->run(); |
| 297 | } |
| 298 | else { |
| 299 | $this->assign('accessContribution', FALSE); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | } |