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