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 | */ |
33 | class CRM_Report_Form_Case_Summary extends CRM_Report_Form { | |
34 | ||
35 | protected $_summary = NULL; | |
36 | protected $_relField = FALSE; | |
01bf13dc | 37 | protected $_exposeContactID = FALSE; |
74cf4551 | 38 | |
dd3aa6f0 | 39 | protected $_customGroupExtends = array('Case'); |
b326c682 | 40 | |
74cf4551 | 41 | /** |
73b448bf | 42 | * Class constructor. |
74cf4551 | 43 | */ |
00be9182 | 44 | public function __construct() { |
9d72cede | 45 | $this->case_types = CRM_Case_PseudoConstant::caseType(); |
01bf13dc | 46 | $this->case_statuses = CRM_Core_OptionGroup::values('case_status'); |
9d72cede | 47 | $rels = CRM_Core_PseudoConstant::relationshipType(); |
6a488035 TO |
48 | foreach ($rels as $relid => $v) { |
49 | $this->rel_types[$relid] = $v['label_b_a']; | |
50 | } | |
51 | ||
9d72cede EM |
52 | $this->deleted_labels = array( |
53 | '' => ts('- select -'), | |
54 | 0 => ts('No'), | |
21dfd5f5 | 55 | 1 => ts('Yes'), |
9d72cede | 56 | ); |
6a488035 TO |
57 | |
58 | $this->_columns = array( | |
9d72cede | 59 | 'civicrm_c2' => array( |
6a488035 | 60 | 'dao' => 'CRM_Contact_DAO_Contact', |
9d72cede EM |
61 | 'fields' => array( |
62 | 'client_name' => array( | |
6a488035 TO |
63 | 'name' => 'sort_name', |
64 | 'title' => ts('Client'), | |
65 | 'required' => TRUE, | |
66 | ), | |
9d72cede | 67 | 'id' => array( |
6a488035 TO |
68 | 'no_display' => TRUE, |
69 | 'required' => TRUE, | |
70 | ), | |
71 | ), | |
72 | ), | |
9d72cede | 73 | 'civicrm_case' => array( |
6a488035 | 74 | 'dao' => 'CRM_Case_DAO_Case', |
9d72cede EM |
75 | 'fields' => array( |
76 | 'id' => array( | |
77 | 'title' => ts('Case ID'), | |
6a488035 TO |
78 | 'required' => TRUE, |
79 | ), | |
80 | 'subject' => array( | |
9d72cede EM |
81 | 'title' => ts('Case Subject'), |
82 | 'default' => TRUE, | |
6a488035 TO |
83 | ), |
84 | 'status_id' => array( | |
9d72cede EM |
85 | 'title' => ts('Status'), |
86 | 'default' => TRUE, | |
6a488035 TO |
87 | ), |
88 | 'case_type_id' => array( | |
9d72cede EM |
89 | 'title' => ts('Case Type'), |
90 | 'default' => TRUE, | |
6a488035 TO |
91 | ), |
92 | 'start_date' => array( | |
9d72cede EM |
93 | 'title' => ts('Start Date'), |
94 | 'default' => TRUE, | |
0fa0ffd2 | 95 | 'type' => CRM_Utils_Type::T_DATE, |
6a488035 TO |
96 | ), |
97 | 'end_date' => array( | |
9d72cede EM |
98 | 'title' => ts('End Date'), |
99 | 'default' => TRUE, | |
0fa0ffd2 | 100 | 'type' => CRM_Utils_Type::T_DATE, |
6a488035 TO |
101 | ), |
102 | 'duration' => array( | |
9d72cede EM |
103 | 'title' => ts('Duration (Days)'), |
104 | 'default' => FALSE, | |
6a488035 TO |
105 | ), |
106 | 'is_deleted' => array( | |
9d72cede EM |
107 | 'title' => ts('Deleted?'), |
108 | 'default' => FALSE, | |
109 | 'type' => CRM_Utils_Type::T_INT, | |
6a488035 TO |
110 | ), |
111 | ), | |
9d72cede EM |
112 | 'filters' => array( |
113 | 'start_date' => array( | |
114 | 'title' => ts('Start Date'), | |
6a488035 TO |
115 | 'operatorType' => CRM_Report_Form::OP_DATE, |
116 | 'type' => CRM_Utils_Type::T_DATE, | |
117 | ), | |
9d72cede EM |
118 | 'end_date' => array( |
119 | 'title' => ts('End Date'), | |
6a488035 TO |
120 | 'operatorType' => CRM_Report_Form::OP_DATE, |
121 | 'type' => CRM_Utils_Type::T_DATE, | |
122 | ), | |
9d72cede EM |
123 | 'case_type_id' => array( |
124 | 'title' => ts('Case Type'), | |
6a488035 | 125 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
7a5c0c6c | 126 | 'options' => CRM_Case_BAO_Case::buildOptions('case_type_id', 'search'), |
6a488035 | 127 | ), |
9d72cede EM |
128 | 'status_id' => array( |
129 | 'title' => ts('Status'), | |
09b5cdcf | 130 | 'type' => CRM_Utils_Type::T_INT, |
6a488035 | 131 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
7a5c0c6c | 132 | 'options' => CRM_Case_BAO_Case::buildOptions('status_id', 'search'), |
6a488035 | 133 | ), |
9d72cede EM |
134 | 'is_deleted' => array( |
135 | 'title' => ts('Deleted?'), | |
6a488035 TO |
136 | 'type' => CRM_Report_Form::OP_INT, |
137 | 'operatorType' => CRM_Report_Form::OP_SELECT, | |
138 | 'options' => $this->deleted_labels, | |
139 | 'default' => 0, | |
140 | ), | |
141 | ), | |
142 | ), | |
9d72cede | 143 | 'civicrm_contact' => array( |
6a488035 | 144 | 'dao' => 'CRM_Contact_DAO_Contact', |
9d72cede EM |
145 | 'fields' => array( |
146 | 'sort_name' => array( | |
147 | 'title' => ts('Staff Member'), | |
6a488035 TO |
148 | 'default' => TRUE, |
149 | ), | |
150 | ), | |
9d72cede EM |
151 | 'filters' => array( |
152 | 'sort_name' => array( | |
153 | 'title' => ts('Staff Member'), | |
6a488035 TO |
154 | ), |
155 | ), | |
156 | ), | |
9d72cede | 157 | 'civicrm_relationship' => array( |
6a488035 | 158 | 'dao' => 'CRM_Contact_DAO_Relationship', |
9d72cede EM |
159 | 'filters' => array( |
160 | 'relationship_type_id' => array( | |
161 | 'title' => ts('Staff Relationship'), | |
09b5cdcf | 162 | 'type' => CRM_Utils_Type::T_INT, |
6a488035 TO |
163 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
164 | 'options' => $this->rel_types, | |
165 | ), | |
166 | ), | |
167 | ), | |
9d72cede | 168 | 'civicrm_relationship_type' => array( |
6a488035 | 169 | 'dao' => 'CRM_Contact_DAO_RelationshipType', |
9d72cede EM |
170 | 'fields' => array( |
171 | 'label_b_a' => array( | |
172 | 'title' => ts('Relationship'), | |
173 | 'default' => TRUE, | |
6a488035 TO |
174 | ), |
175 | ), | |
176 | ), | |
9d72cede | 177 | 'civicrm_case_contact' => array( |
6a488035 TO |
178 | 'dao' => 'CRM_Case_DAO_CaseContact', |
179 | ), | |
180 | ); | |
181 | ||
182 | parent::__construct(); | |
183 | } | |
184 | ||
00be9182 | 185 | public function preProcess() { |
6a488035 TO |
186 | parent::preProcess(); |
187 | } | |
188 | ||
00be9182 | 189 | public function select() { |
6a488035 TO |
190 | $select = array(); |
191 | $this->_columnHeaders = array(); | |
192 | foreach ($this->_columns as $tableName => $table) { | |
193 | if (array_key_exists('fields', $table)) { | |
194 | foreach ($table['fields'] as $fieldName => $field) { | |
9d72cede EM |
195 | if (!empty($field['required']) || |
196 | !empty($this->_params['fields'][$fieldName]) | |
197 | ) { | |
6a488035 TO |
198 | |
199 | if ($tableName == 'civicrm_relationship_type') { | |
200 | $this->_relField = TRUE; | |
201 | } | |
202 | ||
203 | if ($fieldName == 'duration') { | |
204 | $select[] = "IF({$table['fields']['end_date']['dbAlias']} Is Null, '', DATEDIFF({$table['fields']['end_date']['dbAlias']}, {$table['fields']['start_date']['dbAlias']})) as {$tableName}_{$fieldName}"; | |
205 | } | |
206 | else { | |
207 | $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}"; | |
208 | } | |
209 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field); | |
210 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title']; | |
211 | } | |
212 | } | |
213 | } | |
214 | } | |
d1641c51 | 215 | $this->_selectClauses = $select; |
6a488035 TO |
216 | |
217 | $this->_select = "SELECT " . implode(', ', $select) . " "; | |
218 | } | |
219 | ||
74cf4551 EM |
220 | /** |
221 | * @param $fields | |
222 | * @param $files | |
223 | * @param $self | |
224 | * | |
225 | * @return array | |
226 | */ | |
00be9182 | 227 | public static function formRule($fields, $files, $self) { |
6a488035 | 228 | $errors = $grouping = array(); |
9d72cede EM |
229 | if (empty($fields['relationship_type_id_value']) && |
230 | (array_key_exists('sort_name', $fields['fields']) || | |
231 | array_key_exists('label_b_a', $fields['fields'])) | |
232 | ) { | |
6a488035 TO |
233 | $errors['fields'] = ts('Either filter on at least one relationship type, or de-select Staff Member and Relationship from the list of fields.'); |
234 | } | |
9d72cede EM |
235 | if ((!empty($fields['relationship_type_id_value']) || |
236 | !empty($fields['sort_name_value'])) && | |
237 | (!array_key_exists('sort_name', $fields['fields']) || | |
238 | !array_key_exists('label_b_a', $fields['fields'])) | |
239 | ) { | |
6a488035 TO |
240 | $errors['fields'] = ts('To filter on Staff Member or Relationship, please also select Staff Member and Relationship from the list of fields.'); |
241 | } | |
242 | return $errors; | |
243 | } | |
244 | ||
00be9182 | 245 | public function from() { |
6a488035 | 246 | |
9d72cede EM |
247 | $cc = $this->_aliases['civicrm_case']; |
248 | $c = $this->_aliases['civicrm_contact']; | |
249 | $c2 = $this->_aliases['civicrm_c2']; | |
250 | $cr = $this->_aliases['civicrm_relationship']; | |
6a488035 TO |
251 | $crt = $this->_aliases['civicrm_relationship_type']; |
252 | $ccc = $this->_aliases['civicrm_case_contact']; | |
253 | ||
254 | if ($this->_relField) { | |
255 | $this->_from = " | |
2f4c2f5d | 256 | FROM civicrm_contact $c |
6a488035 TO |
257 | inner join civicrm_relationship $cr on {$c}.id = ${cr}.contact_id_b |
258 | inner join civicrm_case $cc on ${cc}.id = ${cr}.case_id | |
259 | inner join civicrm_relationship_type $crt on ${crt}.id=${cr}.relationship_type_id | |
260 | inner join civicrm_case_contact $ccc on ${ccc}.case_id = ${cc}.id | |
261 | inner join civicrm_contact $c2 on ${c2}.id=${ccc}.contact_id | |
262 | "; | |
263 | } | |
264 | else { | |
265 | $this->_from = " | |
e5dab8dd | 266 | FROM civicrm_case $cc |
6a488035 TO |
267 | inner join civicrm_case_contact $ccc on ${ccc}.case_id = ${cc}.id |
268 | inner join civicrm_contact $c2 on ${c2}.id=${ccc}.contact_id | |
269 | "; | |
270 | } | |
271 | } | |
272 | ||
00be9182 | 273 | public function where() { |
6a488035 TO |
274 | $clauses = array(); |
275 | $this->_having = ''; | |
276 | foreach ($this->_columns as $tableName => $table) { | |
277 | if (array_key_exists('filters', $table)) { | |
278 | foreach ($table['filters'] as $fieldName => $field) { | |
279 | $clause = NULL; | |
84178120 | 280 | if (CRM_Utils_Array::value("operatorType", $field) & CRM_Report_Form::OP_DATE |
9d72cede | 281 | ) { |
6a488035 | 282 | $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params); |
9d72cede EM |
283 | $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params); |
284 | $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params); | |
6a488035 TO |
285 | |
286 | $clause = $this->dateClause($field['dbAlias'], $relative, $from, $to, | |
287 | CRM_Utils_Array::value('type', $field) | |
288 | ); | |
289 | } | |
290 | else { | |
291 | ||
292 | $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params); | |
293 | if ($fieldName == 'case_type_id') { | |
294 | $value = CRM_Utils_Array::value("{$fieldName}_value", $this->_params); | |
295 | if (!empty($value)) { | |
75314e67 | 296 | $operator = ''; |
297 | if ($op == 'notin') { | |
298 | $operator = 'NOT'; | |
299 | } | |
6e987e3a | 300 | |
301 | $regexp = "[[:cntrl:]]*" . implode('[[:>:]]*|[[:<:]]*', $value) . "[[:cntrl:]]*"; | |
302 | $clause = "{$field['dbAlias']} {$operator} REGEXP '{$regexp}'"; | |
6a488035 TO |
303 | } |
304 | $op = NULL; | |
305 | } | |
306 | ||
307 | if ($op) { | |
308 | $clause = $this->whereClause($field, | |
309 | $op, | |
310 | CRM_Utils_Array::value("{$fieldName}_value", $this->_params), | |
311 | CRM_Utils_Array::value("{$fieldName}_min", $this->_params), | |
312 | CRM_Utils_Array::value("{$fieldName}_max", $this->_params) | |
313 | ); | |
314 | } | |
315 | } | |
316 | ||
317 | if (!empty($clause)) { | |
318 | $clauses[] = $clause; | |
319 | } | |
320 | } | |
321 | } | |
322 | } | |
323 | ||
324 | if (empty($clauses)) { | |
325 | $this->_where = "WHERE ( 1 ) "; | |
326 | } | |
327 | else { | |
328 | $this->_where = "WHERE " . implode(' AND ', $clauses); | |
329 | } | |
330 | } | |
331 | ||
00be9182 | 332 | public function groupBy() { |
e5dab8dd | 333 | $this->_groupBy = ""; |
6a488035 TO |
334 | } |
335 | ||
00be9182 | 336 | public function postProcess() { |
6a488035 TO |
337 | |
338 | $this->beginPostProcess(); | |
339 | ||
340 | $sql = $this->buildQuery(TRUE); | |
341 | ||
342 | $rows = $graphRows = array(); | |
343 | $this->buildRows($sql, $rows); | |
344 | ||
345 | $this->formatDisplay($rows); | |
346 | $this->doTemplateAssignment($rows); | |
347 | $this->endPostProcess($rows); | |
348 | } | |
349 | ||
74cf4551 | 350 | /** |
4b62bc4f EM |
351 | * Alter display of rows. |
352 | * | |
353 | * Iterate through the rows retrieved via SQL and make changes for display purposes, | |
354 | * such as rendering contacts as links. | |
355 | * | |
356 | * @param array $rows | |
357 | * Rows generated by SQL, with an array for each row. | |
74cf4551 | 358 | */ |
00be9182 | 359 | public function alterDisplay(&$rows) { |
6a488035 TO |
360 | $entryFound = FALSE; |
361 | foreach ($rows as $rowNum => $row) { | |
362 | if (array_key_exists('civicrm_case_status_id', $row)) { | |
363 | if ($value = $row['civicrm_case_status_id']) { | |
364 | $rows[$rowNum]['civicrm_case_status_id'] = $this->case_statuses[$value]; | |
365 | $entryFound = TRUE; | |
366 | } | |
367 | } | |
368 | ||
9d72cede EM |
369 | if (array_key_exists('civicrm_case_case_type_id', $row) && |
370 | !empty($rows[$rowNum]['civicrm_case_case_type_id']) | |
371 | ) { | |
372 | $value = $row['civicrm_case_case_type_id']; | |
6a488035 | 373 | $typeIds = explode(CRM_Core_DAO::VALUE_SEPARATOR, $value); |
9d72cede | 374 | $value = array(); |
6a488035 TO |
375 | foreach ($typeIds as $typeId) { |
376 | if ($typeId) { | |
377 | $value[$typeId] = $this->case_types[$typeId]; | |
378 | } | |
379 | } | |
380 | $rows[$rowNum]['civicrm_case_case_type_id'] = implode(', ', $value); | |
381 | $entryFound = TRUE; | |
382 | } | |
383 | ||
384 | // convert Case ID and Subject to links to Manage Case | |
9d72cede EM |
385 | if (array_key_exists('civicrm_case_id', $row) && |
386 | !empty($rows[$rowNum]['civicrm_c2_id']) | |
387 | ) { | |
6a488035 | 388 | $url = CRM_Utils_System::url("civicrm/contact/view/case", |
9d72cede EM |
389 | 'reset=1&action=view&cid=' . $row['civicrm_c2_id'] . '&id=' . |
390 | $row['civicrm_case_id'], | |
6a488035 TO |
391 | $this->_absoluteUrl |
392 | ); | |
393 | $rows[$rowNum]['civicrm_case_id_link'] = $url; | |
394 | $rows[$rowNum]['civicrm_case_id_hover'] = ts("Manage Case"); | |
395 | $entryFound = TRUE; | |
396 | } | |
9d72cede EM |
397 | if (array_key_exists('civicrm_case_subject', $row) && |
398 | !empty($rows[$rowNum]['civicrm_c2_id']) | |
399 | ) { | |
6a488035 | 400 | $url = CRM_Utils_System::url("civicrm/contact/view/case", |
9d72cede EM |
401 | 'reset=1&action=view&cid=' . $row['civicrm_c2_id'] . '&id=' . |
402 | $row['civicrm_case_id'], | |
6a488035 TO |
403 | $this->_absoluteUrl |
404 | ); | |
405 | $rows[$rowNum]['civicrm_case_subject_link'] = $url; | |
406 | $rows[$rowNum]['civicrm_case_subject_hover'] = ts("Manage Case"); | |
407 | $entryFound = TRUE; | |
408 | } | |
409 | ||
410 | if (array_key_exists('civicrm_case_is_deleted', $row)) { | |
411 | $value = $row['civicrm_case_is_deleted']; | |
412 | $rows[$rowNum]['civicrm_case_is_deleted'] = $this->deleted_labels[$value]; | |
413 | $entryFound = TRUE; | |
414 | } | |
415 | ||
416 | if (!$entryFound) { | |
417 | break; | |
418 | } | |
419 | } | |
420 | } | |
96025800 | 421 | |
6a488035 | 422 | } |