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