4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.3 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
32 * @copyright CiviCRM LLC (c) 2004-2013
36 class CRM_Report_Form_Case_Demographics
extends CRM_Report_Form
{
38 protected $_summary = NULL;
40 protected $_emailField = FALSE;
42 protected $_phoneField = FALSE;
43 function __construct() {
44 $this->_columns
= array(
47 'dao' => 'CRM_Contact_DAO_Contact',
51 array('title' => ts('Contact Name'),
56 array('title' => ts('Gender'),
60 array('title' => ts('Birthdate'),
72 array('title' => ts('Contact Name')),
74 array('title' => ts('Contact Type'),
75 'operatorType' => CRM_Report_Form
::OP_SELECT
,
76 'options' => array('' => ts('-select-'),
77 'Individual' => ts('Individual'),
78 'Organization' => ts('Organization'),
79 'Household' => ts('Household'),
81 'default' => 'Individual',
84 array('title' => ts('Contact ID'),
88 'grouping' => 'contact-fields',
92 'dao' => 'CRM_Core_DAO_Email',
96 array('title' => ts('Email'),
100 'grouping' => 'contact-fields',
104 'dao' => 'CRM_Core_DAO_Address',
105 'grouping' => 'contact-fields',
109 array('default' => FALSE),
111 array('default' => TRUE),
112 'postal_code' => NULL,
113 'state_province_id' =>
114 array('title' => ts('State/Province'),
117 array('title' => ts('Country'),
125 array( 'title' => ts( 'Country' ),
126 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
127 'options' => CRM_Core_PseudoConstant::country( ),
129 'state_province_id' =>
130 array( 'title' => ts( 'State / Province' ),
131 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
132 'options' => CRM_Core_PseudoConstant::stateProvince( ), ),
138 'dao' => 'CRM_Core_DAO_Phone',
140 array('phone' => NULL),
141 'grouping' => 'contact-fields',
143 'civicrm_activity' =>
145 'dao' => 'CRM_Activity_DAO_Activity',
147 array('id' => array('title' => ts('Activity ID'),
148 'no_display' => TRUE,
155 'dao' => 'CRM_Case_DAO_Case',
157 array('id' => array('title' => ts('Case ID'),
160 'start_date' => array('title' => ts('Case Start'),
163 'end_date' => array('title' => ts('Case End'),
169 'case_id_filter' => array('name' => 'id',
170 'title' => ts('Cases?'),
171 'operatorType' => CRM_Report_Form
::OP_SELECT
,
172 'options' => array(1 => ts('Exclude non-case'), 2 => ts('Exclude cases'), 3 => ts('Include Both')),
175 'start_date' => array('title' => ts('Case Start'),
176 'operatorType' => CRM_Report_Form
::OP_DATE
,
178 'end_date' => array('title' => ts('Case End'),
179 'operatorType' => CRM_Report_Form
::OP_DATE
,
185 'dao' => 'CRM_Contact_DAO_Group',
191 'name' => 'group_id',
192 'title' => ts('Group'),
193 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
195 'options' => CRM_Core_PseudoConstant
::group(),
201 $this->_tagFilter
= TRUE;
203 $open_case_val = CRM_Core_OptionGroup
::getValue('activity_type', 'Open Case', 'name');
204 $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
205 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 $curFields = array();
209 while ($crmDAO->fetch()) {
210 if ($curTable == '') {
211 $curTable = $crmDAO->table_name
;
212 $curExt = $crmDAO->ext
;
214 elseif ($curTable != $crmDAO->table_name
) {
216 $this->_columns
[$curTable] = array(
217 'dao' => 'CRM_Contact_DAO_Contact',
218 'fields' => $curFields,
221 $curTable = $crmDAO->table_name
;
222 $curExt = $crmDAO->ext
;
223 $curFields = array();
226 $curFields[$crmDAO->column_name
] = array('title' => $crmDAO->label
);
228 if (!empty($curFields)) {
230 $this->_columns
[$curTable] = array(
231 'dao' => 'CRM_Contact_DAO_Contact',
232 'fields' => $curFields,
237 $this->_genders
= CRM_Core_PseudoConstant
::gender();
239 parent
::__construct();
242 function preProcess() {
243 parent
::preProcess();
248 $this->_columnHeaders
= array();
249 foreach ($this->_columns
as $tableName => $table) {
250 if (array_key_exists('fields', $table)) {
251 foreach ($table['fields'] as $fieldName => $field) {
252 if (CRM_Utils_Array
::value('required', $field) ||
253 CRM_Utils_Array
::value($fieldName, $this->_params
['fields'])
255 if ($tableName == 'civicrm_email') {
256 $this->_emailField
= TRUE;
258 elseif ($tableName == 'civicrm_phone') {
259 $this->_phoneField
= TRUE;
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'];
270 $this->_select
= "SELECT " . implode(', ', $select) . " ";
273 static function formRule($fields, $files, $self) {
274 $errors = $grouping = array();
280 FROM civicrm_contact {$this->_aliases['civicrm_contact']}
281 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
282 ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id AND
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
287 LEFT JOIN civicrm_activity {$this->_aliases['civicrm_activity']} ON {$this->_aliases['civicrm_activity']}.id = cca.activity_id
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";
297 if ($this->_emailField
) {
299 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
300 ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND
301 {$this->_aliases['civicrm_email']}.is_primary = 1) ";
304 if ($this->_phoneField
) {
306 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
307 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND
308 {$this->_aliases['civicrm_phone']}.is_primary = 1 ";
315 foreach ($this->_columns
as $tableName => $table) {
316 if (array_key_exists('filters', $table)) {
317 foreach ($table['filters'] as $fieldName => $field) {
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
);
324 $clause = $this->dateClause($field['dbAlias'], $relative, $from, $to, CRM_Utils_type
::T_DATE
);
327 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
329 // handle special case
330 if ($fieldName == 'case_id_filter') {
331 $choice = CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
);
333 $clause = "({$this->_aliases['civicrm_case']}.id Is Not Null)";
335 elseif ($choice == 2) {
336 $clause = "({$this->_aliases['civicrm_case']}.id Is Null)";
340 $clause = $this->whereClause($field,
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
)
350 if (!empty($clause)) {
351 $clauses[] = $clause;
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))";
361 $this->_where
= "WHERE " . implode(' AND ', $clauses);
365 $this->_groupBy
= "GROUP BY {$this->_aliases['civicrm_contact']}.id, {$this->_aliases['civicrm_case']}.id";
369 $this->_orderBy
= "ORDER BY {$this->_aliases['civicrm_contact']}.sort_name, {$this->_aliases['civicrm_contact']}.id, {$this->_aliases['civicrm_case']}.id";
372 function postProcess() {
374 $this->beginPostProcess();
376 $sql = $this->buildQuery(TRUE);
377 //CRM_Core_Error::debug('sql', $sql);
378 $rows = $graphRows = array();
379 $this->buildRows($sql, $rows);
381 $this->formatDisplay($rows);
382 $this->doTemplateAssignment($rows);
383 $this->endPostProcess($rows);
386 function alterDisplay(&$rows) {
387 // custom code to alter rows
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)
395 $url = CRM_Utils_System
::url('civicrm/contact/view',
396 'reset=1&cid=' . $row['civicrm_contact_id'],
399 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
400 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contact details for this contact.");
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];
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);
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);
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;
438 // skip looking further in rows, if first row itself doesn't
439 // have the column we need
446 function getCustomFieldLabel($fname, $val) {
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'),
456 return CRM_Core_DAO
::singleValueQuery($query, $params);