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