3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class generates view mode for CiviCase.
21 class CRM_Case_Form_CaseView
extends CRM_Core_Form
{
23 * Check for merge cases.
26 private $_mergeCases = FALSE;
29 * Set variables up before form is built.
31 public function preProcess() {
32 $this->_showRelatedCases
= $_GET['relatedCases'] ??
NULL;
34 $xmlProcessorProcess = new CRM_Case_XMLProcessor_Process();
35 $isMultiClient = $xmlProcessorProcess->getAllowMultipleCaseClients();
36 $this->assign('multiClient', $isMultiClient);
38 //pull the related cases.
39 $this->assign('showRelatedCases', FALSE);
40 if ($this->_showRelatedCases
) {
41 $relatedCases = $this->get('relatedCases');
42 if (!isset($relatedCases)) {
43 $cId = CRM_Utils_Request
::retrieve('cid', 'Integer');
44 $caseId = CRM_Utils_Request
::retrieve('id', 'Integer');
45 $relatedCases = CRM_Case_BAO_Case
::getRelatedCases($caseId);
47 $this->assign('relatedCases', $relatedCases);
48 $this->assign('showRelatedCases', TRUE);
49 CRM_Utils_System
::setTitle(ts('Related Cases'));
53 $this->_hasAccessToAllCases
= CRM_Core_Permission
::check('access all cases and activities');
54 $this->assign('hasAccessToAllCases', $this->_hasAccessToAllCases
);
56 $this->assign('contactID', $this->_contactID
= (int) $this->get('cid'));
57 $this->assign('caseID', $this->_caseID
= (int) $this->get('id'));
60 if (!CRM_Case_BAO_Case
::accessCase($this->_caseID
, FALSE)) {
61 CRM_Core_Error
::statusBounce(ts('You do not have permission to access this case.'));
64 $fulltext = CRM_Utils_Request
::retrieve('context', 'Alphanumeric');
65 if ($fulltext == 'fulltext') {
66 $this->assign('fulltext', $fulltext);
69 $this->assign('contactType', CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactID
, 'contact_type'));
70 $this->assign('userID', CRM_Core_Session
::getLoggedInContactID());
72 //retrieve details about case
73 $params = ['id' => $this->_caseID
];
75 $returnProperties = ['case_type_id', 'subject', 'status_id', 'start_date'];
76 CRM_Core_DAO
::commonRetrieve('CRM_Case_BAO_Case', $params, $values, $returnProperties);
78 $statuses = CRM_Case_PseudoConstant
::caseStatus('label', FALSE);
79 $caseTypeName = CRM_Case_BAO_Case
::getCaseType($this->_caseID
, 'name');
80 $caseType = CRM_Case_BAO_Case
::getCaseType($this->_caseID
);
82 $this->_caseDetails
= [
83 'case_type' => $caseType,
84 'case_status' => $statuses[$values['case_status_id']] ??
NULL,
85 'case_subject' => $values['subject'] ??
NULL,
86 'case_start_date' => $values['case_start_date'],
88 $this->_caseType
= $caseTypeName;
89 $this->assign('caseDetails', $this->_caseDetails
);
91 // add to recently viewed
93 $url = CRM_Utils_System
::url('civicrm/contact/view/case',
94 "action=view&reset=1&id={$this->_caseID}&cid={$this->_contactID}&context=home"
97 $displayName = CRM_Contact_BAO_Contact
::displayName($this->_contactID
);
98 $this->assign('displayName', $displayName);
100 CRM_Utils_System
::setTitle($displayName . ' - ' . $caseType);
103 if (CRM_Core_Permission
::checkActionPermission('CiviCase', CRM_Core_Action
::DELETE
)) {
104 $recentOther['deleteUrl'] = CRM_Utils_System
::url('civicrm/contact/view/case',
105 "action=delete&reset=1&id={$this->_caseID}&cid={$this->_contactID}&context=home"
109 // Add the recently viewed case
110 CRM_Utils_Recent
::add($displayName . ' - ' . $caseType,
119 //get the related cases for given case.
120 $relatedCases = $this->get('relatedCases');
121 if (!isset($relatedCases)) {
122 $relatedCases = CRM_Case_BAO_Case
::getRelatedCases($this->_caseID
);
123 $relatedCases = empty($relatedCases) ?
FALSE : $relatedCases;
124 $this->set('relatedCases', $relatedCases);
126 $this->assign('hasRelatedCases', (bool) $relatedCases);
128 $this->assign('relatedCaseLabel', ts('%1 Related Case', [
129 'count' => count($relatedCases),
130 'plural' => '%1 Related Cases',
132 $this->assign('relatedCaseUrl', CRM_Utils_System
::url('civicrm/contact/view/case', [
133 'id' => $this->_caseID
,
134 'cid' => $this->_contactID
,
140 $entitySubType = !empty($values['case_type_id']) ?
$values['case_type_id'] : NULL;
141 $this->assign('caseTypeID', $entitySubType);
142 $groupTree = CRM_Core_BAO_CustomGroup
::getTree('Case',
148 CRM_Core_BAO_CustomGroup
::buildCustomDataView($this, $groupTree, FALSE, NULL, NULL, NULL, $this->_caseID
);
152 * Set default values for the form.
156 public function setDefaultValues() {
162 * Build the form object.
164 public function buildQuickForm() {
165 //this call is for show related cases.
166 if ($this->_showRelatedCases
) {
170 $allowedRelationshipTypes = CRM_Contact_BAO_Relationship
::getContactRelationshipType($this->_contactID
);
171 $relationshipTypeMetadata = CRM_Contact_Form_Relationship
::getRelationshipTypeMetadata($allowedRelationshipTypes);
173 $caseTypeDefinition = civicrm_api3('CaseType', 'getsingle', ['name' => $this->_caseType
])['definition'];
175 foreach ($caseTypeDefinition['caseRoles'] as $role) {
176 if (!empty($role['groups'])) {
177 $relationshipType = civicrm_api3('RelationshipType', 'get', [
179 'name_a_b' => $role['name'],
180 'name_b_a' => $role['name'],
181 'options' => ['limit' => 1, 'or' => [["name_a_b", "name_b_a"]]],
183 if (($relationshipType['values'][0]['name_a_b'] ??
NULL) === $role['name']) {
184 $relationshipTypeMetadata[$relationshipType['id']]['group_a'] = $role['groups'];
186 if (($relationshipType['values'][0]['name_b_a'] ??
NULL) === $role['name']) {
187 $relationshipTypeMetadata[$relationshipType['id']]['group_b'] = $role['groups'];
192 CRM_Core_Resources
::singleton()
193 ->addScriptFile('civicrm', 'js/crm.livePage.js', 1, 'html-header')
194 ->addScriptFile('civicrm', 'templates/CRM/Case/Form/CaseView.js', 2, 'html-header')
195 ->addVars('relationshipTypes', $relationshipTypeMetadata);
197 $xmlProcessor = new CRM_Case_XMLProcessor_Process();
198 $caseRoles = $xmlProcessor->get($this->_caseType
, 'CaseRoles');
199 $reports = $xmlProcessor->get($this->_caseType
, 'ActivitySets');
201 //adding case manager.CRM-4510.
202 $managerRoleId = $xmlProcessor->getCaseManagerRoleId($this->_caseType
);
203 if (!empty($managerRoleId)) {
204 $caseRoles[$managerRoleId] = $caseRoles[$managerRoleId] . '<br />' . '(' . ts('Case Manager') . ')';
207 $aTypes = $xmlProcessor->get($this->_caseType
, 'ActivityTypes', TRUE);
209 $allActTypes = CRM_Activity_BAO_Activity
::buildOptions('activity_type_id', 'validate');
210 $emailActivityType = array_search('Email', $allActTypes);
211 $pdfActivityType = array_search('Print PDF Letter', $allActTypes);
213 if ($pdfActivityType) {
214 $this->assign('exportDoc', CRM_Utils_System
::url('civicrm/activity/pdf/add',
215 "action=add&context=standalone&reset=1&cid={$this->_contactID}&caseid={$this->_caseID}&atype=$pdfActivityType"));
218 // remove Open Case activity type since we're inside an existing case
219 if ($openActTypeId = array_search('Open Case', $allActTypes)) {
220 unset($aTypes[$openActTypeId]);
223 // Only show "link cases" activity if other cases exist.
224 $linkActTypeId = array_search('Link Cases', $allActTypes);
225 if ($linkActTypeId) {
226 $count = civicrm_api3('Case', 'getcount', [
227 'check_permissions' => TRUE,
228 'id' => ['!=' => $this->_caseID
],
232 unset($aTypes[$linkActTypeId]);
236 if (!$xmlProcessor->getNaturalActivityTypeSort()) {
240 $activityLinks = ['' => ts('Add Activity')];
241 foreach ($aTypes as $type => $label) {
242 if ($type == $emailActivityType) {
243 $url = CRM_Utils_System
::url('civicrm/activity/email/add',
244 "action=add&context=standalone&reset=1&caseid={$this->_caseID}&atype=$type",
248 elseif ($type == $pdfActivityType) {
249 $url = CRM_Utils_System
::url('civicrm/activity/pdf/add',
250 "action=add&context=standalone&reset=1&cid={$this->_contactID}&caseid={$this->_caseID}&atype=$type",
254 $url = CRM_Utils_System
::url('civicrm/case/activity',
255 "action=add&reset=1&cid={$this->_contactID}&caseid={$this->_caseID}&atype=$type",
259 $activityLinks[$url] = $label;
262 $this->add('select', 'add_activity_type_id', '', $activityLinks, FALSE, ['class' => 'crm-select2 crm-action-menu fa-calendar-check-o twenty']);
263 if ($this->_hasAccessToAllCases
) {
264 $this->add('select', 'report_id', '',
265 ['' => ts('Activity Audit')] +
$reports,
267 ['class' => 'crm-select2 crm-action-menu fa-list-alt']
269 $this->add('select', 'timeline_id', '',
270 ['' => ts('Add Timeline')] +
$reports,
272 ['class' => 'crm-select2 crm-action-menu fa-list-ol']
275 // This button is hidden but gets clicked by javascript at
276 // https://github.com/civicrm/civicrm-core/blob/bd28ecf8121a85bc069cad3ab912a0c3dff8fdc5/templates/CRM/Case/Form/CaseView.js#L194
277 // by the onChange handler for the above timeline_id select.
278 $this->addElement('submit', $this->getButtonName('next'), ' ', ['class' => 'hiddenElement']);
280 $this->buildMergeCaseForm();
283 self
::activityForm($this, $aTypes);
285 //get case related relationships (Case Role)
286 $caseRelationships = CRM_Case_BAO_Case
::getCaseRoles($this->_contactID
, $this->_caseID
);
288 //save special label because we unset it in the loop
289 $managerLabel = empty($managerRoleId) ?
'' : $caseRoles[$managerRoleId];
291 foreach ($caseRelationships as $key => & $value) {
292 if (!empty($managerRoleId)) {
293 if (substr($managerRoleId, 0, -4) == $value['relation_type'] && substr($managerRoleId, -3) == $value['relationship_direction']) {
294 $value['relation'] = $managerLabel;
298 //calculate roles that don't have relationships
299 if (!empty($caseRoles[$value['relation_type']])) {
300 unset($caseRoles[$value['relation_type']]);
304 $this->assign('caseRelationships', $caseRelationships);
306 //also add client as role. CRM-4438
307 $caseRoles['client'] = CRM_Case_BAO_Case
::getContactNames($this->_caseID
);
309 $this->assign('caseRoles', $caseRoles);
311 // Retrieve ALL client relationships
312 $relClient = CRM_Contact_BAO_Relationship
::getRelationship($this->_contactID
,
313 CRM_Contact_BAO_Relationship
::CURRENT
,
314 0, 0, 0, NULL, NULL, FALSE
317 // Now build 'Other Relationships' array by removing relationships that are already listed under Case Roles
318 // so they don't show up twice.
319 $clientRelationships = [];
320 foreach ($relClient as $r) {
321 if (!array_key_exists($r['id'], $caseRelationships)) {
322 $clientRelationships[] = $r;
325 $this->assign('clientRelationships', $clientRelationships);
327 // Now global contact list that appears on all cases.
328 $globalGroupInfo = [];
329 CRM_Case_BAO_Case
::getGlobalContacts($globalGroupInfo);
330 $this->assign('globalGroupInfo', $globalGroupInfo);
332 // List relationship types for adding an arbitrary new role to the case
335 ts('Relationship Type'),
336 ['' => ts('- select type -')] +
$allowedRelationshipTypes,
338 ['class' => 'crm-select2 twenty', 'data-select-params' => '{"allowClear": false}']
341 $hookCaseSummary = CRM_Utils_Hook
::caseSummary($this->_caseID
);
342 if (is_array($hookCaseSummary)) {
343 $this->assign('hookCaseSummary', $hookCaseSummary);
346 $allTags = CRM_Core_BAO_Tag
::getColorTags('civicrm_case');
348 if (!empty($allTags)) {
349 $this->add('select2', 'case_tag', ts('Tags'), $allTags, FALSE,
350 ['id' => 'tags', 'multiple' => 'multiple']
353 $tags = CRM_Core_BAO_EntityTag
::getTag($this->_caseID
, 'civicrm_case');
355 foreach ($tags as $tid) {
356 $tagInfo = CRM_Utils_Array
::findInTree($tid, $allTags);
358 $tags[$tid] = $tagInfo;
365 $this->setDefaults(['case_tag' => implode(',', array_keys($tags))]);
367 $this->assign('tags', $tags);
368 $this->assign('showTags', TRUE);
371 $this->assign('showTags', FALSE);
374 // build tagset widget
376 // see if we have any tagsets which can be assigned to cases
377 $parentNames = CRM_Core_BAO_Tag
::getTagSet('civicrm_case');
380 $this->assign('showTags', TRUE);
381 $tagSetItems = civicrm_api3('entityTag', 'get', [
382 'entity_id' => $this->_caseID
,
383 'entity_table' => 'civicrm_case',
384 'tag_id.parent_id.is_tagset' => 1,
385 'options' => ['limit' => 0],
386 'return' => ["tag_id.parent_id", "tag_id.parent_id.name", "tag_id.name"],
388 foreach ($tagSetItems['values'] as $tag) {
390 $tag['tag_id.parent_id'] => [
391 'name' => $tag['tag_id.parent_id.name'],
395 $tagSetTags[$tag['tag_id.parent_id']]['items'][] = $tag['tag_id.name'];
398 $this->assign('tagSetTags', $tagSetTags);
399 CRM_Core_Form_Tag
::buildQuickForm($this, $parentNames, 'civicrm_case', $this->_caseID
, FALSE, TRUE);
404 'name' => ts('Done'),
405 'spacing' => ' ',
414 public function postProcess() {
415 $params = $this->controller
->exportValues($this->_name
);
416 $buttonName = $this->controller
->getButtonName();
419 $url = CRM_Utils_System
::url('civicrm/contact/view/case',
420 "reset=1&action=view&cid={$this->_contactID}&id={$this->_caseID}&show=1"
422 $session = CRM_Core_Session
::singleton();
423 $session->pushUserContext($url);
425 if (!empty($params['timeline_id']) && !empty($_POST['_qf_CaseView_next'])) {
426 civicrm_api3('Case', 'addtimeline', [
427 'case_id' => $this->_caseID
,
428 'timeline' => $params['timeline_id'],
431 $xmlProcessor = new CRM_Case_XMLProcessor_Process();
432 $reports = $xmlProcessor->get($this->_caseType
, 'ActivitySets');
433 CRM_Core_Session
::setStatus(ts('Activities from the %1 activity set have been added to this case.',
434 [1 => $reports[$params['timeline_id']]]
435 ), ts('Done'), 'success');
437 elseif ($this->_mergeCases
&&
438 $buttonName == '_qf_CaseView_next_merge_case'
441 $mainCaseId = $params['merge_case_id'];
442 $otherCaseId = $this->_caseID
;
445 CRM_Case_BAO_Case
::mergeCases($this->_contactID
, $mainCaseId, NULL, $otherCaseId);
447 //redirect user to main case view.
448 $url = CRM_Utils_System
::url('civicrm/contact/view/case',
449 "reset=1&action=view&cid={$this->_contactID}&id={$mainCaseId}&show=1"
451 $session = CRM_Core_Session
::singleton();
452 $session->pushUserContext($url);
457 * Build the activity selector/datatable
458 * @param CRM_Core_Form $form
459 * @param array $aTypes
460 * To include acivities related to current case id $form->_caseID.
462 public static function activityForm($form, $aTypes = []) {
463 $caseRelationships = CRM_Case_BAO_Case
::getCaseRoles($form->_contactID
, $form->_caseID
);
464 //build reporter select
465 $reporters = ["" => ts(' - any reporter - ')];
466 foreach ($caseRelationships as $key => & $value) {
467 $reporters[$value['cid']] = $value['name'] . " ( {$value['relation']} )";
469 $form->add('select', 'reporter_id', ts('Reporter/Role'), $reporters, FALSE, ['id' => 'reporter_id_' . $form->_caseID
]);
471 // take all case activity types for search filter, CRM-7187
473 $allCaseActTypes = CRM_Case_PseudoConstant
::caseActivityType();
474 foreach ($allCaseActTypes as $typeDetails) {
475 if (!in_array($typeDetails['name'], ['Open Case'])) {
476 $aTypesFilter[$typeDetails['id']] = $typeDetails['label'] ??
NULL;
479 $aTypesFilter = $aTypesFilter +
$aTypes;
480 asort($aTypesFilter);
481 $form->add('select', 'activity_type_filter_id', ts('Activity Type'), ['' => ts('- select activity type -')] +
$aTypesFilter, FALSE, ['id' => 'activity_type_filter_id_' . $form->_caseID
]);
483 $activityStatus = CRM_Core_PseudoConstant
::activityStatus();
484 $form->add('select', 'status_id', ts('Status'), ["" => ts(' - any status - ')] +
$activityStatus, FALSE, ['id' => 'status_id_' . $form->_caseID
]);
486 // activity date search filters
487 $form->add('datepicker', 'activity_date_low_' . $form->_caseID
, ts('Activity Dates - From'), [], FALSE, ['time' => FALSE]);
488 $form->add('datepicker', 'activity_date_high_' . $form->_caseID
, ts('To'), [], FALSE, ['time' => FALSE]);
490 if (CRM_Core_Permission
::check('administer CiviCRM')) {
491 $form->add('checkbox', 'activity_deleted', ts('Deleted Activities'), '', FALSE, ['id' => 'activity_deleted_' . $form->_caseID
]);
496 * Form elements for merging cases
498 public function buildMergeCaseForm() {
500 $result = civicrm_api3('Case', 'get', [
501 'check_permissions' => TRUE,
502 'contact_id' => $this->_contactID
,
505 'id' => ['!=' => $this->_caseID
],
506 'return' => ['id', 'start_date', 'case_type_id.title'],
508 foreach ($result['values'] as $id => $case) {
509 $otherCases[$id] = "#$id: {$case['case_type_id.title']} " . ts('(opened %1)', [1 => $case['start_date']]);
512 $this->assign('mergeCases', $this->_mergeCases
= (bool) $otherCases);
515 $this->add('select', 'merge_case_id',
516 ts('Select Case for Merge'),
518 '' => ts('- select case -'),
521 ['class' => 'crm-select2 huge']
523 // This button is hidden but gets clicked by javascript at
524 // https://github.com/civicrm/civicrm-core/blob/bd28ecf8121a85bc069cad3ab912a0c3dff8fdc5/templates/CRM/Case/Form/CaseView.js#L55
525 // when the mergeCasesDialog is saved.
526 $this->addElement('submit',
527 $this->getButtonName('next', 'merge_case'),
530 'class' => 'hiddenElement',