Merge pull request #22316 from braders/core-3003-preserve-tab-between-pageloads
[civicrm-core.git] / CRM / Contribute / Page / Tab.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17 class CRM_Contribute_Page_Tab extends CRM_Core_Page {
18
19 /**
20 * The permission we have on this contact
21 *
22 * @var string
23 */
24 public $_permission = NULL;
25
26 /**
27 * The contact ID for the contributions we are acting on
28 * @var int
29 */
30 public $_contactId = NULL;
31
32 /**
33 * The recurring contribution ID (if any)
34 * @var int
35 */
36 public $_crid = NULL;
37
38 /**
39 * This method returns the links that are given for recur search row.
40 * currently the links added for each row are:
41 * - View
42 * - Edit
43 * - Cancel
44 *
45 * @param int $recurID
46 * @param string $context
47 *
48 * @return array
49 */
50 public static function recurLinks(int $recurID, $context = 'contribution') {
51 $paymentProcessorObj = Civi\Payment\System::singleton()->getById(CRM_Contribute_BAO_ContributionRecur::getPaymentProcessorID($recurID));
52 $links = [
53 CRM_Core_Action::VIEW => [
54 'name' => ts('View'),
55 'title' => ts('View Recurring Payment'),
56 'url' => 'civicrm/contact/view/contributionrecur',
57 'qs' => "reset=1&id=%%crid%%&cid=%%cid%%&context={$context}",
58 ],
59 ];
60
61 // In case there extension which have recurring payment and then
62 // extension is disabled and in that case payment object may be null
63 // To avoid the fatal error, return with VIEW link.
64 if (!is_object($paymentProcessorObj)) {
65 return $links;
66 }
67
68 $templateContribution = CRM_Contribute_BAO_ContributionRecur::getTemplateContribution($recurID);
69 if (
70 (CRM_Core_Permission::check('edit contributions') || $context !== 'contribution') &&
71 ($paymentProcessorObj->supports('ChangeSubscriptionAmount')
72 || $paymentProcessorObj->supports('EditRecurringContribution')
73 )) {
74 $links[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 }
81
82 $links[CRM_Core_Action::DISABLE] = [
83 'name' => ts('Cancel'),
84 'title' => ts('Cancel'),
85 'ref' => 'crm-enable-disable',
86 ];
87
88 if ($paymentProcessorObj->supports('cancelRecurring')) {
89 unset($links[CRM_Core_Action::DISABLE]['extra'], $links[CRM_Core_Action::DISABLE]['ref']);
90 $links[CRM_Core_Action::DISABLE]['url'] = "civicrm/contribute/unsubscribe";
91 $links[CRM_Core_Action::DISABLE]['qs'] = "reset=1&crid=%%crid%%&cid=%%cid%%&context={$context}";
92 }
93
94 if ($paymentProcessorObj->supports('UpdateSubscriptionBillingInfo')) {
95 $links[CRM_Core_Action::RENEW] = [
96 'name' => ts('Change Billing Details'),
97 'title' => ts('Change Billing Details'),
98 'url' => 'civicrm/contribute/updatebilling',
99 'qs' => "reset=1&crid=%%crid%%&cid=%%cid%%&context={$context}",
100 ];
101 }
102 if (!empty($templateContribution['id']) && $paymentProcessorObj->supportsEditRecurringContribution()) {
103 // Use constant CRM_Core_Action::PREVIEW as there is no such thing as view template.
104 // And reusing view will mangle the actions.
105 $links[CRM_Core_Action::PREVIEW] = [
106 'name' => ts('View Template'),
107 'title' => ts('View Template Contribution'),
108 'url' => 'civicrm/contact/view/contribution',
109 'qs' => "reset=1&id={$templateContribution['id']}&cid=%%cid%%&action=view&context={$context}&force_create_template=1",
110 ];
111 }
112
113 return $links;
114 }
115
116 /**
117 * Get the recur links to return for self service.
118 *
119 * These are the links to present to a logged in user wishing
120 * to service their own
121 *
122 * @param int $recurID
123 *
124 * @return array|array[]
125 * @throws \CRM_Core_Exception
126 * @throws \CiviCRM_API3_Exception
127 */
128 public static function selfServiceRecurLinks(int $recurID): array {
129 $links = [];
130 $paymentProcessorObj = Civi\Payment\System::singleton()->getById(CRM_Contribute_BAO_ContributionRecur::getPaymentProcessorID($recurID));
131 // In case there extension which have recurring payment and then
132 // extension is disabled and in that case payment object may be null
133 // To avoid the fatal error, return with VIEW link.
134 if (!is_object($paymentProcessorObj)) {
135 return $links;
136 }
137 if ($paymentProcessorObj->supports('cancelRecurring')
138 && $paymentProcessorObj->subscriptionURL($recurID, 'recur', 'cancel')
139 ) {
140 $url = $paymentProcessorObj->subscriptionURL($recurID, 'recur', 'cancel');
141 $links[CRM_Core_Action::DISABLE] = [
142 'url' => $url,
143 'name' => ts('Cancel'),
144 'title' => ts('Cancel'),
145 // Only display on-site links in a popup.
146 'class' => (stripos($url, 'http') !== FALSE) ? 'no-popup' : '',
147 ];
148 }
149
150 if ($paymentProcessorObj->supports('UpdateSubscriptionBillingInfo')
151 && $paymentProcessorObj->subscriptionURL($recurID, 'recur', 'billing')
152 ) {
153 $url = $paymentProcessorObj->subscriptionURL($recurID, 'recur', 'billing');
154 $links[CRM_Core_Action::RENEW] = [
155 'name' => ts('Change Billing Details'),
156 'title' => ts('Change Billing Details'),
157 'url' => $url,
158 // Only display on-site links in a popup.
159 'class' => (stripos($url, 'http') !== FALSE) ? 'no-popup' : '',
160 ];
161 }
162
163 if (($paymentProcessorObj->supports('ChangeSubscriptionAmount')
164 || $paymentProcessorObj->supports('EditRecurringContribution'))
165 && $paymentProcessorObj->subscriptionURL($recurID, 'recur', 'update')
166 ) {
167 $url = $paymentProcessorObj->subscriptionURL($recurID, 'recur', 'update');
168 $links[CRM_Core_Action::UPDATE] = [
169 'name' => ts('Edit'),
170 'title' => ts('Edit Recurring Payment'),
171 'url' => $url,
172 // Only display on-site links in a popup.
173 'class' => (stripos($url, 'http') !== FALSE) ? 'no-popup' : '',
174 ];
175 }
176 return $links;
177 }
178
179 /**
180 * Get recurring links appropriate to viewing a user dashboard.
181 *
182 * A contact should be able to see links appropriate to them (e.g
183 * payment processor cancel page) if viewing their own dashboard and
184 * links appropriate to the contact they are viewing, if they have
185 * permission, if viewing another user.
186 *
187 * @param int $recurID
188 * @param int $contactID
189 *
190 * @return array|array[]
191 * @throws \CRM_Core_Exception
192 * @throws \CiviCRM_API3_Exception
193 */
194 public static function dashboardRecurLinks(int $recurID, int $contactID): array {
195 $links = [];
196 if ($contactID && $contactID === CRM_Core_Session::getLoggedInContactID()) {
197 $links = self::selfServiceRecurLinks($recurID);
198 }
199 $links += self::recurLinks($recurID, 'dashboard');
200 return $links;
201 }
202
203 /**
204 * called when action is browse.
205 *
206 */
207 public function browse() {
208 // add annual contribution
209 $annual = [];
210 [$annual['count'], $annual['amount'], $annual['avg']] = CRM_Contribute_BAO_Contribution::annual($this->_contactId);
211 $this->assign('annual', $annual);
212
213 $controller = new CRM_Core_Controller_Simple(
214 'CRM_Contribute_Form_Search',
215 ts('Contributions'),
216 $this->_action,
217 FALSE, FALSE, TRUE
218 );
219 $controller->setEmbedded(TRUE);
220 $controller->reset();
221 $controller->set('cid', $this->_contactId);
222 $controller->set('crid', $this->_crid);
223 $controller->set('context', 'contribution');
224 $controller->set('limit', 50);
225 $controller->process();
226 $controller->run();
227
228 // add recurring block
229 $this->addRecurringContributionsBlock();
230
231 // enable/disable soft credit records for test contribution
232 $isTest = 0;
233 if (CRM_Utils_Request::retrieve('isTest', 'Positive', $this)) {
234 $isTest = 1;
235 }
236 $this->assign('isTest', $isTest);
237
238 $softCreditList = CRM_Contribute_BAO_ContributionSoft::getSoftContributionList($this->_contactId, NULL, $isTest);
239
240 if (!empty($softCreditList)) {
241 $softCreditTotals = [];
242
243 list($softCreditTotals['count'],
244 $softCreditTotals['cancel']['count'],
245 $softCreditTotals['amount'],
246 $softCreditTotals['avg'],
247 // to get cancel amount
248 $softCreditTotals['cancel']['amount']
249 ) = CRM_Contribute_BAO_ContributionSoft::getSoftContributionTotals($this->_contactId, $isTest);
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 $tabCount = CRM_Contact_BAO_Contact::getCountComponent('contribution', $this->_contactId);
260 $this->assign('tabCount', $tabCount);
261 $this->ajaxResponse['tabCount'] = $tabCount;
262 }
263 }
264
265 /**
266 * Get all the recurring contribution information and assign to the template
267 */
268 private function addRecurringContributionsBlock() {
269 [$activeContributions, $activeContributionsCount] = $this->getActiveRecurringContributions();
270 [$inactiveRecurringContributions, $inactiveContributionsCount] = $this->getInactiveRecurringContributions();
271
272 if (!empty($activeContributions) || !empty($inactiveRecurringContributions)) {
273 // assign vars to templates
274 $this->assign('action', $this->_action);
275 $this->assign('activeRecurRows', $activeContributions);
276 $this->assign('contributionRecurCount', $activeContributionsCount + $inactiveContributionsCount);
277 $this->assign('inactiveRecurRows', $inactiveRecurringContributions);
278 $this->assign('recur', TRUE);
279 }
280 }
281
282 /**
283 * Loads active recurring contributions for the current contact and formats
284 * them to be used on the form.
285 *
286 * @return array
287 */
288 private function getActiveRecurringContributions() {
289 try {
290 $contributionRecurResult = civicrm_api3('ContributionRecur', 'get', [
291 'contact_id' => $this->_contactId,
292 'contribution_status_id' => ['NOT IN' => CRM_Contribute_BAO_ContributionRecur::getInactiveStatuses()],
293 'options' => ['limit' => 0, 'sort' => 'is_test, start_date DESC'],
294 ]);
295 $recurContributions = $contributionRecurResult['values'] ?? NULL;
296 }
297 catch (Exception $e) {
298 $recurContributions = [];
299 }
300
301 return $this->buildRecurringContributionsArray($recurContributions);
302 }
303
304 /**
305 * Loads inactive recurring contributions for the current contact and formats
306 * them to be used on the form.
307 *
308 * @return array
309 */
310 private function getInactiveRecurringContributions() {
311 try {
312 $contributionRecurResult = civicrm_api3('ContributionRecur', 'get', [
313 'contact_id' => $this->_contactId,
314 'contribution_status_id' => ['IN' => CRM_Contribute_BAO_ContributionRecur::getInactiveStatuses()],
315 'options' => ['limit' => 0, 'sort' => 'is_test, start_date DESC'],
316 ]);
317 $recurContributions = $contributionRecurResult['values'] ?? NULL;
318 }
319 catch (Exception $e) {
320 $recurContributions = NULL;
321 }
322
323 return $this->buildRecurringContributionsArray($recurContributions);
324 }
325
326 /**
327 * @param $recurContributions
328 *
329 * @return array
330 */
331 private function buildRecurringContributionsArray($recurContributions) {
332 $liveRecurringContributionCount = 0;
333 foreach ($recurContributions as $recurId => $recurDetail) {
334 // Is recurring contribution active?
335 $recurContributions[$recurId]['is_active'] = !in_array(CRM_Contribute_PseudoConstant::contributionStatus($recurDetail['contribution_status_id'], 'name'), CRM_Contribute_BAO_ContributionRecur::getInactiveStatuses());
336 if ($recurContributions[$recurId]['is_active']) {
337 $actionMask = array_sum(array_keys(self::recurLinks((int) $recurId)));
338 }
339 else {
340 $actionMask = CRM_Core_Action::mask([CRM_Core_Permission::VIEW]);
341 }
342
343 if (empty($recurDetail['is_test'])) {
344 $liveRecurringContributionCount++;
345 }
346
347 // Get the name of the payment processor
348 if (!empty($recurDetail['payment_processor_id'])) {
349 $recurContributions[$recurId]['payment_processor'] = CRM_Financial_BAO_PaymentProcessor::getPaymentProcessorName($recurDetail['payment_processor_id']);
350 }
351 // Get the label for the contribution status
352 if (!empty($recurDetail['contribution_status_id'])) {
353 $recurContributions[$recurId]['contribution_status'] = CRM_Core_PseudoConstant::getLabel('CRM_Contribute_BAO_ContributionRecur', 'contribution_status_id', $recurDetail['contribution_status_id']);
354 }
355
356 $recurContributions[$recurId]['action'] = CRM_Core_Action::formLink(self::recurLinks((int) $recurId), $actionMask,
357 [
358 'cid' => $this->_contactId,
359 'crid' => $recurId,
360 'cxt' => 'contribution',
361 ],
362 ts('more'),
363 FALSE,
364 'contribution.selector.recurring',
365 'Contribution',
366 $recurId
367 );
368 }
369
370 return [$recurContributions, $liveRecurringContributionCount];
371 }
372
373 /**
374 * called when action is view.
375 *
376 * @return mixed
377 */
378 public function view() {
379 $controller = new CRM_Core_Controller_Simple(
380 'CRM_Contribute_Form_ContributionView',
381 ts('View Contribution'),
382 $this->_action
383 );
384 $controller->setEmbedded(TRUE);
385 $controller->set('id', $this->_id);
386 $controller->set('cid', $this->_contactId);
387
388 return $controller->run();
389 }
390
391 /**
392 * called when action is update or new.
393 *
394 * @return mixed
395 * @throws \CRM_Core_Exception
396 * @throws \Exception
397 */
398 public function edit() {
399 // set https for offline cc transaction
400 $mode = CRM_Utils_Request::retrieve('mode', 'Alphanumeric', $this);
401 if ($mode == 'test' || $mode == 'live') {
402 CRM_Utils_System::redirectToSSL();
403 }
404
405 $controller = new CRM_Core_Controller_Simple(
406 'CRM_Contribute_Form_Contribution',
407 'Create Contribution',
408 $this->_action
409 );
410 $controller->setEmbedded(TRUE);
411 $controller->set('id', $this->_id);
412 $controller->set('cid', $this->_contactId);
413
414 return $controller->run();
415 }
416
417 /**
418 * @throws \CRM_Core_Exception
419 * @throws \CiviCRM_API3_Exception
420 */
421 public function preProcess() {
422 $context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this);
423 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'browse');
424 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
425
426 if ($context == 'standalone') {
427 $this->_action = CRM_Core_Action::ADD;
428 }
429 else {
430 $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, empty($this->_id));
431 if (empty($this->_contactId)) {
432 $this->_contactId = civicrm_api3('contribution', 'getvalue', [
433 'id' => $this->_id,
434 'return' => 'contact_id',
435 ]);
436 }
437 $this->assign('contactId', $this->_contactId);
438
439 // check logged in url permission
440 CRM_Contact_Page_View::checkUserPermission($this);
441 }
442 $this->assign('action', $this->_action);
443
444 if ($this->_permission == CRM_Core_Permission::EDIT && !CRM_Core_Permission::check('edit contributions')) {
445 // demote to view since user does not have edit contrib rights
446 $this->_permission = CRM_Core_Permission::VIEW;
447 $this->assign('permission', 'view');
448 }
449 }
450
451 /**
452 * the main function that is called when the page
453 * loads, it decides the which action has to be taken for the page.
454 *
455 * @throws \CRM_Core_Exception
456 * @throws \CiviCRM_API3_Exception
457 */
458 public function run() {
459 $this->preProcess();
460
461 // check if we can process credit card contribs
462 $this->assign('newCredit', CRM_Core_Config::isEnabledBackOfficeCreditCardPayments());
463
464 $this->setContext();
465
466 if ($this->_action & CRM_Core_Action::VIEW) {
467 $this->view();
468 }
469 elseif ($this->_action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD | CRM_Core_Action::DELETE)) {
470 $this->edit();
471 }
472 else {
473 $this->browse();
474 }
475
476 parent::run();
477 }
478
479 /**
480 * @throws \CRM_Core_Exception
481 */
482 public function setContext() {
483 $qfKey = CRM_Utils_Request::retrieve('key', 'String', $this);
484 $context = CRM_Utils_Request::retrieve('context', 'Alphanumeric',
485 $this, FALSE, 'search'
486 );
487 $compContext = CRM_Utils_Request::retrieve('compContext', 'String', $this);
488
489 $searchContext = CRM_Utils_Request::retrieve('searchContext', 'String', $this);
490
491 //swap the context.
492 if ($context == 'search' && $compContext) {
493 $context = $compContext;
494 }
495 else {
496 $compContext = NULL;
497 }
498
499 // make sure we dont get tricked with a bad key
500 // so check format
501 if (!CRM_Core_Key::valid($qfKey)) {
502 $qfKey = NULL;
503 }
504
505 switch ($context) {
506 case 'user':
507 $url = CRM_Utils_System::url('civicrm/user', 'reset=1');
508 break;
509
510 case 'dashboard':
511 $url = CRM_Utils_System::url('civicrm/contribute',
512 'reset=1'
513 );
514 break;
515
516 case 'pledgeDashboard':
517 $url = CRM_Utils_System::url('civicrm/pledge',
518 'reset=1'
519 );
520 break;
521
522 case 'contribution':
523 $url = CRM_Utils_System::url('civicrm/contact/view',
524 "reset=1&force=1&cid={$this->_contactId}&selectedChild=contribute"
525 );
526 break;
527
528 case 'search':
529 case 'advanced':
530 $extraParams = "force=1";
531 if ($qfKey) {
532 $extraParams .= "&qfKey=$qfKey";
533 }
534
535 $this->assign('searchKey', $qfKey);
536 if ($context == 'advanced') {
537 $url = CRM_Utils_System::url('civicrm/contact/search/advanced', $extraParams);
538 }
539 elseif ($searchContext) {
540 $url = CRM_Utils_System::url("civicrm/$searchContext/search", $extraParams);
541 }
542 else {
543 $url = CRM_Utils_System::url('civicrm/contribute/search', $extraParams);
544 }
545 break;
546
547 case 'home':
548 $url = CRM_Utils_System::url('civicrm/dashboard', 'reset=1');
549 break;
550
551 case 'activity':
552 $url = CRM_Utils_System::url('civicrm/contact/view',
553 "reset=1&force=1&cid={$this->_contactId}&selectedChild=activity"
554 );
555 break;
556
557 case 'member':
558 case 'membership':
559 $componentId = CRM_Utils_Request::retrieve('compId', 'Positive', $this);
560 $componentAction = CRM_Utils_Request::retrieve('compAction', 'Integer', $this);
561
562 $context = 'membership';
563 $searchKey = NULL;
564 if ($compContext) {
565 $context = 'search';
566 if ($qfKey) {
567 $searchKey = "&key=$qfKey";
568 }
569 $compContext = "&compContext={$compContext}";
570 }
571 if ($componentAction & CRM_Core_Action::VIEW) {
572 $action = 'view';
573 }
574 else {
575 $action = 'update';
576 }
577 $url = CRM_Utils_System::url('civicrm/contact/view/membership',
578 "reset=1&action={$action}&id={$componentId}&cid={$this->_contactId}&context={$context}&selectedChild=member{$searchKey}{$compContext}"
579 );
580 break;
581
582 case 'participant':
583 $componentId = CRM_Utils_Request::retrieve('compId', 'Positive', $this);
584 $componentAction = CRM_Utils_Request::retrieve('compAction', 'Integer', $this);
585
586 $context = 'participant';
587 $searchKey = NULL;
588 if ($compContext) {
589 $context = 'search';
590 if ($qfKey) {
591 $searchKey = "&key=$qfKey";
592 }
593 $compContext = "&compContext={$compContext}";
594 }
595 if ($componentAction == CRM_Core_Action::VIEW) {
596 $action = 'view';
597 }
598 else {
599 $action = 'update';
600 }
601 $url = CRM_Utils_System::url('civicrm/contact/view/participant',
602 "reset=1&action={$action}&id={$componentId}&cid={$this->_contactId}&context={$context}&selectedChild=event{$searchKey}{$compContext}"
603 );
604 break;
605
606 case 'pledge':
607 $url = CRM_Utils_System::url('civicrm/contact/view',
608 "reset=1&force=1&cid={$this->_contactId}&selectedChild=pledge"
609 );
610 break;
611
612 case 'standalone':
613 $url = CRM_Utils_System::url('civicrm/dashboard', 'reset=1');
614 break;
615
616 case 'fulltext':
617 $keyName = '&qfKey';
618 $urlParams = 'force=1';
619 $urlString = 'civicrm/contact/search/custom';
620 if ($this->_action == CRM_Core_Action::UPDATE) {
621 if ($this->_contactId) {
622 $urlParams .= '&cid=' . $this->_contactId;
623 }
624 $keyName = '&key';
625 $urlParams .= '&context=fulltext&action=view';
626 $urlString = 'civicrm/contact/view/contribution';
627 }
628 if ($qfKey) {
629 $urlParams .= "$keyName=$qfKey";
630 }
631 $this->assign('searchKey', $qfKey);
632 $url = CRM_Utils_System::url($urlString, $urlParams);
633 break;
634
635 default:
636 $cid = NULL;
637 if ($this->_contactId) {
638 $cid = '&cid=' . $this->_contactId;
639 }
640 $url = CRM_Utils_System::url('civicrm/contribute/search',
641 'reset=1&force=1' . $cid
642 );
643 break;
644 }
645
646 $session = CRM_Core_Session::singleton();
647 $session->pushUserContext($url);
648 }
649
650 }