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