Merge pull request #15283 from christianwach/lab-1200-3
[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 permission we have on this contact
37 *
38 * @var string
39 */
40 public $_permission = NULL;
41
42 /**
43 * The contact ID for the contributions we are acting on
44 * @var int
45 */
46 public $_contactId = NULL;
47
48 /**
49 * The recurring contribution ID (if any)
50 * @var int
51 */
52 public $_crid = NULL;
53
54 /**
55 * This method returns the links that are given for recur search row.
56 * currently the links added for each row are:
57 * - View
58 * - Edit
59 * - Cancel
60 *
61 * @param bool $recurID
62 * @param string $context
63 *
64 * @return array
65 */
66 public static function recurLinks($recurID = FALSE, $context = 'contribution') {
67 $links = [
68 CRM_Core_Action::VIEW => [
69 'name' => ts('View'),
70 'title' => ts('View Recurring Payment'),
71 'url' => 'civicrm/contact/view/contributionrecur',
72 'qs' => "reset=1&id=%%crid%%&cid=%%cid%%&context={$context}",
73 ],
74 CRM_Core_Action::UPDATE => [
75 'name' => ts('Edit'),
76 'title' => ts('Edit Recurring Payment'),
77 'url' => 'civicrm/contribute/updaterecur',
78 'qs' => "reset=1&action=update&crid=%%crid%%&cid=%%cid%%&context={$context}",
79 ],
80 CRM_Core_Action::DISABLE => [
81 'name' => ts('Cancel'),
82 'title' => ts('Cancel'),
83 'ref' => 'crm-enable-disable',
84 ],
85 ];
86
87 if ($recurID) {
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}";
94 }
95
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}",
102 ];
103 }
104
105 if (!$paymentProcessorObj->supports('ChangeSubscriptionAmount') && !$paymentProcessorObj->supports('EditRecurringContribution')) {
106 unset($links[CRM_Core_Action::UPDATE]);
107 }
108 }
109 else {
110 unset($links[CRM_Core_Action::DISABLE]);
111 unset($links[CRM_Core_Action::UPDATE]);
112 }
113 }
114
115 return $links;
116 }
117
118 /**
119 * called when action is browse.
120 *
121 */
122 public function browse() {
123 // add annual contribution
124 $annual = [];
125 list($annual['count'],
126 $annual['amount'],
127 $annual['avg']
128 ) = CRM_Contribute_BAO_Contribution::annual($this->_contactId);
129 $this->assign('annual', $annual);
130
131 $controller = new CRM_Core_Controller_Simple(
132 'CRM_Contribute_Form_Search',
133 ts('Contributions'),
134 $this->_action,
135 FALSE, FALSE, TRUE
136 );
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();
144 $controller->run();
145
146 // add recurring block
147 $this->addRecurringContributionsBlock();
148
149 // enable/disable soft credit records for test contribution
150 $isTest = 0;
151 if (CRM_Utils_Request::retrieve('isTest', 'Positive', $this)) {
152 $isTest = 1;
153 }
154 $this->assign('isTest', $isTest);
155
156 $softCreditList = CRM_Contribute_BAO_ContributionSoft::getSoftContributionList($this->_contactId, NULL, $isTest);
157
158 if (!empty($softCreditList)) {
159 $softCreditTotals = [];
160
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);
168
169 $this->assign('softCredit', TRUE);
170 $this->assign('softCreditRows', $softCreditList);
171 $this->assign('softCreditTotals', $softCreditTotals);
172 }
173
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;
180 }
181 }
182
183 /**
184 * Get all the recurring contribution information and assign to the template
185 */
186 private function addRecurringContributionsBlock() {
187 list($activeContributions, $activeContributionsCount) = $this->getActiveRecurringContributions();
188 list($inactiveRecurringContributions, $inactiveContributionsCount) = $this->getInactiveRecurringContributions();
189
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);
197 }
198 }
199
200 /**
201 * Loads active recurring contributions for the current contact and formats
202 * them to be used on the form.
203 *
204 * @return array
205 */
206 private function getActiveRecurringContributions() {
207 try {
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'],
212 ]);
213 $recurContributions = CRM_Utils_Array::value('values', $contributionRecurResult);
214 }
215 catch (Exception $e) {
216 $recurContributions = [];
217 }
218
219 return $this->buildRecurringContributionsArray($recurContributions);
220 }
221
222 /**
223 * Loads inactive recurring contributions for the current contact and formats
224 * them to be used on the form.
225 *
226 * @return array
227 */
228 private function getInactiveRecurringContributions() {
229 try {
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'],
234 ]);
235 $recurContributions = CRM_Utils_Array::value('values', $contributionRecurResult);
236 }
237 catch (Exception $e) {
238 $recurContributions = NULL;
239 }
240
241 return $this->buildRecurringContributionsArray($recurContributions);
242 }
243
244 /**
245 * @param $recurContributions
246 *
247 * @return array
248 */
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)));
256 }
257 else {
258 $actionMask = CRM_Core_Action::mask([CRM_Core_Permission::VIEW]);
259 }
260
261 if (empty($recurDetail['is_test'])) {
262 $liveRecurringContributionCount++;
263 }
264
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']);
268 }
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']);
272 }
273
274 $recurContributions[$recurId]['action'] = CRM_Core_Action::formLink(self::recurLinks($recurId), $actionMask,
275 [
276 'cid' => $this->_contactId,
277 'crid' => $recurId,
278 'cxt' => 'contribution',
279 ],
280 ts('more'),
281 FALSE,
282 'contribution.selector.recurring',
283 'Contribution',
284 $recurId
285 );
286 }
287
288 return [$recurContributions, $liveRecurringContributionCount];
289 }
290
291 /**
292 * called when action is view.
293 *
294 * @return mixed
295 */
296 public function view() {
297 $controller = new CRM_Core_Controller_Simple(
298 'CRM_Contribute_Form_ContributionView',
299 ts('View Contribution'),
300 $this->_action
301 );
302 $controller->setEmbedded(TRUE);
303 $controller->set('id', $this->_id);
304 $controller->set('cid', $this->_contactId);
305
306 return $controller->run();
307 }
308
309 /**
310 * called when action is update or new.
311 *
312 * @return mixed
313 * @throws \CRM_Core_Exception
314 * @throws \Exception
315 */
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();
321 }
322
323 $controller = new CRM_Core_Controller_Simple(
324 'CRM_Contribute_Form_Contribution',
325 'Create Contribution',
326 $this->_action
327 );
328 $controller->setEmbedded(TRUE);
329 $controller->set('id', $this->_id);
330 $controller->set('cid', $this->_contactId);
331
332 return $controller->run();
333 }
334
335 /**
336 * @throws \CRM_Core_Exception
337 * @throws \CiviCRM_API3_Exception
338 */
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);
343
344 if ($context == 'standalone') {
345 $this->_action = CRM_Core_Action::ADD;
346 }
347 else {
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', [
351 'id' => $this->_id,
352 'return' => 'contact_id',
353 ]);
354 }
355 $this->assign('contactId', $this->_contactId);
356
357 // check logged in url permission
358 CRM_Contact_Page_View::checkUserPermission($this);
359 }
360 $this->assign('action', $this->_action);
361
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');
366 }
367 }
368
369 /**
370 * the main function that is called when the page
371 * loads, it decides the which action has to be taken for the page.
372 *
373 * @throws \CRM_Core_Exception
374 * @throws \CiviCRM_API3_Exception
375 */
376 public function run() {
377 $this->preProcess();
378
379 // check if we can process credit card contribs
380 $this->assign('newCredit', CRM_Core_Config::isEnabledBackOfficeCreditCardPayments());
381
382 $this->setContext();
383
384 if ($this->_action & CRM_Core_Action::VIEW) {
385 $this->view();
386 }
387 elseif ($this->_action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD | CRM_Core_Action::DELETE)) {
388 $this->edit();
389 }
390 else {
391 $this->browse();
392 }
393
394 parent::run();
395 }
396
397 /**
398 * @throws \CRM_Core_Exception
399 */
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'
404 );
405 $compContext = CRM_Utils_Request::retrieve('compContext', 'String', $this);
406
407 $searchContext = CRM_Utils_Request::retrieve('searchContext', 'String', $this);
408
409 //swap the context.
410 if ($context == 'search' && $compContext) {
411 $context = $compContext;
412 }
413 else {
414 $compContext = NULL;
415 }
416
417 // make sure we dont get tricked with a bad key
418 // so check format
419 if (!CRM_Core_Key::valid($qfKey)) {
420 $qfKey = NULL;
421 }
422
423 switch ($context) {
424 case 'user':
425 $url = CRM_Utils_System::url('civicrm/user', 'reset=1');
426 break;
427
428 case 'dashboard':
429 $url = CRM_Utils_System::url('civicrm/contribute',
430 'reset=1'
431 );
432 break;
433
434 case 'pledgeDashboard':
435 $url = CRM_Utils_System::url('civicrm/pledge',
436 'reset=1'
437 );
438 break;
439
440 case 'contribution':
441 $url = CRM_Utils_System::url('civicrm/contact/view',
442 "reset=1&force=1&cid={$this->_contactId}&selectedChild=contribute"
443 );
444 break;
445
446 case 'search':
447 case 'advanced':
448 $extraParams = "force=1";
449 if ($qfKey) {
450 $extraParams .= "&qfKey=$qfKey";
451 }
452
453 $this->assign('searchKey', $qfKey);
454 if ($context == 'advanced') {
455 $url = CRM_Utils_System::url('civicrm/contact/search/advanced', $extraParams);
456 }
457 elseif ($searchContext) {
458 $url = CRM_Utils_System::url("civicrm/$searchContext/search", $extraParams);
459 }
460 else {
461 $url = CRM_Utils_System::url('civicrm/contribute/search', $extraParams);
462 }
463 break;
464
465 case 'home':
466 $url = CRM_Utils_System::url('civicrm/dashboard', 'reset=1');
467 break;
468
469 case 'activity':
470 $url = CRM_Utils_System::url('civicrm/contact/view',
471 "reset=1&force=1&cid={$this->_contactId}&selectedChild=activity"
472 );
473 break;
474
475 case 'member':
476 case 'membership':
477 $componentId = CRM_Utils_Request::retrieve('compId', 'Positive', $this);
478 $componentAction = CRM_Utils_Request::retrieve('compAction', 'Integer', $this);
479
480 $context = 'membership';
481 $searchKey = NULL;
482 if ($compContext) {
483 $context = 'search';
484 if ($qfKey) {
485 $searchKey = "&key=$qfKey";
486 }
487 $compContext = "&compContext={$compContext}";
488 }
489 if ($componentAction & CRM_Core_Action::VIEW) {
490 $action = 'view';
491 }
492 else {
493 $action = 'update';
494 }
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}"
497 );
498 break;
499
500 case 'participant':
501 $componentId = CRM_Utils_Request::retrieve('compId', 'Positive', $this);
502 $componentAction = CRM_Utils_Request::retrieve('compAction', 'Integer', $this);
503
504 $context = 'participant';
505 $searchKey = NULL;
506 if ($compContext) {
507 $context = 'search';
508 if ($qfKey) {
509 $searchKey = "&key=$qfKey";
510 }
511 $compContext = "&compContext={$compContext}";
512 }
513 if ($componentAction == CRM_Core_Action::VIEW) {
514 $action = 'view';
515 }
516 else {
517 $action = 'update';
518 }
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}"
521 );
522 break;
523
524 case 'pledge':
525 $url = CRM_Utils_System::url('civicrm/contact/view',
526 "reset=1&force=1&cid={$this->_contactId}&selectedChild=pledge"
527 );
528 break;
529
530 case 'standalone':
531 $url = CRM_Utils_System::url('civicrm/dashboard', 'reset=1');
532 break;
533
534 case 'fulltext':
535 $keyName = '&qfKey';
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;
541 }
542 $keyName = '&key';
543 $urlParams .= '&context=fulltext&action=view';
544 $urlString = 'civicrm/contact/view/contribution';
545 }
546 if ($qfKey) {
547 $urlParams .= "$keyName=$qfKey";
548 }
549 $this->assign('searchKey', $qfKey);
550 $url = CRM_Utils_System::url($urlString, $urlParams);
551 break;
552
553 default:
554 $cid = NULL;
555 if ($this->_contactId) {
556 $cid = '&cid=' . $this->_contactId;
557 }
558 $url = CRM_Utils_System::url('civicrm/contribute/search',
559 'reset=1&force=1' . $cid
560 );
561 break;
562 }
563
564 $session = CRM_Core_Session::singleton();
565 $session->pushUserContext($url);
566 }
567
568 }