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