Merge pull request #4185 from samuelsov/CRM-15330
[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 */
108 public function browse() {
109 // add annual contribution
110 $annual = array();
111 list($annual['count'],
112 $annual['amount'],
113 $annual['avg']
114 ) = CRM_Contribute_BAO_Contribution::annual($this->_contactId);
115 $this->assign('annual', $annual);
116
117 $controller = new CRM_Core_Controller_Simple(
118 'CRM_Contribute_Form_Search',
119 ts('Contributions'),
120 $this->_action,
121 FALSE, FALSE, TRUE
122 );
123 $controller->setEmbedded(TRUE);
124 $controller->reset();
125 $controller->set('cid', $this->_contactId);
126 $controller->set('crid', $this->_crid);
127 $controller->set('context', 'contribution');
128 $controller->set('limit', 50);
129 $controller->process();
130 $controller->run();
131
132 // add recurring block
133 $action = array_sum(array_keys($this->recurLinks()));
134 $params = CRM_Contribute_BAO_ContributionRecur::getRecurContributions($this->_contactId);
135
136 if (!empty($params)) {
137 foreach ($params as $ids => $recur) {
138 $action = array_sum(array_keys($this->recurLinks($ids)));
139 // no action allowed if it's not active
140 $params[$ids]['is_active'] = ($recur['contribution_status_id'] != 3);
141
142 if ($params[$ids]['is_active']) {
143 $details = CRM_Contribute_BAO_ContributionRecur::getSubscriptionDetails($params[$ids]['id'], 'recur');
144 $hideUpdate = $details->membership_id & $details->auto_renew;
145
146 if ($hideUpdate) {
147 $action -= CRM_Core_Action::UPDATE;
148 }
149
150 $params[$ids]['action'] = CRM_Core_Action::formLink(self::recurLinks($ids), $action,
151 array(
152 'cid' => $this->_contactId,
153 'crid' => $ids,
154 'cxt' => 'contribution',
155 ),
156 ts('more'),
157 FALSE,
158 'contribution.selector.recurring',
159 'Contribution',
160 $ids
161 );
162 }
163 }
164 // assign vars to templates
165 $this->assign('action', $this->_action);
166 $this->assign('recurRows', $params);
167 $this->assign('recur', TRUE);
168 }
169
170 //enable/disable soft credit records for test contribution
171 $isTest = 0;
172 if (CRM_Utils_Request::retrieve('isTest', 'Positive', $this)) {
173 $isTest = 1;
174 }
175 $this->assign('isTest', $isTest);
176
177 $softCreditList = CRM_Contribute_BAO_ContributionSoft::getSoftContributionList($this->_contactId, NULL, $isTest);
178
179 if (!empty($softCreditList)) {
180 $softCreditTotals = array();
181
182 list($softCreditTotals['amount'],
183 $softCreditTotals['avg'],
184 $softCreditTotals['currency']
185 ) = CRM_Contribute_BAO_ContributionSoft::getSoftContributionTotals($this->_contactId, $isTest);
186
187 $this->assign('softCredit', TRUE);
188 $this->assign('softCreditRows', $softCreditList);
189 $this->assign('softCreditTotals', $softCreditTotals);
190 }
191
192 if ($this->_contactId) {
193 $displayName = CRM_Contact_BAO_Contact::displayName($this->_contactId);
194 $this->assign('displayName', $displayName);
195 $this->ajaxResponse['tabCount'] = CRM_Contact_BAO_Contact::getCountComponent('contribution', $this->_contactId);
196 }
197 }
198
199 /**
200 * called when action is view
201 *
202 * @return null
203 */
204 public function view() {
205 $controller = new CRM_Core_Controller_Simple(
206 'CRM_Contribute_Form_ContributionView',
207 ts('View Contribution'),
208 $this->_action
209 );
210 $controller->setEmbedded(TRUE);
211 $controller->set('id', $this->_id);
212 $controller->set('cid', $this->_contactId);
213
214 return $controller->run();
215 }
216
217 /**
218 * called when action is update or new
219 *
220 * @return null
221 */
222 public function edit() {
223 // set https for offline cc transaction
224 $mode = CRM_Utils_Request::retrieve('mode', 'String', $this);
225 if ($mode == 'test' || $mode == 'live') {
226 CRM_Utils_System::redirectToSSL();
227 }
228
229 $controller = new CRM_Core_Controller_Simple(
230 'CRM_Contribute_Form_Contribution',
231 'Create Contribution',
232 $this->_action
233 );
234 $controller->setEmbedded(TRUE);
235 $controller->set('id', $this->_id);
236 $controller->set('cid', $this->_contactId);
237
238 return $controller->run();
239 }
240
241 public function preProcess() {
242 $context = CRM_Utils_Request::retrieve('context', 'String', $this);
243 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'browse');
244 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
245
246 if ($context == 'standalone') {
247 $this->_action = CRM_Core_Action::ADD;
248 }
249 else {
250 $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, empty($this->_id));
251 if (empty($this->_contactId)) {
252 $this->_contactId = civicrm_api3('contribution', 'getvalue', array(
253 'id' => $this->_id,
254 'return' => 'contact_id',
255 ));
256 }
257 $this->assign('contactId', $this->_contactId);
258
259 // check logged in url permission
260 CRM_Contact_Page_View::checkUserPermission($this);
261 }
262 $this->assign('action', $this->_action);
263
264 if ($this->_permission == CRM_Core_Permission::EDIT && !CRM_Core_Permission::check('edit contributions')) {
265 // demote to view since user does not have edit contrib rights
266 $this->_permission = CRM_Core_Permission::VIEW;
267 $this->assign('permission', 'view');
268 }
269 }
270
271 /**
272 * the main function that is called when the page
273 * loads, it decides the which action has to be taken for the page.
274 *
275 * @return null
276 */
277 public function run() {
278 $this->preProcess();
279
280 // check if we can process credit card contribs
281 $this->assign('newCredit', CRM_Core_Config::isEnabledBackOfficeCreditCardPayments());
282
283 $this->setContext();
284
285 if ($this->_action & CRM_Core_Action::VIEW) {
286 $this->view();
287 }
288 elseif ($this->_action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD | CRM_Core_Action::DELETE)) {
289 $this->edit();
290 }
291 else {
292 $this->browse();
293 }
294
295 return parent::run();
296 }
297
298 public function setContext() {
299 $qfKey = CRM_Utils_Request::retrieve('key', 'String', $this);
300 $context = CRM_Utils_Request::retrieve('context', 'String',
301 $this, FALSE, 'search'
302 );
303 $compContext = CRM_Utils_Request::retrieve('compContext', 'String', $this);
304
305 $searchContext = CRM_Utils_Request::retrieve('searchContext', 'String', $this);
306
307 //swap the context.
308 if ($context == 'search' && $compContext) {
309 $context = $compContext;
310 }
311 else {
312 $compContext = NULL;
313 }
314
315 // make sure we dont get tricked with a bad key
316 // so check format
317 if (!CRM_Core_Key::valid($qfKey)) {
318 $qfKey = NULL;
319 }
320
321 $session = CRM_Core_Session::singleton();
322
323 switch ($context) {
324 case 'user':
325 $url = CRM_Utils_System::url('civicrm/user', 'reset=1');
326 break;
327
328 case 'dashboard':
329 $url = CRM_Utils_System::url('civicrm/contribute',
330 'reset=1'
331 );
332 break;
333
334 case 'pledgeDashboard':
335 $url = CRM_Utils_System::url('civicrm/pledge',
336 'reset=1'
337 );
338 break;
339
340 case 'contribution':
341 $url = CRM_Utils_System::url('civicrm/contact/view',
342 "reset=1&force=1&cid={$this->_contactId}&selectedChild=contribute"
343 );
344 break;
345
346 case 'search':
347 case 'advanced':
348 $extraParams = "force=1";
349 if ($qfKey) {
350 $extraParams .= "&qfKey=$qfKey";
351 }
352
353 $this->assign('searchKey', $qfKey);
354 if ($context == 'advanced') {
355 $url = CRM_Utils_System::url('civicrm/contact/search/advanced', $extraParams);
356 }
357 elseif ($searchContext) {
358 $url = CRM_Utils_System::url("civicrm/$searchContext/search", $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 }