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