Merge pull request #14184 from mattwire/relationshipcolumns
[civicrm-core.git] / CRM / Contribute / Page / Tab.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
6b83d5bd 31 * @copyright CiviCRM LLC (c) 2004-2019
6a488035
TO
32 */
33class CRM_Contribute_Page_Tab extends CRM_Core_Page {
34
35 /**
04d15518 36 * The permission we have on this contact
6a488035 37 *
04d15518 38 * @var string
6a488035 39 */
6a488035 40 public $_permission = NULL;
04d15518
MWMC
41
42 /**
43 * The contact ID for the contributions we are acting on
44 * @var int
45 */
6a488035 46 public $_contactId = NULL;
04d15518
MWMC
47
48 /**
49 * The recurring contribution ID (if any)
50 * @var int
51 */
6a488035
TO
52 public $_crid = NULL;
53
6a488035
TO
54 /**
55 * This method returns the links that are given for recur search row.
56 * currently the links added for each row are:
57 * - View
58 * - Edit
59 * - Cancel
60 *
6c8f6e67
EM
61 * @param bool $recurID
62 * @param string $context
63 *
6a488035 64 * @return array
6a488035 65 */
04d15518
MWMC
66 public static function recurLinks($recurID = FALSE, $context = 'contribution') {
67 $links = [
68 CRM_Core_Action::VIEW => [
69 'name' => ts('View'),
70 'title' => ts('View Recurring Payment'),
71 'url' => 'civicrm/contact/view/contributionrecur',
72 'qs' => "reset=1&id=%%crid%%&cid=%%cid%%&context={$context}",
73 ],
74 CRM_Core_Action::UPDATE => [
75 'name' => ts('Edit'),
76 'title' => ts('Edit Recurring Payment'),
77 'url' => 'civicrm/contribute/updaterecur',
78 'qs' => "reset=1&action=update&crid=%%crid%%&cid=%%cid%%&context={$context}",
79 ],
80 CRM_Core_Action::DISABLE => [
81 'name' => ts('Cancel'),
82 'title' => ts('Cancel'),
83 'ref' => 'crm-enable-disable',
84 ],
85 ];
6a488035
TO
86
87 if ($recurID) {
87011153 88 $paymentProcessorObj = CRM_Contribute_BAO_ContributionRecur::getPaymentProcessorObject($recurID);
04d15518
MWMC
89 if ($paymentProcessorObj) {
90 if ($paymentProcessorObj->supports('cancelRecurring')) {
91 unset($links[CRM_Core_Action::DISABLE]['extra'], $links[CRM_Core_Action::DISABLE]['ref']);
92 $links[CRM_Core_Action::DISABLE]['url'] = "civicrm/contribute/unsubscribe";
93 $links[CRM_Core_Action::DISABLE]['qs'] = "reset=1&crid=%%crid%%&cid=%%cid%%&context={$context}";
94 }
95
96 if ($paymentProcessorObj->supports('UpdateSubscriptionBillingInfo')) {
97 $links[CRM_Core_Action::RENEW] = [
98 'name' => ts('Change Billing Details'),
99 'title' => ts('Change Billing Details'),
100 'url' => 'civicrm/contribute/updatebilling',
101 'qs' => "reset=1&crid=%%crid%%&cid=%%cid%%&context={$context}",
102 ];
103 }
104
105 if (!$paymentProcessorObj->supports('ChangeSubscriptionAmount') && !$paymentProcessorObj->supports('EditRecurringContribution')) {
106 unset($links[CRM_Core_Action::UPDATE]);
107 }
6a488035 108 }
04d15518
MWMC
109 else {
110 unset($links[CRM_Core_Action::DISABLE]);
c6d558dc
MWMC
111 unset($links[CRM_Core_Action::UPDATE]);
112 }
6a488035
TO
113 }
114
04d15518 115 return $links;
6a488035 116 }
6a488035
TO
117
118 /**
fe482240 119 * called when action is browse.
6a488035 120 *
6a488035 121 */
00be9182 122 public function browse() {
6a488035 123 // add annual contribution
be2fb01f 124 $annual = [];
6a488035
TO
125 list($annual['count'],
126 $annual['amount'],
127 $annual['avg']
353ffa53 128 ) = CRM_Contribute_BAO_Contribution::annual($this->_contactId);
6a488035
TO
129 $this->assign('annual', $annual);
130
131 $controller = new CRM_Core_Controller_Simple(
132 'CRM_Contribute_Form_Search',
133 ts('Contributions'),
134 $this->_action,
135 FALSE, FALSE, TRUE
136 );
137 $controller->setEmbedded(TRUE);
138 $controller->reset();
139 $controller->set('cid', $this->_contactId);
140 $controller->set('crid', $this->_crid);
7a49693c 141 $controller->set('context', 'contribution');
c2874d9c 142 $controller->set('limit', 50);
6a488035
TO
143 $controller->process();
144 $controller->run();
145
146 // add recurring block
46097c9c
MW
147 $this->addRecurringContributionsBlock();
148
149 // enable/disable soft credit records for test contribution
150 $isTest = 0;
151 if (CRM_Utils_Request::retrieve('isTest', 'Positive', $this)) {
152 $isTest = 1;
153 }
154 $this->assign('isTest', $isTest);
155
156 $softCreditList = CRM_Contribute_BAO_ContributionSoft::getSoftContributionList($this->_contactId, NULL, $isTest);
157
158 if (!empty($softCreditList)) {
be2fb01f 159 $softCreditTotals = [];
46097c9c 160
d9c8c3c8
PF
161 list($softCreditTotals['count'],
162 $softCreditTotals['cancel']['count'],
163 $softCreditTotals['amount'],
46097c9c 164 $softCreditTotals['avg'],
1330f57a
SL
165 // to get cancel amount
166 $softCreditTotals['cancel']['amount']
46097c9c
MW
167 ) = CRM_Contribute_BAO_ContributionSoft::getSoftContributionTotals($this->_contactId, $isTest);
168
169 $this->assign('softCredit', TRUE);
170 $this->assign('softCreditRows', $softCreditList);
171 $this->assign('softCreditTotals', $softCreditTotals);
172 }
173
174 if ($this->_contactId) {
175 $displayName = CRM_Contact_BAO_Contact::displayName($this->_contactId);
176 $this->assign('displayName', $displayName);
7255972e
MWMC
177 $tabCount = CRM_Contact_BAO_Contact::getCountComponent('contribution', $this->_contactId);
178 $this->assign('tabCount', $tabCount);
179 $this->ajaxResponse['tabCount'] = $tabCount;
46097c9c
MW
180 }
181 }
182
183 /**
184 * Get all the recurring contribution information and assign to the template
185 */
186 private function addRecurringContributionsBlock() {
e2ef9c65
PN
187 list($activeContributions, $activeContributionsCount) = $this->getActiveRecurringContributions();
188 list($inactiveRecurringContributions, $inactiveContributionsCount) = $this->getInactiveRecurringContributions();
5963b706
CR
189
190 if (!empty($activeContributions) || !empty($inactiveRecurringContributions)) {
191 // assign vars to templates
192 $this->assign('action', $this->_action);
193 $this->assign('activeRecurRows', $activeContributions);
51e31e45 194 $this->assign('contributionRecurCount', $activeContributionsCount + $inactiveContributionsCount);
5963b706
CR
195 $this->assign('inactiveRecurRows', $inactiveRecurringContributions);
196 $this->assign('recur', TRUE);
197 }
198 }
199
200 /**
201 * Loads active recurring contributions for the current contact and formats
202 * them to be used on the form.
203 *
04d15518 204 * @return array
5963b706
CR
205 */
206 private function getActiveRecurringContributions() {
9e475c2d 207 try {
be2fb01f 208 $contributionRecurResult = civicrm_api3('ContributionRecur', 'get', [
9e475c2d 209 'contact_id' => $this->_contactId,
be2fb01f 210 'contribution_status_id' => ['NOT IN' => CRM_Contribute_BAO_ContributionRecur::getInactiveStatuses()],
0b3146b3 211 'options' => ['limit' => 0, 'sort' => 'is_test, start_date DESC'],
be2fb01f 212 ]);
5963b706
CR
213 $recurContributions = CRM_Utils_Array::value('values', $contributionRecurResult);
214 }
215 catch (Exception $e) {
be2fb01f 216 $recurContributions = [];
5963b706
CR
217 }
218
219 return $this->buildRecurringContributionsArray($recurContributions);
220 }
221
222 /**
223 * Loads inactive recurring contributions for the current contact and formats
224 * them to be used on the form.
225 *
04d15518 226 * @return array
5963b706
CR
227 */
228 private function getInactiveRecurringContributions() {
9e475c2d 229 try {
be2fb01f 230 $contributionRecurResult = civicrm_api3('ContributionRecur', 'get', [
9e475c2d 231 'contact_id' => $this->_contactId,
be2fb01f 232 'contribution_status_id' => ['IN' => CRM_Contribute_BAO_ContributionRecur::getInactiveStatuses()],
0b3146b3 233 'options' => ['limit' => 0, 'sort' => 'is_test, start_date DESC'],
be2fb01f 234 ]);
9e475c2d
MW
235 $recurContributions = CRM_Utils_Array::value('values', $contributionRecurResult);
236 }
237 catch (Exception $e) {
238 $recurContributions = NULL;
239 }
6a488035 240
5963b706
CR
241 return $this->buildRecurringContributionsArray($recurContributions);
242 }
6a488035 243
5963b706
CR
244 /**
245 * @param $recurContributions
246 *
04d15518 247 * @return array
5963b706
CR
248 */
249 private function buildRecurringContributionsArray($recurContributions) {
e2ef9c65 250 $liveRecurringContributionCount = 0;
5963b706 251 foreach ($recurContributions as $recurId => $recurDetail) {
44c118da
MWMC
252 // Is recurring contribution active?
253 $recurContributions[$recurId]['is_active'] = !in_array(CRM_Contribute_PseudoConstant::contributionStatus($recurDetail['contribution_status_id'], 'name'), CRM_Contribute_BAO_ContributionRecur::getInactiveStatuses());
254 if ($recurContributions[$recurId]['is_active']) {
255 $actionMask = array_sum(array_keys(self::recurLinks($recurId)));
256 }
257 else {
258 $actionMask = CRM_Core_Action::mask([CRM_Core_Permission::VIEW]);
259 }
46097c9c 260
e2ef9c65
PN
261 if (empty($recurDetail['is_test'])) {
262 $liveRecurringContributionCount++;
263 }
264
5963b706
CR
265 // Get the name of the payment processor
266 if (!empty($recurDetail['payment_processor_id'])) {
267 $recurContributions[$recurId]['payment_processor'] = CRM_Financial_BAO_PaymentProcessor::getPaymentProcessorName($recurDetail['payment_processor_id']);
268 }
269 // Get the label for the contribution status
270 if (!empty($recurDetail['contribution_status_id'])) {
271 $recurContributions[$recurId]['contribution_status'] = CRM_Core_PseudoConstant::getLabel('CRM_Contribute_BAO_ContributionRecur', 'contribution_status_id', $recurDetail['contribution_status_id']);
272 }
6a488035 273
44c118da 274 $recurContributions[$recurId]['action'] = CRM_Core_Action::formLink(self::recurLinks($recurId), $actionMask,
be2fb01f 275 [
44c118da
MWMC
276 'cid' => $this->_contactId,
277 'crid' => $recurId,
278 'cxt' => 'contribution',
be2fb01f 279 ],
44c118da
MWMC
280 ts('more'),
281 FALSE,
282 'contribution.selector.recurring',
283 'Contribution',
284 $recurId
285 );
6a488035 286 }
5963b706 287
e2ef9c65 288 return [$recurContributions, $liveRecurringContributionCount];
6a488035
TO
289 }
290
291 /**
fe482240 292 * called when action is view.
6a488035 293 *
9e475c2d 294 * @return mixed
6a488035 295 */
00be9182 296 public function view() {
6a488035
TO
297 $controller = new CRM_Core_Controller_Simple(
298 'CRM_Contribute_Form_ContributionView',
299 ts('View Contribution'),
300 $this->_action
301 );
302 $controller->setEmbedded(TRUE);
303 $controller->set('id', $this->_id);
304 $controller->set('cid', $this->_contactId);
305
306 return $controller->run();
307 }
308
309 /**
fe482240 310 * called when action is update or new.
6a488035 311 *
9e475c2d
MW
312 * @return mixed
313 * @throws \CRM_Core_Exception
314 * @throws \Exception
6a488035 315 */
00be9182 316 public function edit() {
6a488035 317 // set https for offline cc transaction
19046166 318 $mode = CRM_Utils_Request::retrieve('mode', 'Alphanumeric', $this);
6a488035
TO
319 if ($mode == 'test' || $mode == 'live') {
320 CRM_Utils_System::redirectToSSL();
321 }
322
323 $controller = new CRM_Core_Controller_Simple(
324 'CRM_Contribute_Form_Contribution',
325 'Create Contribution',
326 $this->_action
327 );
328 $controller->setEmbedded(TRUE);
329 $controller->set('id', $this->_id);
330 $controller->set('cid', $this->_contactId);
331
332 return $controller->run();
333 }
334
04d15518
MWMC
335 /**
336 * @throws \CRM_Core_Exception
337 * @throws \CiviCRM_API3_Exception
338 */
00be9182 339 public function preProcess() {
edc80cda 340 $context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this);
6a488035 341 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'browse');
353ffa53 342 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
6a488035
TO
343
344 if ($context == 'standalone') {
345 $this->_action = CRM_Core_Action::ADD;
346 }
347 else {
f9009fe0
EM
348 $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, empty($this->_id));
349 if (empty($this->_contactId)) {
be2fb01f 350 $this->_contactId = civicrm_api3('contribution', 'getvalue', [
1330f57a
SL
351 'id' => $this->_id,
352 'return' => 'contact_id',
353 ]);
f9009fe0 354 }
6a488035
TO
355 $this->assign('contactId', $this->_contactId);
356
357 // check logged in url permission
358 CRM_Contact_Page_View::checkUserPermission($this);
6a488035
TO
359 }
360 $this->assign('action', $this->_action);
361
362 if ($this->_permission == CRM_Core_Permission::EDIT && !CRM_Core_Permission::check('edit contributions')) {
363 // demote to view since user does not have edit contrib rights
364 $this->_permission = CRM_Core_Permission::VIEW;
365 $this->assign('permission', 'view');
366 }
367 }
368
369 /**
dc195289 370 * the main function that is called when the page
6a488035
TO
371 * loads, it decides the which action has to be taken for the page.
372 *
04d15518
MWMC
373 * @throws \CRM_Core_Exception
374 * @throws \CiviCRM_API3_Exception
6a488035 375 */
00be9182 376 public function run() {
6a488035
TO
377 $this->preProcess();
378
379 // check if we can process credit card contribs
9be1374d 380 $this->assign('newCredit', CRM_Core_Config::isEnabledBackOfficeCreditCardPayments());
6a488035
TO
381
382 $this->setContext();
383
384 if ($this->_action & CRM_Core_Action::VIEW) {
385 $this->view();
386 }
387 elseif ($this->_action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD | CRM_Core_Action::DELETE)) {
388 $this->edit();
389 }
390 else {
391 $this->browse();
392 }
393
04d15518 394 parent::run();
6a488035
TO
395 }
396
04d15518
MWMC
397 /**
398 * @throws \CRM_Core_Exception
399 */
00be9182 400 public function setContext() {
6a488035 401 $qfKey = CRM_Utils_Request::retrieve('key', 'String', $this);
edc80cda 402 $context = CRM_Utils_Request::retrieve('context', 'Alphanumeric',
6a488035
TO
403 $this, FALSE, 'search'
404 );
405 $compContext = CRM_Utils_Request::retrieve('compContext', 'String', $this);
406
29571f63
AS
407 $searchContext = CRM_Utils_Request::retrieve('searchContext', 'String', $this);
408
6a488035
TO
409 //swap the context.
410 if ($context == 'search' && $compContext) {
411 $context = $compContext;
412 }
413 else {
414 $compContext = NULL;
415 }
416
417 // make sure we dont get tricked with a bad key
418 // so check format
419 if (!CRM_Core_Key::valid($qfKey)) {
420 $qfKey = NULL;
421 }
422
6a488035
TO
423 switch ($context) {
424 case 'user':
425 $url = CRM_Utils_System::url('civicrm/user', 'reset=1');
426 break;
427
428 case 'dashboard':
429 $url = CRM_Utils_System::url('civicrm/contribute',
430 'reset=1'
431 );
432 break;
433
434 case 'pledgeDashboard':
435 $url = CRM_Utils_System::url('civicrm/pledge',
436 'reset=1'
437 );
438 break;
439
440 case 'contribution':
6a488035 441 $url = CRM_Utils_System::url('civicrm/contact/view',
8381af80 442 "reset=1&force=1&cid={$this->_contactId}&selectedChild=contribute"
6a488035
TO
443 );
444 break;
445
446 case 'search':
447 case 'advanced':
448 $extraParams = "force=1";
449 if ($qfKey) {
450 $extraParams .= "&qfKey=$qfKey";
451 }
452
453 $this->assign('searchKey', $qfKey);
454 if ($context == 'advanced') {
455 $url = CRM_Utils_System::url('civicrm/contact/search/advanced', $extraParams);
456 }
4c9b6178 457 elseif ($searchContext) {
29571f63
AS
458 $url = CRM_Utils_System::url("civicrm/$searchContext/search", $extraParams);
459 }
6a488035
TO
460 else {
461 $url = CRM_Utils_System::url('civicrm/contribute/search', $extraParams);
462 }
463 break;
464
465 case 'home':
466 $url = CRM_Utils_System::url('civicrm/dashboard', 'reset=1');
467 break;
468
469 case 'activity':
470 $url = CRM_Utils_System::url('civicrm/contact/view',
471 "reset=1&force=1&cid={$this->_contactId}&selectedChild=activity"
472 );
473 break;
474
475 case 'member':
476 case 'membership':
477 $componentId = CRM_Utils_Request::retrieve('compId', 'Positive', $this);
478 $componentAction = CRM_Utils_Request::retrieve('compAction', 'Integer', $this);
479
480 $context = 'membership';
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/membership',
496 "reset=1&action={$action}&id={$componentId}&cid={$this->_contactId}&context={$context}&selectedChild=member{$searchKey}{$compContext}"
497 );
498 break;
499
500 case 'participant':
501 $componentId = CRM_Utils_Request::retrieve('compId', 'Positive', $this);
502 $componentAction = CRM_Utils_Request::retrieve('compAction', 'Integer', $this);
503
504 $context = 'participant';
505 $searchKey = NULL;
506 if ($compContext) {
507 $context = 'search';
508 if ($qfKey) {
509 $searchKey = "&key=$qfKey";
510 }
511 $compContext = "&compContext={$compContext}";
512 }
513 if ($componentAction == CRM_Core_Action::VIEW) {
514 $action = 'view';
515 }
516 else {
517 $action = 'update';
518 }
519 $url = CRM_Utils_System::url('civicrm/contact/view/participant',
520 "reset=1&action={$action}&id={$componentId}&cid={$this->_contactId}&context={$context}&selectedChild=event{$searchKey}{$compContext}"
521 );
522 break;
523
524 case 'pledge':
525 $url = CRM_Utils_System::url('civicrm/contact/view',
526 "reset=1&force=1&cid={$this->_contactId}&selectedChild=pledge"
527 );
528 break;
529
530 case 'standalone':
531 $url = CRM_Utils_System::url('civicrm/dashboard', 'reset=1');
532 break;
533
534 case 'fulltext':
353ffa53 535 $keyName = '&qfKey';
6a488035
TO
536 $urlParams = 'force=1';
537 $urlString = 'civicrm/contact/search/custom';
538 if ($this->_action == CRM_Core_Action::UPDATE) {
539 if ($this->_contactId) {
540 $urlParams .= '&cid=' . $this->_contactId;
541 }
542 $keyName = '&key';
543 $urlParams .= '&context=fulltext&action=view';
544 $urlString = 'civicrm/contact/view/contribution';
545 }
546 if ($qfKey) {
547 $urlParams .= "$keyName=$qfKey";
548 }
549 $this->assign('searchKey', $qfKey);
550 $url = CRM_Utils_System::url($urlString, $urlParams);
551 break;
552
553 default:
554 $cid = NULL;
555 if ($this->_contactId) {
556 $cid = '&cid=' . $this->_contactId;
557 }
558 $url = CRM_Utils_System::url('civicrm/contribute/search',
559 'reset=1&force=1' . $cid
560 );
561 break;
562 }
563
564 $session = CRM_Core_Session::singleton();
565 $session->pushUserContext($url);
566 }
96025800 567
6a488035 568}