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