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