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