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