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