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