3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2015
35 * This class generates view mode for CiviCase.
37 class CRM_Case_Form_CaseView
extends CRM_Core_Form
{
39 * Check for merge cases.
42 private $_mergeCases = FALSE;
45 * Set variables up before form is built.
47 public function preProcess() {
48 $this->_showRelatedCases
= CRM_Utils_Array
::value('relatedCases', $_GET);
50 $xmlProcessorProcess = new CRM_Case_XMLProcessor_Process();
51 $isMultiClient = $xmlProcessorProcess->getAllowMultipleCaseClients();
52 $this->assign('multiClient', $isMultiClient);
54 //pull the related cases.
55 $this->assign('showRelatedCases', FALSE);
56 if ($this->_showRelatedCases
) {
57 $relatedCases = $this->get('relatedCases');
58 if (!isset($relatedCases)) {
59 $cId = CRM_Utils_Request
::retrieve('cid', 'Integer', CRM_Core_DAO
::$_nullObject);
60 $caseId = CRM_Utils_Request
::retrieve('id', 'Integer', CRM_Core_DAO
::$_nullObject);
61 $relatedCases = CRM_Case_BAO_Case
::getRelatedCases($caseId, $cId);
63 $this->assign('relatedCases', $relatedCases);
64 $this->assign('showRelatedCases', TRUE);
65 CRM_Utils_System
::setTitle(ts('Related Cases'));
69 //check for civicase access.
70 if (!CRM_Case_BAO_Case
::accessCiviCase()) {
71 CRM_Core_Error
::fatal(ts('You are not authorized to access this page.'));
73 $this->_hasAccessToAllCases
= CRM_Core_Permission
::check('access all cases and activities');
74 $this->assign('hasAccessToAllCases', $this->_hasAccessToAllCases
);
76 $this->_contactID
= $this->get('cid');
77 $this->_caseID
= $this->get('id');
79 $fulltext = CRM_Utils_Request
::retrieve('context', 'String', CRM_Core_DAO
::$_nullObject);
80 if ($fulltext == 'fulltext') {
81 $this->assign('fulltext', $fulltext);
84 $this->assign('caseID', $this->_caseID
);
85 $this->assign('contactID', $this->_contactID
);
88 $this->_userCases
= array();
89 $session = CRM_Core_Session
::singleton();
90 $userID = $session->get('userID');
91 if (!$this->_hasAccessToAllCases
) {
92 $this->_userCases
= CRM_Case_BAO_Case
::getCases(FALSE, $userID, 'any');
93 if (!array_key_exists($this->_caseID
, $this->_userCases
)) {
94 CRM_Core_Error
::fatal(ts('You are not authorized to access this page.'));
97 $this->assign('userID', $userID);
99 if (CRM_Case_BAO_Case
::caseCount($this->_contactID
) >= 2) {
100 $this->_mergeCases
= TRUE;
102 $this->assign('mergeCases', $this->_mergeCases
);
104 //retrieve details about case
105 $params = array('id' => $this->_caseID
);
107 $returnProperties = array('case_type_id', 'subject', 'status_id', 'start_date');
108 CRM_Core_DAO
::commonRetrieve('CRM_Case_BAO_Case', $params, $values, $returnProperties);
110 $statuses = CRM_Case_PseudoConstant
::caseStatus('label', FALSE);
111 $caseTypeName = CRM_Case_BAO_Case
::getCaseType($this->_caseID
, 'name');
112 $caseType = CRM_Case_BAO_Case
::getCaseType($this->_caseID
);
114 $this->_caseDetails
= array(
115 'case_type' => $caseType,
116 'case_status' => CRM_Utils_Array
::value($values['case_status_id'], $statuses),
117 'case_subject' => CRM_Utils_Array
::value('subject', $values),
118 'case_start_date' => $values['case_start_date'],
120 $this->_caseType
= $caseTypeName;
121 $this->assign('caseDetails', $this->_caseDetails
);
123 $reportUrl = CRM_Utils_System
::url('civicrm/case/report',
124 "reset=1&cid={$this->_contactID}&caseid={$this->_caseID}&asn=",
127 $this->assign('reportUrl', $reportUrl);
129 // add to recently viewed
131 $url = CRM_Utils_System
::url('civicrm/contact/view/case',
132 "action=view&reset=1&id={$this->_caseID}&cid={$this->_contactID}&context=home"
135 $displayName = CRM_Contact_BAO_Contact
::displayName($this->_contactID
);
136 $this->assign('displayName', $displayName);
138 CRM_Utils_System
::setTitle($displayName . ' - ' . $caseType);
140 $recentOther = array();
141 if (CRM_Core_Permission
::checkActionPermission('CiviCase', CRM_Core_Action
::DELETE
)) {
142 $recentOther['deleteUrl'] = CRM_Utils_System
::url('civicrm/contact/view/case',
143 "action=delete&reset=1&id={$this->_caseID}&cid={$this->_contactID}&context=home"
147 // Add the recently viewed case
148 CRM_Utils_Recent
::add($displayName . ' - ' . $caseType,
157 //get the related cases for given case.
158 $relatedCases = $this->get('relatedCases');
159 if (!isset($relatedCases)) {
160 $relatedCases = CRM_Case_BAO_Case
::getRelatedCases($this->_caseID
, $this->_contactID
);
161 $relatedCases = empty($relatedCases) ?
FALSE : $relatedCases;
162 $this->set('relatedCases', $relatedCases);
164 $this->assign('hasRelatedCases', (bool) $relatedCases);
166 $this->assign('relatedCaseLabel', ts('%1 Related Case', array(
167 'count' => count($relatedCases),
168 'plural' => '%1 Related Cases',
170 $this->assign('relatedCaseUrl', CRM_Utils_System
::url('civicrm/contact/view/case', array(
171 'id' => $this->_caseID
,
172 'cid' => $this->_contactID
,
178 $entitySubType = !empty($values['case_type_id']) ?
$values['case_type_id'] : NULL;
179 $this->assign('caseTypeID', $entitySubType);
180 $groupTree = &CRM_Core_BAO_CustomGroup
::getTree('Case',
186 CRM_Core_BAO_CustomGroup
::buildCustomDataView($this,
192 * Set default values for the form.
196 public function setDefaultValues() {
202 * Build the form object.
204 public function buildQuickForm() {
205 //this call is for show related cases.
206 if ($this->_showRelatedCases
) {
210 CRM_Core_Resources
::singleton()
211 ->addScriptFile('civicrm', 'js/crm.livePage.js', 1, 'html-header')
212 ->addScriptFile('civicrm', 'templates/CRM/Case/Form/CaseView.js', 2, 'html-header');
214 $xmlProcessor = new CRM_Case_XMLProcessor_Process();
215 $caseRoles = $xmlProcessor->get($this->_caseType
, 'CaseRoles');
216 $reports = $xmlProcessor->get($this->_caseType
, 'ActivitySets');
218 //adding case manager.CRM-4510.
219 $managerRoleId = $xmlProcessor->getCaseManagerRoleId($this->_caseType
);
220 if (!empty($managerRoleId)) {
221 $caseRoles[$managerRoleId] = $caseRoles[$managerRoleId] . '<br />' . '(' . ts('Case Manager') . ')';
224 $aTypes = $xmlProcessor->get($this->_caseType
, 'ActivityTypes', TRUE);
226 $allActTypes = CRM_Core_PseudoConstant
::activityType(TRUE, TRUE, FALSE, 'name');
228 $emailActivityType = array_search('Email', $allActTypes);
229 $pdfActivityType = array_search('Print PDF Letter', $allActTypes);
231 // remove Open Case activity type since we're inside an existing case
232 if ($openActTypeId = array_search('Open Case', $allActTypes)) {
233 unset($aTypes[$openActTypeId]);
236 //check for link cases.
237 $unclosedCases = CRM_Case_BAO_Case
::getUnclosedCases(NULL, array($this->_caseID
));
238 if (empty($unclosedCases) && ($linkActTypeId = array_search('Link Cases', $allActTypes))) {
239 unset($aTypes[$linkActTypeId]);
242 if (!$xmlProcessor->getNaturalActivityTypeSort()) {
246 $activityLinks = array('' => ts('Add Activity'));
247 foreach ($aTypes as $type => $label) {
248 if ($type == $emailActivityType) {
249 $url = CRM_Utils_System
::url('civicrm/activity/email/add',
250 "action=add&context=standalone&reset=1&caseid={$this->_caseID}&atype=$type",
254 elseif ($type == $pdfActivityType) {
255 $url = CRM_Utils_System
::url('civicrm/activity/pdf/add',
256 "action=add&context=standalone&reset=1&cid={$this->_contactID}&caseid={$this->_caseID}&atype=$type",
260 $url = CRM_Utils_System
::url('civicrm/case/activity',
261 "action=add&reset=1&cid={$this->_contactID}&caseid={$this->_caseID}&atype=$type",
265 $activityLinks[$url] = $label;
268 $this->add('select', 'add_activity_type_id', '', $activityLinks, FALSE, array('class' => 'crm-select2 crm-action-menu action-icon-plus twenty'));
269 if ($this->_hasAccessToAllCases
) {
270 $this->add('select', 'report_id', '',
271 array('' => ts('Activity Audit')) +
$reports,
273 array('class' => 'crm-select2 crm-action-menu action-icon-clipboard')
275 $this->add('select', 'timeline_id', '',
276 array('' => ts('Add Timeline')) +
$reports,
278 array('class' => 'crm-select2 crm-action-menu action-icon-play')
281 $this->addElement('submit', $this->getButtonName('next'), ' ', array('class' => 'hiddenElement'));
283 if ($this->_mergeCases
) {
284 $allCases = CRM_Case_BAO_Case
::getContactCases($this->_contactID
);
285 $otherCases = array();
286 foreach ($allCases as $caseId => $details) {
287 //filter current and own cases.
288 if (($caseId == $this->_caseID
) ||
289 (!$this->_hasAccessToAllCases
&&
290 !array_key_exists($caseId, $this->_userCases
)
296 $otherCases[$caseId] = 'Case ID: ' . $caseId . ' Type: ' . $details['case_type'] . ' Start: ' . $details['case_start_date'];
298 if (empty($otherCases)) {
299 $this->_mergeCases
= FALSE;
300 $this->assign('mergeCases', $this->_mergeCases
);
303 $this->add('select', 'merge_case_id',
304 ts('Select Case for Merge'),
306 '' => ts('- select case -'),
309 array('class' => 'crm-select2 huge')
311 $this->addElement('submit',
312 $this->getButtonName('next', 'merge_case'),
315 'class' => 'hiddenElement',
322 self
::activityForm($this, $aTypes);
324 //get case related relationships (Case Role)
325 $caseRelationships = CRM_Case_BAO_Case
::getCaseRoles($this->_contactID
, $this->_caseID
);
327 //save special label because we unset it in the loop
328 $managerLabel = empty($managerRoleId) ?
'' : $caseRoles[$managerRoleId];
330 foreach ($caseRelationships as $key => & $value) {
331 if (!empty($managerRoleId)) {
332 if ($managerRoleId == $value['relation_type']) {
333 $value['relation'] = $managerLabel;
337 //calculate roles that don't have relationships
338 if (!empty($caseRoles[$value['relation_type']])) {
339 unset($caseRoles[$value['relation_type']]);
343 $this->assign('caseRelationships', $caseRelationships);
345 //also add client as role. CRM-4438
346 $caseRoles['client'] = CRM_Case_BAO_Case
::getContactNames($this->_caseID
);
348 $this->assign('caseRoles', $caseRoles);
350 // Retrieve ALL client relationships
351 $relClient = CRM_Contact_BAO_Relationship
::getRelationship($this->_contactID
,
352 CRM_Contact_BAO_Relationship
::CURRENT
,
353 0, 0, 0, NULL, NULL, FALSE
356 // Now build 'Other Relationships' array by removing relationships that are already listed under Case Roles
357 // so they don't show up twice.
358 $clientRelationships = array();
359 foreach ($relClient as $r) {
360 if (!array_key_exists($r['id'], $caseRelationships)) {
361 $clientRelationships[] = $r;
364 $this->assign('clientRelationships', $clientRelationships);
366 // Now global contact list that appears on all cases.
367 $globalGroupInfo = array();
368 CRM_Case_BAO_Case
::getGlobalContacts($globalGroupInfo);
369 $this->assign('globalGroupInfo', $globalGroupInfo);
371 // List of relationship types
372 $baoRel = new CRM_Contact_BAO_Relationship();
373 $relType = $baoRel->getRelationType('Individual');
374 $roleTypes = array();
375 foreach ($relType as $k => $v) {
376 $roleTypes[substr($k, 0, strpos($k, '_'))] = $v;
378 $this->add('select', 'role_type', ts('Relationship Type'), array('' => ts('- select type -')) +
$roleTypes, FALSE, array('class' => 'crm-select2 twenty'));
380 $hookCaseSummary = CRM_Utils_Hook
::caseSummary($this->_caseID
);
381 if (is_array($hookCaseSummary)) {
382 $this->assign('hookCaseSummary', $hookCaseSummary);
385 CRM_Core_BAO_Tag
::getTags('civicrm_case', $allTags, NULL,
386 ' ', TRUE);
388 if (!empty($allTags)) {
389 $this->add('select', 'case_tag', ts('Tags'), $allTags, FALSE,
390 array('id' => 'tags', 'multiple' => 'multiple', 'class' => 'crm-select2')
393 $tags = CRM_Core_BAO_EntityTag
::getTag($this->_caseID
, 'civicrm_case');
395 $this->setDefaults(array('case_tag' => $tags));
397 foreach ($tags as $tid) {
398 if (isset($allTags[$tid])) {
399 $tags[$tid] = $allTags[$tid];
406 $this->assign('tags', implode(', ', array_filter($tags)));
407 $this->assign('showTags', TRUE);
410 $this->assign('showTags', FALSE);
413 // build tagset widget
415 // see if we have any tagsets which can be assigned to cases
416 $parentNames = CRM_Core_BAO_Tag
::getTagSet('civicrm_case');
418 $this->assign('showTagsets', TRUE);
421 $this->assign('showTagsets', FALSE);
423 CRM_Core_Form_Tag
::buildQuickForm($this, $parentNames, 'civicrm_case', $this->_caseID
, FALSE, TRUE);
425 $this->addButtons(array(
428 'name' => ts('Done'),
429 'spacing' => ' ',
439 public function postProcess() {
440 $params = $this->controller
->exportValues($this->_name
);
441 $buttonName = $this->controller
->getButtonName();
444 $url = CRM_Utils_System
::url('civicrm/contact/view/case',
445 "reset=1&action=view&cid={$this->_contactID}&id={$this->_caseID}&show=1"
447 $session = CRM_Core_Session
::singleton();
448 $session->pushUserContext($url);
450 if (!empty($params['timeline_id']) && !empty($_POST['_qf_CaseView_next'])) {
451 $session = CRM_Core_Session
::singleton();
452 $this->_uid
= $session->get('userID');
453 $xmlProcessor = new CRM_Case_XMLProcessor_Process();
454 $xmlProcessorParams = array(
455 'clientID' => $this->_contactID
,
456 'creatorID' => $this->_uid
,
457 'standardTimeline' => 0,
458 'activity_date_time' => date('YmdHis'),
459 'caseID' => $this->_caseID
,
460 'caseType' => $this->_caseType
,
461 'activitySetName' => $params['timeline_id'],
463 $xmlProcessor->run($this->_caseType
, $xmlProcessorParams);
464 $reports = $xmlProcessor->get($this->_caseType
, 'ActivitySets');
466 CRM_Core_Session
::setStatus(ts('Activities from the %1 activity set have been added to this case.',
467 array(1 => $reports[$params['timeline_id']])
468 ), ts('Done'), 'success');
470 elseif ($this->_mergeCases
&&
471 $buttonName == '_qf_CaseView_next_merge_case'
474 $mainCaseId = $params['merge_case_id'];
475 $otherCaseId = $this->_caseID
;
478 CRM_Case_BAO_Case
::mergeCases($this->_contactID
, $mainCaseId, NULL, $otherCaseId);
480 //redirect user to main case view.
481 $url = CRM_Utils_System
::url('civicrm/contact/view/case',
482 "reset=1&action=view&cid={$this->_contactID}&id={$mainCaseId}&show=1"
484 $session = CRM_Core_Session
::singleton();
485 $session->pushUserContext($url);
490 * Build the activity selector/datatable
491 * @param CRM_Core_Form $form
492 * @param array $aTypes
493 * To include acivities related to current case id $form->_caseID.
495 public static function activityForm($form, $aTypes = array()) {
496 $caseRelationships = CRM_Case_BAO_Case
::getCaseRoles($form->_contactID
, $form->_caseID
);
497 //build reporter select
498 $reporters = array("" => ts(' - any reporter - '));
499 foreach ($caseRelationships as $key => & $value) {
500 $reporters[$value['cid']] = $value['name'] . " ( {$value['relation']} )";
502 $form->add('select', 'reporter_id', ts('Reporter/Role'), $reporters, FALSE, array('id' => 'reporter_id_' . $form->_caseID
));
504 // take all case activity types for search filter, CRM-7187
505 $aTypesFilter = array();
506 $allCaseActTypes = CRM_Case_PseudoConstant
::caseActivityType();
507 foreach ($allCaseActTypes as $typeDetails) {
508 if (!in_array($typeDetails['name'], array('Open Case'))) {
509 $aTypesFilter[$typeDetails['id']] = CRM_Utils_Array
::value('label', $typeDetails);
512 $aTypesFilter = $aTypesFilter +
$aTypes;
513 asort($aTypesFilter);
514 $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
));
516 $activityStatus = CRM_Core_PseudoConstant
::activityStatus();
517 $form->add('select', 'status_id', ts('Status'), array("" => ts(' - any status - ')) +
$activityStatus, FALSE, array('id' => 'status_id_' . $form->_caseID
));
520 $form->addDate('activity_date_low_' . $form->_caseID
, ts('Activity Dates - From'), FALSE, array('formatType' => 'searchDate'));
521 $form->addDate('activity_date_high_' . $form->_caseID
, ts('To'), FALSE, array('formatType' => 'searchDate'));
523 if (CRM_Core_Permission
::check('administer CiviCRM')) {
524 $form->add('checkbox', 'activity_deleted', ts('Deleted Activities'), '', FALSE, array('id' => 'activity_deleted_' . $form->_caseID
));