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 permission we have on this contact
40 public $_permission = NULL;
43 * The contact ID for the contributions we are acting on
46 public $_contactId = NULL;
49 * The recurring contribution ID (if any)
55 * This method returns the links that are given for recur search row.
56 * currently the links added for each row are:
61 * @param bool $recurID
62 * @param string $context
66 public static function recurLinks($recurID = FALSE, $context = 'contribution') {
68 CRM_Core_Action
::VIEW
=> [
70 'title' => ts('View Recurring Payment'),
71 'url' => 'civicrm/contact/view/contributionrecur',
72 'qs' => "reset=1&id=%%crid%%&cid=%%cid%%&context={$context}",
74 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}",
80 CRM_Core_Action
::DISABLE
=> [
81 'name' => ts('Cancel'),
82 'title' => ts('Cancel'),
83 'ref' => 'crm-enable-disable',
88 $paymentProcessorObj = CRM_Contribute_BAO_ContributionRecur
::getPaymentProcessorObject($recurID);
89 if ($paymentProcessorObj) {
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}",
105 if (!$paymentProcessorObj->supports('ChangeSubscriptionAmount') && !$paymentProcessorObj->supports('EditRecurringContribution')) {
106 unset($links[CRM_Core_Action
::UPDATE
]);
110 unset($links[CRM_Core_Action
::DISABLE
]);
111 unset($links[CRM_Core_Action
::UPDATE
]);
119 * called when action is browse.
122 public function browse() {
123 // add annual contribution
125 list($annual['count'],
128 ) = CRM_Contribute_BAO_Contribution
::annual($this->_contactId
);
129 $this->assign('annual', $annual);
131 $controller = new CRM_Core_Controller_Simple(
132 'CRM_Contribute_Form_Search',
137 $controller->setEmbedded(TRUE);
138 $controller->reset();
139 $controller->set('cid', $this->_contactId
);
140 $controller->set('crid', $this->_crid
);
141 $controller->set('context', 'contribution');
142 $controller->set('limit', 50);
143 $controller->process();
146 // add recurring block
147 $this->addRecurringContributionsBlock();
149 // enable/disable soft credit records for test contribution
151 if (CRM_Utils_Request
::retrieve('isTest', 'Positive', $this)) {
154 $this->assign('isTest', $isTest);
156 $softCreditList = CRM_Contribute_BAO_ContributionSoft
::getSoftContributionList($this->_contactId
, NULL, $isTest);
158 if (!empty($softCreditList)) {
159 $softCreditTotals = [];
161 list($softCreditTotals['count'],
162 $softCreditTotals['cancel']['count'],
163 $softCreditTotals['amount'],
164 $softCreditTotals['avg'],
165 // to get cancel amount
166 $softCreditTotals['cancel']['amount']
167 ) = CRM_Contribute_BAO_ContributionSoft
::getSoftContributionTotals($this->_contactId
, $isTest);
169 $this->assign('softCredit', TRUE);
170 $this->assign('softCreditRows', $softCreditList);
171 $this->assign('softCreditTotals', $softCreditTotals);
174 if ($this->_contactId
) {
175 $displayName = CRM_Contact_BAO_Contact
::displayName($this->_contactId
);
176 $this->assign('displayName', $displayName);
177 $tabCount = CRM_Contact_BAO_Contact
::getCountComponent('contribution', $this->_contactId
);
178 $this->assign('tabCount', $tabCount);
179 $this->ajaxResponse
['tabCount'] = $tabCount;
184 * Get all the recurring contribution information and assign to the template
186 private function addRecurringContributionsBlock() {
187 list($activeContributions, $activeContributionsCount) = $this->getActiveRecurringContributions();
188 list($inactiveRecurringContributions, $inactiveContributionsCount) = $this->getInactiveRecurringContributions();
190 if (!empty($activeContributions) ||
!empty($inactiveRecurringContributions)) {
191 // assign vars to templates
192 $this->assign('action', $this->_action
);
193 $this->assign('activeRecurRows', $activeContributions);
194 $this->assign('contributionRecurCount', $activeContributionsCount +
$inactiveContributionsCount);
195 $this->assign('inactiveRecurRows', $inactiveRecurringContributions);
196 $this->assign('recur', TRUE);
201 * Loads active recurring contributions for the current contact and formats
202 * them to be used on the form.
206 private function getActiveRecurringContributions() {
208 $contributionRecurResult = civicrm_api3('ContributionRecur', 'get', [
209 'contact_id' => $this->_contactId
,
210 'contribution_status_id' => ['NOT IN' => CRM_Contribute_BAO_ContributionRecur
::getInactiveStatuses()],
211 'options' => ['limit' => 0, 'sort' => 'is_test, start_date DESC'],
213 $recurContributions = CRM_Utils_Array
::value('values', $contributionRecurResult);
215 catch (Exception
$e) {
216 $recurContributions = [];
219 return $this->buildRecurringContributionsArray($recurContributions);
223 * Loads inactive recurring contributions for the current contact and formats
224 * them to be used on the form.
228 private function getInactiveRecurringContributions() {
230 $contributionRecurResult = civicrm_api3('ContributionRecur', 'get', [
231 'contact_id' => $this->_contactId
,
232 'contribution_status_id' => ['IN' => CRM_Contribute_BAO_ContributionRecur
::getInactiveStatuses()],
233 'options' => ['limit' => 0, 'sort' => 'is_test, start_date DESC'],
235 $recurContributions = CRM_Utils_Array
::value('values', $contributionRecurResult);
237 catch (Exception
$e) {
238 $recurContributions = NULL;
241 return $this->buildRecurringContributionsArray($recurContributions);
245 * @param $recurContributions
249 private function buildRecurringContributionsArray($recurContributions) {
250 $liveRecurringContributionCount = 0;
251 foreach ($recurContributions as $recurId => $recurDetail) {
252 // Is recurring contribution active?
253 $recurContributions[$recurId]['is_active'] = !in_array(CRM_Contribute_PseudoConstant
::contributionStatus($recurDetail['contribution_status_id'], 'name'), CRM_Contribute_BAO_ContributionRecur
::getInactiveStatuses());
254 if ($recurContributions[$recurId]['is_active']) {
255 $actionMask = array_sum(array_keys(self
::recurLinks($recurId)));
258 $actionMask = CRM_Core_Action
::mask([CRM_Core_Permission
::VIEW
]);
261 if (empty($recurDetail['is_test'])) {
262 $liveRecurringContributionCount++
;
265 // Get the name of the payment processor
266 if (!empty($recurDetail['payment_processor_id'])) {
267 $recurContributions[$recurId]['payment_processor'] = CRM_Financial_BAO_PaymentProcessor
::getPaymentProcessorName($recurDetail['payment_processor_id']);
269 // Get the label for the contribution status
270 if (!empty($recurDetail['contribution_status_id'])) {
271 $recurContributions[$recurId]['contribution_status'] = CRM_Core_PseudoConstant
::getLabel('CRM_Contribute_BAO_ContributionRecur', 'contribution_status_id', $recurDetail['contribution_status_id']);
274 $recurContributions[$recurId]['action'] = CRM_Core_Action
::formLink(self
::recurLinks($recurId), $actionMask,
276 'cid' => $this->_contactId
,
278 'cxt' => 'contribution',
282 'contribution.selector.recurring',
288 return [$recurContributions, $liveRecurringContributionCount];
292 * called when action is view.
296 public function view() {
297 $controller = new CRM_Core_Controller_Simple(
298 'CRM_Contribute_Form_ContributionView',
299 ts('View Contribution'),
302 $controller->setEmbedded(TRUE);
303 $controller->set('id', $this->_id
);
304 $controller->set('cid', $this->_contactId
);
306 return $controller->run();
310 * called when action is update or new.
313 * @throws \CRM_Core_Exception
316 public function edit() {
317 // set https for offline cc transaction
318 $mode = CRM_Utils_Request
::retrieve('mode', 'Alphanumeric', $this);
319 if ($mode == 'test' ||
$mode == 'live') {
320 CRM_Utils_System
::redirectToSSL();
323 $controller = new CRM_Core_Controller_Simple(
324 'CRM_Contribute_Form_Contribution',
325 'Create Contribution',
328 $controller->setEmbedded(TRUE);
329 $controller->set('id', $this->_id
);
330 $controller->set('cid', $this->_contactId
);
332 return $controller->run();
336 * @throws \CRM_Core_Exception
337 * @throws \CiviCRM_API3_Exception
339 public function preProcess() {
340 $context = CRM_Utils_Request
::retrieve('context', 'Alphanumeric', $this);
341 $this->_action
= CRM_Utils_Request
::retrieve('action', 'String', $this, FALSE, 'browse');
342 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive', $this);
344 if ($context == 'standalone') {
345 $this->_action
= CRM_Core_Action
::ADD
;
348 $this->_contactId
= CRM_Utils_Request
::retrieve('cid', 'Positive', $this, empty($this->_id
));
349 if (empty($this->_contactId
)) {
350 $this->_contactId
= civicrm_api3('contribution', 'getvalue', [
352 'return' => 'contact_id',
355 $this->assign('contactId', $this->_contactId
);
357 // check logged in url permission
358 CRM_Contact_Page_View
::checkUserPermission($this);
360 $this->assign('action', $this->_action
);
362 if ($this->_permission
== CRM_Core_Permission
::EDIT
&& !CRM_Core_Permission
::check('edit contributions')) {
363 // demote to view since user does not have edit contrib rights
364 $this->_permission
= CRM_Core_Permission
::VIEW
;
365 $this->assign('permission', 'view');
370 * the main function that is called when the page
371 * loads, it decides the which action has to be taken for the page.
373 * @throws \CRM_Core_Exception
374 * @throws \CiviCRM_API3_Exception
376 public function run() {
379 // check if we can process credit card contribs
380 $this->assign('newCredit', CRM_Core_Config
::isEnabledBackOfficeCreditCardPayments());
384 if ($this->_action
& CRM_Core_Action
::VIEW
) {
387 elseif ($this->_action
& (CRM_Core_Action
::UPDATE | CRM_Core_Action
::ADD | CRM_Core_Action
::DELETE
)) {
398 * @throws \CRM_Core_Exception
400 public function setContext() {
401 $qfKey = CRM_Utils_Request
::retrieve('key', 'String', $this);
402 $context = CRM_Utils_Request
::retrieve('context', 'Alphanumeric',
403 $this, FALSE, 'search'
405 $compContext = CRM_Utils_Request
::retrieve('compContext', 'String', $this);
407 $searchContext = CRM_Utils_Request
::retrieve('searchContext', 'String', $this);
410 if ($context == 'search' && $compContext) {
411 $context = $compContext;
417 // make sure we dont get tricked with a bad key
419 if (!CRM_Core_Key
::valid($qfKey)) {
425 $url = CRM_Utils_System
::url('civicrm/user', 'reset=1');
429 $url = CRM_Utils_System
::url('civicrm/contribute',
434 case 'pledgeDashboard':
435 $url = CRM_Utils_System
::url('civicrm/pledge',
441 $url = CRM_Utils_System
::url('civicrm/contact/view',
442 "reset=1&force=1&cid={$this->_contactId}&selectedChild=contribute"
448 $extraParams = "force=1";
450 $extraParams .= "&qfKey=$qfKey";
453 $this->assign('searchKey', $qfKey);
454 if ($context == 'advanced') {
455 $url = CRM_Utils_System
::url('civicrm/contact/search/advanced', $extraParams);
457 elseif ($searchContext) {
458 $url = CRM_Utils_System
::url("civicrm/$searchContext/search", $extraParams);
461 $url = CRM_Utils_System
::url('civicrm/contribute/search', $extraParams);
466 $url = CRM_Utils_System
::url('civicrm/dashboard', 'reset=1');
470 $url = CRM_Utils_System
::url('civicrm/contact/view',
471 "reset=1&force=1&cid={$this->_contactId}&selectedChild=activity"
477 $componentId = CRM_Utils_Request
::retrieve('compId', 'Positive', $this);
478 $componentAction = CRM_Utils_Request
::retrieve('compAction', 'Integer', $this);
480 $context = 'membership';
485 $searchKey = "&key=$qfKey";
487 $compContext = "&compContext={$compContext}";
489 if ($componentAction & CRM_Core_Action
::VIEW
) {
495 $url = CRM_Utils_System
::url('civicrm/contact/view/membership',
496 "reset=1&action={$action}&id={$componentId}&cid={$this->_contactId}&context={$context}&selectedChild=member{$searchKey}{$compContext}"
501 $componentId = CRM_Utils_Request
::retrieve('compId', 'Positive', $this);
502 $componentAction = CRM_Utils_Request
::retrieve('compAction', 'Integer', $this);
504 $context = 'participant';
509 $searchKey = "&key=$qfKey";
511 $compContext = "&compContext={$compContext}";
513 if ($componentAction == CRM_Core_Action
::VIEW
) {
519 $url = CRM_Utils_System
::url('civicrm/contact/view/participant',
520 "reset=1&action={$action}&id={$componentId}&cid={$this->_contactId}&context={$context}&selectedChild=event{$searchKey}{$compContext}"
525 $url = CRM_Utils_System
::url('civicrm/contact/view',
526 "reset=1&force=1&cid={$this->_contactId}&selectedChild=pledge"
531 $url = CRM_Utils_System
::url('civicrm/dashboard', 'reset=1');
536 $urlParams = 'force=1';
537 $urlString = 'civicrm/contact/search/custom';
538 if ($this->_action
== CRM_Core_Action
::UPDATE
) {
539 if ($this->_contactId
) {
540 $urlParams .= '&cid=' . $this->_contactId
;
543 $urlParams .= '&context=fulltext&action=view';
544 $urlString = 'civicrm/contact/view/contribution';
547 $urlParams .= "$keyName=$qfKey";
549 $this->assign('searchKey', $qfKey);
550 $url = CRM_Utils_System
::url($urlString, $urlParams);
555 if ($this->_contactId
) {
556 $cid = '&cid=' . $this->_contactId
;
558 $url = CRM_Utils_System
::url('civicrm/contribute/search',
559 'reset=1&force=1' . $cid
564 $session = CRM_Core_Session
::singleton();
565 $session->pushUserContext($url);