Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
0f03f337 | 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
0f03f337 | 31 | * @copyright CiviCRM LLC (c) 2004-2017 |
6a488035 TO |
32 | * $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 | ||
5e851416 BS |
57 | protected $_caseTypeNameOrderBy = FALSE; |
58 | ||
74cf4551 | 59 | /** |
74cf4551 | 60 | */ |
00be9182 | 61 | public function __construct() { |
01bf13dc | 62 | $this->case_statuses = CRM_Core_OptionGroup::values('case_status'); |
9d72cede EM |
63 | $this->case_types = CRM_Case_PseudoConstant::caseType(); |
64 | $rels = CRM_Core_PseudoConstant::relationshipType(); | |
6a488035 TO |
65 | foreach ($rels as $relid => $v) { |
66 | $this->rel_types[$relid] = $v['label_b_a']; | |
67 | } | |
68 | ||
9d72cede EM |
69 | $this->deleted_labels = array( |
70 | '' => ts('- select -'), | |
71 | 0 => ts('No'), | |
21dfd5f5 | 72 | 1 => ts('Yes'), |
9d72cede | 73 | ); |
a31b650c | 74 | |
6a488035 TO |
75 | $this->caseActivityTypes = array(); |
76 | foreach (CRM_Case_PseudoConstant::caseActivityType() as $typeDetail) { | |
77 | $this->caseActivityTypes[$typeDetail['id']] = $typeDetail['label']; | |
78 | } | |
79 | ||
80 | $this->_columns = array( | |
9d72cede | 81 | 'civicrm_case' => array( |
6a488035 | 82 | 'dao' => 'CRM_Case_DAO_Case', |
9d72cede EM |
83 | 'fields' => array( |
84 | 'id' => array( | |
85 | 'title' => ts('Case ID'), | |
6a488035 TO |
86 | 'no_display' => TRUE, |
87 | 'required' => TRUE, | |
88 | ), | |
9d72cede EM |
89 | 'subject' => array( |
90 | 'title' => ts('Subject'), | |
6a488035 TO |
91 | 'required' => TRUE, |
92 | ), | |
9d72cede EM |
93 | 'start_date' => array( |
94 | 'title' => ts('Start Date'), | |
6a488035 TO |
95 | 'type' => CRM_Utils_Type::T_DATE, |
96 | ), | |
9d72cede EM |
97 | 'end_date' => array( |
98 | 'title' => ts('End Date'), | |
6a488035 TO |
99 | 'type' => CRM_Utils_Type::T_DATE, |
100 | ), | |
101 | 'status_id' => array('title' => ts('Case Status')), | |
102 | 'case_type_id' => array('title' => ts('Case Type')), | |
9d72cede EM |
103 | 'is_deleted' => array( |
104 | 'title' => ts('Deleted?'), | |
a31b650c RN |
105 | 'default' => FALSE, |
106 | 'type' => CRM_Utils_Type::T_INT, | |
107 | ), | |
6a488035 | 108 | ), |
9d72cede EM |
109 | 'filters' => array( |
110 | 'start_date' => array( | |
111 | 'title' => ts('Start Date'), | |
6a488035 TO |
112 | 'operatorType' => CRM_Report_Form::OP_DATE, |
113 | 'type' => CRM_Utils_Type::T_DATE, | |
114 | ), | |
9d72cede EM |
115 | 'end_date' => array( |
116 | 'title' => ts('End Date'), | |
6a488035 TO |
117 | 'operatorType' => CRM_Report_Form::OP_DATE, |
118 | 'type' => CRM_Utils_Type::T_DATE, | |
119 | ), | |
9d72cede EM |
120 | 'status_id' => array( |
121 | 'title' => ts('Case Status'), | |
09b5cdcf | 122 | 'type' => CRM_Utils_Type::T_INT, |
6a488035 | 123 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
7a5c0c6c | 124 | 'options' => CRM_Case_BAO_Case::buildOptions('status_id', 'search'), |
6a488035 | 125 | ), |
9d72cede EM |
126 | 'case_type_id' => array( |
127 | 'title' => ts('Case Type'), | |
09b5cdcf | 128 | 'type' => CRM_Utils_Type::T_INT, |
6a488035 | 129 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
7a5c0c6c | 130 | 'options' => CRM_Case_BAO_Case::buildOptions('case_type_id', 'search'), |
6a488035 | 131 | ), |
9d72cede EM |
132 | 'is_deleted' => array( |
133 | 'title' => ts('Deleted?'), | |
a31b650c RN |
134 | 'type' => CRM_Utils_Type::T_INT, |
135 | 'operatorType' => CRM_Report_Form::OP_SELECT, | |
136 | 'options' => $this->deleted_labels, | |
137 | 'default' => 0, | |
138 | ), | |
6a488035 | 139 | ), |
5e851416 BS |
140 | 'order_bys' => array( |
141 | 'start_date' => array( | |
142 | 'title' => ts('Start Date'), | |
143 | 'default_weight' => 1, | |
144 | ), | |
145 | 'end_date' => array( | |
146 | 'title' => ts('End Date'), | |
147 | ), | |
148 | 'status_id' => array( | |
149 | 'title' => ts('Status'), | |
150 | ), | |
151 | ), | |
6a488035 | 152 | ), |
9d72cede | 153 | 'civicrm_contact' => array( |
6a488035 | 154 | 'dao' => 'CRM_Contact_DAO_Contact', |
9d72cede | 155 | 'fields' => array( |
6a488035 TO |
156 | 'client_sort_name' => array( |
157 | 'name' => 'sort_name', | |
158 | 'title' => ts('Client Name'), | |
159 | 'required' => TRUE, | |
160 | ), | |
161 | 'id' => array( | |
162 | 'no_display' => TRUE, | |
163 | 'required' => TRUE, | |
164 | ), | |
165 | ), | |
9d72cede | 166 | 'filters' => array( |
6a488035 TO |
167 | 'sort_name' => array('title' => ts('Client Name')), |
168 | ), | |
169 | ), | |
9d72cede | 170 | 'civicrm_relationship' => array( |
6a488035 | 171 | 'dao' => 'CRM_Contact_DAO_Relationship', |
9d72cede EM |
172 | 'fields' => array( |
173 | 'case_role' => array( | |
174 | 'name' => 'relationship_type_id', | |
6a488035 TO |
175 | 'title' => ts('Case Role(s)'), |
176 | ), | |
177 | ), | |
9d72cede EM |
178 | 'filters' => array( |
179 | 'case_role' => array( | |
6a488035 TO |
180 | 'name' => 'relationship_type_id', |
181 | 'title' => ts('Case Role(s)'), | |
09b5cdcf | 182 | 'type' => CRM_Utils_Type::T_INT, |
6a488035 TO |
183 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
184 | 'options' => $this->rel_types, | |
185 | ), | |
186 | ), | |
187 | ), | |
9d72cede | 188 | 'civicrm_email' => array( |
6a488035 | 189 | 'dao' => 'CRM_Core_DAO_Email', |
9d72cede EM |
190 | 'fields' => array( |
191 | 'email' => array( | |
192 | 'title' => ts('Email'), | |
6a488035 TO |
193 | 'no_repeat' => TRUE, |
194 | ), | |
195 | ), | |
196 | 'grouping' => 'contact-fields', | |
197 | ), | |
9d72cede | 198 | 'civicrm_phone' => array( |
6a488035 | 199 | 'dao' => 'CRM_Core_DAO_Phone', |
9d72cede EM |
200 | 'fields' => array( |
201 | 'phone' => array( | |
202 | 'title' => ts('Phone'), | |
6a488035 TO |
203 | 'no_repeat' => TRUE, |
204 | ), | |
205 | ), | |
206 | 'grouping' => 'contact-fields', | |
207 | ), | |
9d72cede | 208 | 'civicrm_address' => array( |
6a488035 | 209 | 'dao' => 'CRM_Core_DAO_Address', |
9d72cede | 210 | 'fields' => array( |
6a488035 | 211 | 'street_address' => NULL, |
9d72cede EM |
212 | 'state_province_id' => array( |
213 | 'title' => ts('State/Province'), | |
6a488035 TO |
214 | ), |
215 | 'country_id' => array('title' => ts('Country')), | |
216 | ), | |
217 | 'grouping' => 'contact-fields', | |
9d72cede EM |
218 | 'filters' => array( |
219 | 'country_id' => array( | |
220 | 'title' => ts('Country'), | |
6a488035 TO |
221 | 'type' => CRM_Utils_Type::T_INT, |
222 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
223 | 'options' => CRM_Core_PseudoConstant::country(), | |
224 | ), | |
9d72cede EM |
225 | 'state_province_id' => array( |
226 | 'title' => ts('State/Province'), | |
6a488035 TO |
227 | 'type' => CRM_Utils_Type::T_INT, |
228 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
229 | 'options' => CRM_Core_PseudoConstant::stateProvince(), | |
230 | ), | |
231 | ), | |
232 | ), | |
9d72cede | 233 | 'civicrm_worldregion' => array( |
6a488035 | 234 | 'dao' => 'CRM_Core_DAO_Worldregion', |
9d72cede | 235 | 'filters' => array( |
6a488035 TO |
236 | 'worldregion_id' => array( |
237 | 'name' => 'id', | |
f418f381 | 238 | 'title' => ts('World Region'), |
6a488035 TO |
239 | 'type' => CRM_Utils_Type::T_INT, |
240 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
241 | 'options' => CRM_Core_PseudoConstant::worldRegion(), | |
242 | ), | |
243 | ), | |
244 | ), | |
9d72cede | 245 | 'civicrm_country' => array( |
6a488035 TO |
246 | 'dao' => 'CRM_Core_DAO_Country', |
247 | ), | |
9d72cede | 248 | 'civicrm_activity_last' => array( |
6a488035 | 249 | 'dao' => 'CRM_Activity_DAO_Activity', |
9d72cede EM |
250 | 'filters' => array( |
251 | 'last_activity_date_time' => array( | |
6a488035 TO |
252 | 'name' => 'activity_date_time', |
253 | 'title' => ts('Last Action Date'), | |
254 | 'operatorType' => CRM_Report_Form::OP_DATE, | |
255 | ), | |
256 | ), | |
257 | 'alias' => 'civireport_activity_last', | |
258 | ), | |
9d72cede | 259 | 'civicrm_activity_last_completed' => array( |
6a488035 | 260 | 'dao' => 'CRM_Activity_DAO_Activity', |
9d72cede EM |
261 | 'fields' => array( |
262 | 'last_completed_activity_subject' => array( | |
6a488035 TO |
263 | 'name' => 'subject', |
264 | 'title' => ts('Subject of the last completed activity in the case'), | |
265 | ), | |
9d72cede | 266 | 'last_completed_activity_type' => array( |
6a488035 TO |
267 | 'name' => 'activity_type_id', |
268 | 'title' => ts('Activity type of the last completed activity'), | |
269 | ), | |
270 | ), | |
271 | ), | |
272 | ); | |
273 | ||
274 | $this->_options = array( | |
9d72cede EM |
275 | 'my_cases' => array( |
276 | 'title' => ts('My Cases'), | |
6a488035 TO |
277 | 'type' => 'checkbox', |
278 | ), | |
279 | ); | |
280 | parent::__construct(); | |
281 | } | |
282 | ||
00be9182 | 283 | public function preProcess() { |
6a488035 TO |
284 | parent::preProcess(); |
285 | } | |
286 | ||
00be9182 | 287 | public function buildQuickForm() { |
6a488035 TO |
288 | parent::buildQuickForm(); |
289 | $this->caseDetailSpecialColumnsAdd(); | |
290 | } | |
291 | ||
00be9182 | 292 | public function caseDetailSpecialColumnsAdd() { |
6a488035 TO |
293 | $elements = array(); |
294 | $elements[] = &$this->createElement('select', 'case_activity_all_dates', NULL, | |
295 | array( | |
21dfd5f5 | 296 | '' => ts('- select -'), |
9d72cede | 297 | ) + $this->caseActivityTypes |
6a488035 TO |
298 | ); |
299 | $this->addGroup($elements, 'case_detail_extra'); | |
300 | ||
9d72cede EM |
301 | $this->_caseDetailExtra = array( |
302 | 'case_activity_all_dates' => array( | |
303 | 'title' => ts('List of all dates of activities of Type'), | |
6a488035 TO |
304 | 'name' => 'activity_date_time', |
305 | ), | |
306 | ); | |
307 | ||
308 | $this->assign('caseDetailExtra', $this->_caseDetailExtra); | |
309 | } | |
310 | ||
00be9182 | 311 | public function select() { |
6a488035 TO |
312 | $select = array(); |
313 | $this->_columnHeaders = array(); | |
314 | foreach ($this->_columns as $tableName => $table) { | |
315 | if (array_key_exists('fields', $table)) { | |
316 | foreach ($table['fields'] as $fieldName => $field) { | |
317 | if ($tableName == 'civicrm_address') { | |
318 | $this->_addressField = TRUE; | |
319 | } | |
9d72cede EM |
320 | if (!empty($field['required']) || |
321 | !empty($this->_params['fields'][$fieldName]) | |
322 | ) { | |
6a488035 TO |
323 | if ($tableName == 'civicrm_email') { |
324 | $this->_emailField = TRUE; | |
325 | } | |
326 | elseif ($tableName == 'civicrm_phone') { | |
327 | $this->_phoneField = TRUE; | |
328 | } | |
329 | elseif ($tableName == 'civicrm_relationship') { | |
330 | $this->_relField = TRUE; | |
331 | } | |
332 | if ($fieldName == 'sort_name') { | |
2f4c2f5d | 333 | $select[] = "GROUP_CONCAT({$field['dbAlias']} ORDER BY {$field['dbAlias']} ) |
6a488035 TO |
334 | as {$tableName}_{$fieldName}"; |
335 | } | |
336 | if ($tableName == 'civicrm_activity_last_completed') { | |
337 | $this->_activityLastCompleted = TRUE; | |
338 | } | |
339 | ||
340 | if ($fieldName == 'case_role') { | |
341 | $select[] = "GROUP_CONCAT(DISTINCT({$field['dbAlias']}) ORDER BY {$field['dbAlias']}) as {$tableName}_{$fieldName}"; | |
342 | } | |
343 | else { | |
344 | $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}"; | |
345 | } | |
346 | ||
347 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field); | |
348 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title']; | |
349 | } | |
350 | } | |
351 | } | |
352 | } | |
5e851416 BS |
353 | |
354 | if ($orderBys = $this->_params['order_bys']) { | |
355 | foreach ($orderBys as $orderBy) { | |
356 | if ($orderBy['column'] == 'case_type_name') { | |
357 | $select[] = "civireport_case_types.title as case_type_name"; | |
358 | $this->_caseTypeNameOrderBy = TRUE; | |
359 | } | |
360 | } | |
361 | } | |
362 | ||
d1641c51 | 363 | $this->_selectClauses = $select; |
6a488035 TO |
364 | |
365 | $this->_select = 'SELECT ' . implode(', ', $select) . ' '; | |
366 | } | |
367 | ||
00be9182 | 368 | public function from() { |
6a488035 TO |
369 | |
370 | $case = $this->_aliases['civicrm_case']; | |
371 | $conact = $this->_aliases['civicrm_contact']; | |
372 | ||
373 | $this->_from = " | |
374 | FROM civicrm_case $case | |
375 | LEFT JOIN civicrm_case_contact civireport_case_contact on civireport_case_contact.case_id = {$case}.id | |
376 | LEFT JOIN civicrm_contact $conact ON {$conact}.id = civireport_case_contact.contact_id | |
377 | "; | |
378 | if ($this->_relField) { | |
379 | $this->_from .= " | |
380 | LEFT JOIN civicrm_relationship {$this->_aliases['civicrm_relationship']} ON {$this->_aliases['civicrm_relationship']}.case_id = {$case}.id | |
381 | "; | |
382 | } | |
383 | ||
384 | if ($this->_addressField) { | |
385 | $this->_from .= " | |
2f4c2f5d | 386 | LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']} |
387 | ON {$conact}.id = {$this->_aliases['civicrm_address']}.contact_id AND | |
6a488035 TO |
388 | {$this->_aliases['civicrm_address']}.is_primary = 1 "; |
389 | } | |
390 | if ($this->_emailField) { | |
2f4c2f5d | 391 | $this->_from .= " |
392 | LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']} | |
393 | ON {$conact}.id = {$this->_aliases['civicrm_email']}.contact_id AND | |
6a488035 TO |
394 | {$this->_aliases['civicrm_email']}.is_primary = 1 "; |
395 | } | |
396 | if ($this->_phoneField) { | |
397 | $this->_from .= " | |
2f4c2f5d | 398 | LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']} |
399 | ON ( {$conact}.id = {$this->_aliases['civicrm_phone']}.contact_id AND | |
6a488035 TO |
400 | {$this->_aliases['civicrm_phone']}.is_primary = 1) "; |
401 | } | |
402 | if ($this->_worldRegionField) { | |
403 | $this->_from .= " | |
404 | LEFT JOIN civicrm_country {$this->_aliases['civicrm_country']} | |
405 | ON {$this->_aliases['civicrm_country']}.id ={$this->_aliases['civicrm_address']}.country_id | |
406 | LEFT JOIN civicrm_worldregion {$this->_aliases['civicrm_worldregion']} | |
407 | ON {$this->_aliases['civicrm_country']}.region_id = {$this->_aliases['civicrm_worldregion']}.id "; | |
408 | } | |
409 | ||
410 | // Include clause for last activity of the case | |
411 | if ($this->_activityLast) { | |
412 | $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) )"; | |
413 | } | |
414 | ||
415 | // Include clause for last completed activity of the case | |
416 | if ($this->_activityLastCompleted) { | |
417 | $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 ) )"; | |
418 | } | |
5e851416 BS |
419 | |
420 | //include case type name | |
421 | if ($this->_caseTypeNameOrderBy) { | |
422 | $this->_from .= " | |
423 | LEFT JOIN civicrm_case_type civireport_case_types | |
424 | ON {$case}.case_type_id = civireport_case_types.id | |
425 | "; | |
426 | } | |
6a488035 TO |
427 | } |
428 | ||
00be9182 | 429 | public function where() { |
6a488035 TO |
430 | $clauses = array(); |
431 | $this->_having = ''; | |
432 | foreach ($this->_columns as $tableName => $table) { | |
433 | if (array_key_exists('filters', $table)) { | |
434 | foreach ($table['filters'] as $fieldName => $field) { | |
435 | $clause = NULL; | |
436 | ||
437 | if (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE) { | |
438 | $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params); | |
9d72cede EM |
439 | $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params); |
440 | $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params); | |
6a488035 TO |
441 | |
442 | $clause = $this->dateClause($field['dbAlias'], $relative, $from, $to, $field['type']); | |
443 | } | |
444 | else { | |
445 | ||
446 | $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params); | |
9d72cede EM |
447 | if ($fieldName == 'case_type_id' && |
448 | !empty($this->_params['case_type_id_value']) | |
449 | ) { | |
6a488035 | 450 | foreach ($this->_params['case_type_id_value'] as $key => $value) { |
6d491668 | 451 | $this->_params['case_type_id_value'][$key] = $value; |
6a488035 TO |
452 | } |
453 | } | |
454 | ||
455 | if ($op) { | |
456 | $clause = $this->whereClause($field, | |
457 | $op, | |
458 | CRM_Utils_Array::value("{$fieldName}_value", $this->_params), | |
459 | CRM_Utils_Array::value("{$fieldName}_min", $this->_params), | |
460 | CRM_Utils_Array::value("{$fieldName}_max", $this->_params) | |
461 | ); | |
462 | } | |
463 | } | |
464 | ||
465 | if (!empty($clause)) { | |
466 | $clauses[] = $clause; | |
467 | } | |
468 | } | |
469 | } | |
470 | } | |
471 | ||
472 | if (isset($this->_params['options']['my_cases'])) { | |
9d72cede EM |
473 | $session = CRM_Core_Session::singleton(); |
474 | $userID = $session->get('userID'); | |
6a488035 TO |
475 | $clauses[] = "{$this->_aliases['civicrm_contact']}.id = {$userID}"; |
476 | } | |
477 | ||
478 | if (empty($clauses)) { | |
479 | $this->_where = 'WHERE ( 1 ) '; | |
480 | } | |
481 | else { | |
482 | $this->_where = 'WHERE ' . implode(' AND ', $clauses); | |
483 | } | |
484 | } | |
485 | ||
00be9182 | 486 | public function groupBy() { |
b708c08d | 487 | $this->_groupBy = CRM_Contact_BAO_Query::getGroupByFromSelectColumns($this->_selectClauses, "{$this->_aliases['civicrm_case']}.id"); |
6a488035 TO |
488 | } |
489 | ||
74cf4551 EM |
490 | /** |
491 | * @param $rows | |
492 | * | |
493 | * @return array | |
494 | */ | |
00be9182 | 495 | public function statistics(&$rows) { |
6a488035 TO |
496 | $statistics = parent::statistics($rows); |
497 | ||
9d72cede EM |
498 | $select = "select COUNT( DISTINCT( {$this->_aliases['civicrm_address']}.country_id))"; |
499 | $sql = "{$select} {$this->_from} {$this->_where}"; | |
6a488035 TO |
500 | $countryCount = CRM_Core_DAO::singleValueQuery($sql); |
501 | ||
6a488035 TO |
502 | $statistics['counts']['case'] = array( |
503 | 'title' => ts('Total Number of Cases '), | |
504 | 'value' => isset($statistics['counts']['rowsFound']) ? $statistics['counts']['rowsFound']['value'] : count($rows), | |
505 | ); | |
506 | $statistics['counts']['country'] = array( | |
507 | 'title' => ts('Total Number of Countries '), | |
508 | 'value' => $countryCount, | |
509 | ); | |
510 | ||
511 | return $statistics; | |
512 | } | |
513 | ||
00be9182 | 514 | public function orderBy() { |
5e851416 BS |
515 | parent::orderBy(); |
516 | ||
517 | if ($this->_caseTypeNameOrderBy) { | |
518 | $this->_orderBy = str_replace('case_civireport.case_type_name', 'civireport_case_types.title', $this->_orderBy); | |
519 | } | |
6a488035 TO |
520 | } |
521 | ||
00be9182 | 522 | public function caseDetailSpecialColumnProcess() { |
6a488035 TO |
523 | if (!$this->_includeCaseDetailExtra) { |
524 | return; | |
525 | } | |
526 | ||
527 | $from = $select = array(); | |
528 | $case = $this->_aliases['civicrm_case']; | |
529 | ||
530 | if ($activityType = CRM_Utils_Array::value('case_activity_all_dates', $this->_params['case_detail_extra'])) { | |
531 | $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"; | |
532 | ||
2f4c2f5d | 533 | $from[] = " LEFT JOIN civicrm_case_activity civireport_case_activity_all_{$activityType} ON ( civireport_case_activity_all_{$activityType}.case_id = {$case}.id) |
6a488035 TO |
534 | 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})"; |
535 | ||
536 | $this->_columnHeaders['case_activity_all_dates'] = array( | |
795492f3 | 537 | 'title' => $this->_caseDetailExtra['case_activity_all_dates']['title'] . ": {$this->caseActivityTypes[$activityType]}", |
6a488035 TO |
538 | 'type' => CRM_Utils_Array::value('type', $this->_caseDetailExtra['case_activity_all_dates']), |
539 | ); | |
540 | } | |
541 | ||
542 | $this->_select .= ', ' . implode(', ', $select) . ' '; | |
543 | $this->_from .= ' ' . implode(' ', $from) . ' '; | |
544 | } | |
545 | ||
00be9182 | 546 | public function postProcess() { |
6a488035 TO |
547 | |
548 | $this->beginPostProcess(); | |
549 | ||
550 | $this->checkEnabledFields(); | |
551 | ||
552 | $this->buildQuery(TRUE); | |
553 | ||
554 | $this->caseDetailSpecialColumnProcess(); | |
555 | ||
556 | $sql = "{$this->_select} {$this->_from} {$this->_where} {$this->_groupBy} {$this->_having} {$this->_orderBy} {$this->_limit}"; | |
557 | ||
558 | $rows = $graphRows = array(); | |
559 | $this->buildRows($sql, $rows); | |
560 | ||
561 | $this->formatDisplay($rows); | |
562 | ||
563 | $this->doTemplateAssignment($rows); | |
564 | $this->endPostProcess($rows); | |
565 | } | |
566 | ||
00be9182 | 567 | public function checkEnabledFields() { |
9d72cede EM |
568 | if (isset($this->_params['worldregion_id_value']) && |
569 | !empty($this->_params['worldregion_id_value']) | |
570 | ) { | |
6a488035 TO |
571 | $this->_addressField = TRUE; |
572 | $this->_worldRegionField = TRUE; | |
573 | } | |
574 | ||
575 | if (isset($this->_params['case_role_value']) | |
576 | && !empty($this->_params['case_role_value']) | |
577 | ) { | |
578 | $this->_relField = TRUE; | |
579 | } | |
580 | ||
9d72cede EM |
581 | if (!empty($this->_params['activity_date_time_relative']) || |
582 | !empty($this->_params['activity_date_time_from']) || | |
6a488035 TO |
583 | CRM_Utils_Array::value('activity_date_time_to', $this->_params) |
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 | } | |
646 | if (array_key_exists('civicrm_address_country_id', $row)) { | |
647 | if ($value = $row['civicrm_address_country_id']) { | |
648 | $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant::country($value, FALSE); | |
649 | } | |
650 | $entryFound = TRUE; | |
651 | } | |
652 | if (array_key_exists('civicrm_address_state_province_id', $row)) { | |
653 | if ($value = $row['civicrm_address_state_province_id']) { | |
654 | $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant::stateProvince($value, FALSE); | |
655 | } | |
656 | $entryFound = TRUE; | |
657 | } | |
9d72cede EM |
658 | if (array_key_exists('civicrm_activity_last_completed_last_completed_activity_subject', $row) && |
659 | empty($row['civicrm_activity_last_completed_last_completed_activity_subject']) | |
660 | ) { | |
2f186e81 | 661 | $rows[$rowNum]['civicrm_activity_last_completed_last_completed_activity_subject'] = ts('(no subject)'); |
6a488035 TO |
662 | $entryFound = TRUE; |
663 | } | |
664 | if (array_key_exists('civicrm_contact_client_sort_name', $row) && | |
665 | array_key_exists('civicrm_contact_id', $row) | |
666 | ) { | |
667 | $url = CRM_Utils_System::url("civicrm/contact/view", | |
668 | 'reset=1&cid=' . $row['civicrm_contact_id'], | |
669 | $this->_absoluteUrl | |
670 | ); | |
671 | $rows[$rowNum]['civicrm_contact_client_sort_name_link'] = $url; | |
672 | $rows[$rowNum]['civicrm_contact_client_sort_name_hover'] = ts("View Contact Summary for this Contact"); | |
673 | $entryFound = TRUE; | |
674 | } | |
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 | ||
6a488035 TO |
699 | if (!$entryFound) { |
700 | break; | |
701 | } | |
702 | } | |
703 | } | |
96025800 | 704 | |
6a488035 | 705 | } |