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