Merge pull request #4896 from totten/master-movedep
[civicrm-core.git] / CRM / Grant / Page / Tab.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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
36 /**
37 * This class handle grant related functions
38 *
39 */
40 class CRM_Grant_Page_Tab extends CRM_Contact_Page_View {
41
42 /**
43 * The action links that we need to display for the browse screen
44 *
45 * @var array
46 * @static
47 */
48 static $_links = NULL;
49 public $_permission = NULL;
50 public $_contactId = NULL;
51
52 /**
53 * called when action is browse
54 *
55 * return null
56 */
57 function browse() {
58 $controller = new CRM_Core_Controller_Simple('CRM_Grant_Form_Search', ts('Grants'), $this->_action);
59 $controller->setEmbedded(TRUE);
60 $controller->reset();
61 $controller->set('cid', $this->_contactId);
62 $controller->set('context', 'grant');
63 $controller->process();
64 $controller->run();
65
66 if ($this->_contactId) {
67 $displayName = CRM_Contact_BAO_Contact::displayName($this->_contactId);
68 $this->assign('displayName', $displayName);
69 $this->ajaxResponse['tabCount'] = CRM_Contact_BAO_Contact::getCountComponent('grant', $this->_contactId);
70 }
71 }
72
73 /**
74 * called when action is view
75 *
76 * return null
77 */
78 public function view() {
79 $controller = new CRM_Core_Controller_Simple('CRM_Grant_Form_GrantView', 'View Grant', $this->_action);
80 $controller->setEmbedded(TRUE);
81 $controller->set('id', $this->_id);
82 $controller->set('cid', $this->_contactId);
83
84 return $controller->run();
85 }
86
87 /**
88 * called when action is update or new
89 *
90 * return null
91 */
92 public function edit() {
93 $controller = new CRM_Core_Controller_Simple('CRM_Grant_Form_Grant', 'Create grant', $this->_action);
94 $controller->setEmbedded(TRUE);
95 $controller->set('id', $this->_id);
96 $controller->set('cid', $this->_contactId);
97
98 return $controller->run();
99 }
100
101 /**
102 * Build all the data structures needed to build the form
103 *
104 * @return void
105 */
106 public function preProcess() {
107 $context = CRM_Utils_Request::retrieve('context', 'String', $this);
108 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'browse');
109 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
110
111 if ($context == 'standalone') {
112 $this->_action = CRM_Core_Action::ADD;
113 }
114 else {
115 $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE);
116 $displayName = CRM_Contact_BAO_Contact::displayName($this->_contactId);
117 $this->assign('contactId', $this->_contactId);
118 $this->assign('displayName', $displayName);
119
120 // check logged in url permission
121 CRM_Contact_Page_View::checkUserPermission($this);
122 }
123 $this->assign('action', $this->_action);
124
125 if ($this->_permission == CRM_Core_Permission::EDIT && !CRM_Core_Permission::check('edit grants')) {
126 // demote to view since user does not have edit grant rights
127 $this->_permission = CRM_Core_Permission::VIEW;
128 $this->assign('permission', 'view');
129 }
130 }
131
132 /**
133 * the main function that is called when the page loads,
134 * it decides the which action has to be taken for the page.
135 *
136 * return null
137 */
138 public function run() {
139 $this->preProcess();
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 else {
150 $this->browse();
151 }
152 return parent::run();
153 }
154
155 public function setContext() {
156 $context = CRM_Utils_Request::retrieve('context', 'String', $this);
157 $this->_id = CRM_Utils_Request::retrieve('id', 'Integer', $this);
158 $session = CRM_Core_Session::singleton();
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 'search':
168 $urlParams = 'force=1';
169 if ($qfKey) {
170 $urlParams .= "&qfKey=$qfKey";
171 }
172 $this->assign('searchKey', $qfKey);
173
174 $url = CRM_Utils_System::url('civicrm/grant/search', $urlParams);
175 break;
176
177 case 'dashboard':
178 $url = CRM_Utils_System::url('civicrm/grant', 'reset=1');
179 break;
180
181 case 'edit':
182 $url = CRM_utils_System::url('civicrm/contact/view/grant', 'reset=1&id=' . $this->_id . '&cid=' . $this->_contactId . '&action=view&context=grant&selectedChild=grant');
183 break;
184
185 case 'grant':
186 $url = CRM_Utils_System::url('civicrm/contact/view', 'action=browse&selectedChild=grant&cid=' . $this->_contactId);
187 break;
188
189 case 'standalone':
190 $url = CRM_Utils_System::url('civicrm/dashboard', 'reset=1');
191 break;
192
193 default:
194 $cid = NULL;
195 if ($this->_contactId) {
196 $cid = '&cid=' . $this->_contactId;
197 }
198 $url = CRM_Utils_System::url('civicrm/grant/search', 'reset=1&force=1' . $cid);
199 break;
200 }
201 $session->pushUserContext($url);
202
203 if (CRM_Utils_Request::retrieve('confirmed', 'Boolean',
204 CRM_Core_DAO::$_nullObject
205 )
206 ) {
207 CRM_Grant_BAO_Grant::del($this->_id);
208 CRM_Utils_System::redirect($url);
209 }
210 }
211 }