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
);
86 $this->assign('contactType', CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactID
, 'contact_type'));
89 $this->_userCases
= array();
90 $session = CRM_Core_Session
::singleton();
91 $userID = $session->get('userID');
92 if (!$this->_hasAccessToAllCases
) {
93 $this->_userCases
= CRM_Case_BAO_Case
::getCases(FALSE, $userID, 'any');
94 if (!array_key_exists($this->_caseID
, $this->_userCases
)) {
95 CRM_Core_Error
::fatal(ts('You are not authorized to access this page.'));
98 $this->assign('userID', $userID);
100 if (CRM_Case_BAO_Case
::caseCount($this->_contactID
) >= 2) {
101 $this->_mergeCases
= TRUE;
103 $this->assign('mergeCases', $this->_mergeCases
);
105 //retrieve details about case
106 $params = array('id' => $this->_caseID
);
108 $returnProperties = array('case_type_id', 'subject', 'status_id', 'start_date');
109 CRM_Core_DAO
::commonRetrieve('CRM_Case_BAO_Case', $params, $values, $returnProperties);
111 $statuses = CRM_Case_PseudoConstant
::caseStatus('label', FALSE);
112 $caseTypeName = CRM_Case_BAO_Case
::getCaseType($this->_caseID
, 'name');
113 $caseType = CRM_Case_BAO_Case
::getCaseType($this->_caseID
);
115 $this->_caseDetails
= array(
116 'case_type' => $caseType,
117 'case_status' => CRM_Utils_Array
::value($values['case_status_id'], $statuses),
118 'case_subject' => CRM_Utils_Array
::value('subject', $values),
119 'case_start_date' => $values['case_start_date'],
121 $this->_caseType
= $caseTypeName;
122 $this->assign('caseDetails', $this->_caseDetails
);
124 $reportUrl = CRM_Utils_System
::url('civicrm/case/report',
125 "reset=1&cid={$this->_contactID}&caseid={$this->_caseID}&asn=",
128 $this->assign('reportUrl', $reportUrl);
130 // add to recently viewed
132 $url = CRM_Utils_System
::url('civicrm/contact/view/case',
133 "action=view&reset=1&id={$this->_caseID}&cid={$this->_contactID}&context=home"
136 $displayName = CRM_Contact_BAO_Contact
::displayName($this->_contactID
);
137 $this->assign('displayName', $displayName);
139 CRM_Utils_System
::setTitle($displayName . ' - ' . $caseType);
141 $recentOther = array();
142 if (CRM_Core_Permission
::checkActionPermission('CiviCase', CRM_Core_Action
::DELETE
)) {
143 $recentOther['deleteUrl'] = CRM_Utils_System
::url('civicrm/contact/view/case',
144 "action=delete&reset=1&id={$this->_caseID}&cid={$this->_contactID}&context=home"
148 // Add the recently viewed case
149 CRM_Utils_Recent
::add($displayName . ' - ' . $caseType,
158 //get the related cases for given case.
159 $relatedCases = $this->get('relatedCases');
160 if (!isset($relatedCases)) {
161 $relatedCases = CRM_Case_BAO_Case
::getRelatedCases($this->_caseID
, $this->_contactID
);
162 $relatedCases = empty($relatedCases) ?
FALSE : $relatedCases;
163 $this->set('relatedCases', $relatedCases);
165 $this->assign('hasRelatedCases', (bool) $relatedCases);
167 $this->assign('relatedCaseLabel', ts('%1 Related Case', array(
168 'count' => count($relatedCases),
169 'plural' => '%1 Related Cases',
171 $this->assign('relatedCaseUrl', CRM_Utils_System
::url('civicrm/contact/view/case', array(
172 'id' => $this->_caseID
,
173 'cid' => $this->_contactID
,
179 $entitySubType = !empty($values['case_type_id']) ?
$values['case_type_id'] : NULL;
180 $this->assign('caseTypeID', $entitySubType);
181 $groupTree = &CRM_Core_BAO_CustomGroup
::getTree('Case',
187 CRM_Core_BAO_CustomGroup
::buildCustomDataView($this,
193 * Set default values for the form.
197 public function setDefaultValues() {
203 * Build the form object.
205 public function buildQuickForm() {
206 //this call is for show related cases.
207 if ($this->_showRelatedCases
) {
211 $allowedRelationshipTypes = CRM_Contact_BAO_Relationship
::getContactRelationshipType($this->_contactID
);
213 CRM_Core_Resources
::singleton()
214 ->addScriptFile('civicrm', 'js/crm.livePage.js', 1, 'html-header')
215 ->addScriptFile('civicrm', 'templates/CRM/Case/Form/CaseView.js', 2, 'html-header')
216 ->addVars('relationshipTypes', CRM_Contact_Form_Relationship
::getRelationshipTypeMetadata($allowedRelationshipTypes));
218 $xmlProcessor = new CRM_Case_XMLProcessor_Process();
219 $caseRoles = $xmlProcessor->get($this->_caseType
, 'CaseRoles');
220 $reports = $xmlProcessor->get($this->_caseType
, 'ActivitySets');
222 //adding case manager.CRM-4510.
223 $managerRoleId = $xmlProcessor->getCaseManagerRoleId($this->_caseType
);
224 if (!empty($managerRoleId)) {
225 $caseRoles[$managerRoleId] = $caseRoles[$managerRoleId] . '<br />' . '(' . ts('Case Manager') . ')';
228 $aTypes = $xmlProcessor->get($this->_caseType
, 'ActivityTypes', TRUE);
230 $allActTypes = CRM_Core_PseudoConstant
::activityType(TRUE, TRUE, FALSE, 'name');
232 $emailActivityType = array_search('Email', $allActTypes);
233 $pdfActivityType = array_search('Print PDF Letter', $allActTypes);
235 // remove Open Case activity type since we're inside an existing case
236 if ($openActTypeId = array_search('Open Case', $allActTypes)) {
237 unset($aTypes[$openActTypeId]);
240 //check for link cases.
241 $unclosedCases = CRM_Case_BAO_Case
::getUnclosedCases(NULL, array($this->_caseID
));
242 if (empty($unclosedCases) && ($linkActTypeId = array_search('Link Cases', $allActTypes))) {
243 unset($aTypes[$linkActTypeId]);
246 if (!$xmlProcessor->getNaturalActivityTypeSort()) {
250 $activityLinks = array('' => ts('Add Activity'));
251 foreach ($aTypes as $type => $label) {
252 if ($type == $emailActivityType) {
253 $url = CRM_Utils_System
::url('civicrm/activity/email/add',
254 "action=add&context=standalone&reset=1&caseid={$this->_caseID}&atype=$type",
258 elseif ($type == $pdfActivityType) {
259 $url = CRM_Utils_System
::url('civicrm/activity/pdf/add',
260 "action=add&context=standalone&reset=1&cid={$this->_contactID}&caseid={$this->_caseID}&atype=$type",
264 $url = CRM_Utils_System
::url('civicrm/case/activity',
265 "action=add&reset=1&cid={$this->_contactID}&caseid={$this->_caseID}&atype=$type",
269 $activityLinks[$url] = $label;
272 $this->add('select', 'add_activity_type_id', '', $activityLinks, FALSE, array('class' => 'crm-select2 crm-action-menu action-icon-plus twenty'));
273 if ($this->_hasAccessToAllCases
) {
274 $this->add('select', 'report_id', '',
275 array('' => ts('Activity Audit')) +
$reports,
277 array('class' => 'crm-select2 crm-action-menu action-icon-clipboard')
279 $this->add('select', 'timeline_id', '',
280 array('' => ts('Add Timeline')) +
$reports,
282 array('class' => 'crm-select2 crm-action-menu action-icon-play')
285 $this->addElement('submit', $this->getButtonName('next'), ' ', array('class' => 'hiddenElement'));
287 if ($this->_mergeCases
) {
288 $allCases = CRM_Case_BAO_Case
::getContactCases($this->_contactID
);
289 $otherCases = array();
290 foreach ($allCases as $caseId => $details) {
291 //filter current and own cases.
292 if (($caseId == $this->_caseID
) ||
293 (!$this->_hasAccessToAllCases
&&
294 !array_key_exists($caseId, $this->_userCases
)
300 $otherCases[$caseId] = 'Case ID: ' . $caseId . ' Type: ' . $details['case_type'] . ' Start: ' . $details['case_start_date'];
302 if (empty($otherCases)) {
303 $this->_mergeCases
= FALSE;
304 $this->assign('mergeCases', $this->_mergeCases
);
307 $this->add('select', 'merge_case_id',
308 ts('Select Case for Merge'),
310 '' => ts('- select case -'),
313 array('class' => 'crm-select2 huge')
315 $this->addElement('submit',
316 $this->getButtonName('next', 'merge_case'),
319 'class' => 'hiddenElement',
326 self
::activityForm($this, $aTypes);
328 //get case related relationships (Case Role)
329 $caseRelationships = CRM_Case_BAO_Case
::getCaseRoles($this->_contactID
, $this->_caseID
);
331 //save special label because we unset it in the loop
332 $managerLabel = empty($managerRoleId) ?
'' : $caseRoles[$managerRoleId];
334 foreach ($caseRelationships as $key => & $value) {
335 if (!empty($managerRoleId)) {
336 if ($managerRoleId == $value['relation_type']) {
337 $value['relation'] = $managerLabel;
341 //calculate roles that don't have relationships
342 if (!empty($caseRoles[$value['relation_type']])) {
343 unset($caseRoles[$value['relation_type']]);
347 $this->assign('caseRelationships', $caseRelationships);
349 //also add client as role. CRM-4438
350 $caseRoles['client'] = CRM_Case_BAO_Case
::getContactNames($this->_caseID
);
352 $this->assign('caseRoles', $caseRoles);
354 // Retrieve ALL client relationships
355 $relClient = CRM_Contact_BAO_Relationship
::getRelationship($this->_contactID
,
356 CRM_Contact_BAO_Relationship
::CURRENT
,
357 0, 0, 0, NULL, NULL, FALSE
360 // Now build 'Other Relationships' array by removing relationships that are already listed under Case Roles
361 // so they don't show up twice.
362 $clientRelationships = array();
363 foreach ($relClient as $r) {
364 if (!array_key_exists($r['id'], $caseRelationships)) {
365 $clientRelationships[] = $r;
368 $this->assign('clientRelationships', $clientRelationships);
370 // Now global contact list that appears on all cases.
371 $globalGroupInfo = array();
372 CRM_Case_BAO_Case
::getGlobalContacts($globalGroupInfo);
373 $this->assign('globalGroupInfo', $globalGroupInfo);
375 // List relationship types for adding an arbitrary new role to the case
378 ts('Relationship Type'),
379 array('' => ts('- select type -')) +
$allowedRelationshipTypes,
381 array('class' => 'crm-select2 twenty', 'data-select-params' => '{"allowClear": false}')
384 $hookCaseSummary = CRM_Utils_Hook
::caseSummary($this->_caseID
);
385 if (is_array($hookCaseSummary)) {
386 $this->assign('hookCaseSummary', $hookCaseSummary);
389 CRM_Core_BAO_Tag
::getTags('civicrm_case', $allTags, NULL,
390 ' ', TRUE);
392 if (!empty($allTags)) {
393 $this->add('select', 'case_tag', ts('Tags'), $allTags, FALSE,
394 array('id' => 'tags', 'multiple' => 'multiple', 'class' => 'crm-select2')
397 $tags = CRM_Core_BAO_EntityTag
::getTag($this->_caseID
, 'civicrm_case');
399 $this->setDefaults(array('case_tag' => $tags));
401 foreach ($tags as $tid) {
402 if (isset($allTags[$tid])) {
403 $tags[$tid] = $allTags[$tid];
410 $this->assign('tags', implode(', ', array_filter($tags)));
411 $this->assign('showTags', TRUE);
414 $this->assign('showTags', FALSE);
417 // build tagset widget
419 // see if we have any tagsets which can be assigned to cases
420 $parentNames = CRM_Core_BAO_Tag
::getTagSet('civicrm_case');
422 $this->assign('showTagsets', TRUE);
425 $this->assign('showTagsets', FALSE);
427 CRM_Core_Form_Tag
::buildQuickForm($this, $parentNames, 'civicrm_case', $this->_caseID
, FALSE, TRUE);
429 $this->addButtons(array(
432 'name' => ts('Done'),
433 'spacing' => ' ',
443 public function postProcess() {
444 $params = $this->controller
->exportValues($this->_name
);
445 $buttonName = $this->controller
->getButtonName();
448 $url = CRM_Utils_System
::url('civicrm/contact/view/case',
449 "reset=1&action=view&cid={$this->_contactID}&id={$this->_caseID}&show=1"
451 $session = CRM_Core_Session
::singleton();
452 $session->pushUserContext($url);
454 if (!empty($params['timeline_id']) && !empty($_POST['_qf_CaseView_next'])) {
455 $session = CRM_Core_Session
::singleton();
456 $this->_uid
= $session->get('userID');
457 $xmlProcessor = new CRM_Case_XMLProcessor_Process();
458 $xmlProcessorParams = array(
459 'clientID' => $this->_contactID
,
460 'creatorID' => $this->_uid
,
461 'standardTimeline' => 0,
462 'activity_date_time' => date('YmdHis'),
463 'caseID' => $this->_caseID
,
464 'caseType' => $this->_caseType
,
465 'activitySetName' => $params['timeline_id'],
467 $xmlProcessor->run($this->_caseType
, $xmlProcessorParams);
468 $reports = $xmlProcessor->get($this->_caseType
, 'ActivitySets');
470 CRM_Core_Session
::setStatus(ts('Activities from the %1 activity set have been added to this case.',
471 array(1 => $reports[$params['timeline_id']])
472 ), ts('Done'), 'success');
474 elseif ($this->_mergeCases
&&
475 $buttonName == '_qf_CaseView_next_merge_case'
478 $mainCaseId = $params['merge_case_id'];
479 $otherCaseId = $this->_caseID
;
482 CRM_Case_BAO_Case
::mergeCases($this->_contactID
, $mainCaseId, NULL, $otherCaseId);
484 //redirect user to main case view.
485 $url = CRM_Utils_System
::url('civicrm/contact/view/case',
486 "reset=1&action=view&cid={$this->_contactID}&id={$mainCaseId}&show=1"
488 $session = CRM_Core_Session
::singleton();
489 $session->pushUserContext($url);
494 * Build the activity selector/datatable
495 * @param CRM_Core_Form $form
496 * @param array $aTypes
497 * To include acivities related to current case id $form->_caseID.
499 public static function activityForm($form, $aTypes = array()) {
500 $caseRelationships = CRM_Case_BAO_Case
::getCaseRoles($form->_contactID
, $form->_caseID
);
501 //build reporter select
502 $reporters = array("" => ts(' - any reporter - '));
503 foreach ($caseRelationships as $key => & $value) {
504 $reporters[$value['cid']] = $value['name'] . " ( {$value['relation']} )";
506 $form->add('select', 'reporter_id', ts('Reporter/Role'), $reporters, FALSE, array('id' => 'reporter_id_' . $form->_caseID
));
508 // take all case activity types for search filter, CRM-7187
509 $aTypesFilter = array();
510 $allCaseActTypes = CRM_Case_PseudoConstant
::caseActivityType();
511 foreach ($allCaseActTypes as $typeDetails) {
512 if (!in_array($typeDetails['name'], array('Open Case'))) {
513 $aTypesFilter[$typeDetails['id']] = CRM_Utils_Array
::value('label', $typeDetails);
516 $aTypesFilter = $aTypesFilter +
$aTypes;
517 asort($aTypesFilter);
518 $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
));
520 $activityStatus = CRM_Core_PseudoConstant
::activityStatus();
521 $form->add('select', 'status_id', ts('Status'), array("" => ts(' - any status - ')) +
$activityStatus, FALSE, array('id' => 'status_id_' . $form->_caseID
));
524 $form->addDate('activity_date_low_' . $form->_caseID
, ts('Activity Dates - From'), FALSE, array('formatType' => 'searchDate'));
525 $form->addDate('activity_date_high_' . $form->_caseID
, ts('To'), FALSE, array('formatType' => 'searchDate'));
527 if (CRM_Core_Permission
::check('administer CiviCRM')) {
528 $form->add('checkbox', 'activity_deleted', ts('Deleted Activities'), '', FALSE, array('id' => 'activity_deleted_' . $form->_caseID
));