a21f4585f2d6abd515f30e849a9c4f5d5a82eb3b
[civicrm-core.git] / CRM / Case / Form / CaseView.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * This class generates view mode for CiviCase
38 *
39 */
40 class CRM_Case_Form_CaseView extends CRM_Core_Form {
41 /**
42 * Check for merge cases.
43 * @var bool
44 */
45 private $_mergeCases = FALSE;
46
47 /**
48 * Set variables up before form is built
49 *
50 * @return void
51 */
52 public function preProcess() {
53 $this->_showRelatedCases = CRM_Utils_Array::value('relatedCases', $_GET);
54
55 $xmlProcessorProcess = new CRM_Case_XMLProcessor_Process();
56 $isMultiClient = $xmlProcessorProcess->getAllowMultipleCaseClients();
57 $this->assign('multiClient', $isMultiClient);
58
59 //pull the related cases.
60 $this->assign('showRelatedCases', FALSE);
61 if ($this->_showRelatedCases) {
62 $relatedCases = $this->get('relatedCases');
63 if (!isset($relatedCases)) {
64 $cId = CRM_Utils_Request::retrieve('cid', 'Integer', CRM_Core_DAO::$_nullObject);
65 $caseId = CRM_Utils_Request::retrieve('id', 'Integer', CRM_Core_DAO::$_nullObject);
66 $relatedCases = CRM_Case_BAO_Case::getRelatedCases($caseId, $cId);
67 }
68 $this->assign('relatedCases', $relatedCases);
69 $this->assign('showRelatedCases', TRUE);
70 CRM_Utils_System::setTitle(ts('Related Cases'));
71 return;
72 }
73
74 //check for civicase access.
75 if (!CRM_Case_BAO_Case::accessCiviCase()) {
76 CRM_Core_Error::fatal(ts('You are not authorized to access this page.'));
77 }
78 $this->_hasAccessToAllCases = CRM_Core_Permission::check('access all cases and activities');
79 $this->assign('hasAccessToAllCases', $this->_hasAccessToAllCases);
80
81 $this->_contactID = $this->get('cid');
82 $this->_caseID = $this->get('id');
83
84 $fulltext = CRM_Utils_Request::retrieve('context', 'String', CRM_Core_DAO::$_nullObject);
85 if ($fulltext == 'fulltext') {
86 $this->assign('fulltext', $fulltext);
87 }
88
89 $this->assign('caseID', $this->_caseID);
90 $this->assign('contactID', $this->_contactID);
91
92 //validate case id.
93 $this->_userCases = array();
94 $session = CRM_Core_Session::singleton();
95 $userID = $session->get('userID');
96 if (!$this->_hasAccessToAllCases) {
97 $this->_userCases = CRM_Case_BAO_Case::getCases(FALSE, $userID, 'any');
98 if (!array_key_exists($this->_caseID, $this->_userCases)) {
99 CRM_Core_Error::fatal(ts('You are not authorized to access this page.'));
100 }
101 }
102 $this->assign('userID', $userID);
103
104 if (CRM_Case_BAO_Case::caseCount($this->_contactID) >= 2) {
105 $this->_mergeCases = TRUE;
106 }
107 $this->assign('mergeCases', $this->_mergeCases);
108
109 //retrieve details about case
110 $params = array('id' => $this->_caseID);
111
112 $returnProperties = array('case_type_id', 'subject', 'status_id', 'start_date');
113 CRM_Core_DAO::commonRetrieve('CRM_Case_BAO_Case', $params, $values, $returnProperties);
114
115 $statuses = CRM_Case_PseudoConstant::caseStatus('label', FALSE);
116 $caseTypeName = CRM_Case_BAO_Case::getCaseType($this->_caseID, 'name');
117 $caseType = CRM_Case_BAO_Case::getCaseType($this->_caseID);
118
119 $this->_caseDetails = array(
120 'case_type' => $caseType,
121 'case_status' => CRM_Utils_Array::value($values['case_status_id'], $statuses),
122 'case_subject' => CRM_Utils_Array::value('subject', $values),
123 'case_start_date' => $values['case_start_date'],
124 );
125 $this->_caseType = $caseTypeName;
126 $this->assign('caseDetails', $this->_caseDetails);
127
128 $reportUrl = CRM_Utils_System::url('civicrm/case/report',
129 "reset=1&cid={$this->_contactID}&caseid={$this->_caseID}&asn=",
130 FALSE, NULL, FALSE
131 );
132 $this->assign('reportUrl', $reportUrl);
133
134 // add to recently viewed
135
136 $url = CRM_Utils_System::url('civicrm/contact/view/case',
137 "action=view&reset=1&id={$this->_caseID}&cid={$this->_contactID}&context=home"
138 );
139
140 $displayName = CRM_Contact_BAO_Contact::displayName($this->_contactID);
141 $this->assign('displayName', $displayName);
142
143 CRM_Utils_System::setTitle($displayName . ' - ' . $caseType);
144
145 $recentOther = array();
146 if (CRM_Core_Permission::checkActionPermission('CiviCase', CRM_Core_Action::DELETE)) {
147 $recentOther['deleteUrl'] = CRM_Utils_System::url('civicrm/contact/view/case',
148 "action=delete&reset=1&id={$this->_caseID}&cid={$this->_contactID}&context=home"
149 );
150 }
151
152 // Add the recently viewed case
153 CRM_Utils_Recent::add($displayName . ' - ' . $caseType,
154 $url,
155 $this->_caseID,
156 'Case',
157 $this->_contactID,
158 NULL,
159 $recentOther
160 );
161
162
163 //get the related cases for given case.
164 $relatedCases = $this->get('relatedCases');
165 if (!isset($relatedCases)) {
166 $relatedCases = CRM_Case_BAO_Case::getRelatedCases($this->_caseID, $this->_contactID);
167 $relatedCases = empty($relatedCases) ? FALSE : $relatedCases;
168 $this->set('relatedCases', $relatedCases);
169 }
170 $this->assign('hasRelatedCases', (bool) $relatedCases);
171 if ($relatedCases) {
172 $this->assign('relatedCaseLabel', ts('%1 Related Case', array('count' => count($relatedCases), 'plural' => '%1 Related Cases')));
173 $this->assign('relatedCaseUrl', CRM_Utils_System::url('civicrm/contact/view/case', array(
174 'id' => $this->_caseID,
175 'cid' => $this->_contactID,
176 'relatedCases' => 1,
177 'action' => 'view',
178 )));
179 }
180
181 $entitySubType = !empty($values['case_type_id']) ? $values['case_type_id'] : NULL;
182 $this->assign('caseTypeID', $entitySubType);
183 $groupTree = &CRM_Core_BAO_CustomGroup::getTree('Case',
184 $this,
185 $this->_caseID,
186 NULL,
187 $entitySubType
188 );
189 CRM_Core_BAO_CustomGroup::buildCustomDataView($this,
190 $groupTree
191 );
192 }
193
194 /**
195 * Set default values for the form. For edit/view mode
196 * the default values are retrieved from the database
197 *
198 *
199 * @return void
200 */
201 public function setDefaultValues() {
202 $defaults = array();
203 return $defaults;
204 }
205
206 /**
207 * Build the form object
208 *
209 * @return void
210 */
211 public function buildQuickForm() {
212 //this call is for show related cases.
213 if ($this->_showRelatedCases) {
214 return;
215 }
216
217 CRM_Core_Resources::singleton()
218 ->addScriptFile('civicrm', 'js/crm.livePage.js', 1, 'html-header')
219 ->addScriptFile('civicrm', 'templates/CRM/Case/Form/CaseView.js', 2, 'html-header');
220
221 $xmlProcessor = new CRM_Case_XMLProcessor_Process();
222 $caseRoles = $xmlProcessor->get($this->_caseType, 'CaseRoles');
223 $reports = $xmlProcessor->get($this->_caseType, 'ActivitySets');
224
225 //adding case manager.CRM-4510.
226 $managerRoleId = $xmlProcessor->getCaseManagerRoleId($this->_caseType);
227 if (!empty($managerRoleId)) {
228 $caseRoles[$managerRoleId] = $caseRoles[$managerRoleId] . '<br />' . '(' . ts('Case Manager') . ')';
229 }
230
231 $aTypes = $xmlProcessor->get($this->_caseType, 'ActivityTypes', TRUE);
232
233 $allActTypes = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'name');
234
235 $emailActivityType = array_search('Email', $allActTypes);
236 $pdfActivityType = array_search('Print PDF Letter', $allActTypes);
237
238 // remove Open Case activity type since we're inside an existing case
239 if ($openActTypeId = array_search('Open Case', $allActTypes)) {
240 unset($aTypes[$openActTypeId]);
241 }
242
243 //check for link cases.
244 $unclosedCases = CRM_Case_BAO_Case::getUnclosedCases(NULL, array($this->_caseID));
245 if (empty($unclosedCases) && ($linkActTypeId = array_search('Link Cases', $allActTypes))) {
246 unset($aTypes[$linkActTypeId]);
247 }
248
249 if (!$xmlProcessor->getNaturalActivityTypeSort()) {
250 asort($aTypes);
251 }
252
253 $activityLinks = array('' => ts('Add Activity'));
254 foreach ($aTypes as $type => $label) {
255 if ($type == $emailActivityType) {
256 $url = CRM_Utils_System::url('civicrm/activity/email/add',
257 "action=add&context=standalone&reset=1&caseid={$this->_caseID}&atype=$type",
258 FALSE, NULL, FALSE
259 );
260 }
261 else if ($type == $pdfActivityType ) {
262 $url = CRM_Utils_System::url('civicrm/activity/pdf/add',
263 "action=add&context=standalone&reset=1&cid={$this->_contactID}&caseid={$this->_caseID}&atype=$type",
264 FALSE, NULL, FALSE );
265 }
266 else {
267 $url = CRM_Utils_System::url('civicrm/case/activity',
268 "action=add&reset=1&cid={$this->_contactID}&caseid={$this->_caseID}&atype=$type",
269 FALSE, NULL, FALSE
270 );
271 }
272 $activityLinks[$url] = $label;
273 }
274
275 $this->add('select', 'add_activity_type_id', '', $activityLinks, FALSE, array('class' => 'crm-select2 crm-action-menu action-icon-plus twenty'));
276 if ($this->_hasAccessToAllCases) {
277 $this->add('select', 'report_id', '',
278 array('' => ts('Activity Audit')) + $reports,
279 FALSE,
280 array('class' => 'crm-select2 crm-action-menu action-icon-clipboard')
281 );
282 $this->add('select', 'timeline_id', '',
283 array('' => ts('Add Timeline')) + $reports,
284 FALSE,
285 array('class' => 'crm-select2 crm-action-menu action-icon-play')
286 );
287 }
288 $this->addElement('submit', $this->getButtonName('next'), ' ', array('class' => 'hiddenElement'));
289
290 if ($this->_mergeCases) {
291 $allCases = CRM_Case_BAO_Case::getContactCases($this->_contactID);
292 $otherCases = array();
293 foreach ($allCases as $caseId => $details) {
294 //filter current and own cases.
295 if (($caseId == $this->_caseID) ||
296 (!$this->_hasAccessToAllCases &&
297 !array_key_exists($caseId, $this->_userCases)
298 )
299 ) {
300 continue;
301 }
302
303 $otherCases[$caseId] = 'Case ID: ' . $caseId . ' Type: ' . $details['case_type'] . ' Start: ' . $details['case_start_date'];
304 }
305 if (empty($otherCases)) {
306 $this->_mergeCases = FALSE;
307 $this->assign('mergeCases', $this->_mergeCases);
308 }
309 else {
310 $this->add('select', 'merge_case_id',
311 ts('Select Case for Merge'),
312 array(
313 '' => ts('- select case -')) + $otherCases,
314 FALSE,
315 array('class' => 'crm-select2 huge')
316 );
317 $this->addElement('submit',
318 $this->getButtonName('next', 'merge_case'),
319 ts('Merge'),
320 array(
321 'class' => 'crm-form-submit-inline hiddenElement',
322 )
323 );
324 }
325 }
326
327 //call activity form
328 self::activityForm($this, $aTypes);
329
330 //get case related relationships (Case Role)
331 $caseRelationships = CRM_Case_BAO_Case::getCaseRoles($this->_contactID, $this->_caseID);
332
333 //save special label because we unset it in the loop
334 $managerLabel = empty($managerRoleId) ? '' : $caseRoles[$managerRoleId];
335
336 foreach ($caseRelationships as $key => & $value) {
337 if (!empty($managerRoleId)) {
338 if ($managerRoleId == $value['relation_type']) {
339 $value['relation'] = $managerLabel;
340 }
341 }
342
343 //calculate roles that don't have relationships
344 if (!empty($caseRoles[$value['relation_type']])) {
345 unset($caseRoles[$value['relation_type']]);
346 }
347 }
348
349 $this->assign('caseRelationships', $caseRelationships);
350
351 //also add client as role. CRM-4438
352 $caseRoles['client'] = CRM_Case_BAO_Case::getContactNames($this->_caseID);
353
354 $this->assign('caseRoles', $caseRoles);
355
356 // Retrieve ALL client relationships
357 $relClient = CRM_Contact_BAO_Relationship::getRelationship($this->_contactID,
358 CRM_Contact_BAO_Relationship::CURRENT,
359 0, 0, 0, NULL, NULL, FALSE
360 );
361
362 // Now build 'Other Relationships' array by removing relationships that are already listed under Case Roles
363 // so they don't show up twice.
364 $clientRelationships = array();
365 foreach ($relClient as $r) {
366 if (!array_key_exists($r['id'], $caseRelationships)) {
367 $clientRelationships[] = $r;
368 }
369 }
370 $this->assign('clientRelationships', $clientRelationships);
371
372 // Now global contact list that appears on all cases.
373 $globalGroupInfo = array();
374 CRM_Case_BAO_Case::getGlobalContacts($globalGroupInfo);
375 $this->assign('globalGroupInfo', $globalGroupInfo);
376
377 // List of relationship types
378 $baoRel = new CRM_Contact_BAO_Relationship();
379 $relType = $baoRel->getRelationType('Individual');
380 $roleTypes = array();
381 foreach ($relType as $k => $v) {
382 $roleTypes[substr($k, 0, strpos($k, '_'))] = $v;
383 }
384 $this->add('select', 'role_type', ts('Relationship Type'), array('' => ts('- select type -')) + $roleTypes, FALSE, array('class' => 'crm-select2 twenty'));
385
386 $hookCaseSummary = CRM_Utils_Hook::caseSummary($this->_caseID);
387 if (is_array($hookCaseSummary)) {
388 $this->assign('hookCaseSummary', $hookCaseSummary);
389 }
390
391 CRM_Core_BAO_Tag::getTags('civicrm_case', $allTags, NULL,
392 '&nbsp;&nbsp;', TRUE);
393
394 if (!empty($allTags)) {
395 $this->add('select', 'case_tag', ts('Tags'), $allTags, FALSE,
396 array('id' => 'tags', 'multiple' => 'multiple', 'class' => 'crm-select2')
397 );
398
399 $tags = CRM_Core_BAO_EntityTag::getTag($this->_caseID, 'civicrm_case');
400
401 $this->setDefaults(array('case_tag' => $tags));
402
403 foreach ($tags as $tid) {
404 if (isset($allTags[$tid])) {
405 $tags[$tid] = $allTags[$tid];
406 }
407 else {
408 unset($tags[$tid]);
409 }
410 }
411
412 $this->assign('tags', implode(', ', array_filter($tags)));
413 $this->assign('showTags', TRUE);
414 }
415 else {
416 $this->assign('showTags', FALSE);
417 }
418
419 // build tagset widget
420
421 // see if we have any tagsets which can be assigned to cases
422 $parentNames = CRM_Core_BAO_Tag::getTagSet('civicrm_case');
423 if ($parentNames) {
424 $this->assign('showTagsets', TRUE);
425 }
426 else {
427 $this->assign('showTagsets', FALSE);
428 }
429 CRM_Core_Form_Tag::buildQuickForm($this, $parentNames, 'civicrm_case', $this->_caseID, FALSE, TRUE);
430
431 $this->addButtons(array(
432 array(
433 'type' => 'cancel',
434 'name' => ts('Done'),
435 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
436 'isDefault' => TRUE,
437 ),
438 )
439 );
440 }
441
442 /**
443 * Process the form
444 *
445 * @return void
446 */
447 public function postProcess() {
448 $params = $this->controller->exportValues($this->_name);
449 $buttonName = $this->controller->getButtonName();
450
451 // user context
452 $url = CRM_Utils_System::url('civicrm/contact/view/case',
453 "reset=1&action=view&cid={$this->_contactID}&id={$this->_caseID}&show=1"
454 );
455 $session = CRM_Core_Session::singleton();
456 $session->pushUserContext($url);
457
458 if (!empty($params['timeline_id']) && !empty($_POST['_qf_CaseView_next'])) {
459 $session = CRM_Core_Session::singleton();
460 $this->_uid = $session->get('userID');
461 $xmlProcessor = new CRM_Case_XMLProcessor_Process();
462 $xmlProcessorParams = array(
463 'clientID' => $this->_contactID,
464 'creatorID' => $this->_uid,
465 'standardTimeline' => 0,
466 'activity_date_time' => date('YmdHis'),
467 'caseID' => $this->_caseID,
468 'caseType' => $this->_caseType,
469 'activitySetName' => $params['timeline_id'],
470 );
471 $xmlProcessor->run($this->_caseType, $xmlProcessorParams);
472 $reports = $xmlProcessor->get($this->_caseType, 'ActivitySets');
473
474 CRM_Core_Session::setStatus(ts('Activities from the %1 activity set have been added to this case.',
475 array(1 => $reports[$params['timeline_id']])
476 ), ts('Done'), 'success');
477 }
478 elseif ($this->_mergeCases &&
479 $buttonName == '_qf_CaseView_next_merge_case'
480 ) {
481
482 $mainCaseId = $params['merge_case_id'];
483 $otherCaseId = $this->_caseID;
484
485 //merge two cases.
486 CRM_Case_BAO_Case::mergeCases($this->_contactID, $mainCaseId, NULL, $otherCaseId);
487
488 //redirect user to main case view.
489 $url = CRM_Utils_System::url('civicrm/contact/view/case',
490 "reset=1&action=view&cid={$this->_contactID}&id={$mainCaseId}&show=1"
491 );
492 $session = CRM_Core_Session::singleton();
493 $session->pushUserContext($url);
494 }
495 }
496
497 /**
498 * Build the activity selector/datatable
499 * @param CRM_Core_Form $form
500 * @param array $aTypes
501 * To include acivities related to current case id $form->_caseID.
502 */
503 public static function activityForm($form, $aTypes = array()) {
504 $caseRelationships = CRM_Case_BAO_Case::getCaseRoles($form->_contactID, $form->_caseID);
505 //build reporter select
506 $reporters = array("" => ts(' - any reporter - '));
507 foreach ($caseRelationships as $key => & $value) {
508 $reporters[$value['cid']] = $value['name'] . " ( {$value['relation']} )";
509 }
510 $form->add('select', 'reporter_id', ts('Reporter/Role'), $reporters, FALSE, array('id' => 'reporter_id_'.$form->_caseID));
511
512 // take all case activity types for search filter, CRM-7187
513 $aTypesFilter = array();
514 $allCaseActTypes = CRM_Case_PseudoConstant::caseActivityType();
515 foreach ($allCaseActTypes as $typeDetails) {
516 if (!in_array($typeDetails['name'], array('Open Case'))) {
517 $aTypesFilter[$typeDetails['id']] = CRM_Utils_Array::value('label', $typeDetails);
518 }
519 }
520 $aTypesFilter = $aTypesFilter + $aTypes;
521 asort($aTypesFilter);
522 $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));
523
524 $activityStatus = CRM_Core_PseudoConstant::activityStatus();
525 $form->add('select', 'status_id', ts('Status'), array("" => ts(' - any status - ')) + $activityStatus, FALSE, array('id' => 'status_id_'.$form->_caseID));
526
527 // activity dates
528 $form->addDate('activity_date_low_'.$form->_caseID, ts('Activity Dates - From'), FALSE, array('formatType' => 'searchDate'));
529 $form->addDate('activity_date_high_'.$form->_caseID, ts('To'), FALSE, array('formatType' => 'searchDate'));
530
531 if (CRM_Core_Permission::check('administer CiviCRM')) {
532 $form->add('checkbox', 'activity_deleted', ts('Deleted Activities'), '', FALSE, array('id' => 'activity_deleted_'.$form->_caseID));
533 }
534 }
535 }