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