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 // 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, 'any');
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 CRM_Utils_System
::setTitle($displayName . ' - ' . $caseType);
168 $recentOther = array();
169 if (CRM_Core_Permission
::checkActionPermission('CiviCase', CRM_Core_Action
::DELETE
)) {
170 $recentOther['deleteUrl'] = CRM_Utils_System
::url('civicrm/contact/view/case',
171 "action=delete&reset=1&id={$this->_caseID}&cid={$this->_contactID}&context=home"
175 // Add the recently viewed case
176 CRM_Utils_Recent
::add($displayName . ' - ' . $caseType,
186 //get the related cases for given case.
187 $relatedCases = $this->get('relatedCases');
188 if (!isset($relatedCases)) {
189 $relatedCases = CRM_Case_BAO_Case
::getRelatedCases($this->_caseID
, $this->_contactID
);
190 $relatedCases = empty($relatedCases) ?
FALSE : $relatedCases;
191 $this->set('relatedCases', $relatedCases);
193 $this->assign('hasRelatedCases', $relatedCases);
195 $entitySubType = !empty($values['case_type_id']) ?
$values['case_type_id'][0] : NULL;
196 $this->assign('caseTypeID', $entitySubType);
197 $groupTree = &CRM_Core_BAO_CustomGroup
::getTree('Case',
203 CRM_Core_BAO_CustomGroup
::buildCustomDataView($this,
209 * This function sets the default values for the form. For edit/view mode
210 * the default values are retrieved from the database
216 function setDefaultValues() {
222 * Function to build the form
227 public function buildQuickForm() {
228 //this call is for show related cases.
229 if ($this->_showRelatedCases
) {
233 $xmlProcessor = new CRM_Case_XMLProcessor_Process();
234 $caseRoles = $xmlProcessor->get($this->_caseType
, 'CaseRoles');
235 $reports = $xmlProcessor->get($this->_caseType
, 'ActivitySets');
237 //adding case manager.CRM-4510.
238 $managerRoleId = $xmlProcessor->getCaseManagerRoleId($this->_caseType
);
239 if (!empty($managerRoleId)) {
240 $caseRoles[$managerRoleId] = $caseRoles[$managerRoleId] . '<br />' . '(' . ts('Case Manager') . ')';
243 $aTypes = $xmlProcessor->get($this->_caseType
, 'ActivityTypes', TRUE);
245 $allActTypes = CRM_Core_PseudoConstant
::activityType(TRUE, TRUE, FALSE, 'name');
247 // remove Open Case activity type since we're inside an existing case
248 if (($openActTypeId = array_search('Open Case', $allActTypes)) &&
249 array_key_exists($openActTypeId, $aTypes)
251 unset($aTypes[$openActTypeId]);
254 //check for link cases.
255 $unclosedCases = CRM_Case_BAO_Case
::getUnclosedCases(NULL, array($this->_caseID
));
256 if (empty($unclosedCases) &&
257 ($linkActTypeId = array_search('Link Cases', $allActTypes)) &&
258 array_key_exists($linkActTypeId, $aTypes)
260 unset($aTypes[$linkActTypeId]);
263 if (!$xmlProcessor->getNaturalActivityTypeSort()) {
267 $this->add('select', 'activity_type_id', ts('New Activity'), array('' => ts('- select activity type -')) +
$aTypes);
268 if ($this->_hasAccessToAllCases
) {
269 $this->add('select', 'report_id', ts('Run QA Audit / Redact'),
271 '' => ts('- select activity set -')) +
$reports
273 $this->add('select', 'timeline_id', ts('Add Timeline'),
275 '' => ts('- select activity set -')) +
$reports
278 $this->addElement('submit', $this->getButtonName('next'), ts('Go'),
280 'class' => 'form-submit-inline',
281 'onclick' => "return checkSelection( this );",
285 if ($this->_mergeCases
) {
286 $allCases = CRM_Case_BAO_Case
::getContactCases($this->_contactID
);
287 $otherCases = array();
288 foreach ($allCases as $caseId => $details) {
289 //filter current and own cases.
290 if (($caseId == $this->_caseID
) ||
291 (!$this->_hasAccessToAllCases
&&
292 !array_key_exists($caseId, $this->_userCases
)
298 $otherCases[$caseId] = 'Case ID: ' . $caseId . ' Type: ' . $details['case_type'] . ' Start: ' . $details['case_start_date'];
300 if (empty($otherCases)) {
301 $this->_mergeCases
= FALSE;
302 $this->assign('mergeCases', $this->_mergeCases
);
305 $this->add('select', 'merge_case_id',
306 ts('Select Case for Merge'),
308 '' => ts('- select case -')) +
$otherCases
310 $this->addElement('submit',
311 $this->getButtonName('next', 'merge_case'),
314 'class' => 'form-submit-inline',
315 'onclick' => "return checkSelection( this );",
321 $this->add('text', 'change_client_id', ts('Assign to another Client'));
322 $this->add('hidden', 'contact_id', '', array('id' => 'contact_id'));
323 $this->addElement('submit',
324 $this->getButtonName('next', 'edit_client'),
327 'class' => 'form-submit-inline',
328 'onclick' => "return checkSelection( this );",
332 $activityStatus = CRM_Core_PseudoConstant
::activityStatus();
333 $this->add('select', 'status_id', ts('Status'), array("" => ts(' - any status - ')) +
$activityStatus);
336 $this->addDate('activity_date_low', ts('Activity Dates - From'), FALSE, array('formatType' => 'searchDate'));
337 $this->addDate('activity_date_high', ts('To'), FALSE, array('formatType' => 'searchDate'));
339 if (CRM_Core_Permission
::check('administer CiviCRM')) {
340 $this->add('checkbox', 'activity_deleted', ts('Deleted Activities'));
343 //get case related relationships (Case Role)
344 $caseRelationships = CRM_Case_BAO_Case
::getCaseRoles($this->_contactID
, $this->_caseID
);
346 //save special label because we unset it in the loop
347 $managerLabel = empty($managerRoleId) ?
'' : $caseRoles[$managerRoleId];
349 //build reporter select
350 $reporters = array("" => ts(' - any reporter - '));
351 foreach ($caseRelationships as $key => & $value) {
352 $reporters[$value['cid']] = $value['name'] . " ( {$value['relation']} )";
354 if (!empty($managerRoleId)) {
355 if ($managerRoleId == $value['relation_type']) {
356 $value['relation'] = $managerLabel;
360 //calculate roles that don't have relationships
361 if (CRM_Utils_Array
::value($value['relation_type'], $caseRoles)) {
362 unset($caseRoles[$value['relation_type']]);
366 // take all case activity types for search filter, CRM-7187
367 $aTypesFilter = array();
368 $allCaseActTypes = CRM_Case_PseudoConstant
::caseActivityType();
369 foreach ($allCaseActTypes as $typeDetails) {
370 if (!in_array($typeDetails['name'], array(
372 $aTypesFilter[$typeDetails['id']] = CRM_Utils_Array
::value('label', $typeDetails);
375 asort($aTypesFilter);
376 $this->add('select', 'activity_type_filter_id', ts('Activity Type'), array('' => ts('- select activity type -')) +
$aTypesFilter);
378 $this->assign('caseRelationships', $caseRelationships);
380 //also add client as role. CRM-4438
381 $caseRoles['client'] = CRM_Case_BAO_Case
::getContactNames($this->_caseID
);
383 $this->assign('caseRoles', $caseRoles);
385 $this->add('select', 'reporter_id', ts('Reporter/Role'), $reporters);
387 // Retrieve ALL client relationships
388 $relClient = CRM_Contact_BAO_Relationship
::getRelationship($this->_contactID
,
389 CRM_Contact_BAO_Relationship
::CURRENT
,
390 0, 0, 0, NULL, NULL, FALSE
393 // Now build 'Other Relationships' array by removing relationships that are already listed under Case Roles
394 // so they don't show up twice.
395 $clientRelationships = array();
396 foreach ($relClient as $r) {
397 if (!array_key_exists($r['id'], $caseRelationships)) {
398 $clientRelationships[] = $r;
401 $this->assign('clientRelationships', $clientRelationships);
403 // Now global contact list that appears on all cases.
404 $globalGroupInfo = array();
405 $relGlobal = CRM_Case_BAO_Case
::getGlobalContacts($globalGroupInfo);
406 $this->assign('globalRelationships', $relGlobal);
407 $this->assign('globalGroupInfo', $globalGroupInfo);
409 // List of relationship types
410 $baoRel = new CRM_Contact_BAO_Relationship();
411 $relType = $baoRel->getRelationType('Individual');
412 $roleTypes = array();
413 foreach ($relType as $k => $v) {
414 $roleTypes[substr($k, 0, strpos($k, '_'))] = $v;
416 $this->add('select', 'role_type', ts('Relationship Type'), array('' => ts('- select type -')) +
$roleTypes);
418 $hookCaseSummary = CRM_Utils_Hook
::caseSummary($this->_caseID
);
419 if (is_array($hookCaseSummary)) {
420 $this->assign('hookCaseSummary', $hookCaseSummary);
424 $allTags = CRM_Core_BAO_Tag
::getTags('civicrm_case');
426 if (!empty($allTags)) {
427 $this->add('select', 'case_tag', ts('Tags'), $allTags, FALSE,
428 array('id' => 'tags', 'multiple' => 'multiple', 'title' => ts('- select -'))
431 $tags = CRM_Core_BAO_EntityTag
::getTag($this->_caseID
, 'civicrm_case');
433 $this->setDefaults(array('case_tag' => $tags));
435 foreach ($tags as $tid) {
436 $tags[$tid] = $allTags[$tid];
439 $this->assign('tags', implode(', ', array_filter($tags)));
440 $this->assign('showTags', TRUE);
443 $this->assign('showTags', FALSE);
446 // build tagset widget
448 // see if we have any tagsets which can be assigned to cases
449 $parentNames = CRM_Core_BAO_Tag
::getTagSet('civicrm_case');
451 $this->assign('showTagsets', TRUE);
454 $this->assign('showTagsets', FALSE);
456 CRM_Core_Form_Tag
::buildQuickForm($this, $parentNames, 'civicrm_case', $this->_caseID
, FALSE, TRUE);
458 $this->addButtons(array(
461 'name' => ts('Done'),
462 'spacing' => ' ',
475 public function postProcess() {
476 $params = $this->controller
->exportValues($this->_name
);
477 $buttonName = $this->controller
->getButtonName();
480 $url = CRM_Utils_System
::url('civicrm/contact/view/case',
481 "reset=1&action=view&cid={$this->_contactID}&id={$this->_caseID}&show=1"
483 $session = CRM_Core_Session
::singleton();
484 $session->pushUserContext($url);
486 if (CRM_Utils_Array
::value('timeline_id', $params) &&
487 CRM_Utils_Array
::value('_qf_CaseView_next', $_POST)
489 $session = CRM_Core_Session
::singleton();
490 $this->_uid
= $session->get('userID');
491 $xmlProcessor = new CRM_Case_XMLProcessor_Process();
492 $xmlProcessorParams = array(
493 'clientID' => $this->_contactID
,
494 'creatorID' => $this->_uid
,
495 'standardTimeline' => 0,
496 'activity_date_time' => date('YmdHis'),
497 'caseID' => $this->_caseID
,
498 'caseType' => $this->_caseType
,
499 'activitySetName' => $params['timeline_id'],
501 $xmlProcessor->run($this->_caseType
, $xmlProcessorParams);
502 $reports = $xmlProcessor->get($this->_caseType
, 'ActivitySets');
504 CRM_Core_Session
::setStatus(ts('Activities from the %1 activity set have been added to this case.',
505 array(1 => $reports[$params['timeline_id']])
506 ), ts('Done'), 'success');
508 elseif ($this->_mergeCases
&&
509 $buttonName == '_qf_CaseView_next_merge_case'
512 $mainCaseId = $params['merge_case_id'];
513 $otherCaseId = $this->_caseID
;
516 CRM_Case_BAO_Case
::mergeCases($this->_contactID
, $mainCaseId, NULL, $otherCaseId);
518 //redirect user to main case view.
519 $url = CRM_Utils_System
::url('civicrm/contact/view/case',
520 "reset=1&action=view&cid={$this->_contactID}&id={$mainCaseId}&show=1"
522 $session = CRM_Core_Session
::singleton();
523 $session->pushUserContext($url);
525 elseif ($buttonName == '_qf_CaseView_next_edit_client') {
526 $mainCaseId = CRM_Case_BAO_Case
::mergeCases($params['contact_id'], $this->_caseID
, $this->_contactID
, NULL, TRUE);
529 $url = CRM_Utils_System
::url('civicrm/contact/view/case',
530 "reset=1&action=view&cid={$params['contact_id']}&id={$mainCaseId[0]}&show=1"
532 $session = CRM_Core_Session
::singleton();
533 $session->pushUserContext($url);