Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | |
3 | /* | |
4 | +--------------------------------------------------------------------+ | |
06b69b18 | 5 | | CiviCRM version 4.5 | |
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; | |
43 | function __construct() { | |
44 | $this->_columns = array( | |
45 | 'civicrm_contact' => | |
46 | array( | |
47 | 'dao' => 'CRM_Contact_DAO_Contact', | |
48 | 'fields' => | |
49 | array( | |
50 | 'sort_name' => | |
51 | array('title' => ts('Contact Name'), | |
52 | 'required' => TRUE, | |
53 | 'no_repeat' => TRUE, | |
54 | ), | |
55 | 'gender_id' => | |
56 | array('title' => ts('Gender'), | |
57 | 'default' => TRUE, | |
58 | ), | |
59 | 'birth_date' => | |
60 | array('title' => ts('Birthdate'), | |
61 | 'default' => FALSE, | |
62 | ), | |
63 | 'id' => | |
64 | array( | |
65 | 'no_display' => TRUE, | |
66 | 'required' => TRUE, | |
67 | ), | |
68 | ), | |
69 | 'filters' => | |
70 | array( | |
54071e11 EM |
71 | 'sort_name' => array( |
72 | 'title' => ts('Contact Name'), | |
73 | 'operatorType' => CRM_Report_Form::OP_STRING, | |
74 | ), | |
6a488035 TO |
75 | 'contact_type' => |
76 | array('title' => ts('Contact Type'), | |
77 | 'operatorType' => CRM_Report_Form::OP_SELECT, | |
78 | 'options' => array('' => ts('-select-'), | |
79 | 'Individual' => ts('Individual'), | |
80 | 'Organization' => ts('Organization'), | |
81 | 'Household' => ts('Household'), | |
82 | ), | |
83 | 'default' => 'Individual', | |
84 | ), | |
85 | 'id' => | |
86 | array('title' => ts('Contact ID'), | |
87 | 'no_display' => TRUE, | |
88 | ), | |
89 | ), | |
90 | 'grouping' => 'contact-fields', | |
91 | ), | |
92 | 'civicrm_email' => | |
93 | array( | |
94 | 'dao' => 'CRM_Core_DAO_Email', | |
95 | 'fields' => | |
96 | array( | |
97 | 'email' => | |
98 | array('title' => ts('Email'), | |
99 | 'no_repeat' => TRUE, | |
100 | ), | |
101 | ), | |
102 | 'grouping' => 'contact-fields', | |
103 | ), | |
104 | 'civicrm_address' => | |
105 | array( | |
106 | 'dao' => 'CRM_Core_DAO_Address', | |
107 | 'grouping' => 'contact-fields', | |
108 | 'fields' => | |
109 | array( | |
110 | 'street_address' => | |
111 | array('default' => FALSE), | |
112 | 'city' => | |
113 | array('default' => TRUE), | |
114 | 'postal_code' => NULL, | |
115 | 'state_province_id' => | |
116 | array('title' => ts('State/Province'), | |
117 | ), | |
118 | 'country_id' => | |
119 | array('title' => ts('Country'), | |
120 | 'default' => FALSE, | |
121 | ), | |
122 | ), | |
123 | /* | |
2f4c2f5d | 124 | 'filters' => |
6a488035 | 125 | array( |
2f4c2f5d | 126 | 'country_id' => |
6a488035 TO |
127 | array( 'title' => ts( 'Country' ), |
128 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
129 | 'options' => CRM_Core_PseudoConstant::country( ), | |
2f4c2f5d | 130 | ), |
131 | 'state_province_id' => | |
132 | array( 'title' => ts( 'State / Province' ), | |
6a488035 | 133 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
2f4c2f5d | 134 | 'options' => CRM_Core_PseudoConstant::stateProvince( ), ), |
135 | ), | |
6a488035 TO |
136 | */ |
137 | ), | |
138 | 'civicrm_phone' => | |
139 | array( | |
140 | 'dao' => 'CRM_Core_DAO_Phone', | |
141 | 'fields' => | |
142 | array('phone' => NULL), | |
143 | 'grouping' => 'contact-fields', | |
144 | ), | |
145 | 'civicrm_activity' => | |
146 | array( | |
147 | 'dao' => 'CRM_Activity_DAO_Activity', | |
148 | 'fields' => | |
149 | array('id' => array('title' => ts('Activity ID'), | |
150 | 'no_display' => TRUE, | |
151 | 'required' => TRUE, | |
152 | ), | |
153 | ), | |
154 | ), | |
155 | 'civicrm_case' => | |
156 | array( | |
157 | 'dao' => 'CRM_Case_DAO_Case', | |
158 | 'fields' => | |
159 | array('id' => array('title' => ts('Case ID'), | |
160 | 'required' => TRUE, | |
161 | ), | |
162 | 'start_date' => array('title' => ts('Case Start'), | |
163 | 'required' => TRUE, | |
164 | ), | |
165 | 'end_date' => array('title' => ts('Case End'), | |
166 | 'required' => TRUE, | |
167 | ), | |
168 | ), | |
169 | 'filters' => | |
170 | array( | |
171 | 'case_id_filter' => array('name' => 'id', | |
172 | 'title' => ts('Cases?'), | |
173 | 'operatorType' => CRM_Report_Form::OP_SELECT, | |
174 | 'options' => array(1 => ts('Exclude non-case'), 2 => ts('Exclude cases'), 3 => ts('Include Both')), | |
175 | 'default' => 3, | |
176 | ), | |
177 | 'start_date' => array('title' => ts('Case Start'), | |
178 | 'operatorType' => CRM_Report_Form::OP_DATE, | |
179 | ), | |
180 | 'end_date' => array('title' => ts('Case End'), | |
181 | 'operatorType' => CRM_Report_Form::OP_DATE, | |
182 | ), | |
183 | ), | |
184 | ), | |
185 | 'civicrm_group' => | |
186 | array( | |
187 | 'dao' => 'CRM_Contact_DAO_Group', | |
188 | 'alias' => 'cgroup', | |
189 | 'filters' => | |
190 | array( | |
191 | 'gid' => | |
192 | array( | |
193 | 'name' => 'group_id', | |
194 | 'title' => ts('Group'), | |
195 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
196 | 'group' => TRUE, | |
197 | 'options' => CRM_Core_PseudoConstant::group(), | |
198 | ), | |
199 | ), | |
200 | ), | |
201 | ); | |
202 | ||
203 | $this->_tagFilter = TRUE; | |
204 | ||
205 | $open_case_val = CRM_Core_OptionGroup::getValue('activity_type', 'Open Case', 'name'); | |
206 | $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 | |
207 | 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"); | |
208 | $curTable = ''; | |
209 | $curExt = ''; | |
210 | $curFields = array(); | |
211 | while ($crmDAO->fetch()) { | |
212 | if ($curTable == '') { | |
213 | $curTable = $crmDAO->table_name; | |
214 | $curExt = $crmDAO->ext; | |
215 | } | |
216 | elseif ($curTable != $crmDAO->table_name) { | |
217 | // dummy DAO | |
218 | $this->_columns[$curTable] = array( | |
219 | 'dao' => 'CRM_Contact_DAO_Contact', | |
220 | 'fields' => $curFields, | |
221 | 'ext' => $curExt, | |
222 | ); | |
223 | $curTable = $crmDAO->table_name; | |
224 | $curExt = $crmDAO->ext; | |
225 | $curFields = array(); | |
226 | } | |
227 | ||
228 | $curFields[$crmDAO->column_name] = array('title' => $crmDAO->label); | |
229 | } | |
230 | if (!empty($curFields)) { | |
231 | // dummy DAO | |
232 | $this->_columns[$curTable] = array( | |
233 | 'dao' => 'CRM_Contact_DAO_Contact', | |
234 | 'fields' => $curFields, | |
235 | 'ext' => $curExt, | |
236 | ); | |
237 | } | |
238 | ||
26cf88b5 | 239 | $this->_genders = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id'); |
6a488035 TO |
240 | |
241 | parent::__construct(); | |
242 | } | |
243 | ||
244 | function preProcess() { | |
245 | parent::preProcess(); | |
246 | } | |
247 | ||
248 | function select() { | |
249 | $select = array(); | |
250 | $this->_columnHeaders = array(); | |
251 | foreach ($this->_columns as $tableName => $table) { | |
252 | if (array_key_exists('fields', $table)) { | |
253 | foreach ($table['fields'] as $fieldName => $field) { | |
8cc574cf | 254 | if (!empty($field['required']) || !empty($this->_params['fields'][$fieldName])) { |
6a488035 TO |
255 | if ($tableName == 'civicrm_email') { |
256 | $this->_emailField = TRUE; | |
257 | } | |
258 | elseif ($tableName == 'civicrm_phone') { | |
259 | $this->_phoneField = TRUE; | |
260 | } | |
261 | ||
262 | $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}"; | |
263 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field); | |
264 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title']; | |
265 | } | |
266 | } | |
267 | } | |
268 | } | |
269 | ||
270 | $this->_select = "SELECT " . implode(', ', $select) . " "; | |
271 | } | |
272 | ||
273 | static function formRule($fields, $files, $self) { | |
274 | $errors = $grouping = array(); | |
275 | return $errors; | |
276 | } | |
277 | ||
278 | function from() { | |
279 | $this->_from = " | |
280 | FROM civicrm_contact {$this->_aliases['civicrm_contact']} | |
2f4c2f5d | 281 | LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']} |
282 | ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id AND | |
6a488035 TO |
283 | {$this->_aliases['civicrm_address']}.is_primary = 1 ) |
284 | LEFT JOIN civicrm_case_contact ccc ON ccc.contact_id = {$this->_aliases['civicrm_contact']}.id | |
285 | LEFT JOIN civicrm_case {$this->_aliases['civicrm_case']} ON {$this->_aliases['civicrm_case']}.id = ccc.case_id | |
286 | LEFT JOIN civicrm_case_activity cca ON cca.case_id = {$this->_aliases['civicrm_case']}.id | |
2f4c2f5d | 287 | LEFT JOIN civicrm_activity {$this->_aliases['civicrm_activity']} ON {$this->_aliases['civicrm_activity']}.id = cca.activity_id |
6a488035 TO |
288 | "; |
289 | ||
290 | foreach ($this->_columns as $t => $c) { | |
291 | if (substr($t, 0, 13) == 'civicrm_value' || substr($t, 0, 12) == 'custom_value') { | |
292 | $this->_from .= " LEFT JOIN $t {$this->_aliases[$t]} ON {$this->_aliases[$t]}.entity_id = "; | |
293 | $this->_from .= ($c['ext'] == 'Activity') ? "{$this->_aliases['civicrm_activity']}.id" : "{$this->_aliases['civicrm_contact']}.id"; | |
294 | } | |
295 | } | |
296 | ||
297 | if ($this->_emailField) { | |
298 | $this->_from .= " | |
2f4c2f5d | 299 | LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']} |
6a488035 TO |
300 | ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND |
301 | {$this->_aliases['civicrm_email']}.is_primary = 1) "; | |
302 | } | |
303 | ||
304 | if ($this->_phoneField) { | |
305 | $this->_from .= " | |
2f4c2f5d | 306 | LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']} |
307 | ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND | |
6a488035 TO |
308 | {$this->_aliases['civicrm_phone']}.is_primary = 1 "; |
309 | } | |
310 | } | |
311 | ||
312 | function where() { | |
313 | $clauses = array(); | |
314 | $this->_having = ''; | |
315 | foreach ($this->_columns as $tableName => $table) { | |
316 | if (array_key_exists('filters', $table)) { | |
317 | foreach ($table['filters'] as $fieldName => $field) { | |
318 | $clause = NULL; | |
319 | if ($field['operatorType'] & CRM_Report_Form::OP_DATE) { | |
320 | $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params); | |
321 | $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params); | |
322 | $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params); | |
323 | ||
54071e11 | 324 | $clause = $this->dateClause($field['dbAlias'], $relative, $from, $to, CRM_Utils_Type::T_DATE); |
6a488035 TO |
325 | } |
326 | else { | |
327 | $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params); | |
328 | if ($op) { | |
329 | // handle special case | |
330 | if ($fieldName == 'case_id_filter') { | |
331 | $choice = CRM_Utils_Array::value("{$fieldName}_value", $this->_params); | |
332 | if ($choice == 1) { | |
333 | $clause = "({$this->_aliases['civicrm_case']}.id Is Not Null)"; | |
334 | } | |
335 | elseif ($choice == 2) { | |
336 | $clause = "({$this->_aliases['civicrm_case']}.id Is Null)"; | |
337 | } | |
338 | } | |
339 | else { | |
340 | $clause = $this->whereClause($field, | |
341 | $op, | |
342 | CRM_Utils_Array::value("{$fieldName}_value", $this->_params), | |
343 | CRM_Utils_Array::value("{$fieldName}_min", $this->_params), | |
344 | CRM_Utils_Array::value("{$fieldName}_max", $this->_params) | |
345 | ); | |
346 | } | |
347 | } | |
348 | } | |
349 | ||
350 | if (!empty($clause)) { | |
351 | $clauses[] = $clause; | |
352 | } | |
353 | } | |
354 | } | |
355 | } | |
356 | ||
357 | $clauses[] = "(({$this->_aliases['civicrm_case']}.is_deleted = 0) OR ({$this->_aliases['civicrm_case']}.is_deleted Is Null))"; | |
358 | $clauses[] = "(({$this->_aliases['civicrm_activity']}.is_deleted = 0) OR ({$this->_aliases['civicrm_activity']}.is_deleted Is Null))"; | |
359 | $clauses[] = "(({$this->_aliases['civicrm_activity']}.is_current_revision = 1) OR ({$this->_aliases['civicrm_activity']}.is_deleted Is Null))"; | |
360 | ||
361 | $this->_where = "WHERE " . implode(' AND ', $clauses); | |
362 | } | |
363 | ||
364 | function groupBy() { | |
365 | $this->_groupBy = "GROUP BY {$this->_aliases['civicrm_contact']}.id, {$this->_aliases['civicrm_case']}.id"; | |
366 | } | |
367 | ||
368 | function orderBy() { | |
369 | $this->_orderBy = "ORDER BY {$this->_aliases['civicrm_contact']}.sort_name, {$this->_aliases['civicrm_contact']}.id, {$this->_aliases['civicrm_case']}.id"; | |
370 | } | |
371 | ||
372 | function postProcess() { | |
373 | ||
374 | $this->beginPostProcess(); | |
375 | ||
376 | $sql = $this->buildQuery(TRUE); | |
377 | //CRM_Core_Error::debug('sql', $sql); | |
378 | $rows = $graphRows = array(); | |
379 | $this->buildRows($sql, $rows); | |
380 | ||
381 | $this->formatDisplay($rows); | |
382 | $this->doTemplateAssignment($rows); | |
383 | $this->endPostProcess($rows); | |
384 | } | |
385 | ||
386 | function alterDisplay(&$rows) { | |
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) { | |
428 | if (substr($k, 0, 13) == 'civicrm_value' || substr($k, 0, 12) == 'custom_value') { | |
429 | if ($r || $r == '0') { | |
430 | if ($newval = $this->getCustomFieldLabel($k, $r)) { | |
431 | $rows[$rowNum][$k] = $newval; | |
432 | } | |
433 | } | |
434 | $entryFound = TRUE; | |
435 | } | |
436 | } | |
437 | ||
438 | // skip looking further in rows, if first row itself doesn't | |
439 | // have the column we need | |
440 | if (!$entryFound) { | |
441 | break; | |
442 | } | |
443 | } | |
444 | } | |
445 | ||
446 | function getCustomFieldLabel($fname, $val) { | |
447 | $query = " | |
448 | SELECT v.label | |
449 | FROM civicrm_custom_group cg INNER JOIN civicrm_custom_field cf ON cg.id = cf.custom_group_id | |
450 | INNER JOIN civicrm_option_group g ON cf.option_group_id = g.id | |
451 | INNER JOIN civicrm_option_value v ON g.id = v.option_group_id | |
452 | WHERE CONCAT(cg.table_name, '_', cf.column_name) = %1 AND v.value = %2"; | |
453 | $params = array(1 => array($fname, 'String'), | |
454 | 2 => array($val, 'String'), | |
455 | ); | |
456 | return CRM_Core_DAO::singleValueQuery($query, $params); | |
457 | } | |
458 | } | |
459 |