Merge pull request #13974 from eileenmcnaughton/array_format7
[civicrm-core.git] / CRM / Contribute / Page / Tab.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
6b83d5bd 31 * @copyright CiviCRM LLC (c) 2004-2019
6a488035
TO
32 */
33class CRM_Contribute_Page_Tab extends CRM_Core_Page {
34
35 /**
fe482240 36 * The action links that we need to display for the browse screen.
6a488035
TO
37 *
38 * @var array
6a488035
TO
39 */
40 static $_links = NULL;
6a488035
TO
41 static $_recurLinks = NULL;
42 public $_permission = NULL;
43 public $_contactId = NULL;
44 public $_crid = NULL;
45
6a488035
TO
46 /**
47 * This method returns the links that are given for recur search row.
48 * currently the links added for each row are:
49 * - View
50 * - Edit
51 * - Cancel
52 *
6c8f6e67
EM
53 * @param bool $recurID
54 * @param string $context
55 *
6a488035 56 * @return array
6a488035 57 */
00be9182 58 public static function &recurLinks($recurID = FALSE, $context = 'contribution') {
6a488035 59 if (!(self::$_links)) {
be2fb01f
CW
60 self::$_links = [
61 CRM_Core_Action::VIEW => [
6a488035
TO
62 'name' => ts('View'),
63 'title' => ts('View Recurring Payment'),
64 'url' => 'civicrm/contact/view/contributionrecur',
65 'qs' => "reset=1&id=%%crid%%&cid=%%cid%%&context={$context}",
be2fb01f
CW
66 ],
67 CRM_Core_Action::UPDATE => [
6a488035
TO
68 'name' => ts('Edit'),
69 'title' => ts('Edit Recurring Payment'),
70 'url' => 'civicrm/contribute/updaterecur',
71 'qs' => "reset=1&action=update&crid=%%crid%%&cid=%%cid%%&context={$context}",
be2fb01f
CW
72 ],
73 CRM_Core_Action::DISABLE => [
6a488035
TO
74 'name' => ts('Cancel'),
75 'title' => ts('Cancel'),
4d17a233 76 'ref' => 'crm-enable-disable',
be2fb01f
CW
77 ],
78 ];
6a488035
TO
79 }
80
81 if ($recurID) {
afcaa7e9 82 $links = self::$_links;
87011153 83 $paymentProcessorObj = CRM_Contribute_BAO_ContributionRecur::getPaymentProcessorObject($recurID);
84 if (!$paymentProcessorObj) {
c6d558dc
MWMC
85 unset($links[CRM_Core_Action::DISABLE]);
86 unset($links[CRM_Core_Action::UPDATE]);
87 return $links;
88 }
89 if ($paymentProcessorObj->supports('cancelRecurring')) {
afcaa7e9 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}";
6a488035 93 }
1524a007 94
c6d558dc 95 if ($paymentProcessorObj->supports('UpdateSubscriptionBillingInfo')) {
be2fb01f 96 $links[CRM_Core_Action::RENEW] = [
353ffa53 97 'name' => ts('Change Billing Details'),
6a488035
TO
98 'title' => ts('Change Billing Details'),
99 'url' => 'civicrm/contribute/updatebilling',
100 'qs' => "reset=1&crid=%%crid%%&cid=%%cid%%&context={$context}",
be2fb01f 101 ];
6a488035 102 }
c6d558dc
MWMC
103
104 if (!$paymentProcessorObj->supports('ChangeSubscriptionAmount') && !$paymentProcessorObj->supports('EditRecurringContribution')) {
105 unset($links[CRM_Core_Action::UPDATE]);
106 }
afcaa7e9 107 return $links;
6a488035
TO
108 }
109
110 return self::$_links;
111 }
6a488035
TO
112
113 /**
fe482240 114 * called when action is browse.
6a488035 115 *
6a488035 116 */
00be9182 117 public function browse() {
6a488035 118 // add annual contribution
be2fb01f 119 $annual = [];
6a488035
TO
120 list($annual['count'],
121 $annual['amount'],
122 $annual['avg']
353ffa53 123 ) = CRM_Contribute_BAO_Contribution::annual($this->_contactId);
6a488035
TO
124 $this->assign('annual', $annual);
125
126 $controller = new CRM_Core_Controller_Simple(
127 'CRM_Contribute_Form_Search',
128 ts('Contributions'),
129 $this->_action,
130 FALSE, FALSE, TRUE
131 );
132 $controller->setEmbedded(TRUE);
133 $controller->reset();
134 $controller->set('cid', $this->_contactId);
135 $controller->set('crid', $this->_crid);
7a49693c 136 $controller->set('context', 'contribution');
c2874d9c 137 $controller->set('limit', 50);
6a488035
TO
138 $controller->process();
139 $controller->run();
140
141 // add recurring block
46097c9c
MW
142 $this->addRecurringContributionsBlock();
143
144 // enable/disable soft credit records for test contribution
145 $isTest = 0;
146 if (CRM_Utils_Request::retrieve('isTest', 'Positive', $this)) {
147 $isTest = 1;
148 }
149 $this->assign('isTest', $isTest);
150
151 $softCreditList = CRM_Contribute_BAO_ContributionSoft::getSoftContributionList($this->_contactId, NULL, $isTest);
152
153 if (!empty($softCreditList)) {
be2fb01f 154 $softCreditTotals = [];
46097c9c 155
d9c8c3c8
PF
156 list($softCreditTotals['count'],
157 $softCreditTotals['cancel']['count'],
158 $softCreditTotals['amount'],
46097c9c 159 $softCreditTotals['avg'],
d9c8c3c8 160 $softCreditTotals['cancel']['amount'] // to get cancel amount
46097c9c
MW
161 ) = CRM_Contribute_BAO_ContributionSoft::getSoftContributionTotals($this->_contactId, $isTest);
162
163 $this->assign('softCredit', TRUE);
164 $this->assign('softCreditRows', $softCreditList);
165 $this->assign('softCreditTotals', $softCreditTotals);
166 }
167
168 if ($this->_contactId) {
169 $displayName = CRM_Contact_BAO_Contact::displayName($this->_contactId);
170 $this->assign('displayName', $displayName);
7255972e
MWMC
171 $tabCount = CRM_Contact_BAO_Contact::getCountComponent('contribution', $this->_contactId);
172 $this->assign('tabCount', $tabCount);
173 $this->ajaxResponse['tabCount'] = $tabCount;
46097c9c
MW
174 }
175 }
176
177 /**
178 * Get all the recurring contribution information and assign to the template
179 */
180 private function addRecurringContributionsBlock() {
e2ef9c65
PN
181 list($activeContributions, $activeContributionsCount) = $this->getActiveRecurringContributions();
182 list($inactiveRecurringContributions, $inactiveContributionsCount) = $this->getInactiveRecurringContributions();
5963b706
CR
183
184 if (!empty($activeContributions) || !empty($inactiveRecurringContributions)) {
185 // assign vars to templates
186 $this->assign('action', $this->_action);
187 $this->assign('activeRecurRows', $activeContributions);
51e31e45 188 $this->assign('contributionRecurCount', $activeContributionsCount + $inactiveContributionsCount);
5963b706
CR
189 $this->assign('inactiveRecurRows', $inactiveRecurringContributions);
190 $this->assign('recur', TRUE);
191 }
192 }
193
194 /**
195 * Loads active recurring contributions for the current contact and formats
196 * them to be used on the form.
197 *
198 * @return array;
199 */
200 private function getActiveRecurringContributions() {
9e475c2d 201 try {
be2fb01f 202 $contributionRecurResult = civicrm_api3('ContributionRecur', 'get', [
9e475c2d 203 'contact_id' => $this->_contactId,
be2fb01f 204 'contribution_status_id' => ['NOT IN' => CRM_Contribute_BAO_ContributionRecur::getInactiveStatuses()],
0b3146b3 205 'options' => ['limit' => 0, 'sort' => 'is_test, start_date DESC'],
be2fb01f 206 ]);
5963b706
CR
207 $recurContributions = CRM_Utils_Array::value('values', $contributionRecurResult);
208 }
209 catch (Exception $e) {
be2fb01f 210 $recurContributions = [];
5963b706
CR
211 }
212
213 return $this->buildRecurringContributionsArray($recurContributions);
214 }
215
216 /**
217 * Loads inactive recurring contributions for the current contact and formats
218 * them to be used on the form.
219 *
220 * @return array;
221 */
222 private function getInactiveRecurringContributions() {
9e475c2d 223 try {
be2fb01f 224 $contributionRecurResult = civicrm_api3('ContributionRecur', 'get', [
9e475c2d 225 'contact_id' => $this->_contactId,
be2fb01f 226 'contribution_status_id' => ['IN' => CRM_Contribute_BAO_ContributionRecur::getInactiveStatuses()],
0b3146b3 227 'options' => ['limit' => 0, 'sort' => 'is_test, start_date DESC'],
be2fb01f 228 ]);
9e475c2d
MW
229 $recurContributions = CRM_Utils_Array::value('values', $contributionRecurResult);
230 }
231 catch (Exception $e) {
232 $recurContributions = NULL;
233 }
6a488035 234
5963b706
CR
235 return $this->buildRecurringContributionsArray($recurContributions);
236 }
6a488035 237
5963b706
CR
238 /**
239 * @param $recurContributions
240 *
241 * @return mixed
242 */
243 private function buildRecurringContributionsArray($recurContributions) {
e2ef9c65 244 $liveRecurringContributionCount = 0;
5963b706 245 foreach ($recurContributions as $recurId => $recurDetail) {
44c118da
MWMC
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)));
250 }
251 else {
252 $actionMask = CRM_Core_Action::mask([CRM_Core_Permission::VIEW]);
253 }
46097c9c 254
e2ef9c65
PN
255 if (empty($recurDetail['is_test'])) {
256 $liveRecurringContributionCount++;
257 }
258
5963b706
CR
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']);
262 }
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']);
266 }
6a488035 267
44c118da 268 $recurContributions[$recurId]['action'] = CRM_Core_Action::formLink(self::recurLinks($recurId), $actionMask,
be2fb01f 269 [
44c118da
MWMC
270 'cid' => $this->_contactId,
271 'crid' => $recurId,
272 'cxt' => 'contribution',
be2fb01f 273 ],
44c118da
MWMC
274 ts('more'),
275 FALSE,
276 'contribution.selector.recurring',
277 'Contribution',
278 $recurId
279 );
6a488035 280 }
5963b706 281
e2ef9c65 282 return [$recurContributions, $liveRecurringContributionCount];
6a488035
TO
283 }
284
285 /**
fe482240 286 * called when action is view.
6a488035 287 *
9e475c2d 288 * @return mixed
6a488035 289 */
00be9182 290 public function view() {
6a488035
TO
291 $controller = new CRM_Core_Controller_Simple(
292 'CRM_Contribute_Form_ContributionView',
293 ts('View Contribution'),
294 $this->_action
295 );
296 $controller->setEmbedded(TRUE);
297 $controller->set('id', $this->_id);
298 $controller->set('cid', $this->_contactId);
299
300 return $controller->run();
301 }
302
303 /**
fe482240 304 * called when action is update or new.
6a488035 305 *
9e475c2d
MW
306 * @return mixed
307 * @throws \CRM_Core_Exception
308 * @throws \Exception
6a488035 309 */
00be9182 310 public function edit() {
6a488035 311 // set https for offline cc transaction
19046166 312 $mode = CRM_Utils_Request::retrieve('mode', 'Alphanumeric', $this);
6a488035
TO
313 if ($mode == 'test' || $mode == 'live') {
314 CRM_Utils_System::redirectToSSL();
315 }
316
317 $controller = new CRM_Core_Controller_Simple(
318 'CRM_Contribute_Form_Contribution',
319 'Create Contribution',
320 $this->_action
321 );
322 $controller->setEmbedded(TRUE);
323 $controller->set('id', $this->_id);
324 $controller->set('cid', $this->_contactId);
325
326 return $controller->run();
327 }
328
00be9182 329 public function preProcess() {
edc80cda 330 $context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this);
6a488035 331 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'browse');
353ffa53 332 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
6a488035
TO
333
334 if ($context == 'standalone') {
335 $this->_action = CRM_Core_Action::ADD;
336 }
337 else {
f9009fe0
EM
338 $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, empty($this->_id));
339 if (empty($this->_contactId)) {
be2fb01f 340 $this->_contactId = civicrm_api3('contribution', 'getvalue', [
353ffa53 341 'id' => $this->_id,
acb1052e 342 'return' => 'contact_id',
be2fb01f 343 ]);
f9009fe0 344 }
6a488035
TO
345 $this->assign('contactId', $this->_contactId);
346
347 // check logged in url permission
348 CRM_Contact_Page_View::checkUserPermission($this);
6a488035
TO
349 }
350 $this->assign('action', $this->_action);
351
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');
356 }
357 }
358
359 /**
dc195289 360 * the main function that is called when the page
6a488035
TO
361 * loads, it decides the which action has to be taken for the page.
362 *
76e7a76c 363 * @return null
6a488035 364 */
00be9182 365 public function run() {
6a488035
TO
366 $this->preProcess();
367
368 // check if we can process credit card contribs
9be1374d 369 $this->assign('newCredit', CRM_Core_Config::isEnabledBackOfficeCreditCardPayments());
6a488035
TO
370
371 $this->setContext();
372
373 if ($this->_action & CRM_Core_Action::VIEW) {
374 $this->view();
375 }
376 elseif ($this->_action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD | CRM_Core_Action::DELETE)) {
377 $this->edit();
378 }
379 else {
380 $this->browse();
381 }
382
383 return parent::run();
384 }
385
00be9182 386 public function setContext() {
6a488035 387 $qfKey = CRM_Utils_Request::retrieve('key', 'String', $this);
edc80cda 388 $context = CRM_Utils_Request::retrieve('context', 'Alphanumeric',
6a488035
TO
389 $this, FALSE, 'search'
390 );
391 $compContext = CRM_Utils_Request::retrieve('compContext', 'String', $this);
392
29571f63
AS
393 $searchContext = CRM_Utils_Request::retrieve('searchContext', 'String', $this);
394
6a488035
TO
395 //swap the context.
396 if ($context == 'search' && $compContext) {
397 $context = $compContext;
398 }
399 else {
400 $compContext = NULL;
401 }
402
403 // make sure we dont get tricked with a bad key
404 // so check format
405 if (!CRM_Core_Key::valid($qfKey)) {
406 $qfKey = NULL;
407 }
408
6a488035
TO
409 switch ($context) {
410 case 'user':
411 $url = CRM_Utils_System::url('civicrm/user', 'reset=1');
412 break;
413
414 case 'dashboard':
415 $url = CRM_Utils_System::url('civicrm/contribute',
416 'reset=1'
417 );
418 break;
419
420 case 'pledgeDashboard':
421 $url = CRM_Utils_System::url('civicrm/pledge',
422 'reset=1'
423 );
424 break;
425
426 case 'contribution':
6a488035 427 $url = CRM_Utils_System::url('civicrm/contact/view',
8381af80 428 "reset=1&force=1&cid={$this->_contactId}&selectedChild=contribute"
6a488035
TO
429 );
430 break;
431
432 case 'search':
433 case 'advanced':
434 $extraParams = "force=1";
435 if ($qfKey) {
436 $extraParams .= "&qfKey=$qfKey";
437 }
438
439 $this->assign('searchKey', $qfKey);
440 if ($context == 'advanced') {
441 $url = CRM_Utils_System::url('civicrm/contact/search/advanced', $extraParams);
442 }
4c9b6178 443 elseif ($searchContext) {
29571f63
AS
444 $url = CRM_Utils_System::url("civicrm/$searchContext/search", $extraParams);
445 }
6a488035
TO
446 else {
447 $url = CRM_Utils_System::url('civicrm/contribute/search', $extraParams);
448 }
449 break;
450
451 case 'home':
452 $url = CRM_Utils_System::url('civicrm/dashboard', 'reset=1');
453 break;
454
455 case 'activity':
456 $url = CRM_Utils_System::url('civicrm/contact/view',
457 "reset=1&force=1&cid={$this->_contactId}&selectedChild=activity"
458 );
459 break;
460
461 case 'member':
462 case 'membership':
463 $componentId = CRM_Utils_Request::retrieve('compId', 'Positive', $this);
464 $componentAction = CRM_Utils_Request::retrieve('compAction', 'Integer', $this);
465
466 $context = 'membership';
467 $searchKey = NULL;
468 if ($compContext) {
469 $context = 'search';
470 if ($qfKey) {
471 $searchKey = "&key=$qfKey";
472 }
473 $compContext = "&compContext={$compContext}";
474 }
475 if ($componentAction & CRM_Core_Action::VIEW) {
476 $action = 'view';
477 }
478 else {
479 $action = 'update';
480 }
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}"
483 );
484 break;
485
486 case 'participant':
487 $componentId = CRM_Utils_Request::retrieve('compId', 'Positive', $this);
488 $componentAction = CRM_Utils_Request::retrieve('compAction', 'Integer', $this);
489
490 $context = 'participant';
491 $searchKey = NULL;
492 if ($compContext) {
493 $context = 'search';
494 if ($qfKey) {
495 $searchKey = "&key=$qfKey";
496 }
497 $compContext = "&compContext={$compContext}";
498 }
499 if ($componentAction == CRM_Core_Action::VIEW) {
500 $action = 'view';
501 }
502 else {
503 $action = 'update';
504 }
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}"
507 );
508 break;
509
510 case 'pledge':
511 $url = CRM_Utils_System::url('civicrm/contact/view',
512 "reset=1&force=1&cid={$this->_contactId}&selectedChild=pledge"
513 );
514 break;
515
516 case 'standalone':
517 $url = CRM_Utils_System::url('civicrm/dashboard', 'reset=1');
518 break;
519
520 case 'fulltext':
353ffa53 521 $keyName = '&qfKey';
6a488035
TO
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;
527 }
528 $keyName = '&key';
529 $urlParams .= '&context=fulltext&action=view';
530 $urlString = 'civicrm/contact/view/contribution';
531 }
532 if ($qfKey) {
533 $urlParams .= "$keyName=$qfKey";
534 }
535 $this->assign('searchKey', $qfKey);
536 $url = CRM_Utils_System::url($urlString, $urlParams);
537 break;
538
539 default:
540 $cid = NULL;
541 if ($this->_contactId) {
542 $cid = '&cid=' . $this->_contactId;
543 }
544 $url = CRM_Utils_System::url('civicrm/contribute/search',
545 'reset=1&force=1' . $cid
546 );
547 break;
548 }
549
550 $session = CRM_Core_Session::singleton();
551 $session->pushUserContext($url);
552 }
96025800 553
6a488035 554}