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