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