3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 class CRM_Contribute_Page_Tab
extends CRM_Core_Page
{
20 * The permission we have on this contact
24 public $_permission = NULL;
27 * The contact ID for the contributions we are acting on
30 public $_contactId = NULL;
33 * The recurring contribution ID (if any)
39 * This method returns the links that are given for recur search row.
40 * currently the links added for each row are:
46 * @param string $context
50 public static function recurLinks(int $recurID, $context = 'contribution') {
51 $paymentProcessorObj = Civi\Payment\System
::singleton()->getById(CRM_Contribute_BAO_ContributionRecur
::getPaymentProcessorID($recurID));
53 CRM_Core_Action
::VIEW
=> [
55 'title' => ts('View Recurring Payment'),
56 'url' => 'civicrm/contact/view/contributionrecur',
57 'qs' => "reset=1&id=%%crid%%&cid=%%cid%%&context={$context}",
61 $templateContribution = CRM_Contribute_BAO_ContributionRecur
::getTemplateContribution($recurID);
63 (CRM_Core_Permission
::check('edit contributions') ||
$context !== 'contribution') &&
64 ($paymentProcessorObj->supports('ChangeSubscriptionAmount')
65 ||
$paymentProcessorObj->supports('EditRecurringContribution')
67 $links[CRM_Core_Action
::UPDATE
] = [
69 'title' => ts('Edit Recurring Payment'),
70 'url' => 'civicrm/contribute/updaterecur',
71 'qs' => "reset=1&action=update&crid=%%crid%%&cid=%%cid%%&context={$context}",
75 $links[CRM_Core_Action
::DISABLE
] = [
76 'name' => ts('Cancel'),
77 'title' => ts('Cancel'),
78 'ref' => 'crm-enable-disable',
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}";
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}",
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",
110 * Get the recur links to return for self service.
112 * These are the links to present to a logged in user wishing
113 * to service their own
115 * @param int $recurID
117 * @return array|array[]
118 * @throws \CRM_Core_Exception
119 * @throws \CiviCRM_API3_Exception
121 public static function selfServiceRecurLinks(int $recurID): array {
123 $paymentProcessorObj = Civi\Payment\System
::singleton()->getById(CRM_Contribute_BAO_ContributionRecur
::getPaymentProcessorID($recurID));
124 if ($paymentProcessorObj->supports('cancelRecurring')
125 && $paymentProcessorObj->subscriptionURL($recurID, 'recur', 'cancel')
127 $url = $paymentProcessorObj->subscriptionURL($recurID, 'recur', 'cancel');
128 $links[CRM_Core_Action
::DISABLE
] = [
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' : '',
137 if ($paymentProcessorObj->supports('UpdateSubscriptionBillingInfo')
138 && $paymentProcessorObj->subscriptionURL($recurID, 'recur', 'billing')
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'),
145 // Only display on-site links in a popup.
146 'class' => (stripos($url, 'http') !== FALSE) ?
'no-popup' : '',
150 if (($paymentProcessorObj->supports('ChangeSubscriptionAmount')
151 ||
$paymentProcessorObj->supports('EditRecurringContribution'))
152 && $paymentProcessorObj->subscriptionURL($recurID, 'recur', 'update')
154 $url = $paymentProcessorObj->subscriptionURL($recurID, 'recur', 'update');
155 $links[CRM_Core_Action
::UPDATE
] = [
156 'name' => ts('Edit'),
157 'title' => ts('Edit Recurring Payment'),
159 // Only display on-site links in a popup.
160 'class' => (stripos($url, 'http') !== FALSE) ?
'no-popup' : '',
167 * Get recurring links appropriate to viewing a user dashboard.
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.
174 * @param int $recurID
175 * @param int $contactID
177 * @return array|array[]
178 * @throws \CRM_Core_Exception
179 * @throws \CiviCRM_API3_Exception
181 public static function dashboardRecurLinks(int $recurID, int $contactID): array {
183 if ($contactID && $contactID === CRM_Core_Session
::getLoggedInContactID()) {
184 $links = self
::selfServiceRecurLinks($recurID);
186 $links +
= self
::recurLinks($recurID, 'dashboard');
191 * called when action is browse.
194 public function browse() {
195 // add annual contribution
197 [$annual['count'], $annual['amount'], $annual['avg']] = CRM_Contribute_BAO_Contribution
::annual($this->_contactId
);
198 $this->assign('annual', $annual);
200 $controller = new CRM_Core_Controller_Simple(
201 'CRM_Contribute_Form_Search',
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();
215 // add recurring block
216 $this->addRecurringContributionsBlock();
218 // enable/disable soft credit records for test contribution
220 if (CRM_Utils_Request
::retrieve('isTest', 'Positive', $this)) {
223 $this->assign('isTest', $isTest);
225 $softCreditList = CRM_Contribute_BAO_ContributionSoft
::getSoftContributionList($this->_contactId
, NULL, $isTest);
227 if (!empty($softCreditList)) {
228 $softCreditTotals = [];
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);
238 $this->assign('softCredit', TRUE);
239 $this->assign('softCreditRows', $softCreditList);
240 $this->assign('softCreditTotals', $softCreditTotals);
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;
253 * Get all the recurring contribution information and assign to the template
255 private function addRecurringContributionsBlock() {
256 [$activeContributions, $activeContributionsCount] = $this->getActiveRecurringContributions();
257 [$inactiveRecurringContributions, $inactiveContributionsCount] = $this->getInactiveRecurringContributions();
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);
270 * Loads active recurring contributions for the current contact and formats
271 * them to be used on the form.
275 private function getActiveRecurringContributions() {
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'],
282 $recurContributions = $contributionRecurResult['values'] ??
NULL;
284 catch (Exception
$e) {
285 $recurContributions = [];
288 return $this->buildRecurringContributionsArray($recurContributions);
292 * Loads inactive recurring contributions for the current contact and formats
293 * them to be used on the form.
297 private function getInactiveRecurringContributions() {
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'],
304 $recurContributions = $contributionRecurResult['values'] ??
NULL;
306 catch (Exception
$e) {
307 $recurContributions = NULL;
310 return $this->buildRecurringContributionsArray($recurContributions);
314 * @param $recurContributions
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)));
327 $actionMask = CRM_Core_Action
::mask([CRM_Core_Permission
::VIEW
]);
330 if (empty($recurDetail['is_test'])) {
331 $liveRecurringContributionCount++
;
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']);
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']);
343 $recurContributions[$recurId]['action'] = CRM_Core_Action
::formLink(self
::recurLinks((int) $recurId), $actionMask,
345 'cid' => $this->_contactId
,
347 'cxt' => 'contribution',
351 'contribution.selector.recurring',
357 return [$recurContributions, $liveRecurringContributionCount];
361 * called when action is view.
365 public function view() {
366 $controller = new CRM_Core_Controller_Simple(
367 'CRM_Contribute_Form_ContributionView',
368 ts('View Contribution'),
371 $controller->setEmbedded(TRUE);
372 $controller->set('id', $this->_id
);
373 $controller->set('cid', $this->_contactId
);
375 return $controller->run();
379 * called when action is update or new.
382 * @throws \CRM_Core_Exception
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();
392 $controller = new CRM_Core_Controller_Simple(
393 'CRM_Contribute_Form_Contribution',
394 'Create Contribution',
397 $controller->setEmbedded(TRUE);
398 $controller->set('id', $this->_id
);
399 $controller->set('cid', $this->_contactId
);
401 return $controller->run();
405 * @throws \CRM_Core_Exception
406 * @throws \CiviCRM_API3_Exception
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);
413 if ($context == 'standalone') {
414 $this->_action
= CRM_Core_Action
::ADD
;
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', [
421 'return' => 'contact_id',
424 $this->assign('contactId', $this->_contactId
);
426 // check logged in url permission
427 CRM_Contact_Page_View
::checkUserPermission($this);
429 $this->assign('action', $this->_action
);
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');
439 * the main function that is called when the page
440 * loads, it decides the which action has to be taken for the page.
442 * @throws \CRM_Core_Exception
443 * @throws \CiviCRM_API3_Exception
445 public function run() {
448 // check if we can process credit card contribs
449 $this->assign('newCredit', CRM_Core_Config
::isEnabledBackOfficeCreditCardPayments());
453 if ($this->_action
& CRM_Core_Action
::VIEW
) {
456 elseif ($this->_action
& (CRM_Core_Action
::UPDATE | CRM_Core_Action
::ADD | CRM_Core_Action
::DELETE
)) {
467 * @throws \CRM_Core_Exception
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'
474 $compContext = CRM_Utils_Request
::retrieve('compContext', 'String', $this);
476 $searchContext = CRM_Utils_Request
::retrieve('searchContext', 'String', $this);
479 if ($context == 'search' && $compContext) {
480 $context = $compContext;
486 // make sure we dont get tricked with a bad key
488 if (!CRM_Core_Key
::valid($qfKey)) {
494 $url = CRM_Utils_System
::url('civicrm/user', 'reset=1');
498 $url = CRM_Utils_System
::url('civicrm/contribute',
503 case 'pledgeDashboard':
504 $url = CRM_Utils_System
::url('civicrm/pledge',
510 $url = CRM_Utils_System
::url('civicrm/contact/view',
511 "reset=1&force=1&cid={$this->_contactId}&selectedChild=contribute"
517 $extraParams = "force=1";
519 $extraParams .= "&qfKey=$qfKey";
522 $this->assign('searchKey', $qfKey);
523 if ($context == 'advanced') {
524 $url = CRM_Utils_System
::url('civicrm/contact/search/advanced', $extraParams);
526 elseif ($searchContext) {
527 $url = CRM_Utils_System
::url("civicrm/$searchContext/search", $extraParams);
530 $url = CRM_Utils_System
::url('civicrm/contribute/search', $extraParams);
535 $url = CRM_Utils_System
::url('civicrm/dashboard', 'reset=1');
539 $url = CRM_Utils_System
::url('civicrm/contact/view',
540 "reset=1&force=1&cid={$this->_contactId}&selectedChild=activity"
546 $componentId = CRM_Utils_Request
::retrieve('compId', 'Positive', $this);
547 $componentAction = CRM_Utils_Request
::retrieve('compAction', 'Integer', $this);
549 $context = 'membership';
554 $searchKey = "&key=$qfKey";
556 $compContext = "&compContext={$compContext}";
558 if ($componentAction & CRM_Core_Action
::VIEW
) {
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}"
570 $componentId = CRM_Utils_Request
::retrieve('compId', 'Positive', $this);
571 $componentAction = CRM_Utils_Request
::retrieve('compAction', 'Integer', $this);
573 $context = 'participant';
578 $searchKey = "&key=$qfKey";
580 $compContext = "&compContext={$compContext}";
582 if ($componentAction == CRM_Core_Action
::VIEW
) {
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}"
594 $url = CRM_Utils_System
::url('civicrm/contact/view',
595 "reset=1&force=1&cid={$this->_contactId}&selectedChild=pledge"
600 $url = CRM_Utils_System
::url('civicrm/dashboard', 'reset=1');
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
;
612 $urlParams .= '&context=fulltext&action=view';
613 $urlString = 'civicrm/contact/view/contribution';
616 $urlParams .= "$keyName=$qfKey";
618 $this->assign('searchKey', $qfKey);
619 $url = CRM_Utils_System
::url($urlString, $urlParams);
624 if ($this->_contactId
) {
625 $cid = '&cid=' . $this->_contactId
;
627 $url = CRM_Utils_System
::url('civicrm/contribute/search',
628 'reset=1&force=1' . $cid
633 $session = CRM_Core_Session
::singleton();
634 $session->pushUserContext($url);