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