Merge pull request #2452 from totten/lolas-freeform-CRM-14126
[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 CRM_Core_Resources::singleton()->addScriptFile('civicrm', 'js/crm.livePage.js');
113
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', 'Search');
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, $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 // set page title
264 CRM_Contact_Page_View::setTitle($this->_contactId);
265 }
266 $this->assign('action', $this->_action);
267
268 if ($this->_permission == CRM_Core_Permission::EDIT && !CRM_Core_Permission::check('edit contributions')) {
269 // demote to view since user does not have edit contrib rights
270 $this->_permission = CRM_Core_Permission::VIEW;
271 $this->assign('permission', 'view');
272 }
273 }
274
275 /**
276 * This function is the main function that is called when the page
277 * loads, it decides the which action has to be taken for the page.
278 *
279 * return null
280 * @access public
281 */
282 function run() {
283 $this->preProcess();
284
285 // check if we can process credit card contribs
286 CRM_Core_Payment::allowBackofficeCreditCard($this);
287
288 $this->setContext();
289
290 if ($this->_action & CRM_Core_Action::VIEW) {
291 $this->view();
292 }
293 elseif ($this->_action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD | CRM_Core_Action::DELETE)) {
294 $this->edit();
295 }
296 else {
297 $this->browse();
298 }
299
300 return parent::run();
301 }
302
303 function setContext() {
304 $qfKey = CRM_Utils_Request::retrieve('key', 'String', $this);
305 $context = CRM_Utils_Request::retrieve('context', 'String',
306 $this, FALSE, 'search'
307 );
308 $compContext = CRM_Utils_Request::retrieve('compContext', '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 {
361 $url = CRM_Utils_System::url('civicrm/contribute/search', $extraParams);
362 }
363 break;
364
365 case 'home':
366 $url = CRM_Utils_System::url('civicrm/dashboard', 'reset=1');
367 break;
368
369 case 'activity':
370 $url = CRM_Utils_System::url('civicrm/contact/view',
371 "reset=1&force=1&cid={$this->_contactId}&selectedChild=activity"
372 );
373 break;
374
375 case 'member':
376 case 'membership':
377 $componentId = CRM_Utils_Request::retrieve('compId', 'Positive', $this);
378 $componentAction = CRM_Utils_Request::retrieve('compAction', 'Integer', $this);
379
380 $context = 'membership';
381 $searchKey = NULL;
382 if ($compContext) {
383 $context = 'search';
384 if ($qfKey) {
385 $searchKey = "&key=$qfKey";
386 }
387 $compContext = "&compContext={$compContext}";
388 }
389 if ($componentAction & CRM_Core_Action::VIEW) {
390 $action = 'view';
391 }
392 else {
393 $action = 'update';
394 }
395 $url = CRM_Utils_System::url('civicrm/contact/view/membership',
396 "reset=1&action={$action}&id={$componentId}&cid={$this->_contactId}&context={$context}&selectedChild=member{$searchKey}{$compContext}"
397 );
398 break;
399
400 case 'participant':
401 $componentId = CRM_Utils_Request::retrieve('compId', 'Positive', $this);
402 $componentAction = CRM_Utils_Request::retrieve('compAction', 'Integer', $this);
403
404 $context = 'participant';
405 $searchKey = NULL;
406 if ($compContext) {
407 $context = 'search';
408 if ($qfKey) {
409 $searchKey = "&key=$qfKey";
410 }
411 $compContext = "&compContext={$compContext}";
412 }
413 if ($componentAction == CRM_Core_Action::VIEW) {
414 $action = 'view';
415 }
416 else {
417 $action = 'update';
418 }
419 $url = CRM_Utils_System::url('civicrm/contact/view/participant',
420 "reset=1&action={$action}&id={$componentId}&cid={$this->_contactId}&context={$context}&selectedChild=event{$searchKey}{$compContext}"
421 );
422 break;
423
424 case 'pledge':
425 $url = CRM_Utils_System::url('civicrm/contact/view',
426 "reset=1&force=1&cid={$this->_contactId}&selectedChild=pledge"
427 );
428 break;
429
430 case 'standalone':
431 $url = CRM_Utils_System::url('civicrm/dashboard', 'reset=1');
432 break;
433
434 case 'fulltext':
435 $keyName = '&qfKey';
436 $urlParams = 'force=1';
437 $urlString = 'civicrm/contact/search/custom';
438 if ($this->_action == CRM_Core_Action::UPDATE) {
439 if ($this->_contactId) {
440 $urlParams .= '&cid=' . $this->_contactId;
441 }
442 $keyName = '&key';
443 $urlParams .= '&context=fulltext&action=view';
444 $urlString = 'civicrm/contact/view/contribution';
445 }
446 if ($qfKey) {
447 $urlParams .= "$keyName=$qfKey";
448 }
449 $this->assign('searchKey', $qfKey);
450 $url = CRM_Utils_System::url($urlString, $urlParams);
451 break;
452
453 default:
454 $cid = NULL;
455 if ($this->_contactId) {
456 $cid = '&cid=' . $this->_contactId;
457 }
458 $url = CRM_Utils_System::url('civicrm/contribute/search',
459 'reset=1&force=1' . $cid
460 );
461 break;
462 }
463
464 $session = CRM_Core_Session::singleton();
465 $session->pushUserContext($url);
466 }
467 }
468