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