Merge in 5.11
[civicrm-core.git] / CRM / Report / Form / Case / Detail.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
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
6b83d5bd 31 * @copyright CiviCRM LLC (c) 2004-2019
6a488035
TO
32 * $Id$
33 *
34 */
35class CRM_Report_Form_Case_Detail extends CRM_Report_Form {
36
37 protected $_relField = FALSE;
38
6d81d64a 39 protected $_addressField = TRUE;
6a488035
TO
40
41 protected $_activityLast = FALSE;
42
43 protected $_activityLastCompleted = FALSE;
44
45 protected $_includeCaseDetailExtra = FALSE;
46
f519cbaa 47 protected $_caseDetailExtra = [];
430ae6dd 48
f519cbaa 49 protected $_customGroupExtends = ['Case'];
b326c682 50
5e851416
BS
51 protected $_caseTypeNameOrderBy = FALSE;
52
74cf4551 53 /**
74cf4551 54 */
00be9182 55 public function __construct() {
01bf13dc 56 $this->case_statuses = CRM_Core_OptionGroup::values('case_status');
9d72cede
EM
57 $this->case_types = CRM_Case_PseudoConstant::caseType();
58 $rels = CRM_Core_PseudoConstant::relationshipType();
6a488035
TO
59 foreach ($rels as $relid => $v) {
60 $this->rel_types[$relid] = $v['label_b_a'];
61 }
62
f519cbaa 63 $this->deleted_labels = [
9d72cede
EM
64 '' => ts('- select -'),
65 0 => ts('No'),
21dfd5f5 66 1 => ts('Yes'),
f519cbaa 67 ];
a31b650c 68
f519cbaa 69 $this->caseActivityTypes = [];
6a488035
TO
70 foreach (CRM_Case_PseudoConstant::caseActivityType() as $typeDetail) {
71 $this->caseActivityTypes[$typeDetail['id']] = $typeDetail['label'];
72 }
73
f519cbaa 74 $this->_columns = [
75 'civicrm_case' => [
6a488035 76 'dao' => 'CRM_Case_DAO_Case',
f519cbaa 77 'fields' => [
78 'id' => [
9d72cede 79 'title' => ts('Case ID'),
6a488035
TO
80 'no_display' => TRUE,
81 'required' => TRUE,
f519cbaa 82 ],
83 'subject' => [
9d72cede 84 'title' => ts('Subject'),
6a488035 85 'required' => TRUE,
f519cbaa 86 ],
87 'start_date' => [
9d72cede 88 'title' => ts('Start Date'),
6a488035 89 'type' => CRM_Utils_Type::T_DATE,
f519cbaa 90 ],
91 'end_date' => [
9d72cede 92 'title' => ts('End Date'),
6a488035 93 'type' => CRM_Utils_Type::T_DATE,
f519cbaa 94 ],
95 'status_id' => ['title' => ts('Case Status')],
96 'case_type_id' => ['title' => ts('Case Type')],
97 'is_deleted' => [
9d72cede 98 'title' => ts('Deleted?'),
a31b650c
RN
99 'default' => FALSE,
100 'type' => CRM_Utils_Type::T_INT,
f519cbaa 101 ],
102 ],
103 'filters' => [
104 'start_date' => [
9d72cede 105 'title' => ts('Start Date'),
6a488035
TO
106 'operatorType' => CRM_Report_Form::OP_DATE,
107 'type' => CRM_Utils_Type::T_DATE,
f519cbaa 108 ],
109 'end_date' => [
9d72cede 110 'title' => ts('End Date'),
6a488035
TO
111 'operatorType' => CRM_Report_Form::OP_DATE,
112 'type' => CRM_Utils_Type::T_DATE,
f519cbaa 113 ],
114 'status_id' => [
9d72cede 115 'title' => ts('Case Status'),
09b5cdcf 116 'type' => CRM_Utils_Type::T_INT,
6a488035 117 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
7a5c0c6c 118 'options' => CRM_Case_BAO_Case::buildOptions('status_id', 'search'),
f519cbaa 119 ],
120 'case_type_id' => [
9d72cede 121 'title' => ts('Case Type'),
09b5cdcf 122 'type' => CRM_Utils_Type::T_INT,
6a488035 123 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
7a5c0c6c 124 'options' => CRM_Case_BAO_Case::buildOptions('case_type_id', 'search'),
f519cbaa 125 ],
126 'is_deleted' => [
9d72cede 127 'title' => ts('Deleted?'),
a31b650c
RN
128 'type' => CRM_Utils_Type::T_INT,
129 'operatorType' => CRM_Report_Form::OP_SELECT,
130 'options' => $this->deleted_labels,
131 'default' => 0,
f519cbaa 132 ],
133 ],
134 'order_bys' => [
135 'start_date' => [
5e851416
BS
136 'title' => ts('Start Date'),
137 'default_weight' => 1,
f519cbaa 138 ],
139 'end_date' => [
5e851416 140 'title' => ts('End Date'),
f519cbaa 141 ],
142 'status_id' => [
5e851416 143 'title' => ts('Status'),
f519cbaa 144 ],
f519cbaa 145 ],
146 ],
0f880d50 147 'civicrm_case_type' => [
148 'dao' => 'CRM_Case_DAO_Case',
149 'order_bys' => [
150 'case_type_title' => [
151 'title' => 'Case Type',
152 'name' => 'title',
153 ]]
154 ],
f519cbaa 155 'civicrm_contact' => [
6a488035 156 'dao' => 'CRM_Contact_DAO_Contact',
f519cbaa 157 'fields' => [
158 'client_sort_name' => [
6a488035
TO
159 'name' => 'sort_name',
160 'title' => ts('Client Name'),
161 'required' => TRUE,
f519cbaa 162 ],
163 'id' => [
6a488035
TO
164 'no_display' => TRUE,
165 'required' => TRUE,
f519cbaa 166 ],
167 ],
168 'filters' => [
169 'sort_name' => ['title' => ts('Client Name')],
170 ],
171 ],
172 'civicrm_relationship' => [
6a488035 173 'dao' => 'CRM_Contact_DAO_Relationship',
f519cbaa 174 'fields' => [
175 'case_role' => [
9d72cede 176 'name' => 'relationship_type_id',
6a488035 177 'title' => ts('Case Role(s)'),
f519cbaa 178 ],
179 ],
180 'filters' => [
181 'case_role' => [
6a488035
TO
182 'name' => 'relationship_type_id',
183 'title' => ts('Case Role(s)'),
09b5cdcf 184 'type' => CRM_Utils_Type::T_INT,
6a488035
TO
185 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
186 'options' => $this->rel_types,
f519cbaa 187 ],
188 'is_active' => [
742749c5
AP
189 'title' => ts('Active Role?'),
190 'type' => CRM_Utils_Type::T_BOOLEAN,
191 'default' => TRUE,
192 'options' => CRM_Core_SelectValues::boolean(),
f519cbaa 193 ],
194 ],
195 ],
196 'civicrm_email' => [
6a488035 197 'dao' => 'CRM_Core_DAO_Email',
f519cbaa 198 'fields' => [
199 'email' => [
9d72cede 200 'title' => ts('Email'),
6a488035 201 'no_repeat' => TRUE,
f519cbaa 202 ],
203 ],
6a488035 204 'grouping' => 'contact-fields',
f519cbaa 205 ],
206 'civicrm_phone' => [
6a488035 207 'dao' => 'CRM_Core_DAO_Phone',
f519cbaa 208 'fields' => [
209 'phone' => [
9d72cede 210 'title' => ts('Phone'),
6a488035 211 'no_repeat' => TRUE,
f519cbaa 212 ],
213 ],
6a488035 214 'grouping' => 'contact-fields',
f519cbaa 215 ],
216 'civicrm_address' => [
6a488035 217 'dao' => 'CRM_Core_DAO_Address',
f519cbaa 218 'fields' => [
6a488035 219 'street_address' => NULL,
f519cbaa 220 'state_province_id' => [
9d72cede 221 'title' => ts('State/Province'),
f519cbaa 222 ],
223 'country_id' => ['title' => ts('Country')],
224 ],
6a488035 225 'grouping' => 'contact-fields',
f519cbaa 226 'filters' => [
227 'country_id' => [
9d72cede 228 'title' => ts('Country'),
6a488035
TO
229 'type' => CRM_Utils_Type::T_INT,
230 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
231 'options' => CRM_Core_PseudoConstant::country(),
f519cbaa 232 ],
233 'state_province_id' => [
9d72cede 234 'title' => ts('State/Province'),
6a488035
TO
235 'type' => CRM_Utils_Type::T_INT,
236 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
237 'options' => CRM_Core_PseudoConstant::stateProvince(),
f519cbaa 238 ],
239 ],
240 ],
241 'civicrm_worldregion' => [
6a488035 242 'dao' => 'CRM_Core_DAO_Worldregion',
f519cbaa 243 'filters' => [
244 'worldregion_id' => [
6a488035 245 'name' => 'id',
f418f381 246 'title' => ts('World Region'),
6a488035
TO
247 'type' => CRM_Utils_Type::T_INT,
248 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
249 'options' => CRM_Core_PseudoConstant::worldRegion(),
f519cbaa 250 ],
251 ],
252 ],
253 'civicrm_country' => [
6a488035 254 'dao' => 'CRM_Core_DAO_Country',
f519cbaa 255 ],
256 'civicrm_activity_last' => [
6a488035 257 'dao' => 'CRM_Activity_DAO_Activity',
f519cbaa 258 'fields' => [
259 'last_activity_activity_subject' => [
34463fa9
DH
260 'name' => 'subject',
261 'title' => ts('Subject of the last activity in the case'),
f519cbaa 262 ],
263 'last_activity_activity_type' => [
34463fa9
DH
264 'name' => 'activity_type_id',
265 'title' => ts('Activity type of the last activity'),
f519cbaa 266 ],
267 'last_activity_date_time' => [
34463fa9
DH
268 'name' => 'activity_date_time',
269 'title' => ts('Last Action Date'),
270 'operatorType' => CRM_Report_Form::OP_DATE,
f519cbaa 271 ],
272 ],
273 'filters' => [
274 'last_activity_date_time' => [
6a488035
TO
275 'name' => 'activity_date_time',
276 'title' => ts('Last Action Date'),
277 'operatorType' => CRM_Report_Form::OP_DATE,
f519cbaa 278 ],
279 ],
6a488035 280 'alias' => 'civireport_activity_last',
f519cbaa 281 ],
282 'civicrm_activity_last_completed' => [
6a488035 283 'dao' => 'CRM_Activity_DAO_Activity',
f519cbaa 284 'fields' => [
285 'last_completed_activity_subject' => [
6a488035
TO
286 'name' => 'subject',
287 'title' => ts('Subject of the last completed activity in the case'),
f519cbaa 288 ],
289 'last_completed_activity_type' => [
6a488035
TO
290 'name' => 'activity_type_id',
291 'title' => ts('Activity type of the last completed activity'),
f519cbaa 292 ],
293 'last_completed_date_time' => [
34463fa9
DH
294 'name' => 'activity_date_time',
295 'title' => ts('Last Completed Action Date'),
296 'operatorType' => CRM_Report_Form::OP_DATE,
f519cbaa 297 ],
298 ],
299 'filters' => [
300 'last_completed_date_time' => [
34463fa9
DH
301 'name' => 'activity_date_time',
302 'title' => ts('Last Completed Action Date'),
303 'operatorType' => CRM_Report_Form::OP_DATE,
f519cbaa 304 ],
305 ],
306 ],
307 ];
6a488035 308
f519cbaa 309 $this->_options = [
310 'my_cases' => [
9d72cede 311 'title' => ts('My Cases'),
6a488035 312 'type' => 'checkbox',
f519cbaa 313 ],
314 ];
6a488035
TO
315 parent::__construct();
316 }
317
00be9182 318 public function preProcess() {
6a488035
TO
319 parent::preProcess();
320 }
321
00be9182 322 public function buildQuickForm() {
6a488035
TO
323 parent::buildQuickForm();
324 $this->caseDetailSpecialColumnsAdd();
325 }
326
00be9182 327 public function caseDetailSpecialColumnsAdd() {
f519cbaa 328 $elements = [];
6a488035 329 $elements[] = &$this->createElement('select', 'case_activity_all_dates', NULL,
f519cbaa 330 [
21dfd5f5 331 '' => ts('- select -'),
f519cbaa 332 ] + $this->caseActivityTypes
6a488035
TO
333 );
334 $this->addGroup($elements, 'case_detail_extra');
335
f519cbaa 336 $this->_caseDetailExtra = [
337 'case_activity_all_dates' => [
9d72cede 338 'title' => ts('List of all dates of activities of Type'),
6a488035 339 'name' => 'activity_date_time',
f519cbaa 340 ],
341 ];
6a488035
TO
342
343 $this->assign('caseDetailExtra', $this->_caseDetailExtra);
344 }
345
00be9182 346 public function select() {
0f880d50 347 // @todo - get rid of this function & use parent. Use selectWhere to setthe clause for the
348 // few fields that need custom handling.
f519cbaa 349 $select = [];
350 $this->_columnHeaders = [];
6a488035
TO
351 foreach ($this->_columns as $tableName => $table) {
352 if (array_key_exists('fields', $table)) {
353 foreach ($table['fields'] as $fieldName => $field) {
9d72cede
EM
354 if (!empty($field['required']) ||
355 !empty($this->_params['fields'][$fieldName])
356 ) {
6d81d64a 357 if ($tableName == 'civicrm_relationship') {
6a488035
TO
358 $this->_relField = TRUE;
359 }
360 if ($fieldName == 'sort_name') {
2f4c2f5d 361 $select[] = "GROUP_CONCAT({$field['dbAlias']} ORDER BY {$field['dbAlias']} )
6a488035
TO
362 as {$tableName}_{$fieldName}";
363 }
34463fa9
DH
364 if ($tableName == 'civicrm_activity_last') {
365 $this->_activityLast = TRUE;
366 }
6a488035
TO
367 if ($tableName == 'civicrm_activity_last_completed') {
368 $this->_activityLastCompleted = TRUE;
369 }
370
371 if ($fieldName == 'case_role') {
372 $select[] = "GROUP_CONCAT(DISTINCT({$field['dbAlias']}) ORDER BY {$field['dbAlias']}) as {$tableName}_{$fieldName}";
373 }
374 else {
375 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
376 }
377
378 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
379 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title'];
380 }
381 }
382 }
383 }
5e851416 384
d1641c51 385 $this->_selectClauses = $select;
6a488035
TO
386
387 $this->_select = 'SELECT ' . implode(', ', $select) . ' ';
388 }
389
00be9182 390 public function from() {
6a488035
TO
391
392 $case = $this->_aliases['civicrm_case'];
393 $conact = $this->_aliases['civicrm_contact'];
394
395 $this->_from = "
396 FROM civicrm_case $case
397 LEFT JOIN civicrm_case_contact civireport_case_contact on civireport_case_contact.case_id = {$case}.id
398 LEFT JOIN civicrm_contact $conact ON {$conact}.id = civireport_case_contact.contact_id
399 ";
400 if ($this->_relField) {
401 $this->_from .= "
402 LEFT JOIN civicrm_relationship {$this->_aliases['civicrm_relationship']} ON {$this->_aliases['civicrm_relationship']}.case_id = {$case}.id
403";
404 }
405
6d81d64a 406 $this->joinAddressFromContact();
407 $this->joinPhoneFromContact();
408 $this->joinEmailFromContact();
409
410 if ($this->isTableSelected('civicrm_worldregion')) {
6a488035
TO
411 $this->_from .= "
412 LEFT JOIN civicrm_country {$this->_aliases['civicrm_country']}
413 ON {$this->_aliases['civicrm_country']}.id ={$this->_aliases['civicrm_address']}.country_id
414 LEFT JOIN civicrm_worldregion {$this->_aliases['civicrm_worldregion']}
415 ON {$this->_aliases['civicrm_country']}.region_id = {$this->_aliases['civicrm_worldregion']}.id ";
416 }
417
418 // Include clause for last activity of the case
419 if ($this->_activityLast) {
420 $this->_from .= " LEFT JOIN civicrm_activity {$this->_aliases['civicrm_activity_last']} ON ( {$this->_aliases['civicrm_activity_last']}.id = ( SELECT max(activity_id) FROM civicrm_case_activity WHERE case_id = {$case}.id) )";
421 }
422
423 // Include clause for last completed activity of the case
424 if ($this->_activityLastCompleted) {
425 $this->_from .= " LEFT JOIN civicrm_activity {$this->_aliases['civicrm_activity_last_completed']} ON ( {$this->_aliases['civicrm_activity_last_completed']}.id = ( SELECT max(activity_id) FROM civicrm_case_activity cca, civicrm_activity ca WHERE ca.id = cca.activity_id AND cca.case_id = {$case}.id AND ca.status_id = 2 ) )";
426 }
5e851416 427
0f880d50 428 if ($this->isTableSelected('civicrm_case_type')) {
5e851416 429 $this->_from .= "
0f880d50 430 LEFT JOIN civicrm_case_type {$this->_aliases['civicrm_case_type']}
431 ON {$this->_aliases['civicrm_case']}.case_type_id = {$this->_aliases['civicrm_case_type']}.id
5e851416
BS
432 ";
433 }
6a488035
TO
434 }
435
00be9182 436 public function where() {
f519cbaa 437 $clauses = [];
6a488035
TO
438 $this->_having = '';
439 foreach ($this->_columns as $tableName => $table) {
440 if (array_key_exists('filters', $table)) {
441 foreach ($table['filters'] as $fieldName => $field) {
442 $clause = NULL;
443
444 if (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE) {
445 $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params);
9d72cede
EM
446 $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params);
447 $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params);
6a488035
TO
448
449 $clause = $this->dateClause($field['dbAlias'], $relative, $from, $to, $field['type']);
450 }
451 else {
452
453 $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params);
9d72cede
EM
454 if ($fieldName == 'case_type_id' &&
455 !empty($this->_params['case_type_id_value'])
456 ) {
6a488035 457 foreach ($this->_params['case_type_id_value'] as $key => $value) {
6d491668 458 $this->_params['case_type_id_value'][$key] = $value;
6a488035
TO
459 }
460 }
461
462 if ($op) {
463 $clause = $this->whereClause($field,
464 $op,
465 CRM_Utils_Array::value("{$fieldName}_value", $this->_params),
466 CRM_Utils_Array::value("{$fieldName}_min", $this->_params),
467 CRM_Utils_Array::value("{$fieldName}_max", $this->_params)
468 );
469 }
470 }
471
472 if (!empty($clause)) {
473 $clauses[] = $clause;
474 }
475 }
476 }
477 }
478
479 if (isset($this->_params['options']['my_cases'])) {
9d72cede
EM
480 $session = CRM_Core_Session::singleton();
481 $userID = $session->get('userID');
6a488035
TO
482 $clauses[] = "{$this->_aliases['civicrm_contact']}.id = {$userID}";
483 }
484
485 if (empty($clauses)) {
486 $this->_where = 'WHERE ( 1 ) ';
487 }
488 else {
489 $this->_where = 'WHERE ' . implode(' AND ', $clauses);
490 }
491 }
492
00be9182 493 public function groupBy() {
b708c08d 494 $this->_groupBy = CRM_Contact_BAO_Query::getGroupByFromSelectColumns($this->_selectClauses, "{$this->_aliases['civicrm_case']}.id");
6a488035
TO
495 }
496
74cf4551
EM
497 /**
498 * @param $rows
499 *
500 * @return array
501 */
00be9182 502 public function statistics(&$rows) {
6a488035
TO
503 $statistics = parent::statistics($rows);
504
9d72cede
EM
505 $select = "select COUNT( DISTINCT( {$this->_aliases['civicrm_address']}.country_id))";
506 $sql = "{$select} {$this->_from} {$this->_where}";
6a488035
TO
507 $countryCount = CRM_Core_DAO::singleValueQuery($sql);
508
f519cbaa 509 $statistics['counts']['case'] = [
6a488035
TO
510 'title' => ts('Total Number of Cases '),
511 'value' => isset($statistics['counts']['rowsFound']) ? $statistics['counts']['rowsFound']['value'] : count($rows),
f519cbaa 512 ];
513 $statistics['counts']['country'] = [
6a488035
TO
514 'title' => ts('Total Number of Countries '),
515 'value' => $countryCount,
f519cbaa 516 ];
6a488035
TO
517
518 return $statistics;
519 }
520
00be9182 521 public function caseDetailSpecialColumnProcess() {
6a488035
TO
522 if (!$this->_includeCaseDetailExtra) {
523 return;
524 }
525
f519cbaa 526 $from = $select = [];
6a488035
TO
527 $case = $this->_aliases['civicrm_case'];
528
529 if ($activityType = CRM_Utils_Array::value('case_activity_all_dates', $this->_params['case_detail_extra'])) {
530 $select[] = "GROUP_CONCAT(DISTINCT(civireport_activity_all_{$activityType}.{$this->_caseDetailExtra['case_activity_all_dates']['name']}) ORDER BY civireport_activity_all_{$activityType}.{$this->_caseDetailExtra['case_activity_all_dates']['name']}) as case_activity_all_dates";
531
2f4c2f5d 532 $from[] = " LEFT JOIN civicrm_case_activity civireport_case_activity_all_{$activityType} ON ( civireport_case_activity_all_{$activityType}.case_id = {$case}.id)
6a488035
TO
533 LEFT JOIN civicrm_activity civireport_activity_all_{$activityType} ON ( civireport_activity_all_{$activityType}.id = civireport_case_activity_all_{$activityType}.activity_id AND civireport_activity_all_{$activityType}.activity_type_id = {$activityType})";
534
f519cbaa 535 $this->_columnHeaders['case_activity_all_dates'] = [
795492f3 536 'title' => $this->_caseDetailExtra['case_activity_all_dates']['title'] . ": {$this->caseActivityTypes[$activityType]}",
6a488035 537 'type' => CRM_Utils_Array::value('type', $this->_caseDetailExtra['case_activity_all_dates']),
f519cbaa 538 ];
6a488035
TO
539 }
540
541 $this->_select .= ', ' . implode(', ', $select) . ' ';
542 $this->_from .= ' ' . implode(' ', $from) . ' ';
543 }
544
00be9182 545 public function postProcess() {
6a488035
TO
546
547 $this->beginPostProcess();
548
549 $this->checkEnabledFields();
550
551 $this->buildQuery(TRUE);
552
553 $this->caseDetailSpecialColumnProcess();
554
555 $sql = "{$this->_select} {$this->_from} {$this->_where} {$this->_groupBy} {$this->_having} {$this->_orderBy} {$this->_limit}";
556
f519cbaa 557 $rows = $graphRows = [];
6a488035
TO
558 $this->buildRows($sql, $rows);
559
560 $this->formatDisplay($rows);
561
562 $this->doTemplateAssignment($rows);
563 $this->endPostProcess($rows);
564 }
565
00be9182 566 public function checkEnabledFields() {
742749c5
AP
567 if ((isset($this->_params['case_role_value'])
568 && !empty($this->_params['case_role_value'])) ||
f519cbaa 569 (isset($this->_params['is_active_value']))
6a488035
TO
570 ) {
571 $this->_relField = TRUE;
572 }
573
34463fa9
DH
574 if (!empty($this->_params['last_completed_date_time_relative']) ||
575 !empty($this->_params['last_completed_date_time_from']) ||
576 CRM_Utils_Array::value('last_completed_date_time_to', $this->_params)
577 ) {
578 $this->_activityLastCompleted = TRUE;
579 }
580
d578c527
DH
581 if (!empty($this->_params['last_activity_date_time_relative']) ||
582 !empty($this->_params['last_activity_date_time_from']) ||
583 CRM_Utils_Array::value('last_activity_date_time_to', $this->_params)
6a488035
TO
584 ) {
585 $this->_activityLast = TRUE;
586 }
587
588 foreach (array_keys($this->_caseDetailExtra) as $field) {
a7488080 589 if (!empty($this->_params['case_detail_extra'][$field])) {
6a488035
TO
590 $this->_includeCaseDetailExtra = TRUE;
591 break;
592 }
593 }
594 }
595
74cf4551 596 /**
4b62bc4f
EM
597 * Alter display of rows.
598 *
599 * Iterate through the rows retrieved via SQL and make changes for display purposes,
600 * such as rendering contacts as links.
601 *
602 * @param array $rows
603 * Rows generated by SQL, with an array for each row.
74cf4551 604 */
00be9182 605 public function alterDisplay(&$rows) {
6a488035
TO
606 $entryFound = FALSE;
607 $activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, TRUE);
608
609 foreach ($rows as $rowNum => $row) {
610 if (array_key_exists('civicrm_case_status_id', $row)) {
611 if ($value = $row['civicrm_case_status_id']) {
612 $rows[$rowNum]['civicrm_case_status_id'] = $this->case_statuses[$value];
613
614 $entryFound = TRUE;
615 }
616 }
617 if (array_key_exists('civicrm_case_case_type_id', $row)) {
618 if ($value = str_replace(CRM_Core_DAO::VALUE_SEPARATOR, '', $row['civicrm_case_case_type_id'])) {
619 $rows[$rowNum]['civicrm_case_case_type_id'] = $this->case_types[$value];
620
621 $entryFound = TRUE;
622 }
623 }
624 if (array_key_exists('civicrm_case_subject', $row)) {
625 if ($value = $row['civicrm_case_subject']) {
76bec9f7
CW
626 $url = CRM_Utils_System::url("civicrm/case/ajax/details",
627 "caseId={$row['civicrm_case_id']}&contactId={$row['civicrm_contact_id']}",
628 $this->_absoluteUrl
629 );
630 $rows[$rowNum]['civicrm_case_subject'] = "<a class=\"crm-popup\" href=\"$url\">$value</a>";
6a488035
TO
631 $rows[$rowNum]['civicrm_case_subject_hover'] = ts('View Details of Case.');
632
633 $entryFound = TRUE;
634 }
635 }
636 if (array_key_exists('civicrm_relationship_case_role', $row)) {
637 if ($value = $row['civicrm_relationship_case_role']) {
638 $caseRoles = explode(',', $value);
639 foreach ($caseRoles as $num => $caseRole) {
640 $caseRoles[$num] = $this->rel_types[$caseRole];
641 }
642 $rows[$rowNum]['civicrm_relationship_case_role'] = implode('; ', $caseRoles);
643 }
644 $entryFound = TRUE;
645 }
34463fa9
DH
646 if (array_key_exists('civicrm_activity_last_last_activity_activity_subject', $row) &&
647 empty($row['civicrm_activity_last_last_activity_activity_subject'])
648 ) {
649 $rows[$rowNum]['civicrm_activity_last_last_activity_activity_subject'] = ts('(no subject)');
650 $entryFound = TRUE;
651 }
9d72cede
EM
652 if (array_key_exists('civicrm_activity_last_completed_last_completed_activity_subject', $row) &&
653 empty($row['civicrm_activity_last_completed_last_completed_activity_subject'])
654 ) {
2f186e81 655 $rows[$rowNum]['civicrm_activity_last_completed_last_completed_activity_subject'] = ts('(no subject)');
6a488035
TO
656 $entryFound = TRUE;
657 }
658 if (array_key_exists('civicrm_contact_client_sort_name', $row) &&
659 array_key_exists('civicrm_contact_id', $row)
660 ) {
661 $url = CRM_Utils_System::url("civicrm/contact/view",
662 'reset=1&cid=' . $row['civicrm_contact_id'],
663 $this->_absoluteUrl
664 );
665 $rows[$rowNum]['civicrm_contact_client_sort_name_link'] = $url;
666 $rows[$rowNum]['civicrm_contact_client_sort_name_hover'] = ts("View Contact Summary for this Contact");
667 $entryFound = TRUE;
668 }
34463fa9
DH
669 if (array_key_exists('civicrm_activity_last_last_activity_activity_type', $row)) {
670 if ($value = $row['civicrm_activity_last_last_activity_activity_type']) {
671 $rows[$rowNum]['civicrm_activity_last_last_activity_activity_type'] = $activityTypes[$value];
672 }
673 $entryFound = TRUE;
674 }
6a488035
TO
675 if (array_key_exists('civicrm_activity_last_completed_last_completed_activity_type', $row)) {
676 if ($value = $row['civicrm_activity_last_completed_last_completed_activity_type']) {
677 $rows[$rowNum]['civicrm_activity_last_completed_last_completed_activity_type'] = $activityTypes[$value];
678 }
679 $entryFound = TRUE;
680 }
681
682 if (array_key_exists('case_activity_all_dates', $row)) {
683 if ($value = $row['case_activity_all_dates']) {
684 $activityDates = explode(',', $value);
685 foreach ($activityDates as $num => $activityDate) {
686 $activityDates[$num] = CRM_Utils_Date::customFormat($activityDate);
687 }
688 $rows[$rowNum]['case_activity_all_dates'] = implode('; ', $activityDates);
689 }
690 $entryFound = TRUE;
691 }
692
a31b650c
RN
693 if (array_key_exists('civicrm_case_is_deleted', $row)) {
694 $value = $row['civicrm_case_is_deleted'];
695 $rows[$rowNum]['civicrm_case_is_deleted'] = $this->deleted_labels[$value];
696 $entryFound = TRUE;
697 }
698
e3665bb8 699 $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, NULL, NULL) ? TRUE : $entryFound;
6a488035
TO
700 if (!$entryFound) {
701 break;
702 }
703 }
704 }
96025800 705
6a488035 706}