Merge pull request #4820 from kurund/CRM-15705
[civicrm-core.git] / CRM / Case / 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 case related functions
38 *
39 */
40 class CRM_Case_Page_Tab extends CRM_Core_Page {
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 public function preProcess() {
53 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'browse');
54 $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
55
56 //validate case configuration.
57 $configured = CRM_Case_BAO_Case::isCaseConfigured($this->_contactId);
58 $this->assign('notConfigured', !$configured['configured']);
59 $this->assign('allowToAddNewCase', $configured['allowToAddNewCase']);
60 $this->assign('redirectToCaseAdmin', $configured['redirectToCaseAdmin']);
61 if (!$configured['configured'] || $configured['redirectToCaseAdmin']) {
62 return;
63 }
64
65 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
66 $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this);
67
68 if ($this->_contactId) {
69 $this->assign('contactId', $this->_contactId);
70 // check logged in user permission
71 if ($this->_id && ($this->_action & CRM_Core_Action::VIEW)) {
72 //user might have special permissions to view this case, CRM-5666
73 if (!CRM_Core_Permission::check('access all cases and activities')) {
74 $session = CRM_Core_Session::singleton();
75 $userCases = CRM_Case_BAO_Case::getCases(FALSE, $session->get('userID'), 'any');
76 if (!array_key_exists($this->_id, $userCases)) {
77 CRM_Core_Error::fatal(ts('You are not authorized to access this page.'));
78 }
79 }
80 }
81 else {
82 CRM_Contact_Page_View::checkUserPermission($this);
83 }
84 }
85 else {
86 if ($this->_action & CRM_Core_Action::VIEW) {
87 CRM_Core_Error::fatal('Contact Id is required for view action.');
88 }
89 }
90
91 $activityTypes = CRM_Case_PseudoConstant::caseActivityType();
92
93 $this->assign('openCaseId', $activityTypes['Open Case']['id']);
94 $this->assign('changeCaseTypeId', $activityTypes['Change Case Type']['id']);
95 $this->assign('changeCaseStatusId', $activityTypes['Change Case Status']['id']);
96 $this->assign('changeCaseStartDateId', $activityTypes['Change Case Start Date']['id']);
97 }
98
99 /**
100 * View details of a case
101 *
102 * @return void
103 */
104 public function view() {
105 $controller = new CRM_Core_Controller_Simple(
106 'CRM_Case_Form_CaseView',
107 'View Case',
108 $this->_action,
109 FALSE,
110 FALSE,
111 TRUE
112 );
113 $controller->setEmbedded(TRUE);
114 $controller->set('id', $this->_id);
115 $controller->set('cid', $this->_contactId);
116 $controller->run();
117
118 $this->assign('caseId', $this->_id);
119 $output = CRM_Core_Selector_Controller::SESSION;
120 $selector = new CRM_Activity_Selector_Activity($this->_contactId, $this->_permission, FALSE, 'case');
121 $controller =
122 new CRM_Core_Selector_Controller(
123 $selector,
124 $this->get(CRM_Utils_Pager::PAGE_ID),
125 NULL,
126 CRM_Core_Action::VIEW,
127 $this,
128 $output,
129 NULL,
130 $this->_id
131 );
132
133
134 $controller->setEmbedded(TRUE);
135
136 $controller->run();
137 $controller->moveFromSessionToTemplate();
138
139 $this->assign('context', 'case');
140 }
141
142 /**
143 * This function is called when action is browse
144 *
145 * return null
146 */
147 public function browse() {
148
149 $controller = new CRM_Core_Controller_Simple('CRM_Case_Form_Search', ts('Case'), NULL);
150 $controller->setEmbedded(TRUE);
151 $controller->reset();
152 $controller->set('limit', 20);
153 $controller->set('force', 1);
154 $controller->set('context', 'case');
155 $controller->process();
156 $controller->run();
157
158 if ($this->_contactId) {
159 $displayName = CRM_Contact_BAO_Contact::displayName($this->_contactId);
160 $this->assign('displayName', $displayName);
161 $this->ajaxResponse['tabCount'] = CRM_Contact_BAO_Contact::getCountComponent('case', $this->_contactId);
162 }
163 }
164
165 /**
166 * This function is called when action is update or new
167 *
168 * return null
169 */
170 public function edit() {
171 $config = CRM_Core_Config::singleton();
172
173 $controller = new CRM_Core_Controller_Simple(
174 'CRM_Case_Form_Case',
175 'Open Case',
176 $this->_action
177 );
178
179 $controller->setEmbedded(TRUE);
180
181 return $controller->run();
182 }
183
184 /**
185 * This function is the main function that is called when the page loads,
186 * it decides the which action has to be taken for the page.
187 *
188 * return null
189 */
190 public function run() {
191 $contactID = CRM_Utils_Request::retrieve('cid', 'Positive', CRM_Core_DAO::$_nullArray);
192 $context = CRM_Utils_Request::retrieve('context', 'String', $this);
193
194 if ($context == 'standalone' && !$contactID) {
195 $this->_action = CRM_Core_Action::ADD;
196 }
197 else {
198 // we need to call parent preprocess only when we are viewing / editing / adding participant record
199 $this->preProcess();
200 }
201
202 $this->assign('action', $this->_action);
203
204 $this->setContext();
205
206 if ($this->_action & CRM_Core_Action::VIEW) {
207 $this->view();
208 }
209 elseif (($this->_action &
210 (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD |
211 CRM_Core_Action::DELETE | CRM_Core_Action::RENEW
212 )
213 ) ||
214 !empty($_POST)
215 ) {
216 $this->edit();
217 }
218 elseif ($this->_contactId) {
219 $this->browse();
220 }
221
222 return parent::run();
223 }
224
225 /**
226 * Get action links
227 *
228 * @return array (reference) of action links
229 * @static
230 */
231 static
232 public function &links() {
233 $config = CRM_Core_Config::singleton();
234
235 if (!(self::$_links)) {
236 $deleteExtra = ts('Are you sure you want to delete this case?');
237 self::$_links = array(
238 CRM_Core_Action::VIEW => array(
239 'name' => ts('Manage'),
240 'url' => 'civicrm/contact/view/case',
241 'qs' => 'action=view&reset=1&cid=%%cid%%&id=%%id%%',
242 'class' => 'no-popup',
243 'title' => ts('Manage Case'),
244 ),
245 CRM_Core_Action::DELETE => array(
246 'name' => ts('Delete'),
247 'url' => 'civicrm/contact/view/case',
248 'qs' => 'action=delete&reset=1&cid=%%cid%%&id=%%id%%',
249 'title' => ts('Delete Case'),
250 ),
251 );
252 }
253 return self::$_links;
254 }
255
256 public function setContext() {
257 $context = $this->get('context');
258 $url = NULL;
259
260 $qfKey = CRM_Utils_Request::retrieve('key', 'String', $this);
261 //validate the qfKey
262 if (!CRM_Utils_Rule::qfKey($qfKey)) {
263 $qfKey = NULL;
264 }
265
266 switch ($context) {
267 case 'activity':
268 if ($this->_contactId) {
269 $url = CRM_Utils_System::url('civicrm/contact/view',
270 "reset=1&force=1&cid={$this->_contactId}&selectedChild=activity"
271 );
272 }
273 break;
274
275 case 'dashboard':
276 $url = CRM_Utils_System::url('civicrm/case', "reset=1");
277 break;
278
279 case 'search':
280 $urlParams = 'force=1';
281 if ($qfKey) {
282 $urlParams .= "&qfKey=$qfKey";
283 }
284
285 $url = CRM_Utils_System::url('civicrm/case/search', $urlParams);
286 break;
287
288 case 'dashlet':
289 case 'dashletFullscreen':
290 case 'home':
291 case 'standalone':
292 $url = CRM_Utils_System::url('civicrm/dashboard', 'reset=1');
293 break;
294
295 case 'fulltext':
296 $action = CRM_Utils_Request::retrieve('action', 'String', $this);
297 $urlParams = 'force=1';
298 $urlString = 'civicrm/contact/search/custom';
299 if ($action == CRM_Core_Action::RENEW) {
300 if ($this->_contactId) {
301 $urlParams .= '&cid=' . $this->_contactId;
302 }
303 $urlParams .= '&context=fulltext&action=view';
304 $urlString = 'civicrm/contact/view/case';
305 }
306 if ($qfKey) {
307 $urlParams .= "&qfKey=$qfKey";
308 }
309 $url = CRM_Utils_System::url($urlString, $urlParams);
310 break;
311
312 default:
313 if ($this->_contactId) {
314 $url = CRM_Utils_System::url('civicrm/contact/view',
315 "reset=1&force=1&cid={$this->_contactId}&selectedChild=case"
316 );
317 }
318 break;
319 }
320
321 if ($url) {
322 $session = CRM_Core_Session::singleton();
323 $session->pushUserContext($url);
324 }
325 }
326 }