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