Style - Remove @access
[civicrm-core.git] / CRM / Contribute / Page / Tab.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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_Contribute_Page_Tab extends CRM_Core_Page {
36
37 /**
38 * The action links that we need to display for the browse screen
39 *
40 * @var array
41 * @static
42 */
43 static $_links = NULL;
44 static $_recurLinks = NULL;
45 public $_permission = NULL;
46 public $_contactId = NULL;
47 public $_crid = NULL;
48
49 /**
50 * This method returns the links that are given for recur search row.
51 * currently the links added for each row are:
52 * - View
53 * - Edit
54 * - Cancel
55 *
56 * @param bool $recurID
57 * @param string $context
58 *
59 * @return array
60 */
61 public static function &recurLinks($recurID = FALSE, $context = 'contribution') {
62 if (!(self::$_links)) {
63 self::$_links = array(
64 CRM_Core_Action::VIEW => array(
65 'name' => ts('View'),
66 'title' => ts('View Recurring Payment'),
67 'url' => 'civicrm/contact/view/contributionrecur',
68 'qs' => "reset=1&id=%%crid%%&cid=%%cid%%&context={$context}",
69 ),
70 CRM_Core_Action::UPDATE => array(
71 'name' => ts('Edit'),
72 'title' => ts('Edit Recurring Payment'),
73 'url' => 'civicrm/contribute/updaterecur',
74 'qs' => "reset=1&action=update&crid=%%crid%%&cid=%%cid%%&context={$context}",
75 ),
76 CRM_Core_Action::DISABLE => array(
77 'name' => ts('Cancel'),
78 'title' => ts('Cancel'),
79 'ref' => 'crm-enable-disable',
80 ),
81 );
82 }
83
84 if ($recurID) {
85 $paymentProcessorObj = CRM_Financial_BAO_PaymentProcessor::getProcessorForEntity($recurID, 'recur', 'obj');
86 if (is_object( $paymentProcessorObj) && $paymentProcessorObj->isSupported('cancelSubscription')) {
87 unset(self::$_links[CRM_Core_Action::DISABLE]['extra'], self::$_links[CRM_Core_Action::DISABLE]['ref']);
88 self::$_links[CRM_Core_Action::DISABLE]['url'] = "civicrm/contribute/unsubscribe";
89 self::$_links[CRM_Core_Action::DISABLE]['qs'] = "reset=1&crid=%%crid%%&cid=%%cid%%&context={$context}";
90 }
91 if (is_object( $paymentProcessorObj) && $paymentProcessorObj->isSupported('updateSubscriptionBillingInfo')) {
92 self::$_links[CRM_Core_Action::RENEW] = array('name' => ts('Change Billing Details'),
93 'title' => ts('Change Billing Details'),
94 'url' => 'civicrm/contribute/updatebilling',
95 'qs' => "reset=1&crid=%%crid%%&cid=%%cid%%&context={$context}",
96 );
97 }
98 }
99
100 return self::$_links;
101 }
102 // end function
103
104 /**
105 * This function is called when action is browse
106 *
107 * return null
108 */
109 public function browse() {
110 // add annual contribution
111 $annual = array();
112 list($annual['count'],
113 $annual['amount'],
114 $annual['avg']
115 ) = CRM_Contribute_BAO_Contribution::annual($this->_contactId);
116 $this->assign('annual', $annual);
117
118 $controller = new CRM_Core_Controller_Simple(
119 'CRM_Contribute_Form_Search',
120 ts('Contributions'),
121 $this->_action,
122 FALSE, FALSE, TRUE
123 );
124 $controller->setEmbedded(TRUE);
125 $controller->reset();
126 $controller->set('cid', $this->_contactId);
127 $controller->set('crid', $this->_crid);
128 $controller->set('context', 'contribution');
129 $controller->set('limit', 50);
130 $controller->process();
131 $controller->run();
132
133 // add recurring block
134 $action = array_sum(array_keys($this->recurLinks()));
135 $params = CRM_Contribute_BAO_ContributionRecur::getRecurContributions($this->_contactId);
136
137 if (!empty($params)) {
138 foreach ($params as $ids => $recur) {
139 $action = array_sum(array_keys($this->recurLinks($ids)));
140 // no action allowed if it's not active
141 $params[$ids]['is_active'] = ($recur['contribution_status_id'] != 3);
142
143 if ($params[$ids]['is_active']) {
144 $details = CRM_Contribute_BAO_ContributionRecur::getSubscriptionDetails($params[$ids]['id'], 'recur');
145 $hideUpdate = $details->membership_id & $details->auto_renew;
146
147 if ($hideUpdate) {
148 $action -= CRM_Core_Action::UPDATE;
149 }
150
151 $params[$ids]['action'] = CRM_Core_Action::formLink(self::recurLinks($ids), $action,
152 array(
153 'cid' => $this->_contactId,
154 'crid' => $ids,
155 'cxt' => 'contribution',
156 ),
157 ts('more'),
158 FALSE,
159 'contribution.selector.recurring',
160 'Contribution',
161 $ids
162 );
163 }
164 }
165 // assign vars to templates
166 $this->assign('action', $this->_action);
167 $this->assign('recurRows', $params);
168 $this->assign('recur', TRUE);
169 }
170
171 //enable/disable soft credit records for test contribution
172 $isTest = 0;
173 if (CRM_Utils_Request::retrieve('isTest', 'Positive', $this)) {
174 $isTest = 1;
175 }
176 $this->assign('isTest', $isTest);
177
178 $softCreditList = CRM_Contribute_BAO_ContributionSoft::getSoftContributionList($this->_contactId, NULL, $isTest);
179
180 if (!empty($softCreditList)) {
181 $softCreditTotals = array();
182
183 list($softCreditTotals['amount'],
184 $softCreditTotals['avg'],
185 $softCreditTotals['currency']
186 ) = CRM_Contribute_BAO_ContributionSoft::getSoftContributionTotals($this->_contactId, $isTest);
187
188 $this->assign('softCredit', TRUE);
189 $this->assign('softCreditRows', $softCreditList);
190 $this->assign('softCreditTotals', $softCreditTotals);
191 }
192
193 if ($this->_contactId) {
194 $displayName = CRM_Contact_BAO_Contact::displayName($this->_contactId);
195 $this->assign('displayName', $displayName);
196 $this->ajaxResponse['tabCount'] = CRM_Contact_BAO_Contact::getCountComponent('contribution', $this->_contactId);
197 }
198 }
199
200 /**
201 * This function is called when action is view
202 *
203 * return null
204 */
205 public function view() {
206 $controller = new CRM_Core_Controller_Simple(
207 'CRM_Contribute_Form_ContributionView',
208 ts('View Contribution'),
209 $this->_action
210 );
211 $controller->setEmbedded(TRUE);
212 $controller->set('id', $this->_id);
213 $controller->set('cid', $this->_contactId);
214
215 return $controller->run();
216 }
217
218 /**
219 * This function is called when action is update or new
220 *
221 * return null
222 */
223 public function edit() {
224 // set https for offline cc transaction
225 $mode = CRM_Utils_Request::retrieve('mode', 'String', $this);
226 if ($mode == 'test' || $mode == 'live') {
227 CRM_Utils_System::redirectToSSL();
228 }
229
230 $controller = new CRM_Core_Controller_Simple(
231 'CRM_Contribute_Form_Contribution',
232 'Create Contribution',
233 $this->_action
234 );
235 $controller->setEmbedded(TRUE);
236 $controller->set('id', $this->_id);
237 $controller->set('cid', $this->_contactId);
238
239 return $controller->run();
240 }
241
242 public function preProcess() {
243 $context = CRM_Utils_Request::retrieve('context', 'String', $this);
244 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'browse');
245 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
246
247 if ($context == 'standalone') {
248 $this->_action = CRM_Core_Action::ADD;
249 }
250 else {
251 $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, empty($this->_id));
252 if (empty($this->_contactId)) {
253 $this->_contactId = civicrm_api3('contribution', 'getvalue', array('id' => $this->_id, 'return' => 'contact_id'));
254 }
255 $this->assign('contactId', $this->_contactId);
256
257 // check logged in url permission
258 CRM_Contact_Page_View::checkUserPermission($this);
259 }
260 $this->assign('action', $this->_action);
261
262 if ($this->_permission == CRM_Core_Permission::EDIT && !CRM_Core_Permission::check('edit contributions')) {
263 // demote to view since user does not have edit contrib rights
264 $this->_permission = CRM_Core_Permission::VIEW;
265 $this->assign('permission', 'view');
266 }
267 }
268
269 /**
270 * This function is the main function that is called when the page
271 * loads, it decides the which action has to be taken for the page.
272 *
273 * return null
274 */
275 public function run() {
276 $this->preProcess();
277
278 // check if we can process credit card contribs
279 $this->assign('newCredit', CRM_Core_Config::isEnabledBackOfficeCreditCardPayments());
280
281 $this->setContext();
282
283 if ($this->_action & CRM_Core_Action::VIEW) {
284 $this->view();
285 }
286 elseif ($this->_action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD | CRM_Core_Action::DELETE)) {
287 $this->edit();
288 }
289 else {
290 $this->browse();
291 }
292
293 return parent::run();
294 }
295
296 public function setContext() {
297 $qfKey = CRM_Utils_Request::retrieve('key', 'String', $this);
298 $context = CRM_Utils_Request::retrieve('context', 'String',
299 $this, FALSE, 'search'
300 );
301 $compContext = CRM_Utils_Request::retrieve('compContext', 'String', $this);
302
303 $searchContext = CRM_Utils_Request::retrieve('searchContext', 'String', $this);
304
305 //swap the context.
306 if ($context == 'search' && $compContext) {
307 $context = $compContext;
308 }
309 else {
310 $compContext = NULL;
311 }
312
313 // make sure we dont get tricked with a bad key
314 // so check format
315 if (!CRM_Core_Key::valid($qfKey)) {
316 $qfKey = NULL;
317 }
318
319 $session = CRM_Core_Session::singleton();
320
321 switch ($context) {
322 case 'user':
323 $url = CRM_Utils_System::url('civicrm/user', 'reset=1');
324 break;
325
326 case 'dashboard':
327 $url = CRM_Utils_System::url('civicrm/contribute',
328 'reset=1'
329 );
330 break;
331
332 case 'pledgeDashboard':
333 $url = CRM_Utils_System::url('civicrm/pledge',
334 'reset=1'
335 );
336 break;
337
338 case 'contribution':
339 $url = CRM_Utils_System::url('civicrm/contact/view',
340 "reset=1&force=1&cid={$this->_contactId}&selectedChild=contribute"
341 );
342 break;
343
344 case 'search':
345 case 'advanced':
346 $extraParams = "force=1";
347 if ($qfKey) {
348 $extraParams .= "&qfKey=$qfKey";
349 }
350
351 $this->assign('searchKey', $qfKey);
352 if ($context == 'advanced') {
353 $url = CRM_Utils_System::url('civicrm/contact/search/advanced', $extraParams);
354 }
355 else if ($searchContext) {
356 $url = CRM_Utils_System::url("civicrm/$searchContext/search", $extraParams);
357 }
358 else {
359 $url = CRM_Utils_System::url('civicrm/contribute/search', $extraParams);
360 }
361 break;
362
363 case 'home':
364 $url = CRM_Utils_System::url('civicrm/dashboard', 'reset=1');
365 break;
366
367 case 'activity':
368 $url = CRM_Utils_System::url('civicrm/contact/view',
369 "reset=1&force=1&cid={$this->_contactId}&selectedChild=activity"
370 );
371 break;
372
373 case 'member':
374 case 'membership':
375 $componentId = CRM_Utils_Request::retrieve('compId', 'Positive', $this);
376 $componentAction = CRM_Utils_Request::retrieve('compAction', 'Integer', $this);
377
378 $context = 'membership';
379 $searchKey = NULL;
380 if ($compContext) {
381 $context = 'search';
382 if ($qfKey) {
383 $searchKey = "&key=$qfKey";
384 }
385 $compContext = "&compContext={$compContext}";
386 }
387 if ($componentAction & CRM_Core_Action::VIEW) {
388 $action = 'view';
389 }
390 else {
391 $action = 'update';
392 }
393 $url = CRM_Utils_System::url('civicrm/contact/view/membership',
394 "reset=1&action={$action}&id={$componentId}&cid={$this->_contactId}&context={$context}&selectedChild=member{$searchKey}{$compContext}"
395 );
396 break;
397
398 case 'participant':
399 $componentId = CRM_Utils_Request::retrieve('compId', 'Positive', $this);
400 $componentAction = CRM_Utils_Request::retrieve('compAction', 'Integer', $this);
401
402 $context = 'participant';
403 $searchKey = NULL;
404 if ($compContext) {
405 $context = 'search';
406 if ($qfKey) {
407 $searchKey = "&key=$qfKey";
408 }
409 $compContext = "&compContext={$compContext}";
410 }
411 if ($componentAction == CRM_Core_Action::VIEW) {
412 $action = 'view';
413 }
414 else {
415 $action = 'update';
416 }
417 $url = CRM_Utils_System::url('civicrm/contact/view/participant',
418 "reset=1&action={$action}&id={$componentId}&cid={$this->_contactId}&context={$context}&selectedChild=event{$searchKey}{$compContext}"
419 );
420 break;
421
422 case 'pledge':
423 $url = CRM_Utils_System::url('civicrm/contact/view',
424 "reset=1&force=1&cid={$this->_contactId}&selectedChild=pledge"
425 );
426 break;
427
428 case 'standalone':
429 $url = CRM_Utils_System::url('civicrm/dashboard', 'reset=1');
430 break;
431
432 case 'fulltext':
433 $keyName = '&qfKey';
434 $urlParams = 'force=1';
435 $urlString = 'civicrm/contact/search/custom';
436 if ($this->_action == CRM_Core_Action::UPDATE) {
437 if ($this->_contactId) {
438 $urlParams .= '&cid=' . $this->_contactId;
439 }
440 $keyName = '&key';
441 $urlParams .= '&context=fulltext&action=view';
442 $urlString = 'civicrm/contact/view/contribution';
443 }
444 if ($qfKey) {
445 $urlParams .= "$keyName=$qfKey";
446 }
447 $this->assign('searchKey', $qfKey);
448 $url = CRM_Utils_System::url($urlString, $urlParams);
449 break;
450
451 default:
452 $cid = NULL;
453 if ($this->_contactId) {
454 $cid = '&cid=' . $this->_contactId;
455 }
456 $url = CRM_Utils_System::url('civicrm/contribute/search',
457 'reset=1&force=1' . $cid
458 );
459 break;
460 }
461
462 $session = CRM_Core_Session::singleton();
463 $session->pushUserContext($url);
464 }
465 }
466