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