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));
52 $templateContribution = CRM_Contribute_BAO_ContributionRecur
::getTemplateContribution($recurID);
54 CRM_Core_Action
::VIEW
=> [
56 'title' => ts('View Recurring Payment'),
57 'url' => 'civicrm/contact/view/contributionrecur',
58 'qs' => "reset=1&id=%%crid%%&cid=%%cid%%&context={$context}",
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}",
72 (CRM_Core_Permission
::check('edit contributions') ||
$context !== 'contribution') &&
73 ($paymentProcessorObj->supports('ChangeSubscriptionAmount')
74 ||
$paymentProcessorObj->supports('EditRecurringContribution')
76 $links[CRM_Core_Action
::UPDATE
] = [
78 'title' => ts('Edit Recurring Payment'),
79 'url' => 'civicrm/contribute/updaterecur',
80 'qs' => "reset=1&action=update&crid=%%crid%%&cid=%%cid%%&context={$context}",
84 $links[CRM_Core_Action
::DISABLE
] = [
85 'name' => ts('Cancel'),
86 'title' => ts('Cancel'),
87 'ref' => 'crm-enable-disable',
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}";
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}",
109 * Get the recur links to return for self service.
111 * These are the links to present to a logged in user wishing
112 * to service their own
114 * @param int $recurID
116 * @return array|array[]
117 * @throws \CRM_Core_Exception
118 * @throws \CiviCRM_API3_Exception
120 public static function selfServiceRecurLinks(int $recurID): array {
122 $paymentProcessorObj = Civi\Payment\System
::singleton()->getById(CRM_Contribute_BAO_ContributionRecur
::getPaymentProcessorID($recurID));
123 if ($paymentProcessorObj->supports('cancelRecurring')
124 && $paymentProcessorObj->subscriptionURL($recurID, 'recur', 'cancel')
126 $url = $paymentProcessorObj->subscriptionURL($recurID, 'recur', 'cancel');
127 $links[CRM_Core_Action
::DISABLE
] = [
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' : '',
136 if ($paymentProcessorObj->supports('UpdateSubscriptionBillingInfo')
137 && $paymentProcessorObj->subscriptionURL($recurID, 'recur', 'billing')
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'),
144 // Only display on-site links in a popup.
145 'class' => (stripos($url, 'http') !== FALSE) ?
'no-popup' : '',
149 if (($paymentProcessorObj->supports('ChangeSubscriptionAmount')
150 ||
$paymentProcessorObj->supports('EditRecurringContribution'))
151 && $paymentProcessorObj->subscriptionURL($recurID, 'recur', 'update')
153 $url = $paymentProcessorObj->subscriptionURL($recurID, 'recur', 'update');
154 $links[CRM_Core_Action
::UPDATE
] = [
155 'name' => ts('Edit'),
156 'title' => ts('Edit Recurring Payment'),
158 // Only display on-site links in a popup.
159 'class' => (stripos($url, 'http') !== FALSE) ?
'no-popup' : '',
166 * Get recurring links appropriate to viewing a user dashboard.
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.
173 * @param int $recurID
174 * @param int $contactID
176 * @return array|array[]
177 * @throws \CRM_Core_Exception
178 * @throws \CiviCRM_API3_Exception
180 public static function dashboardRecurLinks(int $recurID, int $contactID): array {
182 if ($contactID && $contactID === CRM_Core_Session
::getLoggedInContactID()) {
183 $links = self
::selfServiceRecurLinks($recurID);
185 $links +
= self
::recurLinks($recurID, 'dashboard');
190 * called when action is browse.
193 public function browse() {
194 // add annual contribution
196 [$annual['count'], $annual['amount'], $annual['avg']] = CRM_Contribute_BAO_Contribution
::annual($this->_contactId
);
197 $this->assign('annual', $annual);
199 $controller = new CRM_Core_Controller_Simple(
200 'CRM_Contribute_Form_Search',
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();
214 // add recurring block
215 $this->addRecurringContributionsBlock();
217 // enable/disable soft credit records for test contribution
219 if (CRM_Utils_Request
::retrieve('isTest', 'Positive', $this)) {
222 $this->assign('isTest', $isTest);
224 $softCreditList = CRM_Contribute_BAO_ContributionSoft
::getSoftContributionList($this->_contactId
, NULL, $isTest);
226 if (!empty($softCreditList)) {
227 $softCreditTotals = [];
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);
237 $this->assign('softCredit', TRUE);
238 $this->assign('softCreditRows', $softCreditList);
239 $this->assign('softCreditTotals', $softCreditTotals);
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;
252 * Get all the recurring contribution information and assign to the template
254 private function addRecurringContributionsBlock() {
255 [$activeContributions, $activeContributionsCount] = $this->getActiveRecurringContributions();
256 [$inactiveRecurringContributions, $inactiveContributionsCount] = $this->getInactiveRecurringContributions();
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);
269 * Loads active recurring contributions for the current contact and formats
270 * them to be used on the form.
274 private function getActiveRecurringContributions() {
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'],
281 $recurContributions = $contributionRecurResult['values'] ??
NULL;
283 catch (Exception
$e) {
284 $recurContributions = [];
287 return $this->buildRecurringContributionsArray($recurContributions);
291 * Loads inactive recurring contributions for the current contact and formats
292 * them to be used on the form.
296 private function getInactiveRecurringContributions() {
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'],
303 $recurContributions = $contributionRecurResult['values'] ??
NULL;
305 catch (Exception
$e) {
306 $recurContributions = NULL;
309 return $this->buildRecurringContributionsArray($recurContributions);
313 * @param $recurContributions
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)));
326 $actionMask = CRM_Core_Action
::mask([CRM_Core_Permission
::VIEW
]);
329 if (empty($recurDetail['is_test'])) {
330 $liveRecurringContributionCount++
;
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']);
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']);
342 $recurContributions[$recurId]['action'] = CRM_Core_Action
::formLink(self
::recurLinks((int) $recurId), $actionMask,
344 'cid' => $this->_contactId
,
346 'cxt' => 'contribution',
350 'contribution.selector.recurring',
356 return [$recurContributions, $liveRecurringContributionCount];
360 * called when action is view.
364 public function view() {
365 $controller = new CRM_Core_Controller_Simple(
366 'CRM_Contribute_Form_ContributionView',
367 ts('View Contribution'),
370 $controller->setEmbedded(TRUE);
371 $controller->set('id', $this->_id
);
372 $controller->set('cid', $this->_contactId
);
374 return $controller->run();
378 * called when action is update or new.
381 * @throws \CRM_Core_Exception
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();
391 $controller = new CRM_Core_Controller_Simple(
392 'CRM_Contribute_Form_Contribution',
393 'Create Contribution',
396 $controller->setEmbedded(TRUE);
397 $controller->set('id', $this->_id
);
398 $controller->set('cid', $this->_contactId
);
400 return $controller->run();
404 * @throws \CRM_Core_Exception
405 * @throws \CiviCRM_API3_Exception
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);
412 if ($context == 'standalone') {
413 $this->_action
= CRM_Core_Action
::ADD
;
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', [
420 'return' => 'contact_id',
423 $this->assign('contactId', $this->_contactId
);
425 // check logged in url permission
426 CRM_Contact_Page_View
::checkUserPermission($this);
428 $this->assign('action', $this->_action
);
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');
438 * the main function that is called when the page
439 * loads, it decides the which action has to be taken for the page.
441 * @throws \CRM_Core_Exception
442 * @throws \CiviCRM_API3_Exception
444 public function run() {
447 // check if we can process credit card contribs
448 $this->assign('newCredit', CRM_Core_Config
::isEnabledBackOfficeCreditCardPayments());
452 if ($this->_action
& CRM_Core_Action
::VIEW
) {
455 elseif ($this->_action
& (CRM_Core_Action
::UPDATE | CRM_Core_Action
::ADD | CRM_Core_Action
::DELETE
)) {
466 * @throws \CRM_Core_Exception
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'
473 $compContext = CRM_Utils_Request
::retrieve('compContext', 'String', $this);
475 $searchContext = CRM_Utils_Request
::retrieve('searchContext', 'String', $this);
478 if ($context == 'search' && $compContext) {
479 $context = $compContext;
485 // make sure we dont get tricked with a bad key
487 if (!CRM_Core_Key
::valid($qfKey)) {
493 $url = CRM_Utils_System
::url('civicrm/user', 'reset=1');
497 $url = CRM_Utils_System
::url('civicrm/contribute',
502 case 'pledgeDashboard':
503 $url = CRM_Utils_System
::url('civicrm/pledge',
509 $url = CRM_Utils_System
::url('civicrm/contact/view',
510 "reset=1&force=1&cid={$this->_contactId}&selectedChild=contribute"
516 $extraParams = "force=1";
518 $extraParams .= "&qfKey=$qfKey";
521 $this->assign('searchKey', $qfKey);
522 if ($context == 'advanced') {
523 $url = CRM_Utils_System
::url('civicrm/contact/search/advanced', $extraParams);
525 elseif ($searchContext) {
526 $url = CRM_Utils_System
::url("civicrm/$searchContext/search", $extraParams);
529 $url = CRM_Utils_System
::url('civicrm/contribute/search', $extraParams);
534 $url = CRM_Utils_System
::url('civicrm/dashboard', 'reset=1');
538 $url = CRM_Utils_System
::url('civicrm/contact/view',
539 "reset=1&force=1&cid={$this->_contactId}&selectedChild=activity"
545 $componentId = CRM_Utils_Request
::retrieve('compId', 'Positive', $this);
546 $componentAction = CRM_Utils_Request
::retrieve('compAction', 'Integer', $this);
548 $context = 'membership';
553 $searchKey = "&key=$qfKey";
555 $compContext = "&compContext={$compContext}";
557 if ($componentAction & CRM_Core_Action
::VIEW
) {
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}"
569 $componentId = CRM_Utils_Request
::retrieve('compId', 'Positive', $this);
570 $componentAction = CRM_Utils_Request
::retrieve('compAction', 'Integer', $this);
572 $context = 'participant';
577 $searchKey = "&key=$qfKey";
579 $compContext = "&compContext={$compContext}";
581 if ($componentAction == CRM_Core_Action
::VIEW
) {
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}"
593 $url = CRM_Utils_System
::url('civicrm/contact/view',
594 "reset=1&force=1&cid={$this->_contactId}&selectedChild=pledge"
599 $url = CRM_Utils_System
::url('civicrm/dashboard', 'reset=1');
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
;
611 $urlParams .= '&context=fulltext&action=view';
612 $urlString = 'civicrm/contact/view/contribution';
615 $urlParams .= "$keyName=$qfKey";
617 $this->assign('searchKey', $qfKey);
618 $url = CRM_Utils_System
::url($urlString, $urlParams);
623 if ($this->_contactId
) {
624 $cid = '&cid=' . $this->_contactId
;
626 $url = CRM_Utils_System
::url('civicrm/contribute/search',
627 'reset=1&force=1' . $cid
632 $session = CRM_Core_Session
::singleton();
633 $session->pushUserContext($url);