3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
33 class CRM_Contribute_Page_Tab
extends CRM_Core_Page
{
36 * The action links that we need to display for the browse screen.
40 static $_links = NULL;
41 static $_recurLinks = NULL;
42 public $_permission = NULL;
43 public $_contactId = NULL;
47 * This method returns the links that are given for recur search row.
48 * currently the links added for each row are:
53 * @param bool $recurID
54 * @param string $context
58 public static function &recurLinks($recurID = FALSE, $context = 'contribution') {
59 if (!(self
::$_links)) {
61 CRM_Core_Action
::VIEW
=> [
63 'title' => ts('View Recurring Payment'),
64 'url' => 'civicrm/contact/view/contributionrecur',
65 'qs' => "reset=1&id=%%crid%%&cid=%%cid%%&context={$context}",
67 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}",
73 CRM_Core_Action
::DISABLE
=> [
74 'name' => ts('Cancel'),
75 'title' => ts('Cancel'),
76 'ref' => 'crm-enable-disable',
82 $links = self
::$_links;
83 $paymentProcessorObj = CRM_Contribute_BAO_ContributionRecur
::getPaymentProcessorObject($recurID);
84 if (!$paymentProcessorObj) {
85 unset($links[CRM_Core_Action
::DISABLE
]);
86 unset($links[CRM_Core_Action
::UPDATE
]);
89 if ($paymentProcessorObj->supports('cancelRecurring')) {
90 unset($links[CRM_Core_Action
::DISABLE
]['extra'], $links[CRM_Core_Action
::DISABLE
]['ref']);
91 $links[CRM_Core_Action
::DISABLE
]['url'] = "civicrm/contribute/unsubscribe";
92 $links[CRM_Core_Action
::DISABLE
]['qs'] = "reset=1&crid=%%crid%%&cid=%%cid%%&context={$context}";
95 if ($paymentProcessorObj->supports('UpdateSubscriptionBillingInfo')) {
96 $links[CRM_Core_Action
::RENEW
] = [
97 'name' => ts('Change Billing Details'),
98 'title' => ts('Change Billing Details'),
99 'url' => 'civicrm/contribute/updatebilling',
100 'qs' => "reset=1&crid=%%crid%%&cid=%%cid%%&context={$context}",
104 if (!$paymentProcessorObj->supports('ChangeSubscriptionAmount') && !$paymentProcessorObj->supports('EditRecurringContribution')) {
105 unset($links[CRM_Core_Action
::UPDATE
]);
110 return self
::$_links;
114 * called when action is browse.
117 public function browse() {
118 // add annual contribution
120 list($annual['count'],
123 ) = CRM_Contribute_BAO_Contribution
::annual($this->_contactId
);
124 $this->assign('annual', $annual);
126 $controller = new CRM_Core_Controller_Simple(
127 'CRM_Contribute_Form_Search',
132 $controller->setEmbedded(TRUE);
133 $controller->reset();
134 $controller->set('cid', $this->_contactId
);
135 $controller->set('crid', $this->_crid
);
136 $controller->set('context', 'contribution');
137 $controller->set('limit', 50);
138 $controller->process();
141 // add recurring block
142 $this->addRecurringContributionsBlock();
144 // enable/disable soft credit records for test contribution
146 if (CRM_Utils_Request
::retrieve('isTest', 'Positive', $this)) {
149 $this->assign('isTest', $isTest);
151 $softCreditList = CRM_Contribute_BAO_ContributionSoft
::getSoftContributionList($this->_contactId
, NULL, $isTest);
153 if (!empty($softCreditList)) {
154 $softCreditTotals = [];
156 list($softCreditTotals['count'],
157 $softCreditTotals['cancel']['count'],
158 $softCreditTotals['amount'],
159 $softCreditTotals['avg'],
160 $softCreditTotals['cancel']['amount'] // to get cancel amount
161 ) = CRM_Contribute_BAO_ContributionSoft
::getSoftContributionTotals($this->_contactId
, $isTest);
163 $this->assign('softCredit', TRUE);
164 $this->assign('softCreditRows', $softCreditList);
165 $this->assign('softCreditTotals', $softCreditTotals);
168 if ($this->_contactId
) {
169 $displayName = CRM_Contact_BAO_Contact
::displayName($this->_contactId
);
170 $this->assign('displayName', $displayName);
171 $tabCount = CRM_Contact_BAO_Contact
::getCountComponent('contribution', $this->_contactId
);
172 $this->assign('tabCount', $tabCount);
173 $this->ajaxResponse
['tabCount'] = $tabCount;
178 * Get all the recurring contribution information and assign to the template
180 private function addRecurringContributionsBlock() {
181 list($activeContributions, $activeContributionsCount) = $this->getActiveRecurringContributions();
182 list($inactiveRecurringContributions, $inactiveContributionsCount) = $this->getInactiveRecurringContributions();
184 if (!empty($activeContributions) ||
!empty($inactiveRecurringContributions)) {
185 // assign vars to templates
186 $this->assign('action', $this->_action
);
187 $this->assign('activeRecurRows', $activeContributions);
188 $this->assign('contributionRecurCount', $activeContributionsCount +
$inactiveContributionsCount);
189 $this->assign('inactiveRecurRows', $inactiveRecurringContributions);
190 $this->assign('recur', TRUE);
195 * Loads active recurring contributions for the current contact and formats
196 * them to be used on the form.
200 private function getActiveRecurringContributions() {
202 $contributionRecurResult = civicrm_api3('ContributionRecur', 'get', [
203 'contact_id' => $this->_contactId
,
204 'contribution_status_id' => ['NOT IN' => CRM_Contribute_BAO_ContributionRecur
::getInactiveStatuses()],
205 'options' => ['limit' => 0, 'sort' => 'is_test, start_date DESC'],
207 $recurContributions = CRM_Utils_Array
::value('values', $contributionRecurResult);
209 catch (Exception
$e) {
210 $recurContributions = [];
213 return $this->buildRecurringContributionsArray($recurContributions);
217 * Loads inactive recurring contributions for the current contact and formats
218 * them to be used on the form.
222 private function getInactiveRecurringContributions() {
224 $contributionRecurResult = civicrm_api3('ContributionRecur', 'get', [
225 'contact_id' => $this->_contactId
,
226 'contribution_status_id' => ['IN' => CRM_Contribute_BAO_ContributionRecur
::getInactiveStatuses()],
227 'options' => ['limit' => 0, 'sort' => 'is_test, start_date DESC'],
229 $recurContributions = CRM_Utils_Array
::value('values', $contributionRecurResult);
231 catch (Exception
$e) {
232 $recurContributions = NULL;
235 return $this->buildRecurringContributionsArray($recurContributions);
239 * @param $recurContributions
243 private function buildRecurringContributionsArray($recurContributions) {
244 $liveRecurringContributionCount = 0;
245 foreach ($recurContributions as $recurId => $recurDetail) {
246 // Is recurring contribution active?
247 $recurContributions[$recurId]['is_active'] = !in_array(CRM_Contribute_PseudoConstant
::contributionStatus($recurDetail['contribution_status_id'], 'name'), CRM_Contribute_BAO_ContributionRecur
::getInactiveStatuses());
248 if ($recurContributions[$recurId]['is_active']) {
249 $actionMask = array_sum(array_keys(self
::recurLinks($recurId)));
252 $actionMask = CRM_Core_Action
::mask([CRM_Core_Permission
::VIEW
]);
255 if (empty($recurDetail['is_test'])) {
256 $liveRecurringContributionCount++
;
259 // Get the name of the payment processor
260 if (!empty($recurDetail['payment_processor_id'])) {
261 $recurContributions[$recurId]['payment_processor'] = CRM_Financial_BAO_PaymentProcessor
::getPaymentProcessorName($recurDetail['payment_processor_id']);
263 // Get the label for the contribution status
264 if (!empty($recurDetail['contribution_status_id'])) {
265 $recurContributions[$recurId]['contribution_status'] = CRM_Core_PseudoConstant
::getLabel('CRM_Contribute_BAO_ContributionRecur', 'contribution_status_id', $recurDetail['contribution_status_id']);
268 $recurContributions[$recurId]['action'] = CRM_Core_Action
::formLink(self
::recurLinks($recurId), $actionMask,
270 'cid' => $this->_contactId
,
272 'cxt' => 'contribution',
276 'contribution.selector.recurring',
282 return [$recurContributions, $liveRecurringContributionCount];
286 * called when action is view.
290 public function view() {
291 $controller = new CRM_Core_Controller_Simple(
292 'CRM_Contribute_Form_ContributionView',
293 ts('View Contribution'),
296 $controller->setEmbedded(TRUE);
297 $controller->set('id', $this->_id
);
298 $controller->set('cid', $this->_contactId
);
300 return $controller->run();
304 * called when action is update or new.
307 * @throws \CRM_Core_Exception
310 public function edit() {
311 // set https for offline cc transaction
312 $mode = CRM_Utils_Request
::retrieve('mode', 'Alphanumeric', $this);
313 if ($mode == 'test' ||
$mode == 'live') {
314 CRM_Utils_System
::redirectToSSL();
317 $controller = new CRM_Core_Controller_Simple(
318 'CRM_Contribute_Form_Contribution',
319 'Create Contribution',
322 $controller->setEmbedded(TRUE);
323 $controller->set('id', $this->_id
);
324 $controller->set('cid', $this->_contactId
);
326 return $controller->run();
329 public function preProcess() {
330 $context = CRM_Utils_Request
::retrieve('context', 'Alphanumeric', $this);
331 $this->_action
= CRM_Utils_Request
::retrieve('action', 'String', $this, FALSE, 'browse');
332 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive', $this);
334 if ($context == 'standalone') {
335 $this->_action
= CRM_Core_Action
::ADD
;
338 $this->_contactId
= CRM_Utils_Request
::retrieve('cid', 'Positive', $this, empty($this->_id
));
339 if (empty($this->_contactId
)) {
340 $this->_contactId
= civicrm_api3('contribution', 'getvalue', [
342 'return' => 'contact_id',
345 $this->assign('contactId', $this->_contactId
);
347 // check logged in url permission
348 CRM_Contact_Page_View
::checkUserPermission($this);
350 $this->assign('action', $this->_action
);
352 if ($this->_permission
== CRM_Core_Permission
::EDIT
&& !CRM_Core_Permission
::check('edit contributions')) {
353 // demote to view since user does not have edit contrib rights
354 $this->_permission
= CRM_Core_Permission
::VIEW
;
355 $this->assign('permission', 'view');
360 * the main function that is called when the page
361 * loads, it decides the which action has to be taken for the page.
365 public function run() {
368 // check if we can process credit card contribs
369 $this->assign('newCredit', CRM_Core_Config
::isEnabledBackOfficeCreditCardPayments());
373 if ($this->_action
& CRM_Core_Action
::VIEW
) {
376 elseif ($this->_action
& (CRM_Core_Action
::UPDATE | CRM_Core_Action
::ADD | CRM_Core_Action
::DELETE
)) {
383 return parent
::run();
386 public function setContext() {
387 $qfKey = CRM_Utils_Request
::retrieve('key', 'String', $this);
388 $context = CRM_Utils_Request
::retrieve('context', 'Alphanumeric',
389 $this, FALSE, 'search'
391 $compContext = CRM_Utils_Request
::retrieve('compContext', 'String', $this);
393 $searchContext = CRM_Utils_Request
::retrieve('searchContext', 'String', $this);
396 if ($context == 'search' && $compContext) {
397 $context = $compContext;
403 // make sure we dont get tricked with a bad key
405 if (!CRM_Core_Key
::valid($qfKey)) {
411 $url = CRM_Utils_System
::url('civicrm/user', 'reset=1');
415 $url = CRM_Utils_System
::url('civicrm/contribute',
420 case 'pledgeDashboard':
421 $url = CRM_Utils_System
::url('civicrm/pledge',
427 $url = CRM_Utils_System
::url('civicrm/contact/view',
428 "reset=1&force=1&cid={$this->_contactId}&selectedChild=contribute"
434 $extraParams = "force=1";
436 $extraParams .= "&qfKey=$qfKey";
439 $this->assign('searchKey', $qfKey);
440 if ($context == 'advanced') {
441 $url = CRM_Utils_System
::url('civicrm/contact/search/advanced', $extraParams);
443 elseif ($searchContext) {
444 $url = CRM_Utils_System
::url("civicrm/$searchContext/search", $extraParams);
447 $url = CRM_Utils_System
::url('civicrm/contribute/search', $extraParams);
452 $url = CRM_Utils_System
::url('civicrm/dashboard', 'reset=1');
456 $url = CRM_Utils_System
::url('civicrm/contact/view',
457 "reset=1&force=1&cid={$this->_contactId}&selectedChild=activity"
463 $componentId = CRM_Utils_Request
::retrieve('compId', 'Positive', $this);
464 $componentAction = CRM_Utils_Request
::retrieve('compAction', 'Integer', $this);
466 $context = 'membership';
471 $searchKey = "&key=$qfKey";
473 $compContext = "&compContext={$compContext}";
475 if ($componentAction & CRM_Core_Action
::VIEW
) {
481 $url = CRM_Utils_System
::url('civicrm/contact/view/membership',
482 "reset=1&action={$action}&id={$componentId}&cid={$this->_contactId}&context={$context}&selectedChild=member{$searchKey}{$compContext}"
487 $componentId = CRM_Utils_Request
::retrieve('compId', 'Positive', $this);
488 $componentAction = CRM_Utils_Request
::retrieve('compAction', 'Integer', $this);
490 $context = 'participant';
495 $searchKey = "&key=$qfKey";
497 $compContext = "&compContext={$compContext}";
499 if ($componentAction == CRM_Core_Action
::VIEW
) {
505 $url = CRM_Utils_System
::url('civicrm/contact/view/participant',
506 "reset=1&action={$action}&id={$componentId}&cid={$this->_contactId}&context={$context}&selectedChild=event{$searchKey}{$compContext}"
511 $url = CRM_Utils_System
::url('civicrm/contact/view',
512 "reset=1&force=1&cid={$this->_contactId}&selectedChild=pledge"
517 $url = CRM_Utils_System
::url('civicrm/dashboard', 'reset=1');
522 $urlParams = 'force=1';
523 $urlString = 'civicrm/contact/search/custom';
524 if ($this->_action
== CRM_Core_Action
::UPDATE
) {
525 if ($this->_contactId
) {
526 $urlParams .= '&cid=' . $this->_contactId
;
529 $urlParams .= '&context=fulltext&action=view';
530 $urlString = 'civicrm/contact/view/contribution';
533 $urlParams .= "$keyName=$qfKey";
535 $this->assign('searchKey', $qfKey);
536 $url = CRM_Utils_System
::url($urlString, $urlParams);
541 if ($this->_contactId
) {
542 $cid = '&cid=' . $this->_contactId
;
544 $url = CRM_Utils_System
::url('civicrm/contribute/search',
545 'reset=1&force=1' . $cid
550 $session = CRM_Core_Session
::singleton();
551 $session->pushUserContext($url);