Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | |
3 | /* | |
4 | +--------------------------------------------------------------------+ | |
39de6fd5 | 5 | | CiviCRM version 4.6 | |
6a488035 | 6 | +--------------------------------------------------------------------+ |
06b69b18 | 7 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
8 | +--------------------------------------------------------------------+ |
9 | | This file is a part of CiviCRM. | | |
10 | | | | |
11 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
12 | | under the terms of the GNU Affero General Public License | | |
13 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
14 | | | | |
15 | | CiviCRM is distributed in the hope that it will be useful, but | | |
16 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
17 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
18 | | See the GNU Affero General Public License for more details. | | |
19 | | | | |
20 | | You should have received a copy of the GNU Affero General Public | | |
21 | | License and the CiviCRM Licensing Exception along | | |
22 | | with this program; if not, contact CiviCRM LLC | | |
23 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
24 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
25 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
26 | +--------------------------------------------------------------------+ | |
27 | */ | |
28 | ||
29 | /** | |
30 | * | |
31 | * @package CRM | |
06b69b18 | 32 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
33 | * $Id$ |
34 | * | |
35 | */ | |
36 | class CRM_Report_Form_Case_Demographics extends CRM_Report_Form { | |
37 | ||
38 | protected $_summary = NULL; | |
39 | ||
40 | protected $_emailField = FALSE; | |
41 | ||
42 | protected $_phoneField = FALSE; | |
74cf4551 EM |
43 | |
44 | /** | |
45 | * | |
46 | */ | |
47 | /** | |
48 | * | |
49 | */ | |
00be9182 | 50 | public function __construct() { |
6a488035 | 51 | $this->_columns = array( |
9d72cede | 52 | 'civicrm_contact' => array( |
6a488035 | 53 | 'dao' => 'CRM_Contact_DAO_Contact', |
9d72cede EM |
54 | 'fields' => array( |
55 | 'sort_name' => array( | |
56 | 'title' => ts('Contact Name'), | |
6a488035 TO |
57 | 'required' => TRUE, |
58 | 'no_repeat' => TRUE, | |
59 | ), | |
9d72cede EM |
60 | 'gender_id' => array( |
61 | 'title' => ts('Gender'), | |
6a488035 TO |
62 | 'default' => TRUE, |
63 | ), | |
9d72cede EM |
64 | 'birth_date' => array( |
65 | 'title' => ts('Birthdate'), | |
6a488035 TO |
66 | 'default' => FALSE, |
67 | ), | |
9d72cede | 68 | 'id' => array( |
6a488035 TO |
69 | 'no_display' => TRUE, |
70 | 'required' => TRUE, | |
71 | ), | |
72 | ), | |
9d72cede | 73 | 'filters' => array( |
54071e11 EM |
74 | 'sort_name' => array( |
75 | 'title' => ts('Contact Name'), | |
76 | 'operatorType' => CRM_Report_Form::OP_STRING, | |
77 | ), | |
9d72cede EM |
78 | 'contact_type' => array( |
79 | 'title' => ts('Contact Type'), | |
6a488035 | 80 | 'operatorType' => CRM_Report_Form::OP_SELECT, |
9d72cede EM |
81 | 'options' => array( |
82 | '' => ts('-select-'), | |
6a488035 TO |
83 | 'Individual' => ts('Individual'), |
84 | 'Organization' => ts('Organization'), | |
85 | 'Household' => ts('Household'), | |
86 | ), | |
87 | 'default' => 'Individual', | |
88 | ), | |
9d72cede EM |
89 | 'id' => array( |
90 | 'title' => ts('Contact ID'), | |
6a488035 TO |
91 | 'no_display' => TRUE, |
92 | ), | |
93 | ), | |
94 | 'grouping' => 'contact-fields', | |
95 | ), | |
9d72cede | 96 | 'civicrm_email' => array( |
6a488035 | 97 | 'dao' => 'CRM_Core_DAO_Email', |
9d72cede EM |
98 | 'fields' => array( |
99 | 'email' => array( | |
100 | 'title' => ts('Email'), | |
6a488035 TO |
101 | 'no_repeat' => TRUE, |
102 | ), | |
103 | ), | |
104 | 'grouping' => 'contact-fields', | |
105 | ), | |
9d72cede | 106 | 'civicrm_address' => array( |
6a488035 TO |
107 | 'dao' => 'CRM_Core_DAO_Address', |
108 | 'grouping' => 'contact-fields', | |
9d72cede EM |
109 | 'fields' => array( |
110 | 'street_address' => array('default' => FALSE), | |
111 | 'city' => array('default' => TRUE), | |
6a488035 | 112 | 'postal_code' => NULL, |
9d72cede EM |
113 | 'state_province_id' => array( |
114 | 'title' => ts('State/Province'), | |
6a488035 | 115 | ), |
9d72cede EM |
116 | 'country_id' => array( |
117 | 'title' => ts('Country'), | |
6a488035 TO |
118 | 'default' => FALSE, |
119 | ), | |
120 | ), | |
121 | /* | |
9d72cede EM |
122 | 'filters' => array( |
123 | 'country_id' => array( 'title' => ts( 'Country' ), | |
6a488035 TO |
124 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
125 | 'options' => CRM_Core_PseudoConstant::country( ), | |
2f4c2f5d | 126 | ), |
9d72cede | 127 | 'state_province_id' => array( 'title' => ts( 'State/Province' ), |
6a488035 | 128 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
2f4c2f5d | 129 | 'options' => CRM_Core_PseudoConstant::stateProvince( ), ), |
130 | ), | |
84178120 | 131 | */ |
6a488035 | 132 | ), |
9d72cede | 133 | 'civicrm_phone' => array( |
6a488035 | 134 | 'dao' => 'CRM_Core_DAO_Phone', |
9d72cede | 135 | 'fields' => array('phone' => NULL), |
6a488035 TO |
136 | 'grouping' => 'contact-fields', |
137 | ), | |
9d72cede | 138 | 'civicrm_activity' => array( |
6a488035 | 139 | 'dao' => 'CRM_Activity_DAO_Activity', |
9d72cede EM |
140 | 'fields' => array( |
141 | 'id' => array( | |
142 | 'title' => ts('Activity ID'), | |
6a488035 TO |
143 | 'no_display' => TRUE, |
144 | 'required' => TRUE, | |
145 | ), | |
146 | ), | |
147 | ), | |
9d72cede | 148 | 'civicrm_case' => array( |
6a488035 | 149 | 'dao' => 'CRM_Case_DAO_Case', |
9d72cede EM |
150 | 'fields' => array( |
151 | 'id' => array( | |
152 | 'title' => ts('Case ID'), | |
6a488035 TO |
153 | 'required' => TRUE, |
154 | ), | |
9d72cede EM |
155 | 'start_date' => array( |
156 | 'title' => ts('Case Start'), | |
6a488035 TO |
157 | 'required' => TRUE, |
158 | ), | |
9d72cede EM |
159 | 'end_date' => array( |
160 | 'title' => ts('Case End'), | |
6a488035 TO |
161 | 'required' => TRUE, |
162 | ), | |
163 | ), | |
9d72cede EM |
164 | 'filters' => array( |
165 | 'case_id_filter' => array( | |
166 | 'name' => 'id', | |
6a488035 TO |
167 | 'title' => ts('Cases?'), |
168 | 'operatorType' => CRM_Report_Form::OP_SELECT, | |
9d72cede EM |
169 | 'options' => array( |
170 | 1 => ts('Exclude non-case'), | |
171 | 2 => ts('Exclude cases'), | |
172 | 3 => ts('Include Both') | |
173 | ), | |
6a488035 TO |
174 | 'default' => 3, |
175 | ), | |
9d72cede EM |
176 | 'start_date' => array( |
177 | 'title' => ts('Case Start'), | |
6a488035 TO |
178 | 'operatorType' => CRM_Report_Form::OP_DATE, |
179 | ), | |
9d72cede EM |
180 | 'end_date' => array( |
181 | 'title' => ts('Case End'), | |
6a488035 TO |
182 | 'operatorType' => CRM_Report_Form::OP_DATE, |
183 | ), | |
184 | ), | |
185 | ), | |
6a488035 TO |
186 | ); |
187 | ||
16e2e80c | 188 | $this->_groupFilter = TRUE; |
6a488035 TO |
189 | $this->_tagFilter = TRUE; |
190 | ||
191 | $open_case_val = CRM_Core_OptionGroup::getValue('activity_type', 'Open Case', 'name'); | |
192 | $crmDAO = &CRM_Core_DAO::executeQuery("SELECT cg.table_name, cg.extends AS ext, cf.label, cf.column_name FROM civicrm_custom_group cg INNER JOIN civicrm_custom_field cf ON cg.id = cf.custom_group_id | |
193 | where (cg.extends='Contact' OR cg.extends='Individual' OR cg.extends_entity_column_value='$open_case_val') AND cg.is_active=1 AND cf.is_active=1 ORDER BY cg.table_name"); | |
9d72cede EM |
194 | $curTable = ''; |
195 | $curExt = ''; | |
6a488035 TO |
196 | $curFields = array(); |
197 | while ($crmDAO->fetch()) { | |
198 | if ($curTable == '') { | |
199 | $curTable = $crmDAO->table_name; | |
200 | $curExt = $crmDAO->ext; | |
201 | } | |
202 | elseif ($curTable != $crmDAO->table_name) { | |
203 | // dummy DAO | |
204 | $this->_columns[$curTable] = array( | |
205 | 'dao' => 'CRM_Contact_DAO_Contact', | |
206 | 'fields' => $curFields, | |
207 | 'ext' => $curExt, | |
208 | ); | |
9d72cede EM |
209 | $curTable = $crmDAO->table_name; |
210 | $curExt = $crmDAO->ext; | |
6a488035 TO |
211 | $curFields = array(); |
212 | } | |
213 | ||
214 | $curFields[$crmDAO->column_name] = array('title' => $crmDAO->label); | |
215 | } | |
216 | if (!empty($curFields)) { | |
217 | // dummy DAO | |
218 | $this->_columns[$curTable] = array( | |
219 | 'dao' => 'CRM_Contact_DAO_Contact', | |
220 | 'fields' => $curFields, | |
221 | 'ext' => $curExt, | |
222 | ); | |
223 | } | |
224 | ||
26cf88b5 | 225 | $this->_genders = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id'); |
6a488035 TO |
226 | |
227 | parent::__construct(); | |
228 | } | |
229 | ||
00be9182 | 230 | public function preProcess() { |
6a488035 TO |
231 | parent::preProcess(); |
232 | } | |
233 | ||
00be9182 | 234 | public function select() { |
6a488035 TO |
235 | $select = array(); |
236 | $this->_columnHeaders = array(); | |
237 | foreach ($this->_columns as $tableName => $table) { | |
238 | if (array_key_exists('fields', $table)) { | |
239 | foreach ($table['fields'] as $fieldName => $field) { | |
9d72cede EM |
240 | if (!empty($field['required']) || |
241 | !empty($this->_params['fields'][$fieldName]) | |
242 | ) { | |
6a488035 TO |
243 | if ($tableName == 'civicrm_email') { |
244 | $this->_emailField = TRUE; | |
245 | } | |
246 | elseif ($tableName == 'civicrm_phone') { | |
247 | $this->_phoneField = TRUE; | |
248 | } | |
249 | ||
250 | $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}"; | |
251 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field); | |
252 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title']; | |
253 | } | |
254 | } | |
255 | } | |
256 | } | |
257 | ||
258 | $this->_select = "SELECT " . implode(', ', $select) . " "; | |
259 | } | |
260 | ||
74cf4551 EM |
261 | /** |
262 | * @param $fields | |
263 | * @param $files | |
264 | * @param $self | |
265 | * | |
266 | * @return array | |
267 | */ | |
00be9182 | 268 | public static function formRule($fields, $files, $self) { |
6a488035 TO |
269 | $errors = $grouping = array(); |
270 | return $errors; | |
271 | } | |
272 | ||
00be9182 | 273 | public function from() { |
6a488035 TO |
274 | $this->_from = " |
275 | FROM civicrm_contact {$this->_aliases['civicrm_contact']} | |
2f4c2f5d | 276 | LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']} |
277 | ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id AND | |
6a488035 TO |
278 | {$this->_aliases['civicrm_address']}.is_primary = 1 ) |
279 | LEFT JOIN civicrm_case_contact ccc ON ccc.contact_id = {$this->_aliases['civicrm_contact']}.id | |
280 | LEFT JOIN civicrm_case {$this->_aliases['civicrm_case']} ON {$this->_aliases['civicrm_case']}.id = ccc.case_id | |
281 | LEFT JOIN civicrm_case_activity cca ON cca.case_id = {$this->_aliases['civicrm_case']}.id | |
2f4c2f5d | 282 | LEFT JOIN civicrm_activity {$this->_aliases['civicrm_activity']} ON {$this->_aliases['civicrm_activity']}.id = cca.activity_id |
6a488035 TO |
283 | "; |
284 | ||
285 | foreach ($this->_columns as $t => $c) { | |
9d72cede EM |
286 | if (substr($t, 0, 13) == 'civicrm_value' || |
287 | substr($t, 0, 12) == 'custom_value' | |
288 | ) { | |
6a488035 | 289 | $this->_from .= " LEFT JOIN $t {$this->_aliases[$t]} ON {$this->_aliases[$t]}.entity_id = "; |
9d72cede EM |
290 | $this->_from .= ($c['ext'] == |
291 | 'Activity') ? "{$this->_aliases['civicrm_activity']}.id" : "{$this->_aliases['civicrm_contact']}.id"; | |
6a488035 TO |
292 | } |
293 | } | |
294 | ||
295 | if ($this->_emailField) { | |
296 | $this->_from .= " | |
2f4c2f5d | 297 | LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']} |
6a488035 TO |
298 | ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND |
299 | {$this->_aliases['civicrm_email']}.is_primary = 1) "; | |
300 | } | |
301 | ||
302 | if ($this->_phoneField) { | |
303 | $this->_from .= " | |
2f4c2f5d | 304 | LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']} |
305 | ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND | |
6a488035 TO |
306 | {$this->_aliases['civicrm_phone']}.is_primary = 1 "; |
307 | } | |
308 | } | |
309 | ||
00be9182 | 310 | public function where() { |
6a488035 TO |
311 | $clauses = array(); |
312 | $this->_having = ''; | |
313 | foreach ($this->_columns as $tableName => $table) { | |
314 | if (array_key_exists('filters', $table)) { | |
315 | foreach ($table['filters'] as $fieldName => $field) { | |
316 | $clause = NULL; | |
317 | if ($field['operatorType'] & CRM_Report_Form::OP_DATE) { | |
318 | $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params); | |
9d72cede EM |
319 | $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params); |
320 | $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params); | |
6a488035 | 321 | |
54071e11 | 322 | $clause = $this->dateClause($field['dbAlias'], $relative, $from, $to, CRM_Utils_Type::T_DATE); |
6a488035 TO |
323 | } |
324 | else { | |
325 | $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params); | |
326 | if ($op) { | |
327 | // handle special case | |
328 | if ($fieldName == 'case_id_filter') { | |
329 | $choice = CRM_Utils_Array::value("{$fieldName}_value", $this->_params); | |
330 | if ($choice == 1) { | |
331 | $clause = "({$this->_aliases['civicrm_case']}.id Is Not Null)"; | |
332 | } | |
333 | elseif ($choice == 2) { | |
334 | $clause = "({$this->_aliases['civicrm_case']}.id Is Null)"; | |
335 | } | |
336 | } | |
337 | else { | |
338 | $clause = $this->whereClause($field, | |
339 | $op, | |
340 | CRM_Utils_Array::value("{$fieldName}_value", $this->_params), | |
341 | CRM_Utils_Array::value("{$fieldName}_min", $this->_params), | |
342 | CRM_Utils_Array::value("{$fieldName}_max", $this->_params) | |
343 | ); | |
344 | } | |
345 | } | |
346 | } | |
347 | ||
348 | if (!empty($clause)) { | |
349 | $clauses[] = $clause; | |
350 | } | |
351 | } | |
352 | } | |
353 | } | |
354 | ||
355 | $clauses[] = "(({$this->_aliases['civicrm_case']}.is_deleted = 0) OR ({$this->_aliases['civicrm_case']}.is_deleted Is Null))"; | |
356 | $clauses[] = "(({$this->_aliases['civicrm_activity']}.is_deleted = 0) OR ({$this->_aliases['civicrm_activity']}.is_deleted Is Null))"; | |
357 | $clauses[] = "(({$this->_aliases['civicrm_activity']}.is_current_revision = 1) OR ({$this->_aliases['civicrm_activity']}.is_deleted Is Null))"; | |
358 | ||
359 | $this->_where = "WHERE " . implode(' AND ', $clauses); | |
360 | } | |
361 | ||
00be9182 | 362 | public function groupBy() { |
6a488035 TO |
363 | $this->_groupBy = "GROUP BY {$this->_aliases['civicrm_contact']}.id, {$this->_aliases['civicrm_case']}.id"; |
364 | } | |
365 | ||
00be9182 | 366 | public function orderBy() { |
6a488035 TO |
367 | $this->_orderBy = "ORDER BY {$this->_aliases['civicrm_contact']}.sort_name, {$this->_aliases['civicrm_contact']}.id, {$this->_aliases['civicrm_case']}.id"; |
368 | } | |
369 | ||
00be9182 | 370 | public function postProcess() { |
6a488035 TO |
371 | |
372 | $this->beginPostProcess(); | |
373 | ||
374 | $sql = $this->buildQuery(TRUE); | |
6a488035 TO |
375 | $rows = $graphRows = array(); |
376 | $this->buildRows($sql, $rows); | |
377 | ||
378 | $this->formatDisplay($rows); | |
379 | $this->doTemplateAssignment($rows); | |
380 | $this->endPostProcess($rows); | |
381 | } | |
382 | ||
74cf4551 EM |
383 | /** |
384 | * @param $rows | |
385 | */ | |
00be9182 | 386 | public function alterDisplay(&$rows) { |
6a488035 TO |
387 | // custom code to alter rows |
388 | $entryFound = FALSE; | |
389 | foreach ($rows as $rowNum => $row) { | |
390 | // make count columns point to detail report | |
391 | // convert display name to links | |
392 | if (array_key_exists('civicrm_contact_sort_name', $row) && | |
393 | array_key_exists('civicrm_contact_id', $row) | |
394 | ) { | |
395 | $url = CRM_Utils_System::url('civicrm/contact/view', | |
396 | 'reset=1&cid=' . $row['civicrm_contact_id'], | |
397 | $this->_absoluteUrl | |
398 | ); | |
399 | $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url; | |
400 | $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contact details for this contact."); | |
401 | $entryFound = TRUE; | |
402 | } | |
403 | ||
404 | // handle gender | |
405 | if (array_key_exists('civicrm_contact_gender_id', $row)) { | |
406 | if ($value = $row['civicrm_contact_gender_id']) { | |
407 | $rows[$rowNum]['civicrm_contact_gender_id'] = $this->_genders[$value]; | |
408 | } | |
409 | $entryFound = TRUE; | |
410 | } | |
411 | ||
412 | // handle country | |
413 | if (array_key_exists('civicrm_address_country_id', $row)) { | |
414 | if ($value = $row['civicrm_address_country_id']) { | |
415 | $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant::country($value, FALSE); | |
416 | } | |
417 | $entryFound = TRUE; | |
418 | } | |
419 | if (array_key_exists('civicrm_address_state_province_id', $row)) { | |
420 | if ($value = $row['civicrm_address_state_province_id']) { | |
421 | $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant::stateProvince($value, FALSE); | |
422 | } | |
423 | $entryFound = TRUE; | |
424 | } | |
425 | ||
426 | // handle custom fields | |
427 | foreach ($row as $k => $r) { | |
9d72cede EM |
428 | if (substr($k, 0, 13) == 'civicrm_value' || |
429 | substr($k, 0, 12) == 'custom_value' | |
430 | ) { | |
6a488035 TO |
431 | if ($r || $r == '0') { |
432 | if ($newval = $this->getCustomFieldLabel($k, $r)) { | |
433 | $rows[$rowNum][$k] = $newval; | |
434 | } | |
435 | } | |
436 | $entryFound = TRUE; | |
437 | } | |
438 | } | |
439 | ||
440 | // skip looking further in rows, if first row itself doesn't | |
441 | // have the column we need | |
442 | if (!$entryFound) { | |
443 | break; | |
444 | } | |
445 | } | |
446 | } | |
447 | ||
74cf4551 | 448 | /** |
100fef9d | 449 | * @param string $fname |
74cf4551 EM |
450 | * @param $val |
451 | * | |
452 | * @return null|string | |
453 | */ | |
00be9182 | 454 | public function getCustomFieldLabel($fname, $val) { |
6a488035 TO |
455 | $query = " |
456 | SELECT v.label | |
457 | FROM civicrm_custom_group cg INNER JOIN civicrm_custom_field cf ON cg.id = cf.custom_group_id | |
458 | INNER JOIN civicrm_option_group g ON cf.option_group_id = g.id | |
459 | INNER JOIN civicrm_option_value v ON g.id = v.option_group_id | |
460 | WHERE CONCAT(cg.table_name, '_', cf.column_name) = %1 AND v.value = %2"; | |
9d72cede EM |
461 | $params = array( |
462 | 1 => array($fname, 'String'), | |
6a488035 TO |
463 | 2 => array($val, 'String'), |
464 | ); | |
465 | return CRM_Core_DAO::singleValueQuery($query, $params); | |
466 | } | |
467 | } |