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