Merge pull request #94 from yashodha/CRM-12058
[civicrm-core.git] / CRM / Grant / Page / Tab.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
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
36/**
37 * This class handle grant related functions
38 *
39 */
40class 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 * This function is called when action is browse
54 *
55 * return null
56 * @access public
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 }
70 }
71
72 /**
73 * This function is called when action is view
74 *
75 * return null
76 * @access public
77 */
78 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 * This function is called when action is update or new
89 *
90 * return null
91 * @access public
92 */
93 function edit() {
94 $controller = new CRM_Core_Controller_Simple('CRM_Grant_Form_Grant', 'Create grant', $this->_action);
95 $controller->setEmbedded(TRUE);
96 $controller->set('id', $this->_id);
97 $controller->set('cid', $this->_contactId);
98
99 return $controller->run();
100 }
101
102 /**
103 * build all the data structures needed to build the form
104 *
105 * @return void
106 * @access public
107 */
108 function preProcess() {
109 $context = CRM_Utils_Request::retrieve('context', 'String', $this);
110 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'browse');
111 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
112
113 if ($context == 'standalone') {
114 $this->_action = CRM_Core_Action::ADD;
115 }
116 else {
117 $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE);
118 $displayName = CRM_Contact_BAO_Contact::displayName($this->_contactId);
119 $this->assign('contactId', $this->_contactId);
120 $this->assign('displayName', $displayName);
121
122 // check logged in url permission
123 CRM_Contact_Page_View::checkUserPermission($this);
124
125 // set page title
f08f6e94 126 CRM_Utils_System::setTitle(ts('Grant by') . ' ' . $displayName);
6a488035
TO
127 }
128 $this->assign('action', $this->_action);
129
130 if ($this->_permission == CRM_Core_Permission::EDIT && !CRM_Core_Permission::check('edit grants')) {
131 // demote to view since user does not have edit grant rights
132 $this->_permission = CRM_Core_Permission::VIEW;
133 $this->assign('permission', 'view');
134 }
135 }
136
137 /**
138 * This function is the main function that is called when the page loads,
139 * it decides the which action has to be taken for the page.
140 *
141 * return null
142 * @access public
143 */
144 function run() {
145 $this->preProcess();
146
147 $this->setContext();
148
149 if ($this->_action & CRM_Core_Action::VIEW) {
150 $this->view();
151 }
152 elseif ($this->_action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD | CRM_Core_Action::DELETE)) {
153 $this->edit();
154 }
155 else {
156 $this->browse();
157 }
158 return parent::run();
159 }
160
161 function setContext() {
162 $context = CRM_Utils_Request::retrieve('context', 'String', $this);
163 $this->_id = CRM_Utils_Request::retrieve('id', 'Integer', $this);
164 $session = CRM_Core_Session::singleton();
165
166 $qfKey = CRM_Utils_Request::retrieve('key', 'String', $this);
167 //validate the qfKey
168 if (!CRM_Utils_Rule::qfKey($qfKey)) {
169 $qfKey = NULL;
170 }
171
172 switch ($context) {
173 case 'search':
174 $urlParams = 'force=1';
175 if ($qfKey) {
176 $urlParams .= "&qfKey=$qfKey";
177 }
178 $this->assign('searchKey', $qfKey);
179
180 $url = CRM_Utils_System::url('civicrm/grant/search', $urlParams);
181 break;
182
183 case 'dashboard':
184 $url = CRM_Utils_System::url('civicrm/grant', 'reset=1');
185 break;
186
187 case 'edit':
188 $url = CRM_utils_System::url('civicrm/contact/view/grant', 'reset=1&id=' . $this->_id . '&cid=' . $this->_contactId . '&action=view&context=grant&selectedChild=grant');
189 break;
190
191 case 'grant':
192 $url = CRM_Utils_System::url('civicrm/contact/view', 'action=browse&selectedChild=grant&cid=' . $this->_contactId);
193 break;
194
195 case 'standalone':
196 $url = CRM_Utils_System::url('civicrm/dashboard', 'reset=1');
197 break;
198
199 default:
200 $cid = NULL;
201 if ($this->_contactId) {
202 $cid = '&cid=' . $this->_contactId;
203 }
204 $url = CRM_Utils_System::url('civicrm/grant/search', 'reset=1&force=1' . $cid);
205 break;
206 }
207 $session->pushUserContext($url);
208
209 if (CRM_Utils_Request::retrieve('confirmed', 'Boolean',
210 CRM_Core_DAO::$_nullObject
211 )) {
212 CRM_Grant_BAO_Grant::del($this->_id);
213 CRM_Utils_System::redirect($url);
214 }
215 }
216}
217