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