Merge pull request #7032 from mlutfy/4.7-crm17432
[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->isSupported('cancelSubscription')) {
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 if (is_object($paymentProcessorObj) && $paymentProcessorObj->isSupported('updateSubscriptionBillingInfo')) {
89 self::$_links[CRM_Core_Action::RENEW] = array(
90 'name' => ts('Change Billing Details'),
91 'title' => ts('Change Billing Details'),
92 'url' => 'civicrm/contribute/updatebilling',
93 'qs' => "reset=1&crid=%%crid%%&cid=%%cid%%&context={$context}",
94 );
95 }
96 }
97
98 return self::$_links;
99 }
100 // end function
101
102 /**
103 * called when action is browse.
104 *
105 */
106 public function browse() {
107 // add annual contribution
108 $annual = array();
109 list($annual['count'],
110 $annual['amount'],
111 $annual['avg']
112 ) = CRM_Contribute_BAO_Contribution::annual($this->_contactId);
113 $this->assign('annual', $annual);
114
115 $controller = new CRM_Core_Controller_Simple(
116 'CRM_Contribute_Form_Search',
117 ts('Contributions'),
118 $this->_action,
119 FALSE, FALSE, TRUE
120 );
121 $controller->setEmbedded(TRUE);
122 $controller->reset();
123 $controller->set('cid', $this->_contactId);
124 $controller->set('crid', $this->_crid);
125 $controller->set('context', 'contribution');
126 $controller->set('limit', 50);
127 $controller->process();
128 $controller->run();
129
130 // add recurring block
131 $action = array_sum(array_keys($this->recurLinks()));
132 $params = CRM_Contribute_BAO_ContributionRecur::getRecurContributions($this->_contactId);
133
134 if (!empty($params)) {
135 foreach ($params as $ids => $recur) {
136 $action = array_sum(array_keys($this->recurLinks($ids)));
137 // no action allowed if it's not active
138 $params[$ids]['is_active'] = ($recur['contribution_status_id'] != 3);
139
140 if ($params[$ids]['is_active']) {
141 $details = CRM_Contribute_BAO_ContributionRecur::getSubscriptionDetails($params[$ids]['id'], 'recur');
142 $hideUpdate = $details->membership_id & $details->auto_renew;
143
144 if ($hideUpdate) {
145 $action -= CRM_Core_Action::UPDATE;
146 }
147
148 $params[$ids]['action'] = CRM_Core_Action::formLink(self::recurLinks($ids), $action,
149 array(
150 'cid' => $this->_contactId,
151 'crid' => $ids,
152 'cxt' => 'contribution',
153 ),
154 ts('more'),
155 FALSE,
156 'contribution.selector.recurring',
157 'Contribution',
158 $ids
159 );
160 }
161 }
162 // assign vars to templates
163 $this->assign('action', $this->_action);
164 $this->assign('recurRows', $params);
165 $this->assign('recur', TRUE);
166 }
167
168 //enable/disable soft credit records for test contribution
169 $isTest = 0;
170 if (CRM_Utils_Request::retrieve('isTest', 'Positive', $this)) {
171 $isTest = 1;
172 }
173 $this->assign('isTest', $isTest);
174
175 $softCreditList = CRM_Contribute_BAO_ContributionSoft::getSoftContributionList($this->_contactId, NULL, $isTest);
176
177 if (!empty($softCreditList)) {
178 $softCreditTotals = array();
179
180 list($softCreditTotals['amount'],
181 $softCreditTotals['avg'],
182 $softCreditTotals['currency']
183 ) = CRM_Contribute_BAO_ContributionSoft::getSoftContributionTotals($this->_contactId, $isTest);
184
185 $this->assign('softCredit', TRUE);
186 $this->assign('softCreditRows', $softCreditList);
187 $this->assign('softCreditTotals', $softCreditTotals);
188 }
189
190 if ($this->_contactId) {
191 $displayName = CRM_Contact_BAO_Contact::displayName($this->_contactId);
192 $this->assign('displayName', $displayName);
193 $this->ajaxResponse['tabCount'] = CRM_Contact_BAO_Contact::getCountComponent('contribution', $this->_contactId);
194 }
195 }
196
197 /**
198 * called when action is view.
199 *
200 * @return null
201 */
202 public function view() {
203 $controller = new CRM_Core_Controller_Simple(
204 'CRM_Contribute_Form_ContributionView',
205 ts('View Contribution'),
206 $this->_action
207 );
208 $controller->setEmbedded(TRUE);
209 $controller->set('id', $this->_id);
210 $controller->set('cid', $this->_contactId);
211
212 return $controller->run();
213 }
214
215 /**
216 * called when action is update or new.
217 *
218 * @return null
219 */
220 public function edit() {
221 // set https for offline cc transaction
222 $mode = CRM_Utils_Request::retrieve('mode', 'String', $this);
223 if ($mode == 'test' || $mode == 'live') {
224 CRM_Utils_System::redirectToSSL();
225 }
226
227 $controller = new CRM_Core_Controller_Simple(
228 'CRM_Contribute_Form_Contribution',
229 'Create Contribution',
230 $this->_action
231 );
232 $controller->setEmbedded(TRUE);
233 $controller->set('id', $this->_id);
234 $controller->set('cid', $this->_contactId);
235
236 return $controller->run();
237 }
238
239 public function preProcess() {
240 $context = CRM_Utils_Request::retrieve('context', 'String', $this);
241 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'browse');
242 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
243
244 if ($context == 'standalone') {
245 $this->_action = CRM_Core_Action::ADD;
246 }
247 else {
248 $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, empty($this->_id));
249 if (empty($this->_contactId)) {
250 $this->_contactId = civicrm_api3('contribution', 'getvalue', array(
251 'id' => $this->_id,
252 'return' => 'contact_id',
253 ));
254 }
255 $this->assign('contactId', $this->_contactId);
256
257 // check logged in url permission
258 CRM_Contact_Page_View::checkUserPermission($this);
259 }
260 $this->assign('action', $this->_action);
261
262 if ($this->_permission == CRM_Core_Permission::EDIT && !CRM_Core_Permission::check('edit contributions')) {
263 // demote to view since user does not have edit contrib rights
264 $this->_permission = CRM_Core_Permission::VIEW;
265 $this->assign('permission', 'view');
266 }
267 }
268
269 /**
270 * the main function that is called when the page
271 * loads, it decides the which action has to be taken for the page.
272 *
273 * @return null
274 */
275 public function run() {
276 $this->preProcess();
277
278 // check if we can process credit card contribs
279 $this->assign('newCredit', CRM_Core_Config::isEnabledBackOfficeCreditCardPayments());
280
281 $this->setContext();
282
283 if ($this->_action & CRM_Core_Action::VIEW) {
284 $this->view();
285 }
286 elseif ($this->_action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD | CRM_Core_Action::DELETE)) {
287 $this->edit();
288 }
289 else {
290 $this->browse();
291 }
292
293 return parent::run();
294 }
295
296 public function setContext() {
297 $qfKey = CRM_Utils_Request::retrieve('key', 'String', $this);
298 $context = CRM_Utils_Request::retrieve('context', 'String',
299 $this, FALSE, 'search'
300 );
301 $compContext = CRM_Utils_Request::retrieve('compContext', 'String', $this);
302
303 $searchContext = CRM_Utils_Request::retrieve('searchContext', 'String', $this);
304
305 //swap the context.
306 if ($context == 'search' && $compContext) {
307 $context = $compContext;
308 }
309 else {
310 $compContext = NULL;
311 }
312
313 // make sure we dont get tricked with a bad key
314 // so check format
315 if (!CRM_Core_Key::valid($qfKey)) {
316 $qfKey = NULL;
317 }
318
319 $session = CRM_Core_Session::singleton();
320
321 switch ($context) {
322 case 'user':
323 $url = CRM_Utils_System::url('civicrm/user', 'reset=1');
324 break;
325
326 case 'dashboard':
327 $url = CRM_Utils_System::url('civicrm/contribute',
328 'reset=1'
329 );
330 break;
331
332 case 'pledgeDashboard':
333 $url = CRM_Utils_System::url('civicrm/pledge',
334 'reset=1'
335 );
336 break;
337
338 case 'contribution':
339 $url = CRM_Utils_System::url('civicrm/contact/view',
340 "reset=1&force=1&cid={$this->_contactId}&selectedChild=contribute"
341 );
342 break;
343
344 case 'search':
345 case 'advanced':
346 $extraParams = "force=1";
347 if ($qfKey) {
348 $extraParams .= "&qfKey=$qfKey";
349 }
350
351 $this->assign('searchKey', $qfKey);
352 if ($context == 'advanced') {
353 $url = CRM_Utils_System::url('civicrm/contact/search/advanced', $extraParams);
354 }
355 elseif ($searchContext) {
356 $url = CRM_Utils_System::url("civicrm/$searchContext/search", $extraParams);
357 }
358 else {
359 $url = CRM_Utils_System::url('civicrm/contribute/search', $extraParams);
360 }
361 break;
362
363 case 'home':
364 $url = CRM_Utils_System::url('civicrm/dashboard', 'reset=1');
365 break;
366
367 case 'activity':
368 $url = CRM_Utils_System::url('civicrm/contact/view',
369 "reset=1&force=1&cid={$this->_contactId}&selectedChild=activity"
370 );
371 break;
372
373 case 'member':
374 case 'membership':
375 $componentId = CRM_Utils_Request::retrieve('compId', 'Positive', $this);
376 $componentAction = CRM_Utils_Request::retrieve('compAction', 'Integer', $this);
377
378 $context = 'membership';
379 $searchKey = NULL;
380 if ($compContext) {
381 $context = 'search';
382 if ($qfKey) {
383 $searchKey = "&key=$qfKey";
384 }
385 $compContext = "&compContext={$compContext}";
386 }
387 if ($componentAction & CRM_Core_Action::VIEW) {
388 $action = 'view';
389 }
390 else {
391 $action = 'update';
392 }
393 $url = CRM_Utils_System::url('civicrm/contact/view/membership',
394 "reset=1&action={$action}&id={$componentId}&cid={$this->_contactId}&context={$context}&selectedChild=member{$searchKey}{$compContext}"
395 );
396 break;
397
398 case 'participant':
399 $componentId = CRM_Utils_Request::retrieve('compId', 'Positive', $this);
400 $componentAction = CRM_Utils_Request::retrieve('compAction', 'Integer', $this);
401
402 $context = 'participant';
403 $searchKey = NULL;
404 if ($compContext) {
405 $context = 'search';
406 if ($qfKey) {
407 $searchKey = "&key=$qfKey";
408 }
409 $compContext = "&compContext={$compContext}";
410 }
411 if ($componentAction == CRM_Core_Action::VIEW) {
412 $action = 'view';
413 }
414 else {
415 $action = 'update';
416 }
417 $url = CRM_Utils_System::url('civicrm/contact/view/participant',
418 "reset=1&action={$action}&id={$componentId}&cid={$this->_contactId}&context={$context}&selectedChild=event{$searchKey}{$compContext}"
419 );
420 break;
421
422 case 'pledge':
423 $url = CRM_Utils_System::url('civicrm/contact/view',
424 "reset=1&force=1&cid={$this->_contactId}&selectedChild=pledge"
425 );
426 break;
427
428 case 'standalone':
429 $url = CRM_Utils_System::url('civicrm/dashboard', 'reset=1');
430 break;
431
432 case 'fulltext':
433 $keyName = '&qfKey';
434 $urlParams = 'force=1';
435 $urlString = 'civicrm/contact/search/custom';
436 if ($this->_action == CRM_Core_Action::UPDATE) {
437 if ($this->_contactId) {
438 $urlParams .= '&cid=' . $this->_contactId;
439 }
440 $keyName = '&key';
441 $urlParams .= '&context=fulltext&action=view';
442 $urlString = 'civicrm/contact/view/contribution';
443 }
444 if ($qfKey) {
445 $urlParams .= "$keyName=$qfKey";
446 }
447 $this->assign('searchKey', $qfKey);
448 $url = CRM_Utils_System::url($urlString, $urlParams);
449 break;
450
451 default:
452 $cid = NULL;
453 if ($this->_contactId) {
454 $cid = '&cid=' . $this->_contactId;
455 }
456 $url = CRM_Utils_System::url('civicrm/contribute/search',
457 'reset=1&force=1' . $cid
458 );
459 break;
460 }
461
462 $session = CRM_Core_Session::singleton();
463 $session->pushUserContext($url);
464 }
465
466 }