Merge pull request #4871 from eileenmcnaughton/CRM-15795
[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 * This function is 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 * This function is 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 * This function is 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('id' => $this->_id, 'return' => 'contact_id'));
255 }
256 $this->assign('contactId', $this->_contactId);
257
258 // check logged in url permission
259 CRM_Contact_Page_View::checkUserPermission($this);
260 }
261 $this->assign('action', $this->_action);
262
263 if ($this->_permission == CRM_Core_Permission::EDIT && !CRM_Core_Permission::check('edit contributions')) {
264 // demote to view since user does not have edit contrib rights
265 $this->_permission = CRM_Core_Permission::VIEW;
266 $this->assign('permission', 'view');
267 }
268 }
269
270 /**
271 * This function is the main function that is called when the page
272 * loads, it decides the which action has to be taken for the page.
273 *
274 * return null
275 */
276 public function run() {
277 $this->preProcess();
278
279 // check if we can process credit card contribs
280 $this->assign('newCredit', CRM_Core_Config::isEnabledBackOfficeCreditCardPayments());
281
282 $this->setContext();
283
284 if ($this->_action & CRM_Core_Action::VIEW) {
285 $this->view();
286 }
287 elseif ($this->_action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD | CRM_Core_Action::DELETE)) {
288 $this->edit();
289 }
290 else {
291 $this->browse();
292 }
293
294 return parent::run();
295 }
296
297 public function setContext() {
298 $qfKey = CRM_Utils_Request::retrieve('key', 'String', $this);
299 $context = CRM_Utils_Request::retrieve('context', 'String',
300 $this, FALSE, 'search'
301 );
302 $compContext = CRM_Utils_Request::retrieve('compContext', 'String', $this);
303
304 $searchContext = CRM_Utils_Request::retrieve('searchContext', 'String', $this);
305
306 //swap the context.
307 if ($context == 'search' && $compContext) {
308 $context = $compContext;
309 }
310 else {
311 $compContext = NULL;
312 }
313
314 // make sure we dont get tricked with a bad key
315 // so check format
316 if (!CRM_Core_Key::valid($qfKey)) {
317 $qfKey = NULL;
318 }
319
320 $session = CRM_Core_Session::singleton();
321
322 switch ($context) {
323 case 'user':
324 $url = CRM_Utils_System::url('civicrm/user', 'reset=1');
325 break;
326
327 case 'dashboard':
328 $url = CRM_Utils_System::url('civicrm/contribute',
329 'reset=1'
330 );
331 break;
332
333 case 'pledgeDashboard':
334 $url = CRM_Utils_System::url('civicrm/pledge',
335 'reset=1'
336 );
337 break;
338
339 case 'contribution':
340 $url = CRM_Utils_System::url('civicrm/contact/view',
341 "reset=1&force=1&cid={$this->_contactId}&selectedChild=contribute"
342 );
343 break;
344
345 case 'search':
346 case 'advanced':
347 $extraParams = "force=1";
348 if ($qfKey) {
349 $extraParams .= "&qfKey=$qfKey";
350 }
351
352 $this->assign('searchKey', $qfKey);
353 if ($context == 'advanced') {
354 $url = CRM_Utils_System::url('civicrm/contact/search/advanced', $extraParams);
355 }
356 elseif ($searchContext) {
357 $url = CRM_Utils_System::url("civicrm/$searchContext/search", $extraParams);
358 }
359 else {
360 $url = CRM_Utils_System::url('civicrm/contribute/search', $extraParams);
361 }
362 break;
363
364 case 'home':
365 $url = CRM_Utils_System::url('civicrm/dashboard', 'reset=1');
366 break;
367
368 case 'activity':
369 $url = CRM_Utils_System::url('civicrm/contact/view',
370 "reset=1&force=1&cid={$this->_contactId}&selectedChild=activity"
371 );
372 break;
373
374 case 'member':
375 case 'membership':
376 $componentId = CRM_Utils_Request::retrieve('compId', 'Positive', $this);
377 $componentAction = CRM_Utils_Request::retrieve('compAction', 'Integer', $this);
378
379 $context = 'membership';
380 $searchKey = NULL;
381 if ($compContext) {
382 $context = 'search';
383 if ($qfKey) {
384 $searchKey = "&key=$qfKey";
385 }
386 $compContext = "&compContext={$compContext}";
387 }
388 if ($componentAction & CRM_Core_Action::VIEW) {
389 $action = 'view';
390 }
391 else {
392 $action = 'update';
393 }
394 $url = CRM_Utils_System::url('civicrm/contact/view/membership',
395 "reset=1&action={$action}&id={$componentId}&cid={$this->_contactId}&context={$context}&selectedChild=member{$searchKey}{$compContext}"
396 );
397 break;
398
399 case 'participant':
400 $componentId = CRM_Utils_Request::retrieve('compId', 'Positive', $this);
401 $componentAction = CRM_Utils_Request::retrieve('compAction', 'Integer', $this);
402
403 $context = 'participant';
404 $searchKey = NULL;
405 if ($compContext) {
406 $context = 'search';
407 if ($qfKey) {
408 $searchKey = "&key=$qfKey";
409 }
410 $compContext = "&compContext={$compContext}";
411 }
412 if ($componentAction == CRM_Core_Action::VIEW) {
413 $action = 'view';
414 }
415 else {
416 $action = 'update';
417 }
418 $url = CRM_Utils_System::url('civicrm/contact/view/participant',
419 "reset=1&action={$action}&id={$componentId}&cid={$this->_contactId}&context={$context}&selectedChild=event{$searchKey}{$compContext}"
420 );
421 break;
422
423 case 'pledge':
424 $url = CRM_Utils_System::url('civicrm/contact/view',
425 "reset=1&force=1&cid={$this->_contactId}&selectedChild=pledge"
426 );
427 break;
428
429 case 'standalone':
430 $url = CRM_Utils_System::url('civicrm/dashboard', 'reset=1');
431 break;
432
433 case 'fulltext':
434 $keyName = '&qfKey';
435 $urlParams = 'force=1';
436 $urlString = 'civicrm/contact/search/custom';
437 if ($this->_action == CRM_Core_Action::UPDATE) {
438 if ($this->_contactId) {
439 $urlParams .= '&cid=' . $this->_contactId;
440 }
441 $keyName = '&key';
442 $urlParams .= '&context=fulltext&action=view';
443 $urlString = 'civicrm/contact/view/contribution';
444 }
445 if ($qfKey) {
446 $urlParams .= "$keyName=$qfKey";
447 }
448 $this->assign('searchKey', $qfKey);
449 $url = CRM_Utils_System::url($urlString, $urlParams);
450 break;
451
452 default:
453 $cid = NULL;
454 if ($this->_contactId) {
455 $cid = '&cid=' . $this->_contactId;
456 }
457 $url = CRM_Utils_System::url('civicrm/contribute/search',
458 'reset=1&force=1' . $cid
459 );
460 break;
461 }
462
463 $session = CRM_Core_Session::singleton();
464 $session->pushUserContext($url);
465 }
466 }