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