Merge pull request #11622 from omarabuhussein/CRM-21733-membership-override-improvments
[civicrm-core.git] / CRM / Contribute / Page / Tab.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
0f03f337 6 | Copyright CiviCRM LLC (c) 2004-2017 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
0f03f337 31 * @copyright CiviCRM LLC (c) 2004-2017
6a488035
TO
32 */
33class CRM_Contribute_Page_Tab extends CRM_Core_Page {
34
35 /**
fe482240 36 * The action links that we need to display for the browse screen.
6a488035
TO
37 *
38 * @var array
6a488035
TO
39 */
40 static $_links = NULL;
6a488035
TO
41 static $_recurLinks = NULL;
42 public $_permission = NULL;
43 public $_contactId = NULL;
44 public $_crid = NULL;
45
6a488035
TO
46 /**
47 * This method returns the links that are given for recur search row.
48 * currently the links added for each row are:
49 * - View
50 * - Edit
51 * - Cancel
52 *
6c8f6e67
EM
53 * @param bool $recurID
54 * @param string $context
55 *
6a488035 56 * @return array
6a488035 57 */
00be9182 58 public static function &recurLinks($recurID = FALSE, $context = 'contribution') {
6a488035
TO
59 if (!(self::$_links)) {
60 self::$_links = array(
61 CRM_Core_Action::VIEW => array(
62 'name' => ts('View'),
63 'title' => ts('View Recurring Payment'),
64 'url' => 'civicrm/contact/view/contributionrecur',
65 'qs' => "reset=1&id=%%crid%%&cid=%%cid%%&context={$context}",
66 ),
67 CRM_Core_Action::UPDATE => array(
68 'name' => ts('Edit'),
69 'title' => ts('Edit Recurring Payment'),
70 'url' => 'civicrm/contribute/updaterecur',
71 'qs' => "reset=1&action=update&crid=%%crid%%&cid=%%cid%%&context={$context}",
72 ),
73 CRM_Core_Action::DISABLE => array(
74 'name' => ts('Cancel'),
75 'title' => ts('Cancel'),
4d17a233 76 'ref' => 'crm-enable-disable',
6a488035
TO
77 ),
78 );
79 }
80
81 if ($recurID) {
afcaa7e9 82 $links = self::$_links;
6a488035 83 $paymentProcessorObj = CRM_Financial_BAO_PaymentProcessor::getProcessorForEntity($recurID, 'recur', 'obj');
1524a007 84 if (is_object($paymentProcessorObj) && $paymentProcessorObj->supports('cancelRecurring')) {
afcaa7e9 85 unset($links[CRM_Core_Action::DISABLE]['extra'], $links[CRM_Core_Action::DISABLE]['ref']);
86 $links[CRM_Core_Action::DISABLE]['url'] = "civicrm/contribute/unsubscribe";
87 $links[CRM_Core_Action::DISABLE]['qs'] = "reset=1&crid=%%crid%%&cid=%%cid%%&context={$context}";
6a488035 88 }
1524a007 89
481a74f4 90 if (is_object($paymentProcessorObj) && $paymentProcessorObj->isSupported('updateSubscriptionBillingInfo')) {
afcaa7e9 91 $links[CRM_Core_Action::RENEW] = array(
353ffa53 92 'name' => ts('Change Billing Details'),
6a488035
TO
93 'title' => ts('Change Billing Details'),
94 'url' => 'civicrm/contribute/updatebilling',
95 'qs' => "reset=1&crid=%%crid%%&cid=%%cid%%&context={$context}",
96 );
97 }
afcaa7e9 98 return $links;
6a488035
TO
99 }
100
101 return self::$_links;
102 }
103 // end function
104
105 /**
fe482240 106 * called when action is browse.
6a488035 107 *
6a488035 108 */
00be9182 109 public function browse() {
6a488035
TO
110 // add annual contribution
111 $annual = array();
112 list($annual['count'],
113 $annual['amount'],
114 $annual['avg']
353ffa53 115 ) = CRM_Contribute_BAO_Contribution::annual($this->_contactId);
6a488035
TO
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);
7a49693c 128 $controller->set('context', 'contribution');
c2874d9c 129 $controller->set('limit', 50);
6a488035
TO
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',
87dab4a4
AH
156 ),
157 ts('more'),
158 FALSE,
159 'contribution.selector.recurring',
160 'Contribution',
161 $ids
6a488035
TO
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
6a488035
TO
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
91ef9be0 178 $softCreditList = CRM_Contribute_BAO_ContributionSoft::getSoftContributionList($this->_contactId, NULL, $isTest);
6a488035
TO
179
180 if (!empty($softCreditList)) {
181 $softCreditTotals = array();
182
183 list($softCreditTotals['amount'],
184 $softCreditTotals['avg'],
971cdafb 185 $softCreditTotals['currency'],
186 $softCreditTotals['cancelAmount'] //to get cancel amount
353ffa53 187 ) = CRM_Contribute_BAO_ContributionSoft::getSoftContributionTotals($this->_contactId, $isTest);
6a488035
TO
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);
4e8065a9 197 $this->ajaxResponse['tabCount'] = CRM_Contact_BAO_Contact::getCountComponent('contribution', $this->_contactId);
6a488035
TO
198 }
199 }
200
201 /**
fe482240 202 * called when action is view.
6a488035 203 *
76e7a76c 204 * @return null
6a488035 205 */
00be9182 206 public function view() {
6a488035
TO
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 /**
fe482240 220 * called when action is update or new.
6a488035 221 *
76e7a76c 222 * @return null
6a488035 223 */
00be9182 224 public function edit() {
6a488035
TO
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
00be9182 243 public function preProcess() {
353ffa53 244 $context = CRM_Utils_Request::retrieve('context', 'String', $this);
6a488035 245 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'browse');
353ffa53 246 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
6a488035
TO
247
248 if ($context == 'standalone') {
249 $this->_action = CRM_Core_Action::ADD;
250 }
251 else {
f9009fe0
EM
252 $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, empty($this->_id));
253 if (empty($this->_contactId)) {
353ffa53
TO
254 $this->_contactId = civicrm_api3('contribution', 'getvalue', array(
255 'id' => $this->_id,
acb1052e 256 'return' => 'contact_id',
353ffa53 257 ));
f9009fe0 258 }
6a488035
TO
259 $this->assign('contactId', $this->_contactId);
260
261 // check logged in url permission
262 CRM_Contact_Page_View::checkUserPermission($this);
6a488035
TO
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 /**
dc195289 274 * the main function that is called when the page
6a488035
TO
275 * loads, it decides the which action has to be taken for the page.
276 *
76e7a76c 277 * @return null
6a488035 278 */
00be9182 279 public function run() {
6a488035
TO
280 $this->preProcess();
281
282 // check if we can process credit card contribs
9be1374d 283 $this->assign('newCredit', CRM_Core_Config::isEnabledBackOfficeCreditCardPayments());
6a488035
TO
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
00be9182 300 public function setContext() {
6a488035
TO
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
29571f63
AS
307 $searchContext = CRM_Utils_Request::retrieve('searchContext', 'String', $this);
308
6a488035
TO
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':
6a488035 343 $url = CRM_Utils_System::url('civicrm/contact/view',
8381af80 344 "reset=1&force=1&cid={$this->_contactId}&selectedChild=contribute"
6a488035
TO
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 }
4c9b6178 359 elseif ($searchContext) {
29571f63
AS
360 $url = CRM_Utils_System::url("civicrm/$searchContext/search", $extraParams);
361 }
6a488035
TO
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':
353ffa53 437 $keyName = '&qfKey';
6a488035
TO
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 }
96025800 469
6a488035 470}