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