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