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