CRM-20368 case summary report
[civicrm-core.git] / CRM / Report / Form / Case / Summary.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
0f03f337 6 | Copyright CiviCRM LLC (c) 2004-2017 |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
0f03f337 31 * @copyright CiviCRM LLC (c) 2004-2017
6a488035
TO
32 */
33class CRM_Report_Form_Case_Summary extends CRM_Report_Form {
34
35 protected $_summary = NULL;
36 protected $_relField = FALSE;
01bf13dc 37 protected $_exposeContactID = FALSE;
74cf4551 38
dd3aa6f0 39 protected $_customGroupExtends = array('Case');
b326c682 40
74cf4551 41 /**
73b448bf 42 * Class constructor.
74cf4551 43 */
00be9182 44 public function __construct() {
9d72cede 45 $this->case_types = CRM_Case_PseudoConstant::caseType();
01bf13dc 46 $this->case_statuses = CRM_Core_OptionGroup::values('case_status');
9d72cede 47 $rels = CRM_Core_PseudoConstant::relationshipType();
6a488035
TO
48 foreach ($rels as $relid => $v) {
49 $this->rel_types[$relid] = $v['label_b_a'];
50 }
51
9d72cede
EM
52 $this->deleted_labels = array(
53 '' => ts('- select -'),
54 0 => ts('No'),
21dfd5f5 55 1 => ts('Yes'),
9d72cede 56 );
6a488035
TO
57
58 $this->_columns = array(
9d72cede 59 'civicrm_c2' => array(
6a488035 60 'dao' => 'CRM_Contact_DAO_Contact',
9d72cede
EM
61 'fields' => array(
62 'client_name' => array(
6a488035 63 'name' => 'sort_name',
60c41e01 64 'title' => ts('Contact Name'),
6a488035
TO
65 'required' => TRUE,
66 ),
9d72cede 67 'id' => array(
6a488035
TO
68 'no_display' => TRUE,
69 'required' => TRUE,
70 ),
71 ),
60c41e01
BS
72 'order_bys' => array(
73 'client_name' => array(
74 'title' => ts('Contact Name'),
75 'name' => 'sort_name'
76 ),
77 ),
78 'grouping' => 'case-fields',
6a488035 79 ),
9d72cede 80 'civicrm_case' => array(
6a488035 81 'dao' => 'CRM_Case_DAO_Case',
9d72cede
EM
82 'fields' => array(
83 'id' => array(
84 'title' => ts('Case ID'),
6a488035
TO
85 'required' => TRUE,
86 ),
87 'subject' => array(
9d72cede
EM
88 'title' => ts('Case Subject'),
89 'default' => TRUE,
6a488035
TO
90 ),
91 'status_id' => array(
9d72cede
EM
92 'title' => ts('Status'),
93 'default' => TRUE,
6a488035
TO
94 ),
95 'case_type_id' => array(
9d72cede
EM
96 'title' => ts('Case Type'),
97 'default' => TRUE,
6a488035
TO
98 ),
99 'start_date' => array(
9d72cede
EM
100 'title' => ts('Start Date'),
101 'default' => TRUE,
0fa0ffd2 102 'type' => CRM_Utils_Type::T_DATE,
6a488035
TO
103 ),
104 'end_date' => array(
9d72cede
EM
105 'title' => ts('End Date'),
106 'default' => TRUE,
0fa0ffd2 107 'type' => CRM_Utils_Type::T_DATE,
6a488035
TO
108 ),
109 'duration' => array(
9d72cede
EM
110 'title' => ts('Duration (Days)'),
111 'default' => FALSE,
6a488035
TO
112 ),
113 'is_deleted' => array(
9d72cede
EM
114 'title' => ts('Deleted?'),
115 'default' => FALSE,
116 'type' => CRM_Utils_Type::T_INT,
6a488035
TO
117 ),
118 ),
9d72cede
EM
119 'filters' => array(
120 'start_date' => array(
121 'title' => ts('Start Date'),
6a488035
TO
122 'operatorType' => CRM_Report_Form::OP_DATE,
123 'type' => CRM_Utils_Type::T_DATE,
124 ),
9d72cede
EM
125 'end_date' => array(
126 'title' => ts('End Date'),
6a488035
TO
127 'operatorType' => CRM_Report_Form::OP_DATE,
128 'type' => CRM_Utils_Type::T_DATE,
129 ),
9d72cede
EM
130 'case_type_id' => array(
131 'title' => ts('Case Type'),
6a488035 132 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
7a5c0c6c 133 'options' => CRM_Case_BAO_Case::buildOptions('case_type_id', 'search'),
6a488035 134 ),
9d72cede
EM
135 'status_id' => array(
136 'title' => ts('Status'),
09b5cdcf 137 'type' => CRM_Utils_Type::T_INT,
6a488035 138 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
7a5c0c6c 139 'options' => CRM_Case_BAO_Case::buildOptions('status_id', 'search'),
6a488035 140 ),
9d72cede
EM
141 'is_deleted' => array(
142 'title' => ts('Deleted?'),
6a488035
TO
143 'type' => CRM_Report_Form::OP_INT,
144 'operatorType' => CRM_Report_Form::OP_SELECT,
145 'options' => $this->deleted_labels,
146 'default' => 0,
147 ),
148 ),
60c41e01
BS
149 'order_bys' => array(
150 'start_date' => array(
151 'title' => ts('Start Date'),
152 ),
153 'end_date' => array(
154 'title' => ts('End Date'),
155 ),
156 'status_id' => array(
157 'title' => ts('Status'),
158 ),
159 ),
160 'grouping' => 'case-fields'
6a488035 161 ),
9d72cede 162 'civicrm_contact' => array(
6a488035 163 'dao' => 'CRM_Contact_DAO_Contact',
9d72cede
EM
164 'fields' => array(
165 'sort_name' => array(
166 'title' => ts('Staff Member'),
6a488035
TO
167 'default' => TRUE,
168 ),
169 ),
9d72cede
EM
170 'filters' => array(
171 'sort_name' => array(
172 'title' => ts('Staff Member'),
6a488035
TO
173 ),
174 ),
175 ),
9d72cede 176 'civicrm_relationship' => array(
6a488035 177 'dao' => 'CRM_Contact_DAO_Relationship',
9d72cede
EM
178 'filters' => array(
179 'relationship_type_id' => array(
180 'title' => ts('Staff Relationship'),
09b5cdcf 181 'type' => CRM_Utils_Type::T_INT,
6a488035
TO
182 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
183 'options' => $this->rel_types,
184 ),
185 ),
186 ),
9d72cede 187 'civicrm_relationship_type' => array(
6a488035 188 'dao' => 'CRM_Contact_DAO_RelationshipType',
9d72cede
EM
189 'fields' => array(
190 'label_b_a' => array(
191 'title' => ts('Relationship'),
192 'default' => TRUE,
6a488035
TO
193 ),
194 ),
195 ),
9d72cede 196 'civicrm_case_contact' => array(
6a488035
TO
197 'dao' => 'CRM_Case_DAO_CaseContact',
198 ),
199 );
200
201 parent::__construct();
202 }
203
00be9182 204 public function preProcess() {
6a488035
TO
205 parent::preProcess();
206 }
207
00be9182 208 public function select() {
6a488035
TO
209 $select = array();
210 $this->_columnHeaders = array();
211 foreach ($this->_columns as $tableName => $table) {
212 if (array_key_exists('fields', $table)) {
213 foreach ($table['fields'] as $fieldName => $field) {
9d72cede
EM
214 if (!empty($field['required']) ||
215 !empty($this->_params['fields'][$fieldName])
216 ) {
6a488035
TO
217
218 if ($tableName == 'civicrm_relationship_type') {
219 $this->_relField = TRUE;
220 }
221
222 if ($fieldName == 'duration') {
223 $select[] = "IF({$table['fields']['end_date']['dbAlias']} Is Null, '', DATEDIFF({$table['fields']['end_date']['dbAlias']}, {$table['fields']['start_date']['dbAlias']})) as {$tableName}_{$fieldName}";
224 }
225 else {
226 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
227 }
228 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
229 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title'];
230 }
231 }
232 }
233 }
d1641c51 234 $this->_selectClauses = $select;
6a488035
TO
235
236 $this->_select = "SELECT " . implode(', ', $select) . " ";
237 }
238
74cf4551
EM
239 /**
240 * @param $fields
241 * @param $files
242 * @param $self
243 *
244 * @return array
245 */
00be9182 246 public static function formRule($fields, $files, $self) {
6a488035 247 $errors = $grouping = array();
9d72cede
EM
248 if (empty($fields['relationship_type_id_value']) &&
249 (array_key_exists('sort_name', $fields['fields']) ||
250 array_key_exists('label_b_a', $fields['fields']))
251 ) {
6a488035
TO
252 $errors['fields'] = ts('Either filter on at least one relationship type, or de-select Staff Member and Relationship from the list of fields.');
253 }
9d72cede
EM
254 if ((!empty($fields['relationship_type_id_value']) ||
255 !empty($fields['sort_name_value'])) &&
256 (!array_key_exists('sort_name', $fields['fields']) ||
257 !array_key_exists('label_b_a', $fields['fields']))
258 ) {
6a488035
TO
259 $errors['fields'] = ts('To filter on Staff Member or Relationship, please also select Staff Member and Relationship from the list of fields.');
260 }
261 return $errors;
262 }
263
00be9182 264 public function from() {
6a488035 265
9d72cede
EM
266 $cc = $this->_aliases['civicrm_case'];
267 $c = $this->_aliases['civicrm_contact'];
268 $c2 = $this->_aliases['civicrm_c2'];
269 $cr = $this->_aliases['civicrm_relationship'];
6a488035
TO
270 $crt = $this->_aliases['civicrm_relationship_type'];
271 $ccc = $this->_aliases['civicrm_case_contact'];
272
273 if ($this->_relField) {
274 $this->_from = "
2f4c2f5d 275 FROM civicrm_contact $c
6a488035
TO
276inner join civicrm_relationship $cr on {$c}.id = ${cr}.contact_id_b
277inner join civicrm_case $cc on ${cc}.id = ${cr}.case_id
278inner join civicrm_relationship_type $crt on ${crt}.id=${cr}.relationship_type_id
279inner join civicrm_case_contact $ccc on ${ccc}.case_id = ${cc}.id
280inner join civicrm_contact $c2 on ${c2}.id=${ccc}.contact_id
281";
282 }
283 else {
284 $this->_from = "
e5dab8dd 285 FROM civicrm_case $cc
6a488035
TO
286inner join civicrm_case_contact $ccc on ${ccc}.case_id = ${cc}.id
287inner join civicrm_contact $c2 on ${c2}.id=${ccc}.contact_id
288";
289 }
290 }
291
00be9182 292 public function where() {
6a488035
TO
293 $clauses = array();
294 $this->_having = '';
295 foreach ($this->_columns as $tableName => $table) {
296 if (array_key_exists('filters', $table)) {
297 foreach ($table['filters'] as $fieldName => $field) {
298 $clause = NULL;
84178120 299 if (CRM_Utils_Array::value("operatorType", $field) & CRM_Report_Form::OP_DATE
9d72cede 300 ) {
6a488035 301 $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params);
9d72cede
EM
302 $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params);
303 $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params);
6a488035
TO
304
305 $clause = $this->dateClause($field['dbAlias'], $relative, $from, $to,
306 CRM_Utils_Array::value('type', $field)
307 );
308 }
309 else {
310
311 $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params);
312 if ($fieldName == 'case_type_id') {
313 $value = CRM_Utils_Array::value("{$fieldName}_value", $this->_params);
314 if (!empty($value)) {
75314e67 315 $operator = '';
316 if ($op == 'notin') {
317 $operator = 'NOT';
318 }
6e987e3a 319
320 $regexp = "[[:cntrl:]]*" . implode('[[:>:]]*|[[:<:]]*', $value) . "[[:cntrl:]]*";
321 $clause = "{$field['dbAlias']} {$operator} REGEXP '{$regexp}'";
6a488035
TO
322 }
323 $op = NULL;
324 }
325
326 if ($op) {
327 $clause = $this->whereClause($field,
328 $op,
329 CRM_Utils_Array::value("{$fieldName}_value", $this->_params),
330 CRM_Utils_Array::value("{$fieldName}_min", $this->_params),
331 CRM_Utils_Array::value("{$fieldName}_max", $this->_params)
332 );
333 }
334 }
335
336 if (!empty($clause)) {
337 $clauses[] = $clause;
338 }
339 }
340 }
341 }
342
343 if (empty($clauses)) {
344 $this->_where = "WHERE ( 1 ) ";
345 }
346 else {
347 $this->_where = "WHERE " . implode(' AND ', $clauses);
348 }
349 }
350
00be9182 351 public function groupBy() {
e5dab8dd 352 $this->_groupBy = "";
6a488035
TO
353 }
354
00be9182 355 public function postProcess() {
6a488035
TO
356
357 $this->beginPostProcess();
358
359 $sql = $this->buildQuery(TRUE);
360
361 $rows = $graphRows = array();
362 $this->buildRows($sql, $rows);
363
364 $this->formatDisplay($rows);
365 $this->doTemplateAssignment($rows);
366 $this->endPostProcess($rows);
367 }
368
74cf4551 369 /**
4b62bc4f
EM
370 * Alter display of rows.
371 *
372 * Iterate through the rows retrieved via SQL and make changes for display purposes,
373 * such as rendering contacts as links.
374 *
375 * @param array $rows
376 * Rows generated by SQL, with an array for each row.
74cf4551 377 */
00be9182 378 public function alterDisplay(&$rows) {
6a488035
TO
379 $entryFound = FALSE;
380 foreach ($rows as $rowNum => $row) {
381 if (array_key_exists('civicrm_case_status_id', $row)) {
382 if ($value = $row['civicrm_case_status_id']) {
383 $rows[$rowNum]['civicrm_case_status_id'] = $this->case_statuses[$value];
384 $entryFound = TRUE;
385 }
386 }
387
9d72cede
EM
388 if (array_key_exists('civicrm_case_case_type_id', $row) &&
389 !empty($rows[$rowNum]['civicrm_case_case_type_id'])
390 ) {
391 $value = $row['civicrm_case_case_type_id'];
6a488035 392 $typeIds = explode(CRM_Core_DAO::VALUE_SEPARATOR, $value);
9d72cede 393 $value = array();
6a488035
TO
394 foreach ($typeIds as $typeId) {
395 if ($typeId) {
396 $value[$typeId] = $this->case_types[$typeId];
397 }
398 }
399 $rows[$rowNum]['civicrm_case_case_type_id'] = implode(', ', $value);
400 $entryFound = TRUE;
401 }
402
403 // convert Case ID and Subject to links to Manage Case
9d72cede
EM
404 if (array_key_exists('civicrm_case_id', $row) &&
405 !empty($rows[$rowNum]['civicrm_c2_id'])
406 ) {
6a488035 407 $url = CRM_Utils_System::url("civicrm/contact/view/case",
9d72cede
EM
408 'reset=1&action=view&cid=' . $row['civicrm_c2_id'] . '&id=' .
409 $row['civicrm_case_id'],
6a488035
TO
410 $this->_absoluteUrl
411 );
412 $rows[$rowNum]['civicrm_case_id_link'] = $url;
413 $rows[$rowNum]['civicrm_case_id_hover'] = ts("Manage Case");
414 $entryFound = TRUE;
415 }
9d72cede
EM
416 if (array_key_exists('civicrm_case_subject', $row) &&
417 !empty($rows[$rowNum]['civicrm_c2_id'])
418 ) {
6a488035 419 $url = CRM_Utils_System::url("civicrm/contact/view/case",
9d72cede
EM
420 'reset=1&action=view&cid=' . $row['civicrm_c2_id'] . '&id=' .
421 $row['civicrm_case_id'],
6a488035
TO
422 $this->_absoluteUrl
423 );
424 $rows[$rowNum]['civicrm_case_subject_link'] = $url;
425 $rows[$rowNum]['civicrm_case_subject_hover'] = ts("Manage Case");
426 $entryFound = TRUE;
427 }
428
429 if (array_key_exists('civicrm_case_is_deleted', $row)) {
430 $value = $row['civicrm_case_is_deleted'];
431 $rows[$rowNum]['civicrm_case_is_deleted'] = $this->deleted_labels[$value];
432 $entryFound = TRUE;
433 }
434
435 if (!$entryFound) {
436 break;
437 }
438 }
439 }
96025800 440
6a488035 441}