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 'url' => 'civicrm/contribute/unsubscribe',
86 'qs' => 'reset=1&crid=%%crid%%&cid=%%cid%%&context=' . $context,
89 if ($paymentProcessorObj->supports('UpdateSubscriptionBillingInfo')) {
90 $links[CRM_Core_Action
::RENEW
] = [
91 'name' => ts('Change Billing Details'),
92 'title' => ts('Change Billing Details'),
93 'url' => 'civicrm/contribute/updatebilling',
94 'qs' => "reset=1&crid=%%crid%%&cid=%%cid%%&context={$context}",
97 if (!empty($templateContribution['id']) && $paymentProcessorObj->supportsEditRecurringContribution()) {
98 // Use constant CRM_Core_Action::PREVIEW as there is no such thing as view template.
99 // And reusing view will mangle the actions.
100 $links[CRM_Core_Action
::PREVIEW
] = [
101 'name' => ts('View Template'),
102 'title' => ts('View Template Contribution'),
103 'url' => 'civicrm/contact/view/contribution',
104 'qs' => "reset=1&id={$templateContribution['id']}&cid=%%cid%%&action=view&context={$context}&force_create_template=1",
112 * Get the recur links to return for self service.
114 * These are the links to present to a logged in user wishing
115 * to service their own
117 * @param int $recurID
119 * @return array|array[]
120 * @throws \CRM_Core_Exception
121 * @throws \CiviCRM_API3_Exception
123 public static function selfServiceRecurLinks(int $recurID): array {
125 $paymentProcessorObj = Civi\Payment\System
::singleton()->getById(CRM_Contribute_BAO_ContributionRecur
::getPaymentProcessorID($recurID));
126 // In case there extension which have recurring payment and then
127 // extension is disabled and in that case payment object may be null
128 // To avoid the fatal error, return with VIEW link.
129 if (!is_object($paymentProcessorObj)) {
132 if ($paymentProcessorObj->supports('cancelRecurring')
133 && $paymentProcessorObj->subscriptionURL($recurID, 'recur', 'cancel')
135 $url = $paymentProcessorObj->subscriptionURL($recurID, 'recur', 'cancel');
136 $links[CRM_Core_Action
::DISABLE
] = [
138 'name' => ts('Cancel'),
139 'title' => ts('Cancel'),
140 // Only display on-site links in a popup.
141 'class' => (stripos($url, 'http') !== FALSE) ?
'no-popup' : '',
145 if ($paymentProcessorObj->supports('UpdateSubscriptionBillingInfo')
146 && $paymentProcessorObj->subscriptionURL($recurID, 'recur', 'billing')
148 $url = $paymentProcessorObj->subscriptionURL($recurID, 'recur', 'billing');
149 $links[CRM_Core_Action
::RENEW
] = [
150 'name' => ts('Change Billing Details'),
151 'title' => ts('Change Billing Details'),
153 // Only display on-site links in a popup.
154 'class' => (stripos($url, 'http') !== FALSE) ?
'no-popup' : '',
158 if (($paymentProcessorObj->supports('ChangeSubscriptionAmount')
159 ||
$paymentProcessorObj->supports('EditRecurringContribution'))
160 && $paymentProcessorObj->subscriptionURL($recurID, 'recur', 'update')
162 $url = $paymentProcessorObj->subscriptionURL($recurID, 'recur', 'update');
163 $links[CRM_Core_Action
::UPDATE
] = [
164 'name' => ts('Edit'),
165 'title' => ts('Edit Recurring Payment'),
167 // Only display on-site links in a popup.
168 'class' => (stripos($url, 'http') !== FALSE) ?
'no-popup' : '',
175 * Get recurring links appropriate to viewing a user dashboard.
177 * A contact should be able to see links appropriate to them (e.g
178 * payment processor cancel page) if viewing their own dashboard and
179 * links appropriate to the contact they are viewing, if they have
180 * permission, if viewing another user.
182 * @param int $recurID
183 * @param int $contactID
185 * @return array|array[]
186 * @throws \CRM_Core_Exception
187 * @throws \CiviCRM_API3_Exception
189 public static function dashboardRecurLinks(int $recurID, int $contactID): array {
191 if ($contactID && $contactID === CRM_Core_Session
::getLoggedInContactID()) {
192 $links = self
::selfServiceRecurLinks($recurID);
194 $links +
= self
::recurLinks($recurID, 'dashboard');
199 * called when action is browse.
202 public function browse() {
203 // add annual contribution
205 [$annual['count'], $annual['amount'], $annual['avg']] = CRM_Contribute_BAO_Contribution
::annual($this->_contactId
);
206 $this->assign('annual', $annual);
208 $controller = new CRM_Core_Controller_Simple(
209 'CRM_Contribute_Form_Search',
214 $controller->setEmbedded(TRUE);
215 $controller->reset();
216 $controller->set('cid', $this->_contactId
);
217 $controller->set('crid', $this->_crid
);
218 $controller->set('context', 'contribution');
219 $controller->set('limit', 50);
220 $controller->process();
223 // add recurring block
224 $this->addRecurringContributionsBlock();
226 // enable/disable soft credit records for test contribution
228 if (CRM_Utils_Request
::retrieve('isTest', 'Positive', $this)) {
231 $this->assign('isTest', $isTest);
233 $softCreditList = CRM_Contribute_BAO_ContributionSoft
::getSoftContributionList($this->_contactId
, NULL, $isTest);
235 if (!empty($softCreditList)) {
236 $softCreditTotals = [];
238 list($softCreditTotals['count'],
239 $softCreditTotals['cancel']['count'],
240 $softCreditTotals['amount'],
241 $softCreditTotals['avg'],
242 // to get cancel amount
243 $softCreditTotals['cancel']['amount']
244 ) = CRM_Contribute_BAO_ContributionSoft
::getSoftContributionTotals($this->_contactId
, $isTest);
246 $this->assign('softCredit', TRUE);
247 $this->assign('softCreditRows', $softCreditList);
248 $this->assign('softCreditTotals', $softCreditTotals);
251 if ($this->_contactId
) {
252 $displayName = CRM_Contact_BAO_Contact
::displayName($this->_contactId
);
253 $this->assign('displayName', $displayName);
254 $tabCount = CRM_Contact_BAO_Contact
::getCountComponent('contribution', $this->_contactId
);
255 $this->assign('tabCount', $tabCount);
256 $this->ajaxResponse
['tabCount'] = $tabCount;
261 * Get all the recurring contribution information and assign to the template
263 private function addRecurringContributionsBlock() {
264 [$activeContributions, $activeContributionsCount] = $this->getActiveRecurringContributions();
265 [$inactiveRecurringContributions, $inactiveContributionsCount] = $this->getInactiveRecurringContributions();
267 if (!empty($activeContributions) ||
!empty($inactiveRecurringContributions)) {
268 // assign vars to templates
269 $this->assign('action', $this->_action
);
270 $this->assign('activeRecurRows', $activeContributions);
271 $this->assign('contributionRecurCount', $activeContributionsCount +
$inactiveContributionsCount);
272 $this->assign('inactiveRecurRows', $inactiveRecurringContributions);
273 $this->assign('recur', TRUE);
278 * Loads active recurring contributions for the current contact and formats
279 * them to be used on the form.
283 private function getActiveRecurringContributions() {
285 $contributionRecurResult = civicrm_api3('ContributionRecur', 'get', [
286 'contact_id' => $this->_contactId
,
287 'contribution_status_id' => ['NOT IN' => CRM_Contribute_BAO_ContributionRecur
::getInactiveStatuses()],
288 'options' => ['limit' => 0, 'sort' => 'is_test, start_date DESC'],
290 $recurContributions = $contributionRecurResult['values'] ??
NULL;
292 catch (Exception
$e) {
293 $recurContributions = [];
296 return $this->buildRecurringContributionsArray($recurContributions);
300 * Loads inactive recurring contributions for the current contact and formats
301 * them to be used on the form.
305 private function getInactiveRecurringContributions() {
307 $contributionRecurResult = civicrm_api3('ContributionRecur', 'get', [
308 'contact_id' => $this->_contactId
,
309 'contribution_status_id' => ['IN' => CRM_Contribute_BAO_ContributionRecur
::getInactiveStatuses()],
310 'options' => ['limit' => 0, 'sort' => 'is_test, start_date DESC'],
312 $recurContributions = $contributionRecurResult['values'] ??
NULL;
314 catch (Exception
$e) {
315 $recurContributions = NULL;
318 return $this->buildRecurringContributionsArray($recurContributions);
322 * @param $recurContributions
326 private function buildRecurringContributionsArray($recurContributions) {
327 $liveRecurringContributionCount = 0;
328 foreach ($recurContributions as $recurId => $recurDetail) {
329 // Is recurring contribution active?
330 $recurContributions[$recurId]['is_active'] = !in_array(CRM_Contribute_PseudoConstant
::contributionStatus($recurDetail['contribution_status_id'], 'name'), CRM_Contribute_BAO_ContributionRecur
::getInactiveStatuses());
331 if ($recurContributions[$recurId]['is_active']) {
332 $actionMask = array_sum(array_keys(self
::recurLinks((int) $recurId)));
335 $actionMask = CRM_Core_Action
::mask([CRM_Core_Permission
::VIEW
]);
338 if (empty($recurDetail['is_test'])) {
339 $liveRecurringContributionCount++
;
342 // Get the name of the payment processor
343 if (!empty($recurDetail['payment_processor_id'])) {
344 $recurContributions[$recurId]['payment_processor'] = CRM_Financial_BAO_PaymentProcessor
::getPaymentProcessorName($recurDetail['payment_processor_id']);
346 // Get the label for the contribution status
347 if (!empty($recurDetail['contribution_status_id'])) {
348 $recurContributions[$recurId]['contribution_status'] = CRM_Core_PseudoConstant
::getLabel('CRM_Contribute_BAO_ContributionRecur', 'contribution_status_id', $recurDetail['contribution_status_id']);
351 $recurContributions[$recurId]['action'] = CRM_Core_Action
::formLink(self
::recurLinks((int) $recurId), $actionMask,
353 'cid' => $this->_contactId
,
355 'cxt' => 'contribution',
359 'contribution.selector.recurring',
365 return [$recurContributions, $liveRecurringContributionCount];
369 * called when action is view.
373 public function view() {
374 $controller = new CRM_Core_Controller_Simple(
375 'CRM_Contribute_Form_ContributionView',
376 ts('View Contribution'),
379 $controller->setEmbedded(TRUE);
380 $controller->set('id', $this->_id
);
381 $controller->set('cid', $this->_contactId
);
383 return $controller->run();
387 * called when action is update or new.
390 * @throws \CRM_Core_Exception
393 public function edit() {
394 // set https for offline cc transaction
395 $mode = CRM_Utils_Request
::retrieve('mode', 'Alphanumeric', $this);
396 if ($mode == 'test' ||
$mode == 'live') {
397 CRM_Utils_System
::redirectToSSL();
400 $controller = new CRM_Core_Controller_Simple(
401 'CRM_Contribute_Form_Contribution',
402 'Create Contribution',
405 $controller->setEmbedded(TRUE);
406 $controller->set('id', $this->_id
);
407 $controller->set('cid', $this->_contactId
);
409 return $controller->run();
413 * @throws \CRM_Core_Exception
414 * @throws \CiviCRM_API3_Exception
416 public function preProcess() {
417 $context = CRM_Utils_Request
::retrieve('context', 'Alphanumeric', $this);
418 $this->_action
= CRM_Utils_Request
::retrieve('action', 'String', $this, FALSE, 'browse');
419 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive', $this);
421 if ($context == 'standalone') {
422 $this->_action
= CRM_Core_Action
::ADD
;
425 $this->_contactId
= CRM_Utils_Request
::retrieve('cid', 'Positive', $this, empty($this->_id
));
426 if (empty($this->_contactId
)) {
427 $this->_contactId
= civicrm_api3('contribution', 'getvalue', [
429 'return' => 'contact_id',
432 $this->assign('contactId', $this->_contactId
);
434 // check logged in url permission
435 CRM_Contact_Page_View
::checkUserPermission($this);
437 $this->assign('action', $this->_action
);
439 if ($this->_permission
== CRM_Core_Permission
::EDIT
&& !CRM_Core_Permission
::check('edit contributions')) {
440 // demote to view since user does not have edit contrib rights
441 $this->_permission
= CRM_Core_Permission
::VIEW
;
442 $this->assign('permission', 'view');
447 * the main function that is called when the page
448 * loads, it decides the which action has to be taken for the page.
450 * @throws \CRM_Core_Exception
451 * @throws \CiviCRM_API3_Exception
453 public function run() {
456 // check if we can process credit card contribs
457 $this->assign('newCredit', CRM_Core_Config
::isEnabledBackOfficeCreditCardPayments());
461 if ($this->_action
& CRM_Core_Action
::VIEW
) {
464 elseif ($this->_action
& (CRM_Core_Action
::UPDATE | CRM_Core_Action
::ADD | CRM_Core_Action
::DELETE
)) {
475 * @throws \CRM_Core_Exception
477 public function setContext() {
478 $qfKey = CRM_Utils_Request
::retrieve('key', 'String', $this);
479 $context = CRM_Utils_Request
::retrieve('context', 'Alphanumeric',
480 $this, FALSE, 'search'
482 $compContext = CRM_Utils_Request
::retrieve('compContext', 'String', $this);
484 $searchContext = CRM_Utils_Request
::retrieve('searchContext', 'String', $this);
487 if ($context == 'search' && $compContext) {
488 $context = $compContext;
494 // make sure we dont get tricked with a bad key
496 if (!CRM_Core_Key
::valid($qfKey)) {
502 $url = CRM_Utils_System
::url('civicrm/user', 'reset=1');
506 $url = CRM_Utils_System
::url('civicrm/contribute',
511 case 'pledgeDashboard':
512 $url = CRM_Utils_System
::url('civicrm/pledge',
518 $url = CRM_Utils_System
::url('civicrm/contact/view',
519 "reset=1&force=1&cid={$this->_contactId}&selectedChild=contribute"
525 $extraParams = "force=1";
527 $extraParams .= "&qfKey=$qfKey";
530 $this->assign('searchKey', $qfKey);
531 if ($context == 'advanced') {
532 $url = CRM_Utils_System
::url('civicrm/contact/search/advanced', $extraParams);
534 elseif ($searchContext) {
535 $url = CRM_Utils_System
::url("civicrm/$searchContext/search", $extraParams);
538 $url = CRM_Utils_System
::url('civicrm/contribute/search', $extraParams);
543 $url = CRM_Utils_System
::url('civicrm/dashboard', 'reset=1');
547 $url = CRM_Utils_System
::url('civicrm/contact/view',
548 "reset=1&force=1&cid={$this->_contactId}&selectedChild=activity"
554 $componentId = CRM_Utils_Request
::retrieve('compId', 'Positive', $this);
555 $componentAction = CRM_Utils_Request
::retrieve('compAction', 'Integer', $this);
557 $context = 'membership';
562 $searchKey = "&key=$qfKey";
564 $compContext = "&compContext={$compContext}";
566 if ($componentAction & CRM_Core_Action
::VIEW
) {
572 $url = CRM_Utils_System
::url('civicrm/contact/view/membership',
573 "reset=1&action={$action}&id={$componentId}&cid={$this->_contactId}&context={$context}&selectedChild=member{$searchKey}{$compContext}"
578 $componentId = CRM_Utils_Request
::retrieve('compId', 'Positive', $this);
579 $componentAction = CRM_Utils_Request
::retrieve('compAction', 'Integer', $this);
581 $context = 'participant';
586 $searchKey = "&key=$qfKey";
588 $compContext = "&compContext={$compContext}";
590 if ($componentAction == CRM_Core_Action
::VIEW
) {
596 $url = CRM_Utils_System
::url('civicrm/contact/view/participant',
597 "reset=1&action={$action}&id={$componentId}&cid={$this->_contactId}&context={$context}&selectedChild=event{$searchKey}{$compContext}"
602 $url = CRM_Utils_System
::url('civicrm/contact/view',
603 "reset=1&force=1&cid={$this->_contactId}&selectedChild=pledge"
608 $url = CRM_Utils_System
::url('civicrm/dashboard', 'reset=1');
613 $urlParams = 'force=1';
614 $urlString = 'civicrm/contact/search/custom';
615 if ($this->_action
== CRM_Core_Action
::UPDATE
) {
616 if ($this->_contactId
) {
617 $urlParams .= '&cid=' . $this->_contactId
;
620 $urlParams .= '&context=fulltext&action=view';
621 $urlString = 'civicrm/contact/view/contribution';
624 $urlParams .= "$keyName=$qfKey";
626 $this->assign('searchKey', $qfKey);
627 $url = CRM_Utils_System
::url($urlString, $urlParams);
632 if ($this->_contactId
) {
633 $cid = '&cid=' . $this->_contactId
;
635 $url = CRM_Utils_System
::url('civicrm/contribute/search',
636 'reset=1&force=1' . $cid
641 $session = CRM_Core_Session
::singleton();
642 $session->pushUserContext($url);