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