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