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