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