Merge branch 'master' into master-civimail-abtest
[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 /**
50 * This method returns the links that are given for recur search row.
51 * currently the links added for each row are:
52 * - View
53 * - Edit
54 * - Cancel
55 *
56 * @param bool $recurID
57 * @param string $context
58 *
59 * @return array
60 * @access public
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', 'contribution');
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, NULL, $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, empty($this->_id));
256 if (empty($this->_contactId)) {
257 $this->_contactId = civicrm_api3('contribution', 'getvalue', array('id' => $this->_id, 'return' => 'contact_id'));
258 }
259 $this->assign('contactId', $this->_contactId);
260
261 // check logged in url permission
262 CRM_Contact_Page_View::checkUserPermission($this);
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 $this->assign('newCredit', CRM_Core_Config::isEnabledBackOfficeCreditCardPayments());
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 $searchContext = CRM_Utils_Request::retrieve('searchContext', 'String', $this);
309
310 //swap the context.
311 if ($context == 'search' && $compContext) {
312 $context = $compContext;
313 }
314 else {
315 $compContext = NULL;
316 }
317
318 // make sure we dont get tricked with a bad key
319 // so check format
320 if (!CRM_Core_Key::valid($qfKey)) {
321 $qfKey = NULL;
322 }
323
324 $session = CRM_Core_Session::singleton();
325
326 switch ($context) {
327 case 'user':
328 $url = CRM_Utils_System::url('civicrm/user', 'reset=1');
329 break;
330
331 case 'dashboard':
332 $url = CRM_Utils_System::url('civicrm/contribute',
333 'reset=1'
334 );
335 break;
336
337 case 'pledgeDashboard':
338 $url = CRM_Utils_System::url('civicrm/pledge',
339 'reset=1'
340 );
341 break;
342
343 case 'contribution':
344 $url = CRM_Utils_System::url('civicrm/contact/view',
345 "reset=1&force=1&cid={$this->_contactId}&selectedChild=contribute"
346 );
347 break;
348
349 case 'search':
350 case 'advanced':
351 $extraParams = "force=1";
352 if ($qfKey) {
353 $extraParams .= "&qfKey=$qfKey";
354 }
355
356 $this->assign('searchKey', $qfKey);
357 if ($context == 'advanced') {
358 $url = CRM_Utils_System::url('civicrm/contact/search/advanced', $extraParams);
359 }
360 else if ($searchContext) {
361 $url = CRM_Utils_System::url("civicrm/$searchContext/search", $extraParams);
362 }
363 else {
364 $url = CRM_Utils_System::url('civicrm/contribute/search', $extraParams);
365 }
366 break;
367
368 case 'home':
369 $url = CRM_Utils_System::url('civicrm/dashboard', 'reset=1');
370 break;
371
372 case 'activity':
373 $url = CRM_Utils_System::url('civicrm/contact/view',
374 "reset=1&force=1&cid={$this->_contactId}&selectedChild=activity"
375 );
376 break;
377
378 case 'member':
379 case 'membership':
380 $componentId = CRM_Utils_Request::retrieve('compId', 'Positive', $this);
381 $componentAction = CRM_Utils_Request::retrieve('compAction', 'Integer', $this);
382
383 $context = 'membership';
384 $searchKey = NULL;
385 if ($compContext) {
386 $context = 'search';
387 if ($qfKey) {
388 $searchKey = "&key=$qfKey";
389 }
390 $compContext = "&compContext={$compContext}";
391 }
392 if ($componentAction & CRM_Core_Action::VIEW) {
393 $action = 'view';
394 }
395 else {
396 $action = 'update';
397 }
398 $url = CRM_Utils_System::url('civicrm/contact/view/membership',
399 "reset=1&action={$action}&id={$componentId}&cid={$this->_contactId}&context={$context}&selectedChild=member{$searchKey}{$compContext}"
400 );
401 break;
402
403 case 'participant':
404 $componentId = CRM_Utils_Request::retrieve('compId', 'Positive', $this);
405 $componentAction = CRM_Utils_Request::retrieve('compAction', 'Integer', $this);
406
407 $context = 'participant';
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/participant',
423 "reset=1&action={$action}&id={$componentId}&cid={$this->_contactId}&context={$context}&selectedChild=event{$searchKey}{$compContext}"
424 );
425 break;
426
427 case 'pledge':
428 $url = CRM_Utils_System::url('civicrm/contact/view',
429 "reset=1&force=1&cid={$this->_contactId}&selectedChild=pledge"
430 );
431 break;
432
433 case 'standalone':
434 $url = CRM_Utils_System::url('civicrm/dashboard', 'reset=1');
435 break;
436
437 case 'fulltext':
438 $keyName = '&qfKey';
439 $urlParams = 'force=1';
440 $urlString = 'civicrm/contact/search/custom';
441 if ($this->_action == CRM_Core_Action::UPDATE) {
442 if ($this->_contactId) {
443 $urlParams .= '&cid=' . $this->_contactId;
444 }
445 $keyName = '&key';
446 $urlParams .= '&context=fulltext&action=view';
447 $urlString = 'civicrm/contact/view/contribution';
448 }
449 if ($qfKey) {
450 $urlParams .= "$keyName=$qfKey";
451 }
452 $this->assign('searchKey', $qfKey);
453 $url = CRM_Utils_System::url($urlString, $urlParams);
454 break;
455
456 default:
457 $cid = NULL;
458 if ($this->_contactId) {
459 $cid = '&cid=' . $this->_contactId;
460 }
461 $url = CRM_Utils_System::url('civicrm/contribute/search',
462 'reset=1&force=1' . $cid
463 );
464 break;
465 }
466
467 $session = CRM_Core_Session::singleton();
468 $session->pushUserContext($url);
469 }
470 }
471