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 // 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)) {
68 $templateContribution = CRM_Contribute_BAO_ContributionRecur
::getTemplateContribution($recurID);
70 (CRM_Core_Permission
::check('edit contributions') ||
$context !== 'contribution') &&
71 ($paymentProcessorObj->supports('ChangeSubscriptionAmount')
72 ||
$paymentProcessorObj->supports('EditRecurringContribution')
74 $links[CRM_Core_Action
::UPDATE
] = [
76 'title' => ts('Edit Recurring Payment'),
77 'url' => 'civicrm/contribute/updaterecur',
78 'qs' => "reset=1&action=update&crid=%%crid%%&cid=%%cid%%&context={$context}",
82 $links[CRM_Core_Action
::DISABLE
] = [
83 'name' => ts('Cancel'),
84 'title' => ts('Cancel'),
85 'ref' => 'crm-enable-disable',
88 if ($paymentProcessorObj->supports('cancelRecurring')) {
89 unset($links[CRM_Core_Action
::DISABLE
]['extra'], $links[CRM_Core_Action
::DISABLE
]['ref']);
90 $links[CRM_Core_Action
::DISABLE
]['url'] = "civicrm/contribute/unsubscribe";
91 $links[CRM_Core_Action
::DISABLE
]['qs'] = "reset=1&crid=%%crid%%&cid=%%cid%%&context={$context}";
94 if ($paymentProcessorObj->supports('UpdateSubscriptionBillingInfo')) {
95 $links[CRM_Core_Action
::RENEW
] = [
96 'name' => ts('Change Billing Details'),
97 'title' => ts('Change Billing Details'),
98 'url' => 'civicrm/contribute/updatebilling',
99 'qs' => "reset=1&crid=%%crid%%&cid=%%cid%%&context={$context}",
102 if (!empty($templateContribution['id']) && $paymentProcessorObj->supportsEditRecurringContribution()) {
103 // Use constant CRM_Core_Action::PREVIEW as there is no such thing as view template.
104 // And reusing view will mangle the actions.
105 $links[CRM_Core_Action
::PREVIEW
] = [
106 'name' => ts('View Template'),
107 'title' => ts('View Template Contribution'),
108 'url' => 'civicrm/contact/view/contribution',
109 'qs' => "reset=1&id={$templateContribution['id']}&cid=%%cid%%&action=view&context={$context}&force_create_template=1",
117 * Get the recur links to return for self service.
119 * These are the links to present to a logged in user wishing
120 * to service their own
122 * @param int $recurID
124 * @return array|array[]
125 * @throws \CRM_Core_Exception
126 * @throws \CiviCRM_API3_Exception
128 public static function selfServiceRecurLinks(int $recurID): array {
130 $paymentProcessorObj = Civi\Payment\System
::singleton()->getById(CRM_Contribute_BAO_ContributionRecur
::getPaymentProcessorID($recurID));
131 // In case there extension which have recurring payment and then
132 // extension is disabled and in that case payment object may be null
133 // To avoid the fatal error, return with VIEW link.
134 if (!is_object($paymentProcessorObj)) {
137 if ($paymentProcessorObj->supports('cancelRecurring')
138 && $paymentProcessorObj->subscriptionURL($recurID, 'recur', 'cancel')
140 $url = $paymentProcessorObj->subscriptionURL($recurID, 'recur', 'cancel');
141 $links[CRM_Core_Action
::DISABLE
] = [
143 'name' => ts('Cancel'),
144 'title' => ts('Cancel'),
145 // Only display on-site links in a popup.
146 'class' => (stripos($url, 'http') !== FALSE) ?
'no-popup' : '',
150 if ($paymentProcessorObj->supports('UpdateSubscriptionBillingInfo')
151 && $paymentProcessorObj->subscriptionURL($recurID, 'recur', 'billing')
153 $url = $paymentProcessorObj->subscriptionURL($recurID, 'recur', 'billing');
154 $links[CRM_Core_Action
::RENEW
] = [
155 'name' => ts('Change Billing Details'),
156 'title' => ts('Change Billing Details'),
158 // Only display on-site links in a popup.
159 'class' => (stripos($url, 'http') !== FALSE) ?
'no-popup' : '',
163 if (($paymentProcessorObj->supports('ChangeSubscriptionAmount')
164 ||
$paymentProcessorObj->supports('EditRecurringContribution'))
165 && $paymentProcessorObj->subscriptionURL($recurID, 'recur', 'update')
167 $url = $paymentProcessorObj->subscriptionURL($recurID, 'recur', 'update');
168 $links[CRM_Core_Action
::UPDATE
] = [
169 'name' => ts('Edit'),
170 'title' => ts('Edit Recurring Payment'),
172 // Only display on-site links in a popup.
173 'class' => (stripos($url, 'http') !== FALSE) ?
'no-popup' : '',
180 * Get recurring links appropriate to viewing a user dashboard.
182 * A contact should be able to see links appropriate to them (e.g
183 * payment processor cancel page) if viewing their own dashboard and
184 * links appropriate to the contact they are viewing, if they have
185 * permission, if viewing another user.
187 * @param int $recurID
188 * @param int $contactID
190 * @return array|array[]
191 * @throws \CRM_Core_Exception
192 * @throws \CiviCRM_API3_Exception
194 public static function dashboardRecurLinks(int $recurID, int $contactID): array {
196 if ($contactID && $contactID === CRM_Core_Session
::getLoggedInContactID()) {
197 $links = self
::selfServiceRecurLinks($recurID);
199 $links +
= self
::recurLinks($recurID, 'dashboard');
204 * called when action is browse.
207 public function browse() {
208 // add annual contribution
210 [$annual['count'], $annual['amount'], $annual['avg']] = CRM_Contribute_BAO_Contribution
::annual($this->_contactId
);
211 $this->assign('annual', $annual);
213 $controller = new CRM_Core_Controller_Simple(
214 'CRM_Contribute_Form_Search',
219 $controller->setEmbedded(TRUE);
220 $controller->reset();
221 $controller->set('cid', $this->_contactId
);
222 $controller->set('crid', $this->_crid
);
223 $controller->set('context', 'contribution');
224 $controller->set('limit', 50);
225 $controller->process();
228 // add recurring block
229 $this->addRecurringContributionsBlock();
231 // enable/disable soft credit records for test contribution
233 if (CRM_Utils_Request
::retrieve('isTest', 'Positive', $this)) {
236 $this->assign('isTest', $isTest);
238 $softCreditList = CRM_Contribute_BAO_ContributionSoft
::getSoftContributionList($this->_contactId
, NULL, $isTest);
240 if (!empty($softCreditList)) {
241 $softCreditTotals = [];
243 list($softCreditTotals['count'],
244 $softCreditTotals['cancel']['count'],
245 $softCreditTotals['amount'],
246 $softCreditTotals['avg'],
247 // to get cancel amount
248 $softCreditTotals['cancel']['amount']
249 ) = CRM_Contribute_BAO_ContributionSoft
::getSoftContributionTotals($this->_contactId
, $isTest);
251 $this->assign('softCredit', TRUE);
252 $this->assign('softCreditRows', $softCreditList);
253 $this->assign('softCreditTotals', $softCreditTotals);
256 if ($this->_contactId
) {
257 $displayName = CRM_Contact_BAO_Contact
::displayName($this->_contactId
);
258 $this->assign('displayName', $displayName);
259 $tabCount = CRM_Contact_BAO_Contact
::getCountComponent('contribution', $this->_contactId
);
260 $this->assign('tabCount', $tabCount);
261 $this->ajaxResponse
['tabCount'] = $tabCount;
266 * Get all the recurring contribution information and assign to the template
268 private function addRecurringContributionsBlock() {
269 [$activeContributions, $activeContributionsCount] = $this->getActiveRecurringContributions();
270 [$inactiveRecurringContributions, $inactiveContributionsCount] = $this->getInactiveRecurringContributions();
272 if (!empty($activeContributions) ||
!empty($inactiveRecurringContributions)) {
273 // assign vars to templates
274 $this->assign('action', $this->_action
);
275 $this->assign('activeRecurRows', $activeContributions);
276 $this->assign('contributionRecurCount', $activeContributionsCount +
$inactiveContributionsCount);
277 $this->assign('inactiveRecurRows', $inactiveRecurringContributions);
278 $this->assign('recur', TRUE);
283 * Loads active recurring contributions for the current contact and formats
284 * them to be used on the form.
288 private function getActiveRecurringContributions() {
290 $contributionRecurResult = civicrm_api3('ContributionRecur', 'get', [
291 'contact_id' => $this->_contactId
,
292 'contribution_status_id' => ['NOT IN' => CRM_Contribute_BAO_ContributionRecur
::getInactiveStatuses()],
293 'options' => ['limit' => 0, 'sort' => 'is_test, start_date DESC'],
295 $recurContributions = $contributionRecurResult['values'] ??
NULL;
297 catch (Exception
$e) {
298 $recurContributions = [];
301 return $this->buildRecurringContributionsArray($recurContributions);
305 * Loads inactive recurring contributions for the current contact and formats
306 * them to be used on the form.
310 private function getInactiveRecurringContributions() {
312 $contributionRecurResult = civicrm_api3('ContributionRecur', 'get', [
313 'contact_id' => $this->_contactId
,
314 'contribution_status_id' => ['IN' => CRM_Contribute_BAO_ContributionRecur
::getInactiveStatuses()],
315 'options' => ['limit' => 0, 'sort' => 'is_test, start_date DESC'],
317 $recurContributions = $contributionRecurResult['values'] ??
NULL;
319 catch (Exception
$e) {
320 $recurContributions = NULL;
323 return $this->buildRecurringContributionsArray($recurContributions);
327 * @param $recurContributions
331 private function buildRecurringContributionsArray($recurContributions) {
332 $liveRecurringContributionCount = 0;
333 foreach ($recurContributions as $recurId => $recurDetail) {
334 // Is recurring contribution active?
335 $recurContributions[$recurId]['is_active'] = !in_array(CRM_Contribute_PseudoConstant
::contributionStatus($recurDetail['contribution_status_id'], 'name'), CRM_Contribute_BAO_ContributionRecur
::getInactiveStatuses());
336 if ($recurContributions[$recurId]['is_active']) {
337 $actionMask = array_sum(array_keys(self
::recurLinks((int) $recurId)));
340 $actionMask = CRM_Core_Action
::mask([CRM_Core_Permission
::VIEW
]);
343 if (empty($recurDetail['is_test'])) {
344 $liveRecurringContributionCount++
;
347 // Get the name of the payment processor
348 if (!empty($recurDetail['payment_processor_id'])) {
349 $recurContributions[$recurId]['payment_processor'] = CRM_Financial_BAO_PaymentProcessor
::getPaymentProcessorName($recurDetail['payment_processor_id']);
351 // Get the label for the contribution status
352 if (!empty($recurDetail['contribution_status_id'])) {
353 $recurContributions[$recurId]['contribution_status'] = CRM_Core_PseudoConstant
::getLabel('CRM_Contribute_BAO_ContributionRecur', 'contribution_status_id', $recurDetail['contribution_status_id']);
356 $recurContributions[$recurId]['action'] = CRM_Core_Action
::formLink(self
::recurLinks((int) $recurId), $actionMask,
358 'cid' => $this->_contactId
,
360 'cxt' => 'contribution',
364 'contribution.selector.recurring',
370 return [$recurContributions, $liveRecurringContributionCount];
374 * called when action is view.
378 public function view() {
379 $controller = new CRM_Core_Controller_Simple(
380 'CRM_Contribute_Form_ContributionView',
381 ts('View Contribution'),
384 $controller->setEmbedded(TRUE);
385 $controller->set('id', $this->_id
);
386 $controller->set('cid', $this->_contactId
);
388 return $controller->run();
392 * called when action is update or new.
395 * @throws \CRM_Core_Exception
398 public function edit() {
399 // set https for offline cc transaction
400 $mode = CRM_Utils_Request
::retrieve('mode', 'Alphanumeric', $this);
401 if ($mode == 'test' ||
$mode == 'live') {
402 CRM_Utils_System
::redirectToSSL();
405 $controller = new CRM_Core_Controller_Simple(
406 'CRM_Contribute_Form_Contribution',
407 'Create Contribution',
410 $controller->setEmbedded(TRUE);
411 $controller->set('id', $this->_id
);
412 $controller->set('cid', $this->_contactId
);
414 return $controller->run();
418 * @throws \CRM_Core_Exception
419 * @throws \CiviCRM_API3_Exception
421 public function preProcess() {
422 $context = CRM_Utils_Request
::retrieve('context', 'Alphanumeric', $this);
423 $this->_action
= CRM_Utils_Request
::retrieve('action', 'String', $this, FALSE, 'browse');
424 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive', $this);
426 if ($context == 'standalone') {
427 $this->_action
= CRM_Core_Action
::ADD
;
430 $this->_contactId
= CRM_Utils_Request
::retrieve('cid', 'Positive', $this, empty($this->_id
));
431 if (empty($this->_contactId
)) {
432 $this->_contactId
= civicrm_api3('contribution', 'getvalue', [
434 'return' => 'contact_id',
437 $this->assign('contactId', $this->_contactId
);
439 // check logged in url permission
440 CRM_Contact_Page_View
::checkUserPermission($this);
442 $this->assign('action', $this->_action
);
444 if ($this->_permission
== CRM_Core_Permission
::EDIT
&& !CRM_Core_Permission
::check('edit contributions')) {
445 // demote to view since user does not have edit contrib rights
446 $this->_permission
= CRM_Core_Permission
::VIEW
;
447 $this->assign('permission', 'view');
452 * the main function that is called when the page
453 * loads, it decides the which action has to be taken for the page.
455 * @throws \CRM_Core_Exception
456 * @throws \CiviCRM_API3_Exception
458 public function run() {
461 // check if we can process credit card contribs
462 $this->assign('newCredit', CRM_Core_Config
::isEnabledBackOfficeCreditCardPayments());
466 if ($this->_action
& CRM_Core_Action
::VIEW
) {
469 elseif ($this->_action
& (CRM_Core_Action
::UPDATE | CRM_Core_Action
::ADD | CRM_Core_Action
::DELETE
)) {
480 * @throws \CRM_Core_Exception
482 public function setContext() {
483 $qfKey = CRM_Utils_Request
::retrieve('key', 'String', $this);
484 $context = CRM_Utils_Request
::retrieve('context', 'Alphanumeric',
485 $this, FALSE, 'search'
487 $compContext = CRM_Utils_Request
::retrieve('compContext', 'String', $this);
489 $searchContext = CRM_Utils_Request
::retrieve('searchContext', 'String', $this);
492 if ($context == 'search' && $compContext) {
493 $context = $compContext;
499 // make sure we dont get tricked with a bad key
501 if (!CRM_Core_Key
::valid($qfKey)) {
507 $url = CRM_Utils_System
::url('civicrm/user', 'reset=1');
511 $url = CRM_Utils_System
::url('civicrm/contribute',
516 case 'pledgeDashboard':
517 $url = CRM_Utils_System
::url('civicrm/pledge',
523 $url = CRM_Utils_System
::url('civicrm/contact/view',
524 "reset=1&force=1&cid={$this->_contactId}&selectedChild=contribute"
530 $extraParams = "force=1";
532 $extraParams .= "&qfKey=$qfKey";
535 $this->assign('searchKey', $qfKey);
536 if ($context == 'advanced') {
537 $url = CRM_Utils_System
::url('civicrm/contact/search/advanced', $extraParams);
539 elseif ($searchContext) {
540 $url = CRM_Utils_System
::url("civicrm/$searchContext/search", $extraParams);
543 $url = CRM_Utils_System
::url('civicrm/contribute/search', $extraParams);
548 $url = CRM_Utils_System
::url('civicrm/dashboard', 'reset=1');
552 $url = CRM_Utils_System
::url('civicrm/contact/view',
553 "reset=1&force=1&cid={$this->_contactId}&selectedChild=activity"
559 $componentId = CRM_Utils_Request
::retrieve('compId', 'Positive', $this);
560 $componentAction = CRM_Utils_Request
::retrieve('compAction', 'Integer', $this);
562 $context = 'membership';
567 $searchKey = "&key=$qfKey";
569 $compContext = "&compContext={$compContext}";
571 if ($componentAction & CRM_Core_Action
::VIEW
) {
577 $url = CRM_Utils_System
::url('civicrm/contact/view/membership',
578 "reset=1&action={$action}&id={$componentId}&cid={$this->_contactId}&context={$context}&selectedChild=member{$searchKey}{$compContext}"
583 $componentId = CRM_Utils_Request
::retrieve('compId', 'Positive', $this);
584 $componentAction = CRM_Utils_Request
::retrieve('compAction', 'Integer', $this);
586 $context = 'participant';
591 $searchKey = "&key=$qfKey";
593 $compContext = "&compContext={$compContext}";
595 if ($componentAction == CRM_Core_Action
::VIEW
) {
601 $url = CRM_Utils_System
::url('civicrm/contact/view/participant',
602 "reset=1&action={$action}&id={$componentId}&cid={$this->_contactId}&context={$context}&selectedChild=event{$searchKey}{$compContext}"
607 $url = CRM_Utils_System
::url('civicrm/contact/view',
608 "reset=1&force=1&cid={$this->_contactId}&selectedChild=pledge"
613 $url = CRM_Utils_System
::url('civicrm/dashboard', 'reset=1');
618 $urlParams = 'force=1';
619 $urlString = 'civicrm/contact/search/custom';
620 if ($this->_action
== CRM_Core_Action
::UPDATE
) {
621 if ($this->_contactId
) {
622 $urlParams .= '&cid=' . $this->_contactId
;
625 $urlParams .= '&context=fulltext&action=view';
626 $urlString = 'civicrm/contact/view/contribution';
629 $urlParams .= "$keyName=$qfKey";
631 $this->assign('searchKey', $qfKey);
632 $url = CRM_Utils_System
::url($urlString, $urlParams);
637 if ($this->_contactId
) {
638 $cid = '&cid=' . $this->_contactId
;
640 $url = CRM_Utils_System
::url('civicrm/contribute/search',
641 'reset=1&force=1' . $cid
646 $session = CRM_Core_Session
::singleton();
647 $session->pushUserContext($url);