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