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