3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2015
35 class CRM_Report_Form_Case_Demographics
extends CRM_Report_Form
{
37 protected $_summary = NULL;
39 protected $_emailField = FALSE;
41 protected $_phoneField = FALSE;
47 public function __construct() {
48 $this->_columns
= array(
49 'civicrm_contact' => array(
50 'dao' => 'CRM_Contact_DAO_Contact',
53 'title' => ts('Contact Name'),
58 'title' => ts('Gender'),
61 'birth_date' => array(
62 'title' => ts('Birthdate'),
72 'title' => ts('Contact Name'),
73 'operatorType' => CRM_Report_Form
::OP_STRING
,
75 'contact_type' => array(
76 'title' => ts('Contact Type'),
77 'operatorType' => CRM_Report_Form
::OP_SELECT
,
80 'Individual' => ts('Individual'),
81 'Organization' => ts('Organization'),
82 'Household' => ts('Household'),
84 'default' => 'Individual',
87 'title' => ts('Contact ID'),
91 'grouping' => 'contact-fields',
93 'civicrm_email' => array(
94 'dao' => 'CRM_Core_DAO_Email',
97 'title' => ts('Email'),
101 'grouping' => 'contact-fields',
103 'civicrm_address' => array(
104 'dao' => 'CRM_Core_DAO_Address',
105 'grouping' => 'contact-fields',
107 'street_address' => array('default' => FALSE),
108 'city' => array('default' => TRUE),
109 'postal_code' => NULL,
110 'state_province_id' => array(
111 'title' => ts('State/Province'),
113 'country_id' => array(
114 'title' => ts('Country'),
120 'country_id' => array( 'title' => ts( 'Country' ),
121 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
122 'options' => CRM_Core_PseudoConstant::country( ),
124 'state_province_id' => array( 'title' => ts( 'State/Province' ),
125 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
126 'options' => CRM_Core_PseudoConstant::stateProvince( ), ),
130 'civicrm_phone' => array(
131 'dao' => 'CRM_Core_DAO_Phone',
132 'fields' => array('phone' => NULL),
133 'grouping' => 'contact-fields',
135 'civicrm_activity' => array(
136 'dao' => 'CRM_Activity_DAO_Activity',
139 'title' => ts('Activity ID'),
140 'no_display' => TRUE,
145 'civicrm_case' => array(
146 'dao' => 'CRM_Case_DAO_Case',
149 'title' => ts('Case ID'),
152 'start_date' => array(
153 'title' => ts('Case Start'),
157 'title' => ts('Case End'),
162 'case_id_filter' => array(
164 'title' => ts('Cases?'),
165 'operatorType' => CRM_Report_Form
::OP_SELECT
,
167 1 => ts('Exclude non-case'),
168 2 => ts('Exclude cases'),
169 3 => ts('Include Both'),
173 'start_date' => array(
174 'title' => ts('Case Start'),
175 'operatorType' => CRM_Report_Form
::OP_DATE
,
178 'title' => ts('Case End'),
179 'operatorType' => CRM_Report_Form
::OP_DATE
,
185 $this->_groupFilter
= TRUE;
186 $this->_tagFilter
= TRUE;
188 $open_case_val = CRM_Core_OptionGroup
::getValue('activity_type', 'Open Case', 'name');
189 $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
190 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");
193 $curFields = array();
194 while ($crmDAO->fetch()) {
195 if ($curTable == '') {
196 $curTable = $crmDAO->table_name
;
197 $curExt = $crmDAO->ext
;
199 elseif ($curTable != $crmDAO->table_name
) {
201 $this->_columns
[$curTable] = array(
202 'dao' => 'CRM_Contact_DAO_Contact',
203 'fields' => $curFields,
206 $curTable = $crmDAO->table_name
;
207 $curExt = $crmDAO->ext
;
208 $curFields = array();
211 $curFields[$crmDAO->column_name
] = array('title' => $crmDAO->label
);
213 if (!empty($curFields)) {
215 $this->_columns
[$curTable] = array(
216 'dao' => 'CRM_Contact_DAO_Contact',
217 'fields' => $curFields,
222 $this->_genders
= CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'gender_id');
224 parent
::__construct();
227 public function preProcess() {
228 parent
::preProcess();
231 public function select() {
233 $this->_columnHeaders
= array();
234 foreach ($this->_columns
as $tableName => $table) {
235 if (array_key_exists('fields', $table)) {
236 foreach ($table['fields'] as $fieldName => $field) {
237 if (!empty($field['required']) ||
238 !empty($this->_params
['fields'][$fieldName])
240 if ($tableName == 'civicrm_email') {
241 $this->_emailField
= TRUE;
243 elseif ($tableName == 'civicrm_phone') {
244 $this->_phoneField
= TRUE;
247 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
248 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
249 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'];
255 $this->_select
= "SELECT " . implode(', ', $select) . " ";
265 public static function formRule($fields, $files, $self) {
266 $errors = $grouping = array();
270 public function from() {
272 FROM civicrm_contact {$this->_aliases['civicrm_contact']}
273 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
274 ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id AND
275 {$this->_aliases['civicrm_address']}.is_primary = 1 )
276 LEFT JOIN civicrm_case_contact ccc ON ccc.contact_id = {$this->_aliases['civicrm_contact']}.id
277 LEFT JOIN civicrm_case {$this->_aliases['civicrm_case']} ON {$this->_aliases['civicrm_case']}.id = ccc.case_id
278 LEFT JOIN civicrm_case_activity cca ON cca.case_id = {$this->_aliases['civicrm_case']}.id
279 LEFT JOIN civicrm_activity {$this->_aliases['civicrm_activity']} ON {$this->_aliases['civicrm_activity']}.id = cca.activity_id
282 foreach ($this->_columns
as $t => $c) {
283 if (substr($t, 0, 13) == 'civicrm_value' ||
284 substr($t, 0, 12) == 'custom_value'
286 $this->_from
.= " LEFT JOIN $t {$this->_aliases[$t]} ON {$this->_aliases[$t]}.entity_id = ";
287 $this->_from
.= ($c['ext'] ==
288 'Activity') ?
"{$this->_aliases['civicrm_activity']}.id" : "{$this->_aliases['civicrm_contact']}.id";
292 if ($this->_emailField
) {
294 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
295 ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND
296 {$this->_aliases['civicrm_email']}.is_primary = 1) ";
299 if ($this->_phoneField
) {
301 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
302 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND
303 {$this->_aliases['civicrm_phone']}.is_primary = 1 ";
307 public function where() {
310 foreach ($this->_columns
as $tableName => $table) {
311 if (array_key_exists('filters', $table)) {
312 foreach ($table['filters'] as $fieldName => $field) {
314 if ($field['operatorType'] & CRM_Report_Form
::OP_DATE
) {
315 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
316 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
317 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
319 $clause = $this->dateClause($field['dbAlias'], $relative, $from, $to, CRM_Utils_Type
::T_DATE
);
322 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
324 // handle special case
325 if ($fieldName == 'case_id_filter') {
326 $choice = CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
);
328 $clause = "({$this->_aliases['civicrm_case']}.id Is Not Null)";
330 elseif ($choice == 2) {
331 $clause = "({$this->_aliases['civicrm_case']}.id Is Null)";
335 $clause = $this->whereClause($field,
337 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
338 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
339 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
345 if (!empty($clause)) {
346 $clauses[] = $clause;
352 $clauses[] = "(({$this->_aliases['civicrm_case']}.is_deleted = 0) OR ({$this->_aliases['civicrm_case']}.is_deleted Is Null))";
353 $clauses[] = "(({$this->_aliases['civicrm_activity']}.is_deleted = 0) OR ({$this->_aliases['civicrm_activity']}.is_deleted Is Null))";
354 $clauses[] = "(({$this->_aliases['civicrm_activity']}.is_current_revision = 1) OR ({$this->_aliases['civicrm_activity']}.is_deleted Is Null))";
356 $this->_where
= "WHERE " . implode(' AND ', $clauses);
359 public function groupBy() {
360 $this->_groupBy
= "GROUP BY {$this->_aliases['civicrm_contact']}.id, {$this->_aliases['civicrm_case']}.id";
363 public function orderBy() {
364 $this->_orderBy
= "ORDER BY {$this->_aliases['civicrm_contact']}.sort_name, {$this->_aliases['civicrm_contact']}.id, {$this->_aliases['civicrm_case']}.id";
367 public function postProcess() {
369 $this->beginPostProcess();
371 $sql = $this->buildQuery(TRUE);
372 $rows = $graphRows = array();
373 $this->buildRows($sql, $rows);
375 $this->formatDisplay($rows);
376 $this->doTemplateAssignment($rows);
377 $this->endPostProcess($rows);
381 * Alter display of rows.
383 * Iterate through the rows retrieved via SQL and make changes for display purposes,
384 * such as rendering contacts as links.
387 * Rows generated by SQL, with an array for each row.
389 public function alterDisplay(&$rows) {
391 foreach ($rows as $rowNum => $row) {
392 // make count columns point to detail report
393 // convert display name to links
394 if (array_key_exists('civicrm_contact_sort_name', $row) &&
395 array_key_exists('civicrm_contact_id', $row)
397 $url = CRM_Utils_System
::url('civicrm/contact/view',
398 'reset=1&cid=' . $row['civicrm_contact_id'],
401 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
402 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contact details for this contact.");
407 if (array_key_exists('civicrm_contact_gender_id', $row)) {
408 if ($value = $row['civicrm_contact_gender_id']) {
409 $rows[$rowNum]['civicrm_contact_gender_id'] = $this->_genders
[$value];
415 if (array_key_exists('civicrm_address_country_id', $row)) {
416 if ($value = $row['civicrm_address_country_id']) {
417 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant
::country($value, FALSE);
421 if (array_key_exists('civicrm_address_state_province_id', $row)) {
422 if ($value = $row['civicrm_address_state_province_id']) {
423 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant
::stateProvince($value, FALSE);
428 // handle custom fields
429 foreach ($row as $k => $r) {
430 if (substr($k, 0, 13) == 'civicrm_value' ||
431 substr($k, 0, 12) == 'custom_value'
433 if ($r ||
$r == '0') {
434 if ($newval = $this->getCustomFieldLabel($k, $r)) {
435 $rows[$rowNum][$k] = $newval;
442 // skip looking further in rows, if first row itself doesn't
443 // have the column we need
451 * @param string $fname
454 * @return null|string
456 public function getCustomFieldLabel($fname, $val) {
459 FROM civicrm_custom_group cg INNER JOIN civicrm_custom_field cf ON cg.id = cf.custom_group_id
460 INNER JOIN civicrm_option_group g ON cf.option_group_id = g.id
461 INNER JOIN civicrm_option_value v ON g.id = v.option_group_id
462 WHERE CONCAT(cg.table_name, '_', cf.column_name) = %1 AND v.value = %2";
464 1 => array($fname, 'String'),
465 2 => array($val, 'String'),
467 return CRM_Core_DAO
::singleValueQuery($query, $params);