3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
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 // js for changing activity status
55 CRM_Core_Resources
::singleton()->addScriptFile('civicrm', 'templates/CRM/Case/Form/ActivityChangeStatus.js');
57 $this->_showRelatedCases
= CRM_Utils_Array
::value('relatedCases', $_GET);
59 $xmlProcessorProcess = new CRM_Case_XMLProcessor_Process();
60 $isMultiClient = $xmlProcessorProcess->getAllowMultipleCaseClients();
61 $this->assign('multiClient', $isMultiClient);
63 //pull the related cases.
64 $this->assign('showRelatedCases', FALSE);
65 if ($this->_showRelatedCases
) {
66 $relatedCases = $this->get('relatedCases');
67 if (!isset($relatedCases)) {
68 $cId = CRM_Utils_Request
::retrieve('cid', 'Integer', CRM_Core_DAO
::$_nullObject);
69 $caseId = CRM_Utils_Request
::retrieve('id', 'Integer', CRM_Core_DAO
::$_nullObject);
70 $relatedCases = CRM_Case_BAO_Case
::getRelatedCases($caseId, $cId);
72 $this->assign('relatedCases', $relatedCases);
73 $this->assign('showRelatedCases', TRUE);
77 //check for civicase access.
78 if (!CRM_Case_BAO_Case
::accessCiviCase()) {
79 CRM_Core_Error
::fatal(ts('You are not authorized to access this page.'));
81 $this->_hasAccessToAllCases
= CRM_Core_Permission
::check('access all cases and activities');
82 $this->assign('hasAccessToAllCases', $this->_hasAccessToAllCases
);
84 $this->_contactID
= $this->get('cid');
85 $this->_caseID
= $this->get('id');
87 $fulltext = CRM_Utils_Request
::retrieve('context', 'String', CRM_Core_DAO
::$_nullObject);
88 if ($fulltext == 'fulltext') {
89 $this->assign('fulltext', $fulltext);
92 $this->assign('caseID', $this->_caseID
);
93 $this->assign('contactID', $this->_contactID
);
96 $this->_userCases
= array();
97 $session = CRM_Core_Session
::singleton();
98 $userID = $session->get('userID');
99 if (!$this->_hasAccessToAllCases
) {
100 $this->_userCases
= CRM_Case_BAO_Case
::getCases(FALSE, $userID);
101 if (!array_key_exists($this->_caseID
, $this->_userCases
)) {
102 CRM_Core_Error
::fatal(ts('You are not authorized to access this page.'));
105 $this->assign('userID', $userID);
107 if (CRM_Case_BAO_Case
::caseCount($this->_contactID
) >= 2) {
108 $this->_mergeCases
= TRUE;
110 $this->assign('mergeCases', $this->_mergeCases
);
112 //retrieve details about case
113 $params = array('id' => $this->_caseID
);
115 $returnProperties = array('case_type_id', 'subject', 'status_id', 'start_date');
116 CRM_Core_DAO
::commonRetrieve('CRM_Case_BAO_Case', $params, $values, $returnProperties);
118 $values['case_type_id'] = trim(CRM_Utils_Array
::value('case_type_id', $values),
119 CRM_Core_DAO
::VALUE_SEPARATOR
121 $values['case_type_id'] = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
122 CRM_Utils_Array
::value('case_type_id', $values)
125 $statuses = CRM_Case_PseudoConstant
::caseStatus('label', FALSE);
126 $caseTypeName = CRM_Case_BAO_Case
::getCaseType($this->_caseID
, 'name');
127 $caseType = CRM_Case_BAO_Case
::getCaseType($this->_caseID
);
129 $this->_caseDetails
= array(
130 'case_type' => $caseType,
131 'case_status' => $statuses[$values['case_status_id']],
132 'case_subject' => CRM_Utils_Array
::value('subject', $values),
133 'case_start_date' => $values['case_start_date'],
135 $this->_caseType
= $caseTypeName;
136 $this->assign('caseDetails', $this->_caseDetails
);
138 $newActivityUrl = CRM_Utils_System
::url('civicrm/case/activity',
139 "action=add&reset=1&cid={$this->_contactID}&caseid={$this->_caseID}&atype=",
142 $this->assign('newActivityUrl', $newActivityUrl);
144 // Send Email activity requires a different URL format from all other activities
145 $newActivityEmailUrl = CRM_Utils_System
::url('civicrm/activity/email/add',
146 "action=add&context=standalone&reset=1&caseid={$this->_caseID}&atype=",
149 $this->assign('newActivityEmailUrl', $newActivityEmailUrl);
151 $reportUrl = CRM_Utils_System
::url('civicrm/case/report',
152 "reset=1&cid={$this->_contactID}&caseid={$this->_caseID}&asn=",
155 $this->assign('reportUrl', $reportUrl);
157 // add to recently viewed
159 $url = CRM_Utils_System
::url('civicrm/contact/view/case',
160 "action=view&reset=1&id={$this->_caseID}&cid={$this->_contactID}&context=home"
163 $displayName = CRM_Contact_BAO_Contact
::displayName($this->_contactID
);
164 $this->assign('displayName', $displayName);
166 $title = $displayName . ' - ' . $caseType;
167 CRM_Utils_System
::setTitle(ts('Case Summary for') . ' ' . $title);
169 $recentOther = array();
170 if (CRM_Core_Permission
::checkActionPermission('CiviCase', CRM_Core_Action
::DELETE
)) {
171 $recentOther['deleteUrl'] = CRM_Utils_System
::url('civicrm/contact/view/case',
172 "action=delete&reset=1&id={$this->_caseID}&cid={$this->_contactID}&context=home"
176 // add the recently created case
177 CRM_Utils_Recent
::add($displayName . ' - ' . $caseType,
187 //get the related cases for given case.
188 $relatedCases = $this->get('relatedCases');
189 if (!isset($relatedCases)) {
190 $relatedCases = CRM_Case_BAO_Case
::getRelatedCases($this->_caseID
, $this->_contactID
);
191 $relatedCases = empty($relatedCases) ?
FALSE : $relatedCases;
192 $this->set('relatedCases', $relatedCases);
194 $this->assign('hasRelatedCases', $relatedCases);
196 $entitySubType = !empty($values['case_type_id']) ?
$values['case_type_id'][0] : NULL;
197 $this->assign('caseTypeID', $entitySubType);
198 $groupTree = &CRM_Core_BAO_CustomGroup
::getTree('Case',
204 CRM_Core_BAO_CustomGroup
::buildCustomDataView($this,
210 * This function sets the default values for the form. For edit/view mode
211 * the default values are retrieved from the database
217 function setDefaultValues() {
223 * Function to build the form
228 public function buildQuickForm() {
229 //this call is for show related cases.
230 if ($this->_showRelatedCases
) {
234 $xmlProcessor = new CRM_Case_XMLProcessor_Process();
235 $caseRoles = $xmlProcessor->get($this->_caseType
, 'CaseRoles');
236 $reports = $xmlProcessor->get($this->_caseType
, 'ActivitySets');
238 //adding case manager.CRM-4510.
239 $managerRoleId = $xmlProcessor->getCaseManagerRoleId($this->_caseType
);
240 if (!empty($managerRoleId)) {
241 $caseRoles[$managerRoleId] = $caseRoles[$managerRoleId] . '<br />' . '(' . ts('Case Manager') . ')';
244 $aTypes = $xmlProcessor->get($this->_caseType
, 'ActivityTypes', TRUE);
246 $allActTypes = CRM_Core_PseudoConstant
::activityType(TRUE, TRUE, FALSE, 'name');
248 // remove Open Case activity type since we're inside an existing case
249 if (($openActTypeId = array_search('Open Case', $allActTypes)) &&
250 array_key_exists($openActTypeId, $aTypes)
252 unset($aTypes[$openActTypeId]);
255 //check for link cases.
256 $unclosedCases = CRM_Case_BAO_Case
::getUnclosedCases(NULL, array($this->_caseID
));
257 if (empty($unclosedCases) &&
258 ($linkActTypeId = array_search('Link Cases', $allActTypes)) &&
259 array_key_exists($linkActTypeId, $aTypes)
261 unset($aTypes[$linkActTypeId]);
264 if (!$xmlProcessor->getNaturalActivityTypeSort()) {
268 $this->add('select', 'activity_type_id', ts('New Activity'), array('' => ts('- select activity type -')) +
$aTypes);
269 if ($this->_hasAccessToAllCases
) {
270 $this->add('select', 'report_id', ts('Run QA Audit / Redact'),
272 '' => ts('- select activity set -')) +
$reports
274 $this->add('select', 'timeline_id', ts('Add Timeline'),
276 '' => ts('- select activity set -')) +
$reports
279 $this->addElement('submit', $this->getButtonName('next'), ts('Go'),
281 'class' => 'form-submit-inline',
282 'onclick' => "return checkSelection( this );",
286 if ($this->_mergeCases
) {
287 $allCases = CRM_Case_BAO_Case
::getContactCases($this->_contactID
);
288 $otherCases = array();
289 foreach ($allCases as $caseId => $details) {
290 //filter current and own cases.
291 if (($caseId == $this->_caseID
) ||
292 (!$this->_hasAccessToAllCases
&&
293 !array_key_exists($caseId, $this->_userCases
)
299 $otherCases[$caseId] = 'Case ID: ' . $caseId . ' Type: ' . $details['case_type'] . ' Start: ' . $details['case_start_date'];
301 if (empty($otherCases)) {
302 $this->_mergeCases
= FALSE;
303 $this->assign('mergeCases', $this->_mergeCases
);
306 $this->add('select', 'merge_case_id',
307 ts('Select Case for Merge'),
309 '' => ts('- select case -')) +
$otherCases
311 $this->addElement('submit',
312 $this->getButtonName('next', 'merge_case'),
315 'class' => 'form-submit-inline',
316 'onclick' => "return checkSelection( this );",
322 $this->add('text', 'change_client_id', ts('Assign to another Client'));
323 $this->add('hidden', 'contact_id', '', array('id' => 'contact_id'));
324 $this->addElement('submit',
325 $this->getButtonName('next', 'edit_client'),
328 'class' => 'form-submit-inline',
329 'onclick' => "return checkSelection( this );",
333 $activityStatus = CRM_Core_PseudoConstant
::activityStatus();
334 $this->add('select', 'status_id', ts('Status'), array("" => ts(' - any status - ')) +
$activityStatus);
337 $this->addDate('activity_date_low', ts('Activity Dates - From'), FALSE, array('formatType' => 'searchDate'));
338 $this->addDate('activity_date_high', ts('To'), FALSE, array('formatType' => 'searchDate'));
340 if (CRM_Core_Permission
::check('administer CiviCRM')) {
341 $this->add('checkbox', 'activity_deleted', ts('Deleted Activities'));
344 //get case related relationships (Case Role)
345 $caseRelationships = CRM_Case_BAO_Case
::getCaseRoles($this->_contactID
, $this->_caseID
);
347 //save special label because we unset it in the loop
348 $managerLabel = empty($managerRoleId) ?
'' : $caseRoles[$managerRoleId];
350 //build reporter select
351 $reporters = array("" => ts(' - any reporter - '));
352 foreach ($caseRelationships as $key => & $value) {
353 $reporters[$value['cid']] = $value['name'] . " ( {$value['relation']} )";
355 if (!empty($managerRoleId)) {
356 if ($managerRoleId == $value['relation_type']) {
357 $value['relation'] = $managerLabel;
361 //calculate roles that don't have relationships
362 if (CRM_Utils_Array
::value($value['relation_type'], $caseRoles)) {
363 unset($caseRoles[$value['relation_type']]);
367 // take all case activity types for search filter, CRM-7187
368 $aTypesFilter = array();
369 $allCaseActTypes = CRM_Case_PseudoConstant
::caseActivityType();
370 foreach ($allCaseActTypes as $typeDetails) {
371 if (!in_array($typeDetails['name'], array(
373 $aTypesFilter[$typeDetails['id']] = CRM_Utils_Array
::value('label', $typeDetails);
376 asort($aTypesFilter);
377 $this->add('select', 'activity_type_filter_id', ts('Activity Type'), array('' => ts('- select activity type -')) +
$aTypesFilter);
379 $this->assign('caseRelationships', $caseRelationships);
381 //also add client as role. CRM-4438
382 $caseRoles['client'] = CRM_Case_BAO_Case
::getContactNames($this->_caseID
);
384 $this->assign('caseRoles', $caseRoles);
386 $this->add('select', 'reporter_id', ts('Reporter/Role'), $reporters);
388 // Retrieve ALL client relationships
389 $relClient = CRM_Contact_BAO_Relationship
::getRelationship($this->_contactID
,
390 CRM_Contact_BAO_Relationship
::CURRENT
,
391 0, 0, 0, NULL, NULL, FALSE
394 // Now build 'Other Relationships' array by removing relationships that are already listed under Case Roles
395 // so they don't show up twice.
396 $clientRelationships = array();
397 foreach ($relClient as $r) {
398 if (!array_key_exists($r['id'], $caseRelationships)) {
399 $clientRelationships[] = $r;
402 $this->assign('clientRelationships', $clientRelationships);
404 // Now global contact list that appears on all cases.
405 $globalGroupInfo = array();
406 $relGlobal = CRM_Case_BAO_Case
::getGlobalContacts($globalGroupInfo);
407 $this->assign('globalRelationships', $relGlobal);
408 $this->assign('globalGroupInfo', $globalGroupInfo);
410 // List of relationship types
411 $baoRel = new CRM_Contact_BAO_Relationship();
412 $relType = $baoRel->getRelationType('Individual');
413 $roleTypes = array();
414 foreach ($relType as $k => $v) {
415 $roleTypes[substr($k, 0, strpos($k, '_'))] = $v;
417 $this->add('select', 'role_type', ts('Relationship Type'), array('' => ts('- select type -')) +
$roleTypes);
419 $hookCaseSummary = CRM_Utils_Hook
::caseSummary($this->_caseID
);
420 if (is_array($hookCaseSummary)) {
421 $this->assign('hookCaseSummary', $hookCaseSummary);
425 $allTags = CRM_Core_BAO_Tag
::getTags('civicrm_case');
427 if (!empty($allTags)) {
428 $this->add('select', 'case_tag', ts('Tags'), $allTags, FALSE,
429 array('id' => 'tags', 'multiple' => 'multiple', 'title' => ts('- select -'))
432 $tags = CRM_Core_BAO_EntityTag
::getTag($this->_caseID
, 'civicrm_case');
434 $this->setDefaults(array('case_tag' => $tags));
436 foreach ($tags as $tid) {
437 $tags[$tid] = $allTags[$tid];
440 $this->assign('tags', implode(', ', array_filter($tags)));
441 $this->assign('showTags', TRUE);
444 $this->assign('showTags', FALSE);
447 // build tagset widget
449 // see if we have any tagsets which can be assigned to cases
450 $parentNames = CRM_Core_BAO_Tag
::getTagSet('civicrm_case');
452 $this->assign('showTagsets', TRUE);
455 $this->assign('showTagsets', FALSE);
457 CRM_Core_Form_Tag
::buildQuickForm($this, $parentNames, 'civicrm_case', $this->_caseID
, FALSE, TRUE);
459 $this->addButtons(array(
462 'name' => ts('Done'),
463 'spacing' => ' ',
476 public function postProcess() {
477 $params = $this->controller
->exportValues($this->_name
);
478 $buttonName = $this->controller
->getButtonName();
481 $url = CRM_Utils_System
::url('civicrm/contact/view/case',
482 "reset=1&action=view&cid={$this->_contactID}&id={$this->_caseID}&show=1"
484 $session = CRM_Core_Session
::singleton();
485 $session->pushUserContext($url);
487 if (CRM_Utils_Array
::value('timeline_id', $params) &&
488 CRM_Utils_Array
::value('_qf_CaseView_next', $_POST)
490 $session = CRM_Core_Session
::singleton();
491 $this->_uid
= $session->get('userID');
492 $xmlProcessor = new CRM_Case_XMLProcessor_Process();
493 $xmlProcessorParams = array(
494 'clientID' => $this->_contactID
,
495 'creatorID' => $this->_uid
,
496 'standardTimeline' => 0,
497 'activity_date_time' => date('YmdHis'),
498 'caseID' => $this->_caseID
,
499 'caseType' => $this->_caseType
,
500 'activitySetName' => $params['timeline_id'],
502 $xmlProcessor->run($this->_caseType
, $xmlProcessorParams);
503 $reports = $xmlProcessor->get($this->_caseType
, 'ActivitySets');
505 CRM_Core_Session
::setStatus(ts('Activities from the %1 activity set have been added to this case.',
506 array(1 => $reports[$params['timeline_id']])
507 ), ts('Done'), 'success');
509 elseif ($this->_mergeCases
&&
510 $buttonName == '_qf_CaseView_next_merge_case'
513 $mainCaseId = $params['merge_case_id'];
514 $otherCaseId = $this->_caseID
;
517 CRM_Case_BAO_Case
::mergeCases($this->_contactID
, $mainCaseId, NULL, $otherCaseId);
519 //redirect user to main case view.
520 $url = CRM_Utils_System
::url('civicrm/contact/view/case',
521 "reset=1&action=view&cid={$this->_contactID}&id={$mainCaseId}&show=1"
523 $session = CRM_Core_Session
::singleton();
524 $session->pushUserContext($url);
526 elseif ($buttonName == '_qf_CaseView_next_edit_client') {
527 $mainCaseId = CRM_Case_BAO_Case
::mergeCases($params['contact_id'], $this->_caseID
, $this->_contactID
, NULL, TRUE);
530 $url = CRM_Utils_System
::url('civicrm/contact/view/case',
531 "reset=1&action=view&cid={$params['contact_id']}&id={$mainCaseId[0]}&show=1"
533 $session = CRM_Core_Session
::singleton();
534 $session->pushUserContext($url);