afee03c000856f49735dfba9e68e0a29c51b4758
[civicrm-core.git] / CRM / Case / Form / CaseView.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
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 generates view mode for CiviCase
38 *
39 */
40 class CRM_Case_Form_CaseView extends CRM_Core_Form {
41 /*
42 * check for merge cases.
43 */
44
45 private $_mergeCases = FALSE;
46
47 /**
48 * Function to set variables up before form is built
49 *
50 * @return void
51 * @access public
52 */
53 public function preProcess() {
54 $this->_showRelatedCases = CRM_Utils_Array::value('relatedCases', $_GET);
55
56 $xmlProcessorProcess = new CRM_Case_XMLProcessor_Process();
57 $isMultiClient = $xmlProcessorProcess->getAllowMultipleCaseClients();
58 $this->assign('multiClient', $isMultiClient);
59
60 //pull the related cases.
61 $this->assign('showRelatedCases', FALSE);
62 if ($this->_showRelatedCases) {
63 $relatedCases = $this->get('relatedCases');
64 if (!isset($relatedCases)) {
65 $cId = CRM_Utils_Request::retrieve('cid', 'Integer', CRM_Core_DAO::$_nullObject);
66 $caseId = CRM_Utils_Request::retrieve('id', 'Integer', CRM_Core_DAO::$_nullObject);
67 $relatedCases = CRM_Case_BAO_Case::getRelatedCases($caseId, $cId);
68 }
69 $this->assign('relatedCases', $relatedCases);
70 $this->assign('showRelatedCases', TRUE);
71 CRM_Utils_System::setTitle(ts('Related Cases'));
72 return;
73 }
74
75 //check for civicase access.
76 if (!CRM_Case_BAO_Case::accessCiviCase()) {
77 CRM_Core_Error::fatal(ts('You are not authorized to access this page.'));
78 }
79 $this->_hasAccessToAllCases = CRM_Core_Permission::check('access all cases and activities');
80 $this->assign('hasAccessToAllCases', $this->_hasAccessToAllCases);
81
82 $this->_contactID = $this->get('cid');
83 $this->_caseID = $this->get('id');
84
85 $fulltext = CRM_Utils_Request::retrieve('context', 'String', CRM_Core_DAO::$_nullObject);
86 if ($fulltext == 'fulltext') {
87 $this->assign('fulltext', $fulltext);
88 }
89
90 $this->assign('caseID', $this->_caseID);
91 $this->assign('contactID', $this->_contactID);
92
93 //validate case id.
94 $this->_userCases = array();
95 $session = CRM_Core_Session::singleton();
96 $userID = $session->get('userID');
97 if (!$this->_hasAccessToAllCases) {
98 $this->_userCases = CRM_Case_BAO_Case::getCases(FALSE, $userID, 'any');
99 if (!array_key_exists($this->_caseID, $this->_userCases)) {
100 CRM_Core_Error::fatal(ts('You are not authorized to access this page.'));
101 }
102 }
103 $this->assign('userID', $userID);
104
105 if (CRM_Case_BAO_Case::caseCount($this->_contactID) >= 2) {
106 $this->_mergeCases = TRUE;
107 }
108 $this->assign('mergeCases', $this->_mergeCases);
109
110 //retrieve details about case
111 $params = array('id' => $this->_caseID);
112
113 $returnProperties = array('case_type_id', 'subject', 'status_id', 'start_date');
114 CRM_Core_DAO::commonRetrieve('CRM_Case_BAO_Case', $params, $values, $returnProperties);
115
116 $values['case_type_id'] = trim(CRM_Utils_Array::value('case_type_id', $values),
117 CRM_Core_DAO::VALUE_SEPARATOR
118 );
119 $values['case_type_id'] = explode(CRM_Core_DAO::VALUE_SEPARATOR,
120 CRM_Utils_Array::value('case_type_id', $values)
121 );
122
123 $statuses = CRM_Case_PseudoConstant::caseStatus('label', FALSE);
124 $caseTypeName = CRM_Case_BAO_Case::getCaseType($this->_caseID, 'name');
125 $caseType = CRM_Case_BAO_Case::getCaseType($this->_caseID);
126
127 $this->_caseDetails = array(
128 'case_type' => $caseType,
129 'case_status' => $statuses[$values['case_status_id']],
130 'case_subject' => CRM_Utils_Array::value('subject', $values),
131 'case_start_date' => $values['case_start_date'],
132 );
133 $this->_caseType = $caseTypeName;
134 $this->assign('caseDetails', $this->_caseDetails);
135
136 $reportUrl = CRM_Utils_System::url('civicrm/case/report',
137 "reset=1&cid={$this->_contactID}&caseid={$this->_caseID}&asn=",
138 FALSE, NULL, FALSE
139 );
140 $this->assign('reportUrl', $reportUrl);
141
142 // add to recently viewed
143
144 $url = CRM_Utils_System::url('civicrm/contact/view/case',
145 "action=view&reset=1&id={$this->_caseID}&cid={$this->_contactID}&context=home"
146 );
147
148 $displayName = CRM_Contact_BAO_Contact::displayName($this->_contactID);
149 $this->assign('displayName', $displayName);
150
151 CRM_Utils_System::setTitle($displayName . ' - ' . $caseType);
152
153 $recentOther = array();
154 if (CRM_Core_Permission::checkActionPermission('CiviCase', CRM_Core_Action::DELETE)) {
155 $recentOther['deleteUrl'] = CRM_Utils_System::url('civicrm/contact/view/case',
156 "action=delete&reset=1&id={$this->_caseID}&cid={$this->_contactID}&context=home"
157 );
158 }
159
160 // Add the recently viewed case
161 CRM_Utils_Recent::add($displayName . ' - ' . $caseType,
162 $url,
163 $this->_caseID,
164 'Case',
165 $this->_contactID,
166 NULL,
167 $recentOther
168 );
169
170
171 //get the related cases for given case.
172 $relatedCases = $this->get('relatedCases');
173 if (!isset($relatedCases)) {
174 $relatedCases = CRM_Case_BAO_Case::getRelatedCases($this->_caseID, $this->_contactID);
175 $relatedCases = empty($relatedCases) ? FALSE : $relatedCases;
176 $this->set('relatedCases', $relatedCases);
177 }
178 $this->assign('hasRelatedCases', (bool) $relatedCases);
179 if ($relatedCases) {
180 $this->assign('relatedCaseLabel', ts('%1 Related Case', array('count' => count($relatedCases), 'plural' => '%1 Related Cases')));
181 $this->assign('relatedCaseUrl', CRM_Utils_System::url('civicrm/contact/view/case', array(
182 'id' => $this->_caseID,
183 'cid' => $this->_contactID,
184 'relatedCases' => 1,
185 'action' => 'view',
186 )));
187 }
188
189 $entitySubType = !empty($values['case_type_id']) ? $values['case_type_id'][0] : NULL;
190 $this->assign('caseTypeID', $entitySubType);
191 $groupTree = &CRM_Core_BAO_CustomGroup::getTree('Case',
192 $this,
193 $this->_caseID,
194 NULL,
195 $entitySubType
196 );
197 CRM_Core_BAO_CustomGroup::buildCustomDataView($this,
198 $groupTree
199 );
200 }
201
202 /**
203 * This function sets the default values for the form. For edit/view mode
204 * the default values are retrieved from the database
205 *
206 * @access public
207 *
208 * @return void
209 */
210 function setDefaultValues() {
211 $defaults = array();
212 return $defaults;
213 }
214
215 /**
216 * Function to build the form
217 *
218 * @return void
219 * @access public
220 */
221 public function buildQuickForm() {
222 //this call is for show related cases.
223 if ($this->_showRelatedCases) {
224 return;
225 }
226
227 CRM_Core_Resources::singleton()
228 ->addScriptFile('civicrm', 'js/crm.livePage.js')
229 ->addScriptFile('civicrm', 'templates/CRM/Case/Form/CaseView.js');
230
231 $xmlProcessor = new CRM_Case_XMLProcessor_Process();
232 $caseRoles = $xmlProcessor->get($this->_caseType, 'CaseRoles');
233 $reports = $xmlProcessor->get($this->_caseType, 'ActivitySets');
234
235 //adding case manager.CRM-4510.
236 $managerRoleId = $xmlProcessor->getCaseManagerRoleId($this->_caseType);
237 if (!empty($managerRoleId)) {
238 $caseRoles[$managerRoleId] = $caseRoles[$managerRoleId] . '<br />' . '(' . ts('Case Manager') . ')';
239 }
240
241 $aTypes = $xmlProcessor->get($this->_caseType, 'ActivityTypes', TRUE);
242
243 $allActTypes = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'name');
244
245 $emailActivityType = array_search('Email', $allActTypes);
246
247 // remove Open Case activity type since we're inside an existing case
248 if ($openActTypeId = array_search('Open Case', $allActTypes)) {
249 unset($aTypes[$openActTypeId]);
250 }
251
252 //check for link cases.
253 $unclosedCases = CRM_Case_BAO_Case::getUnclosedCases(NULL, array($this->_caseID));
254 if (empty($unclosedCases) && ($linkActTypeId = array_search('Link Cases', $allActTypes))) {
255 unset($aTypes[$linkActTypeId]);
256 }
257
258 if (!$xmlProcessor->getNaturalActivityTypeSort()) {
259 asort($aTypes);
260 }
261
262 $activityLinks = array('' => ts('Add Activity'));
263 foreach ($aTypes as $type => $label) {
264 if ($type == $emailActivityType) {
265 $url = CRM_Utils_System::url('civicrm/activity/email/add',
266 "action=add&context=standalone&reset=1&caseid={$this->_caseID}&atype=$type",
267 FALSE, NULL, FALSE
268 );
269 }
270 else {
271 $url = CRM_Utils_System::url('civicrm/case/activity',
272 "action=add&reset=1&cid={$this->_contactID}&caseid={$this->_caseID}&atype=$type",
273 FALSE, NULL, FALSE
274 );
275 }
276 $activityLinks[$url] = $label;
277 }
278
279 $this->add('select', 'add_activity_type_id', '', $activityLinks, FALSE, array('class' => 'crm-select2 crm-action-menu twenty'));
280 if ($this->_hasAccessToAllCases) {
281 $this->add('select', 'report_id', '',
282 array('' => ts('Run QA Audit / Redact')) + $reports,
283 FALSE,
284 array('class' => 'crm-select2 crm-action-menu')
285 );
286 $this->add('select', 'timeline_id', '',
287 array('' => ts('Add Timeline')) + $reports,
288 FALSE,
289 array('class' => 'crm-select2 crm-action-menu')
290 );
291 }
292 $this->addElement('submit', $this->getButtonName('next'), ' ', array('class' => 'hiddenElement'));
293
294 if ($this->_mergeCases) {
295 $allCases = CRM_Case_BAO_Case::getContactCases($this->_contactID);
296 $otherCases = array();
297 foreach ($allCases as $caseId => $details) {
298 //filter current and own cases.
299 if (($caseId == $this->_caseID) ||
300 (!$this->_hasAccessToAllCases &&
301 !array_key_exists($caseId, $this->_userCases)
302 )
303 ) {
304 continue;
305 }
306
307 $otherCases[$caseId] = 'Case ID: ' . $caseId . ' Type: ' . $details['case_type'] . ' Start: ' . $details['case_start_date'];
308 }
309 if (empty($otherCases)) {
310 $this->_mergeCases = FALSE;
311 $this->assign('mergeCases', $this->_mergeCases);
312 }
313 else {
314 $this->add('select', 'merge_case_id',
315 ts('Select Case for Merge'),
316 array(
317 '' => ts('- select case -')) + $otherCases,
318 FALSE,
319 array('class' => 'crm-select2 huge')
320 );
321 $this->addElement('submit',
322 $this->getButtonName('next', 'merge_case'),
323 ts('Merge'),
324 array(
325 'class' => 'form-submit-inline hiddenElement',
326 )
327 );
328 }
329 }
330
331 $activityStatus = CRM_Core_PseudoConstant::activityStatus();
332 $this->add('select', 'status_id', ts('Status'), array("" => ts(' - any status - ')) + $activityStatus);
333
334 // activity dates
335 $this->addDate('activity_date_low', ts('Activity Dates - From'), FALSE, array('formatType' => 'searchDate'));
336 $this->addDate('activity_date_high', ts('To'), FALSE, array('formatType' => 'searchDate'));
337
338 if (CRM_Core_Permission::check('administer CiviCRM')) {
339 $this->add('checkbox', 'activity_deleted', ts('Deleted Activities'));
340 }
341
342 //get case related relationships (Case Role)
343 $caseRelationships = CRM_Case_BAO_Case::getCaseRoles($this->_contactID, $this->_caseID);
344
345 //save special label because we unset it in the loop
346 $managerLabel = empty($managerRoleId) ? '' : $caseRoles[$managerRoleId];
347
348 //build reporter select
349 $reporters = array("" => ts(' - any reporter - '));
350 foreach ($caseRelationships as $key => & $value) {
351 $reporters[$value['cid']] = $value['name'] . " ( {$value['relation']} )";
352
353 if (!empty($managerRoleId)) {
354 if ($managerRoleId == $value['relation_type']) {
355 $value['relation'] = $managerLabel;
356 }
357 }
358
359 //calculate roles that don't have relationships
360 if (!empty($caseRoles[$value['relation_type']])) {
361 unset($caseRoles[$value['relation_type']]);
362 }
363 }
364
365 // take all case activity types for search filter, CRM-7187
366 $aTypesFilter = array();
367 $allCaseActTypes = CRM_Case_PseudoConstant::caseActivityType();
368 foreach ($allCaseActTypes as $typeDetails) {
369 if (!in_array($typeDetails['name'], array(
370 'Open Case'))) {
371 $aTypesFilter[$typeDetails['id']] = CRM_Utils_Array::value('label', $typeDetails);
372 }
373 }
374 asort($aTypesFilter);
375 $this->add('select', 'activity_type_filter_id', ts('Activity Type'), array('' => ts('- select activity type -')) + $aTypesFilter);
376
377 $this->assign('caseRelationships', $caseRelationships);
378
379 //also add client as role. CRM-4438
380 $caseRoles['client'] = CRM_Case_BAO_Case::getContactNames($this->_caseID);
381
382 $this->assign('caseRoles', $caseRoles);
383
384 $this->add('select', 'reporter_id', ts('Reporter/Role'), $reporters);
385
386 // Retrieve ALL client relationships
387 $relClient = CRM_Contact_BAO_Relationship::getRelationship($this->_contactID,
388 CRM_Contact_BAO_Relationship::CURRENT,
389 0, 0, 0, NULL, NULL, FALSE
390 );
391
392 // Now build 'Other Relationships' array by removing relationships that are already listed under Case Roles
393 // so they don't show up twice.
394 $clientRelationships = array();
395 foreach ($relClient as $r) {
396 if (!array_key_exists($r['id'], $caseRelationships)) {
397 $clientRelationships[] = $r;
398 }
399 }
400 $this->assign('clientRelationships', $clientRelationships);
401
402 // Now global contact list that appears on all cases.
403 $globalGroupInfo = array();
404 $relGlobal = CRM_Case_BAO_Case::getGlobalContacts($globalGroupInfo);
405 $this->assign('globalRelationships', $relGlobal);
406 $this->assign('globalGroupInfo', $globalGroupInfo);
407
408 // List of relationship types
409 $baoRel = new CRM_Contact_BAO_Relationship();
410 $relType = $baoRel->getRelationType('Individual');
411 $roleTypes = array();
412 foreach ($relType as $k => $v) {
413 $roleTypes[substr($k, 0, strpos($k, '_'))] = $v;
414 }
415 $this->add('select', 'role_type', ts('Relationship Type'), array('' => ts('- select type -')) + $roleTypes);
416
417 $hookCaseSummary = CRM_Utils_Hook::caseSummary($this->_caseID);
418 if (is_array($hookCaseSummary)) {
419 $this->assign('hookCaseSummary', $hookCaseSummary);
420 }
421
422
423 $allTags = CRM_Core_BAO_Tag::getTags('civicrm_case');
424
425 if (!empty($allTags)) {
426 $this->add('select', 'case_tag', ts('Tags'), $allTags, FALSE,
427 array('id' => 'tags', 'multiple' => 'multiple', 'class' => 'crm-select2')
428 );
429
430 $tags = CRM_Core_BAO_EntityTag::getTag($this->_caseID, 'civicrm_case');
431
432 $this->setDefaults(array('case_tag' => $tags));
433
434 foreach ($tags as $tid) {
435 if (isset($allTags[$tid])) {
436 $tags[$tid] = $allTags[$tid];
437 }
438 else {
439 unset($tags[$tid]);
440 }
441 }
442
443 $this->assign('tags', implode(', ', array_filter($tags)));
444 $this->assign('showTags', TRUE);
445 }
446 else {
447 $this->assign('showTags', FALSE);
448 }
449
450 // build tagset widget
451
452 // see if we have any tagsets which can be assigned to cases
453 $parentNames = CRM_Core_BAO_Tag::getTagSet('civicrm_case');
454 if ($parentNames) {
455 $this->assign('showTagsets', TRUE);
456 }
457 else {
458 $this->assign('showTagsets', FALSE);
459 }
460 CRM_Core_Form_Tag::buildQuickForm($this, $parentNames, 'civicrm_case', $this->_caseID, TRUE, TRUE);
461
462 $this->addButtons(array(
463 array(
464 'type' => 'cancel',
465 'name' => ts('Done'),
466 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
467 'isDefault' => TRUE,
468 ),
469 )
470 );
471 }
472
473 /**
474 * Process the form
475 *
476 * @return void
477 * @access public
478 */
479 public function postProcess() {
480 $params = $this->controller->exportValues($this->_name);
481 $buttonName = $this->controller->getButtonName();
482
483 // user context
484 $url = CRM_Utils_System::url('civicrm/contact/view/case',
485 "reset=1&action=view&cid={$this->_contactID}&id={$this->_caseID}&show=1"
486 );
487 $session = CRM_Core_Session::singleton();
488 $session->pushUserContext($url);
489
490 if (!empty($params['timeline_id']) && !empty($_POST['_qf_CaseView_next'])) {
491 $session = CRM_Core_Session::singleton();
492 $this->_uid = $session->get('userID');
493 $xmlProcessor = new CRM_Case_XMLProcessor_Process();
494 $xmlProcessorParams = array(
495 'clientID' => $this->_contactID,
496 'creatorID' => $this->_uid,
497 'standardTimeline' => 0,
498 'activity_date_time' => date('YmdHis'),
499 'caseID' => $this->_caseID,
500 'caseType' => $this->_caseType,
501 'activitySetName' => $params['timeline_id'],
502 );
503 $xmlProcessor->run($this->_caseType, $xmlProcessorParams);
504 $reports = $xmlProcessor->get($this->_caseType, 'ActivitySets');
505
506 CRM_Core_Session::setStatus(ts('Activities from the %1 activity set have been added to this case.',
507 array(1 => $reports[$params['timeline_id']])
508 ), ts('Done'), 'success');
509 }
510 elseif ($this->_mergeCases &&
511 $buttonName == '_qf_CaseView_next_merge_case'
512 ) {
513
514 $mainCaseId = $params['merge_case_id'];
515 $otherCaseId = $this->_caseID;
516
517 //merge two cases.
518 CRM_Case_BAO_Case::mergeCases($this->_contactID, $mainCaseId, NULL, $otherCaseId);
519
520 //redirect user to main case view.
521 $url = CRM_Utils_System::url('civicrm/contact/view/case',
522 "reset=1&action=view&cid={$this->_contactID}&id={$mainCaseId}&show=1"
523 );
524 $session = CRM_Core_Session::singleton();
525 $session->pushUserContext($url);
526 }
527 }
528 }
529