Merge pull request #7495 from mollux/CRM-17747
[civicrm-core.git] / CRM / Contribute / Page / Tab.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
32 */
33 class CRM_Contribute_Page_Tab extends CRM_Core_Page {
34
35 /**
36 * The action links that we need to display for the browse screen.
37 *
38 * @var array
39 */
40 static $_links = NULL;
41 static $_recurLinks = NULL;
42 public $_permission = NULL;
43 public $_contactId = NULL;
44 public $_crid = NULL;
45
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 *
53 * @param bool $recurID
54 * @param string $context
55 *
56 * @return array
57 */
58 public static function &recurLinks($recurID = FALSE, $context = 'contribution') {
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'),
76 'ref' => 'crm-enable-disable',
77 ),
78 );
79 }
80
81 if ($recurID) {
82 $paymentProcessorObj = CRM_Financial_BAO_PaymentProcessor::getProcessorForEntity($recurID, 'recur', 'obj');
83 if (is_object($paymentProcessorObj) && $paymentProcessorObj->supports('cancelRecurring')) {
84 unset(self::$_links[CRM_Core_Action::DISABLE]['extra'], self::$_links[CRM_Core_Action::DISABLE]['ref']);
85 self::$_links[CRM_Core_Action::DISABLE]['url'] = "civicrm/contribute/unsubscribe";
86 self::$_links[CRM_Core_Action::DISABLE]['qs'] = "reset=1&crid=%%crid%%&cid=%%cid%%&context={$context}";
87 }
88
89 if (is_object($paymentProcessorObj) && $paymentProcessorObj->isSupported('updateSubscriptionBillingInfo')) {
90 self::$_links[CRM_Core_Action::RENEW] = array(
91 'name' => ts('Change Billing Details'),
92 'title' => ts('Change Billing Details'),
93 'url' => 'civicrm/contribute/updatebilling',
94 'qs' => "reset=1&crid=%%crid%%&cid=%%cid%%&context={$context}",
95 );
96 }
97 }
98
99 return self::$_links;
100 }
101 // end function
102
103 /**
104 * called when action is browse.
105 *
106 */
107 public function browse() {
108 // add annual contribution
109 $annual = array();
110 list($annual['count'],
111 $annual['amount'],
112 $annual['avg']
113 ) = CRM_Contribute_BAO_Contribution::annual($this->_contactId);
114 $this->assign('annual', $annual);
115
116 $controller = new CRM_Core_Controller_Simple(
117 'CRM_Contribute_Form_Search',
118 ts('Contributions'),
119 $this->_action,
120 FALSE, FALSE, TRUE
121 );
122 $controller->setEmbedded(TRUE);
123 $controller->reset();
124 $controller->set('cid', $this->_contactId);
125 $controller->set('crid', $this->_crid);
126 $controller->set('context', 'contribution');
127 $controller->set('limit', 50);
128 $controller->process();
129 $controller->run();
130
131 // add recurring block
132 $action = array_sum(array_keys($this->recurLinks()));
133 $params = CRM_Contribute_BAO_ContributionRecur::getRecurContributions($this->_contactId);
134
135 if (!empty($params)) {
136 foreach ($params as $ids => $recur) {
137 $action = array_sum(array_keys($this->recurLinks($ids)));
138 // no action allowed if it's not active
139 $params[$ids]['is_active'] = ($recur['contribution_status_id'] != 3);
140
141 if ($params[$ids]['is_active']) {
142 $details = CRM_Contribute_BAO_ContributionRecur::getSubscriptionDetails($params[$ids]['id'], 'recur');
143 $hideUpdate = $details->membership_id & $details->auto_renew;
144
145 if ($hideUpdate) {
146 $action -= CRM_Core_Action::UPDATE;
147 }
148
149 $params[$ids]['action'] = CRM_Core_Action::formLink(self::recurLinks($ids), $action,
150 array(
151 'cid' => $this->_contactId,
152 'crid' => $ids,
153 'cxt' => 'contribution',
154 ),
155 ts('more'),
156 FALSE,
157 'contribution.selector.recurring',
158 'Contribution',
159 $ids
160 );
161 }
162 }
163 // assign vars to templates
164 $this->assign('action', $this->_action);
165 $this->assign('recurRows', $params);
166 $this->assign('recur', TRUE);
167 }
168
169 //enable/disable soft credit records for test contribution
170 $isTest = 0;
171 if (CRM_Utils_Request::retrieve('isTest', 'Positive', $this)) {
172 $isTest = 1;
173 }
174 $this->assign('isTest', $isTest);
175
176 $softCreditList = CRM_Contribute_BAO_ContributionSoft::getSoftContributionList($this->_contactId, NULL, $isTest);
177
178 if (!empty($softCreditList)) {
179 $softCreditTotals = array();
180
181 list($softCreditTotals['amount'],
182 $softCreditTotals['avg'],
183 $softCreditTotals['currency']
184 ) = CRM_Contribute_BAO_ContributionSoft::getSoftContributionTotals($this->_contactId, $isTest);
185
186 $this->assign('softCredit', TRUE);
187 $this->assign('softCreditRows', $softCreditList);
188 $this->assign('softCreditTotals', $softCreditTotals);
189 }
190
191 if ($this->_contactId) {
192 $displayName = CRM_Contact_BAO_Contact::displayName($this->_contactId);
193 $this->assign('displayName', $displayName);
194 $this->ajaxResponse['tabCount'] = CRM_Contact_BAO_Contact::getCountComponent('contribution', $this->_contactId);
195 }
196 }
197
198 /**
199 * called when action is view.
200 *
201 * @return null
202 */
203 public function view() {
204 $controller = new CRM_Core_Controller_Simple(
205 'CRM_Contribute_Form_ContributionView',
206 ts('View Contribution'),
207 $this->_action
208 );
209 $controller->setEmbedded(TRUE);
210 $controller->set('id', $this->_id);
211 $controller->set('cid', $this->_contactId);
212
213 return $controller->run();
214 }
215
216 /**
217 * called when action is update or new.
218 *
219 * @return null
220 */
221 public function edit() {
222 // set https for offline cc transaction
223 $mode = CRM_Utils_Request::retrieve('mode', 'String', $this);
224 if ($mode == 'test' || $mode == 'live') {
225 CRM_Utils_System::redirectToSSL();
226 }
227
228 $controller = new CRM_Core_Controller_Simple(
229 'CRM_Contribute_Form_Contribution',
230 'Create Contribution',
231 $this->_action
232 );
233 $controller->setEmbedded(TRUE);
234 $controller->set('id', $this->_id);
235 $controller->set('cid', $this->_contactId);
236
237 return $controller->run();
238 }
239
240 public function preProcess() {
241 $context = CRM_Utils_Request::retrieve('context', 'String', $this);
242 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'browse');
243 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
244
245 if ($context == 'standalone') {
246 $this->_action = CRM_Core_Action::ADD;
247 }
248 else {
249 $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, empty($this->_id));
250 if (empty($this->_contactId)) {
251 $this->_contactId = civicrm_api3('contribution', 'getvalue', array(
252 'id' => $this->_id,
253 'return' => 'contact_id',
254 ));
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 * 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
467 }