Merge pull request #4887 from pratikshad/broken-webtest
[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 * @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 */
61 public static function &recurLinks($recurID = FALSE, $context = 'contribution') {
62 if (!(self::$_links)) {
63 self::$_links = array(
64 CRM_Core_Action::VIEW => array(
65 'name' => ts('View'),
66 'title' => ts('View Recurring Payment'),
67 'url' => 'civicrm/contact/view/contributionrecur',
68 'qs' => "reset=1&id=%%crid%%&cid=%%cid%%&context={$context}",
69 ),
70 CRM_Core_Action::UPDATE => array(
71 'name' => ts('Edit'),
72 'title' => ts('Edit Recurring Payment'),
73 'url' => 'civicrm/contribute/updaterecur',
74 'qs' => "reset=1&action=update&crid=%%crid%%&cid=%%cid%%&context={$context}",
75 ),
76 CRM_Core_Action::DISABLE => array(
77 'name' => ts('Cancel'),
78 'title' => ts('Cancel'),
79 'ref' => 'crm-enable-disable',
80 ),
81 );
82 }
83
84 if ($recurID) {
85 $paymentProcessorObj = CRM_Financial_BAO_PaymentProcessor::getProcessorForEntity($recurID, 'recur', 'obj');
86 if (is_object($paymentProcessorObj) && $paymentProcessorObj->isSupported('cancelSubscription')) {
87 unset(self::$_links[CRM_Core_Action::DISABLE]['extra'], self::$_links[CRM_Core_Action::DISABLE]['ref']);
88 self::$_links[CRM_Core_Action::DISABLE]['url'] = "civicrm/contribute/unsubscribe";
89 self::$_links[CRM_Core_Action::DISABLE]['qs'] = "reset=1&crid=%%crid%%&cid=%%cid%%&context={$context}";
90 }
91 if (is_object($paymentProcessorObj) && $paymentProcessorObj->isSupported('updateSubscriptionBillingInfo')) {
92 self::$_links[CRM_Core_Action::RENEW] = array(
93 '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 * called when action is browse
107 *
108 * return null
109 */
110 public function browse() {
111 // add annual contribution
112 $annual = array();
113 list($annual['count'],
114 $annual['amount'],
115 $annual['avg']
116 ) = CRM_Contribute_BAO_Contribution::annual($this->_contactId);
117 $this->assign('annual', $annual);
118
119 $controller = new CRM_Core_Controller_Simple(
120 'CRM_Contribute_Form_Search',
121 ts('Contributions'),
122 $this->_action,
123 FALSE, FALSE, TRUE
124 );
125 $controller->setEmbedded(TRUE);
126 $controller->reset();
127 $controller->set('cid', $this->_contactId);
128 $controller->set('crid', $this->_crid);
129 $controller->set('context', 'contribution');
130 $controller->set('limit', 50);
131 $controller->process();
132 $controller->run();
133
134 // add recurring block
135 $action = array_sum(array_keys($this->recurLinks()));
136 $params = CRM_Contribute_BAO_ContributionRecur::getRecurContributions($this->_contactId);
137
138 if (!empty($params)) {
139 foreach ($params as $ids => $recur) {
140 $action = array_sum(array_keys($this->recurLinks($ids)));
141 // no action allowed if it's not active
142 $params[$ids]['is_active'] = ($recur['contribution_status_id'] != 3);
143
144 if ($params[$ids]['is_active']) {
145 $details = CRM_Contribute_BAO_ContributionRecur::getSubscriptionDetails($params[$ids]['id'], 'recur');
146 $hideUpdate = $details->membership_id & $details->auto_renew;
147
148 if ($hideUpdate) {
149 $action -= CRM_Core_Action::UPDATE;
150 }
151
152 $params[$ids]['action'] = CRM_Core_Action::formLink(self::recurLinks($ids), $action,
153 array(
154 'cid' => $this->_contactId,
155 'crid' => $ids,
156 'cxt' => 'contribution',
157 ),
158 ts('more'),
159 FALSE,
160 'contribution.selector.recurring',
161 'Contribution',
162 $ids
163 );
164 }
165 }
166 // assign vars to templates
167 $this->assign('action', $this->_action);
168 $this->assign('recurRows', $params);
169 $this->assign('recur', TRUE);
170 }
171
172 //enable/disable soft credit records for test contribution
173 $isTest = 0;
174 if (CRM_Utils_Request::retrieve('isTest', 'Positive', $this)) {
175 $isTest = 1;
176 }
177 $this->assign('isTest', $isTest);
178
179 $softCreditList = CRM_Contribute_BAO_ContributionSoft::getSoftContributionList($this->_contactId, NULL, $isTest);
180
181 if (!empty($softCreditList)) {
182 $softCreditTotals = array();
183
184 list($softCreditTotals['amount'],
185 $softCreditTotals['avg'],
186 $softCreditTotals['currency']
187 ) = CRM_Contribute_BAO_ContributionSoft::getSoftContributionTotals($this->_contactId, $isTest);
188
189 $this->assign('softCredit', TRUE);
190 $this->assign('softCreditRows', $softCreditList);
191 $this->assign('softCreditTotals', $softCreditTotals);
192 }
193
194 if ($this->_contactId) {
195 $displayName = CRM_Contact_BAO_Contact::displayName($this->_contactId);
196 $this->assign('displayName', $displayName);
197 $this->ajaxResponse['tabCount'] = CRM_Contact_BAO_Contact::getCountComponent('contribution', $this->_contactId);
198 }
199 }
200
201 /**
202 * called when action is view
203 *
204 * return null
205 */
206 public function view() {
207 $controller = new CRM_Core_Controller_Simple(
208 'CRM_Contribute_Form_ContributionView',
209 ts('View Contribution'),
210 $this->_action
211 );
212 $controller->setEmbedded(TRUE);
213 $controller->set('id', $this->_id);
214 $controller->set('cid', $this->_contactId);
215
216 return $controller->run();
217 }
218
219 /**
220 * called when action is update or new
221 *
222 * return null
223 */
224 public function edit() {
225 // set https for offline cc transaction
226 $mode = CRM_Utils_Request::retrieve('mode', 'String', $this);
227 if ($mode == 'test' || $mode == 'live') {
228 CRM_Utils_System::redirectToSSL();
229 }
230
231 $controller = new CRM_Core_Controller_Simple(
232 'CRM_Contribute_Form_Contribution',
233 'Create Contribution',
234 $this->_action
235 );
236 $controller->setEmbedded(TRUE);
237 $controller->set('id', $this->_id);
238 $controller->set('cid', $this->_contactId);
239
240 return $controller->run();
241 }
242
243 public function preProcess() {
244 $context = CRM_Utils_Request::retrieve('context', 'String', $this);
245 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'browse');
246 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
247
248 if ($context == 'standalone') {
249 $this->_action = CRM_Core_Action::ADD;
250 }
251 else {
252 $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, empty($this->_id));
253 if (empty($this->_contactId)) {
254 $this->_contactId = civicrm_api3('contribution', 'getvalue', array(
255 'id' => $this->_id,
256 'return' => 'contact_id'
257 ));
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 * 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 */
279 public 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 public 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 elseif ($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 }