Merge pull request #4198 from sunilpawar/CRM-15332
[civicrm-core.git] / CRM / Pledge / Page / Tab.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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_Pledge_Page_Tab extends CRM_Core_Page {
36 public $_permission = NULL;
37 public $_contactId = NULL;
38
39 /**
40 * This function is called when action is browse
41 *
42 * return null
43 * @access public
44 */
45 function browse() {
46 $controller = new CRM_Core_Controller_Simple('CRM_Pledge_Form_Search', ts('Pledges'), $this->_action);
47 $controller->setEmbedded(TRUE);
48 $controller->reset();
49 $controller->set('cid', $this->_contactId);
50 $controller->set('context', 'pledge');
51 $controller->set('limit', '25');
52 $controller->process();
53 $controller->run();
54
55 if ($this->_contactId) {
56 $displayName = CRM_Contact_BAO_Contact::displayName($this->_contactId);
57 $this->assign('displayName', $displayName);
58 $this->ajaxResponse['tabCount'] = CRM_Contact_BAO_Contact::getCountComponent('pledge', $this->_contactId);
59 // Refresh other tabs with related data
60 $this->ajaxResponse['updateTabs'] = array(
61 '#tab_contribute' => CRM_Contact_BAO_Contact::getCountComponent('contribution', $this->_contactId),
62 '#tab_activity' => CRM_Contact_BAO_Contact::getCountComponent('activity', $this->_contactId),
63 );
64 }
65 }
66
67 /**
68 * This function is called when action is view
69 *
70 * return null
71 * @access public
72 */
73 function view() {
74 $controller = new CRM_Core_Controller_Simple('CRM_Pledge_Form_PledgeView',
75 'View Pledge',
76 $this->_action
77 );
78 $controller->setEmbedded(TRUE);
79 $controller->set('id', $this->_id);
80 $controller->set('cid', $this->_contactId);
81
82 return $controller->run();
83 }
84
85 /**
86 * This function is called when action is update or new
87 *
88 * return null
89 * @access public
90 */
91 function edit() {
92 $controller = new CRM_Core_Controller_Simple('CRM_Pledge_Form_Pledge',
93 'Create Pledge',
94 $this->_action
95 );
96 $controller->setEmbedded(TRUE);
97 $controller->set('id', $this->_id);
98 $controller->set('cid', $this->_contactId);
99
100 return $controller->run();
101 }
102
103 function preProcess() {
104 $context = CRM_Utils_Request::retrieve('context', 'String', $this);
105 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'browse');
106 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
107
108 if ($context == 'standalone') {
109 $this->_action = CRM_Core_Action::ADD;
110 }
111 else {
112 $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE);
113 $this->assign('contactId', $this->_contactId);
114
115 // check logged in url permission
116 CRM_Contact_Page_View::checkUserPermission($this);
117 }
118
119 $this->assign('action', $this->_action);
120
121 if ($this->_permission == CRM_Core_Permission::EDIT && !CRM_Core_Permission::check('edit pledges')) {
122 // demote to view since user does not have edit pledge rights
123 $this->_permission = CRM_Core_Permission::VIEW;
124 $this->assign('permission', 'view');
125 }
126 }
127
128 /**
129 * This function is the main function that is called when the page loads, it decides the which action has to be taken for the page.
130 *
131 * return null
132 * @access public
133 */
134 function run() {
135 $this->preProcess();
136
137 // check if we can process credit card registration
138 $this->assign('newCredit', CRM_Core_Config::isEnabledBackOfficeCreditCardPayments());
139
140 self::setContext($this);
141
142 if ($this->_action & CRM_Core_Action::VIEW) {
143 $this->view();
144 }
145 elseif ($this->_action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD | CRM_Core_Action::DELETE)) {
146 $this->edit();
147 }
148 elseif ($this->_action & CRM_Core_Action::DETACH) {
149 CRM_Pledge_BAO_Pledge::cancel($this->_id);
150 $session = CRM_Core_Session::singleton();
151 $session->setStatus(ts('Pledge has been Cancelled and all scheduled (not completed) payments have been cancelled.<br />'));
152 CRM_Utils_System::redirect($session->popUserContext());
153 }
154 else {
155 $this->browse();
156 }
157
158 return parent::run();
159 }
160
161 public static function setContext(&$form) {
162 $context = CRM_Utils_Request::retrieve('context', 'String', $form, FALSE, 'search');
163
164 $qfKey = CRM_Utils_Request::retrieve('key', 'String', $form);
165 //validate the qfKey
166 if (!CRM_Utils_Rule::qfKey($qfKey)) {
167 $qfKey = NULL;
168 }
169
170 switch ($context) {
171 case 'dashboard':
172 case 'pledgeDashboard':
173 $url = CRM_Utils_System::url('civicrm/pledge', 'reset=1');
174 break;
175
176 case 'search':
177 $urlParams = 'force=1';
178 if ($qfKey) {
179 $urlParams .= "&qfKey=$qfKey";
180 }
181
182 $url = CRM_Utils_System::url('civicrm/pledge/search', $urlParams);
183 break;
184
185 case 'user':
186 $url = CRM_Utils_System::url('civicrm/user', 'reset=1');
187 break;
188
189 case 'pledge':
190 $url = CRM_Utils_System::url('civicrm/contact/view',
191 "reset=1&force=1&cid={$form->_contactId}&selectedChild=pledge"
192 );
193 break;
194
195 case 'home':
196 $url = CRM_Utils_System::url('civicrm/dashboard', 'force=1');
197 break;
198
199 case 'activity':
200 $url = CRM_Utils_System::url('civicrm/contact/view',
201 "reset=1&force=1&cid={$form->_contactId}&selectedChild=activity"
202 );
203 break;
204
205 case 'standalone':
206 $url = CRM_Utils_System::url('civicrm/dashboard', 'reset=1');
207 break;
208
209 default:
210 $cid = NULL;
211 if ($form->_contactId) {
212 $cid = '&cid=' . $form->_contactId;
213 }
214 $url = CRM_Utils_System::url('civicrm/pledge/search',
215 'force=1' . $cid
216 );
217 break;
218 }
219 $session = CRM_Core_Session::singleton();
220 $session->pushUserContext($url);
221 }
222 }
223