3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
37 * This class generates view mode for CiviCase
40 class CRM_Case_Form_CaseView
extends CRM_Core_Form
{
42 * check for merge cases.
45 private $_mergeCases = FALSE;
48 * Function to set variables up before form is built
53 public function preProcess() {
54 $this->_showRelatedCases
= CRM_Utils_Array
::value('relatedCases', $_GET);
56 $xmlProcessorProcess = new CRM_Case_XMLProcessor_Process();
57 $isMultiClient = $xmlProcessorProcess->getAllowMultipleCaseClients();
58 $this->assign('multiClient', $isMultiClient);
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);
69 $this->assign('relatedCases', $relatedCases);
70 $this->assign('showRelatedCases', TRUE);
71 CRM_Utils_System
::setTitle(ts('Related Cases'));
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.'));
79 $this->_hasAccessToAllCases
= CRM_Core_Permission
::check('access all cases and activities');
80 $this->assign('hasAccessToAllCases', $this->_hasAccessToAllCases
);
82 $this->_contactID
= $this->get('cid');
83 $this->_caseID
= $this->get('id');
85 $fulltext = CRM_Utils_Request
::retrieve('context', 'String', CRM_Core_DAO
::$_nullObject);
86 if ($fulltext == 'fulltext') {
87 $this->assign('fulltext', $fulltext);
90 $this->assign('caseID', $this->_caseID
);
91 $this->assign('contactID', $this->_contactID
);
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.'));
103 $this->assign('userID', $userID);
105 if (CRM_Case_BAO_Case
::caseCount($this->_contactID
) >= 2) {
106 $this->_mergeCases
= TRUE;
108 $this->assign('mergeCases', $this->_mergeCases
);
110 //retrieve details about case
111 $params = array('id' => $this->_caseID
);
113 $returnProperties = array('case_type_id', 'subject', 'status_id', 'start_date');
114 CRM_Core_DAO
::commonRetrieve('CRM_Case_BAO_Case', $params, $values, $returnProperties);
116 $values['case_type_id'] = trim(CRM_Utils_Array
::value('case_type_id', $values),
117 CRM_Core_DAO
::VALUE_SEPARATOR
119 $values['case_type_id'] = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
120 CRM_Utils_Array
::value('case_type_id', $values)
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
);
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'],
133 $this->_caseType
= $caseTypeName;
134 $this->assign('caseDetails', $this->_caseDetails
);
136 $reportUrl = CRM_Utils_System
::url('civicrm/case/report',
137 "reset=1&cid={$this->_contactID}&caseid={$this->_caseID}&asn=",
140 $this->assign('reportUrl', $reportUrl);
142 // add to recently viewed
144 $url = CRM_Utils_System
::url('civicrm/contact/view/case',
145 "action=view&reset=1&id={$this->_caseID}&cid={$this->_contactID}&context=home"
148 $displayName = CRM_Contact_BAO_Contact
::displayName($this->_contactID
);
149 $this->assign('displayName', $displayName);
151 CRM_Utils_System
::setTitle($displayName . ' - ' . $caseType);
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"
160 // Add the recently viewed case
161 CRM_Utils_Recent
::add($displayName . ' - ' . $caseType,
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);
178 $this->assign('hasRelatedCases', (bool) $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
,
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',
197 CRM_Core_BAO_CustomGroup
::buildCustomDataView($this,
203 * This function sets the default values for the form. For edit/view mode
204 * the default values are retrieved from the database
210 function setDefaultValues() {
216 * Function to build the form
221 public function buildQuickForm() {
222 //this call is for show related cases.
223 if ($this->_showRelatedCases
) {
227 CRM_Core_Resources
::singleton()
228 ->addScriptFile('civicrm', 'js/crm.livePage.js')
229 ->addScriptFile('civicrm', 'templates/CRM/Case/Form/CaseView.js');
231 $xmlProcessor = new CRM_Case_XMLProcessor_Process();
232 $caseRoles = $xmlProcessor->get($this->_caseType
, 'CaseRoles');
233 $reports = $xmlProcessor->get($this->_caseType
, 'ActivitySets');
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') . ')';
241 $aTypes = $xmlProcessor->get($this->_caseType
, 'ActivityTypes', TRUE);
243 $allActTypes = CRM_Core_PseudoConstant
::activityType(TRUE, TRUE, FALSE, 'name');
245 $emailActivityType = array_search('Email', $allActTypes);
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]);
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]);
258 if (!$xmlProcessor->getNaturalActivityTypeSort()) {
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",
271 $url = CRM_Utils_System
::url('civicrm/case/activity',
272 "action=add&reset=1&cid={$this->_contactID}&caseid={$this->_caseID}&atype=$type",
276 $activityLinks[$url] = $label;
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,
284 array('class' => 'crm-select2 crm-action-menu')
286 $this->add('select', 'timeline_id', '',
287 array('' => ts('Add Timeline')) +
$reports,
289 array('class' => 'crm-select2 crm-action-menu')
292 $this->addElement('submit', $this->getButtonName('next'), ' ', array('class' => 'hiddenElement'));
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
)
307 $otherCases[$caseId] = 'Case ID: ' . $caseId . ' Type: ' . $details['case_type'] . ' Start: ' . $details['case_start_date'];
309 if (empty($otherCases)) {
310 $this->_mergeCases
= FALSE;
311 $this->assign('mergeCases', $this->_mergeCases
);
314 $this->add('select', 'merge_case_id',
315 ts('Select Case for Merge'),
317 '' => ts('- select case -')) +
$otherCases,
319 array('class' => 'crm-select2 huge')
321 $this->addElement('submit',
322 $this->getButtonName('next', 'merge_case'),
325 'class' => 'form-submit-inline hiddenElement',
332 self
::activityForm($this);
334 //get case related relationships (Case Role)
335 $caseRelationships = CRM_Case_BAO_Case
::getCaseRoles($this->_contactID
, $this->_caseID
);
337 //save special label because we unset it in the loop
338 $managerLabel = empty($managerRoleId) ?
'' : $caseRoles[$managerRoleId];
340 foreach ($caseRelationships as $key => & $value) {
341 if (!empty($managerRoleId)) {
342 if ($managerRoleId == $value['relation_type']) {
343 $value['relation'] = $managerLabel;
347 //calculate roles that don't have relationships
348 if (!empty($caseRoles[$value['relation_type']])) {
349 unset($caseRoles[$value['relation_type']]);
353 $this->assign('caseRelationships', $caseRelationships);
355 //also add client as role. CRM-4438
356 $caseRoles['client'] = CRM_Case_BAO_Case
::getContactNames($this->_caseID
);
358 $this->assign('caseRoles', $caseRoles);
360 // Retrieve ALL client relationships
361 $relClient = CRM_Contact_BAO_Relationship
::getRelationship($this->_contactID
,
362 CRM_Contact_BAO_Relationship
::CURRENT
,
363 0, 0, 0, NULL, NULL, FALSE
366 // Now build 'Other Relationships' array by removing relationships that are already listed under Case Roles
367 // so they don't show up twice.
368 $clientRelationships = array();
369 foreach ($relClient as $r) {
370 if (!array_key_exists($r['id'], $caseRelationships)) {
371 $clientRelationships[] = $r;
374 $this->assign('clientRelationships', $clientRelationships);
376 // Now global contact list that appears on all cases.
377 $globalGroupInfo = array();
378 CRM_Case_BAO_Case
::getGlobalContacts($globalGroupInfo);
379 $this->assign('globalGroupInfo', $globalGroupInfo);
381 // List of relationship types
382 $baoRel = new CRM_Contact_BAO_Relationship();
383 $relType = $baoRel->getRelationType('Individual');
384 $roleTypes = array();
385 foreach ($relType as $k => $v) {
386 $roleTypes[substr($k, 0, strpos($k, '_'))] = $v;
388 $this->add('select', 'role_type', ts('Relationship Type'), array('' => ts('- select type -')) +
$roleTypes, FALSE, array('class' => 'crm-select2 twenty'));
390 $hookCaseSummary = CRM_Utils_Hook
::caseSummary($this->_caseID
);
391 if (is_array($hookCaseSummary)) {
392 $this->assign('hookCaseSummary', $hookCaseSummary);
396 $allTags = CRM_Core_BAO_Tag
::getTags('civicrm_case');
398 if (!empty($allTags)) {
399 $this->add('select', 'case_tag', ts('Tags'), $allTags, FALSE,
400 array('id' => 'tags', 'multiple' => 'multiple', 'class' => 'crm-select2')
403 $tags = CRM_Core_BAO_EntityTag
::getTag($this->_caseID
, 'civicrm_case');
405 $this->setDefaults(array('case_tag' => $tags));
407 foreach ($tags as $tid) {
408 if (isset($allTags[$tid])) {
409 $tags[$tid] = $allTags[$tid];
416 $this->assign('tags', implode(', ', array_filter($tags)));
417 $this->assign('showTags', TRUE);
420 $this->assign('showTags', FALSE);
423 // build tagset widget
425 // see if we have any tagsets which can be assigned to cases
426 $parentNames = CRM_Core_BAO_Tag
::getTagSet('civicrm_case');
428 $this->assign('showTagsets', TRUE);
431 $this->assign('showTagsets', FALSE);
433 CRM_Core_Form_Tag
::buildQuickForm($this, $parentNames, 'civicrm_case', $this->_caseID
, TRUE, TRUE);
435 $this->addButtons(array(
438 'name' => ts('Done'),
439 'spacing' => ' ',
452 public function postProcess() {
453 $params = $this->controller
->exportValues($this->_name
);
454 $buttonName = $this->controller
->getButtonName();
457 $url = CRM_Utils_System
::url('civicrm/contact/view/case',
458 "reset=1&action=view&cid={$this->_contactID}&id={$this->_caseID}&show=1"
460 $session = CRM_Core_Session
::singleton();
461 $session->pushUserContext($url);
463 if (!empty($params['timeline_id']) && !empty($_POST['_qf_CaseView_next'])) {
464 $session = CRM_Core_Session
::singleton();
465 $this->_uid
= $session->get('userID');
466 $xmlProcessor = new CRM_Case_XMLProcessor_Process();
467 $xmlProcessorParams = array(
468 'clientID' => $this->_contactID
,
469 'creatorID' => $this->_uid
,
470 'standardTimeline' => 0,
471 'activity_date_time' => date('YmdHis'),
472 'caseID' => $this->_caseID
,
473 'caseType' => $this->_caseType
,
474 'activitySetName' => $params['timeline_id'],
476 $xmlProcessor->run($this->_caseType
, $xmlProcessorParams);
477 $reports = $xmlProcessor->get($this->_caseType
, 'ActivitySets');
479 CRM_Core_Session
::setStatus(ts('Activities from the %1 activity set have been added to this case.',
480 array(1 => $reports[$params['timeline_id']])
481 ), ts('Done'), 'success');
483 elseif ($this->_mergeCases
&&
484 $buttonName == '_qf_CaseView_next_merge_case'
487 $mainCaseId = $params['merge_case_id'];
488 $otherCaseId = $this->_caseID
;
491 CRM_Case_BAO_Case
::mergeCases($this->_contactID
, $mainCaseId, NULL, $otherCaseId);
493 //redirect user to main case view.
494 $url = CRM_Utils_System
::url('civicrm/contact/view/case',
495 "reset=1&action=view&cid={$this->_contactID}&id={$mainCaseId}&show=1"
497 $session = CRM_Core_Session
::singleton();
498 $session->pushUserContext($url);
503 * Build the activity selector/datatable
504 * @param CRM_Core_Form $form
506 static function activityForm($form) {
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']} )";
513 $form->add('select', 'reporter_id', ts('Reporter/Role'), $reporters);
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);
523 asort($aTypesFilter);
524 $form->add('select', 'activity_type_filter_id', ts('Activity Type'), array('' => ts('- select activity type -')) +
$aTypesFilter);
526 $activityStatus = CRM_Core_PseudoConstant
::activityStatus();
527 $form->add('select', 'status_id', ts('Status'), array("" => ts(' - any status - ')) +
$activityStatus);
530 $form->addDate('activity_date_low', ts('Activity Dates - From'), FALSE, array('formatType' => 'searchDate'));
531 $form->addDate('activity_date_high', ts('To'), FALSE, array('formatType' => 'searchDate'));
533 if (CRM_Core_Permission
::check('administer CiviCRM')) {
534 $form->add('checkbox', 'activity_deleted', ts('Deleted Activities'));