send most action links thru hook_civicrm_links
[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');
170 $controller->process();
171 $controller->run();
172
173 // add recurring block
174 $action = array_sum(array_keys($this->recurLinks()));
175 $params = CRM_Contribute_BAO_ContributionRecur::getRecurContributions($this->_contactId);
176
177 if (!empty($params)) {
178 foreach ($params as $ids => $recur) {
179 $action = array_sum(array_keys($this->recurLinks($ids)));
180 // no action allowed if it's not active
181 $params[$ids]['is_active'] = ($recur['contribution_status_id'] != 3);
182
183 if ($params[$ids]['is_active']) {
184 $details = CRM_Contribute_BAO_ContributionRecur::getSubscriptionDetails($params[$ids]['id'], 'recur');
185 $hideUpdate = $details->membership_id & $details->auto_renew;
186
187 if ($hideUpdate) {
188 $action -= CRM_Core_Action::UPDATE;
189 }
190
191 $params[$ids]['action'] = CRM_Core_Action::formLink(self::recurLinks($ids), $action,
192 array(
193 'cid' => $this->_contactId,
194 'crid' => $ids,
195 'cxt' => 'contribution',
87dab4a4
AH
196 ),
197 ts('more'),
198 FALSE,
199 'contribution.selector.recurring',
200 'Contribution',
201 $ids
6a488035
TO
202 );
203 }
204 }
205 // assign vars to templates
206 $this->assign('action', $this->_action);
207 $this->assign('recurRows', $params);
208 $this->assign('recur', TRUE);
209 }
210
211 //add honor block
212 // form all action links
213 $action = array_sum(array_keys($this->honorLinks()));
214
215 $params = array();
216 $params = CRM_Contribute_BAO_Contribution::getHonorContacts($this->_contactId);
217 if (!empty($params)) {
218 foreach ($params as $ids => $honorId) {
219 $params[$ids]['action'] = CRM_Core_Action::formLink(
220 self::honorLinks(),
221 $action,
222 array(
223 'cid' => $honorId['honorId'],
224 'id' => $ids,
225 'cxt' => 'contribution',
226 'contributionType' => $honorId['type_id'],
227 'honorId' => $this->_contactId,
87dab4a4
AH
228 ),
229 ts('more'),
230 FALSE,
231 'contribution.selector.honoree',
232 'Contribution',
233 $ids
6a488035
TO
234 );
235 }
236
237 // assign vars to templates
238 $this->assign('action', $this->_action);
239 $this->assign('honorRows', $params);
240 $this->assign('honor', TRUE);
241 }
242
243 //enable/disable soft credit records for test contribution
244 $isTest = 0;
245 if (CRM_Utils_Request::retrieve('isTest', 'Positive', $this)) {
246 $isTest = 1;
247 }
248 $this->assign('isTest', $isTest);
249
6dcc4d9d 250 $softCreditList = CRM_Contribute_BAO_ContributionSoft::getSoftContributionList($this->_contactId, $isTest);
6a488035
TO
251
252 if (!empty($softCreditList)) {
253 $softCreditTotals = array();
254
255 list($softCreditTotals['amount'],
256 $softCreditTotals['avg'],
257 $softCreditTotals['currency']
6dcc4d9d 258 ) = CRM_Contribute_BAO_ContributionSoft::getSoftContributionTotals($this->_contactId, $isTest);
6a488035
TO
259
260 $this->assign('softCredit', TRUE);
261 $this->assign('softCreditRows', $softCreditList);
262 $this->assign('softCreditTotals', $softCreditTotals);
263 }
264
265 if ($this->_contactId) {
266 $displayName = CRM_Contact_BAO_Contact::displayName($this->_contactId);
267 $this->assign('displayName', $displayName);
268 }
269 }
270
271 /**
272 * This function is called when action is view
273 *
274 * return null
275 * @access public
276 */
277 function view() {
278 $controller = new CRM_Core_Controller_Simple(
279 'CRM_Contribute_Form_ContributionView',
280 ts('View Contribution'),
281 $this->_action
282 );
283 $controller->setEmbedded(TRUE);
284 $controller->set('id', $this->_id);
285 $controller->set('cid', $this->_contactId);
286
287 return $controller->run();
288 }
289
290 /**
291 * This function is called when action is update or new
292 *
293 * return null
294 * @access public
295 */
296 function edit() {
297 // set https for offline cc transaction
298 $mode = CRM_Utils_Request::retrieve('mode', 'String', $this);
299 if ($mode == 'test' || $mode == 'live') {
300 CRM_Utils_System::redirectToSSL();
301 }
302
303 $controller = new CRM_Core_Controller_Simple(
304 'CRM_Contribute_Form_Contribution',
305 'Create Contribution',
306 $this->_action
307 );
308 $controller->setEmbedded(TRUE);
309 $controller->set('id', $this->_id);
310 $controller->set('cid', $this->_contactId);
311
312 return $controller->run();
313 }
314
315 function preProcess() {
316 $context = CRM_Utils_Request::retrieve('context', 'String', $this);
317 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'browse');
318 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
319
320 if ($context == 'standalone') {
321 $this->_action = CRM_Core_Action::ADD;
322 }
323 else {
324 $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE);
325 $this->assign('contactId', $this->_contactId);
326
327 // check logged in url permission
328 CRM_Contact_Page_View::checkUserPermission($this);
329
330 // set page title
331 CRM_Contact_Page_View::setTitle($this->_contactId);
332 }
333 $this->assign('action', $this->_action);
334
335 if ($this->_permission == CRM_Core_Permission::EDIT && !CRM_Core_Permission::check('edit contributions')) {
336 // demote to view since user does not have edit contrib rights
337 $this->_permission = CRM_Core_Permission::VIEW;
338 $this->assign('permission', 'view');
339 }
340 }
341
342 /**
343 * This function is the main function that is called when the page
344 * loads, it decides the which action has to be taken for the page.
345 *
346 * return null
347 * @access public
348 */
349 function run() {
350 $this->preProcess();
351
352 // check if we can process credit card contribs
353 CRM_Core_Payment::allowBackofficeCreditCard($this);
354
355 $this->setContext();
356
357 if ($this->_action & CRM_Core_Action::VIEW) {
358 $this->view();
359 }
360 elseif ($this->_action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD | CRM_Core_Action::DELETE)) {
361 $this->edit();
362 }
363 else {
364 $this->browse();
365 }
366
367 return parent::run();
368 }
369
370 function setContext() {
371 $qfKey = CRM_Utils_Request::retrieve('key', 'String', $this);
372 $context = CRM_Utils_Request::retrieve('context', 'String',
373 $this, FALSE, 'search'
374 );
375 $compContext = CRM_Utils_Request::retrieve('compContext', 'String', $this);
376
377 //swap the context.
378 if ($context == 'search' && $compContext) {
379 $context = $compContext;
380 }
381 else {
382 $compContext = NULL;
383 }
384
385 // make sure we dont get tricked with a bad key
386 // so check format
387 if (!CRM_Core_Key::valid($qfKey)) {
388 $qfKey = NULL;
389 }
390
391 $session = CRM_Core_Session::singleton();
392
393 switch ($context) {
394 case 'user':
395 $url = CRM_Utils_System::url('civicrm/user', 'reset=1');
396 break;
397
398 case 'dashboard':
399 $url = CRM_Utils_System::url('civicrm/contribute',
400 'reset=1'
401 );
402 break;
403
404 case 'pledgeDashboard':
405 $url = CRM_Utils_System::url('civicrm/pledge',
406 'reset=1'
407 );
408 break;
409
410 case 'contribution':
411 $honorId = CRM_Utils_Request::retrieve('honorId', 'Positive', $this, FALSE);
412
413 if ($honorId) {
414 $cid = $honorId;
415 }
416 else {
417 $cid = $this->_contactId;
418 }
419
420 $url = CRM_Utils_System::url('civicrm/contact/view',
421 "reset=1&force=1&cid={$cid}&selectedChild=contribute"
422 );
423 break;
424
425 case 'search':
426 case 'advanced':
427 $extraParams = "force=1";
428 if ($qfKey) {
429 $extraParams .= "&qfKey=$qfKey";
430 }
431
432 $this->assign('searchKey', $qfKey);
433 if ($context == 'advanced') {
434 $url = CRM_Utils_System::url('civicrm/contact/search/advanced', $extraParams);
435 }
436 else {
437 $url = CRM_Utils_System::url('civicrm/contribute/search', $extraParams);
438 }
439 break;
440
441 case 'home':
442 $url = CRM_Utils_System::url('civicrm/dashboard', 'reset=1');
443 break;
444
445 case 'activity':
446 $url = CRM_Utils_System::url('civicrm/contact/view',
447 "reset=1&force=1&cid={$this->_contactId}&selectedChild=activity"
448 );
449 break;
450
451 case 'member':
452 case 'membership':
453 $componentId = CRM_Utils_Request::retrieve('compId', 'Positive', $this);
454 $componentAction = CRM_Utils_Request::retrieve('compAction', 'Integer', $this);
455
456 $context = 'membership';
457 $searchKey = NULL;
458 if ($compContext) {
459 $context = 'search';
460 if ($qfKey) {
461 $searchKey = "&key=$qfKey";
462 }
463 $compContext = "&compContext={$compContext}";
464 }
465 if ($componentAction & CRM_Core_Action::VIEW) {
466 $action = 'view';
467 }
468 else {
469 $action = 'update';
470 }
471 $url = CRM_Utils_System::url('civicrm/contact/view/membership',
472 "reset=1&action={$action}&id={$componentId}&cid={$this->_contactId}&context={$context}&selectedChild=member{$searchKey}{$compContext}"
473 );
474 break;
475
476 case 'participant':
477 $componentId = CRM_Utils_Request::retrieve('compId', 'Positive', $this);
478 $componentAction = CRM_Utils_Request::retrieve('compAction', 'Integer', $this);
479
480 $context = 'participant';
481 $searchKey = NULL;
482 if ($compContext) {
483 $context = 'search';
484 if ($qfKey) {
485 $searchKey = "&key=$qfKey";
486 }
487 $compContext = "&compContext={$compContext}";
488 }
489 if ($componentAction == CRM_Core_Action::VIEW) {
490 $action = 'view';
491 }
492 else {
493 $action = 'update';
494 }
495 $url = CRM_Utils_System::url('civicrm/contact/view/participant',
496 "reset=1&action={$action}&id={$componentId}&cid={$this->_contactId}&context={$context}&selectedChild=event{$searchKey}{$compContext}"
497 );
498 break;
499
500 case 'pledge':
501 $url = CRM_Utils_System::url('civicrm/contact/view',
502 "reset=1&force=1&cid={$this->_contactId}&selectedChild=pledge"
503 );
504 break;
505
506 case 'standalone':
507 $url = CRM_Utils_System::url('civicrm/dashboard', 'reset=1');
508 break;
509
510 case 'fulltext':
511 $keyName = '&qfKey';
512 $urlParams = 'force=1';
513 $urlString = 'civicrm/contact/search/custom';
514 if ($this->_action == CRM_Core_Action::UPDATE) {
515 if ($this->_contactId) {
516 $urlParams .= '&cid=' . $this->_contactId;
517 }
518 $keyName = '&key';
519 $urlParams .= '&context=fulltext&action=view';
520 $urlString = 'civicrm/contact/view/contribution';
521 }
522 if ($qfKey) {
523 $urlParams .= "$keyName=$qfKey";
524 }
525 $this->assign('searchKey', $qfKey);
526 $url = CRM_Utils_System::url($urlString, $urlParams);
527 break;
528
529 default:
530 $cid = NULL;
531 if ($this->_contactId) {
532 $cid = '&cid=' . $this->_contactId;
533 }
534 $url = CRM_Utils_System::url('civicrm/contribute/search',
535 'reset=1&force=1' . $cid
536 );
537 break;
538 }
539
540 $session = CRM_Core_Session::singleton();
541 $session->pushUserContext($url);
542 }
543}
544