}
}
}
+ if (!isset($this->_columns[$tableName]['metadata'][$fieldName])) {
+ $this->_columns[$tableName]['metadata'][$fieldName] = $this->_columns[$tableName][$fieldGrp][$fieldName];
+ }
}
}
}
*
* @param string $sql
*/
- protected function addToDeveloperTab($sql) {
+ public function addToDeveloperTab($sql) {
if (!CRM_Core_Permission::check('view report sql')) {
return;
}
*/
public function select() {
$select = $this->_selectAliases = array();
+ $this->storeGroupByArray();
foreach ($this->_columns as $tableName => $table) {
if (array_key_exists('fields', $table)) {
}
// include statistics columns only if set
- if (!empty($field['statistics'])) {
+ if (!empty($field['statistics']) && !empty($this->_groupByArray)) {
$select = $this->addStatisticsToSelect($field, $tableName, $fieldName, $select);
}
else {
* Build group by clause.
*/
public function groupBy() {
- if (!empty($this->_params['group_bys']) &&
- is_array($this->_params['group_bys'])
- ) {
- foreach ($this->_columns as $tableName => $table) {
- if (array_key_exists('group_bys', $table)) {
- foreach ($table['group_bys'] as $fieldName => $field) {
- if (!empty($this->_params['group_bys'][$fieldName])) {
- $this->_groupByArray[] = $field['dbAlias'];
- }
- }
- }
- }
- }
+ $this->storeGroupByArray();
if (!empty($this->_groupByArray)) {
$this->_groupBy = CRM_Contact_BAO_Query::getGroupByFromSelectColumns($this->_selectClauses, $this->_groupByArray);
$group->find();
$smartGroups = array();
while ($group->fetch()) {
- if (in_array($group->id, $this->_params['gid_value']) &&
+ if (in_array($group->id, (array) $this->_params['gid_value']) &&
$group->saved_search_id
) {
$smartGroups[] = $group->id;
'dao' => 'CRM_Core_DAO_Phone',
'fields' => array(
$options['prefix'] . 'phone' => array(
- 'title' => ts($options['prefix_label'] . 'Phone'),
+ 'title' => $options['prefix_label'] . ts('Phone'),
'name' => 'phone',
),
),
$spec = array(
$options['prefix'] . 'display_name' => array(
'name' => 'display_name',
- 'title' => ts($options['prefix_label'] . 'Contact Name'),
+ 'title' => $options['prefix_label'] . ts('Contact Name'),
'is_fields' => TRUE,
),
$options['prefix'] . 'sort_name' => array(
'name' => 'sort_name',
- 'title' => ts($options['prefix_label'] . 'Contact Name (in sort format)'),
+ 'title' => $options['prefix_label'] . ts('Contact Name (in sort format)'),
'is_fields' => TRUE,
'is_filters' => TRUE,
'is_order_bys' => TRUE,
),
$options['prefix'] . 'id' => array(
'name' => 'id',
- 'title' => ts($options['prefix_label'] . 'Contact ID'),
+ 'title' => $options['prefix_label'] . ts('Contact ID'),
'alter_display' => 'alterContactID',
'type' => CRM_Utils_Type::T_INT,
'is_order_bys' => TRUE,
),
$options['prefix'] . 'external_identifier' => array(
'name' => 'external_identifier',
- 'title' => ts($options['prefix_label'] . 'External ID'),
+ 'title' => $options['prefix_label'] . ts('External ID'),
'type' => CRM_Utils_Type::T_INT,
'is_fields' => TRUE,
),
$options['prefix'] . 'contact_type' => array(
- 'title' => ts($options['prefix_label'] . 'Contact Type'),
+ 'title' => $options['prefix_label'] . ts('Contact Type'),
'name' => 'contact_type',
'operatorType' => CRM_Report_Form::OP_MULTISELECT,
'options' => CRM_Contact_BAO_Contact::buildOptions('contact_type'),
'is_group_bys' => TRUE,
),
$options['prefix'] . 'contact_sub_type' => array(
- 'title' => ts($options['prefix_label'] . 'Contact Sub Type'),
+ 'title' => $options['prefix_label'] . ts('Contact Sub Type'),
'name' => 'contact_sub_type',
'operatorType' => CRM_Report_Form::OP_MULTISELECT,
'options' => CRM_Contact_BAO_Contact::buildOptions('contact_sub_type'),
'is_group_bys' => TRUE,
),
$options['prefix'] . 'is_deleted' => array(
- 'title' => ts($options['prefix_label'] . 'Is deleted'),
+ 'title' => $options['prefix_label'] . ts('Is deleted'),
'name' => 'is_deleted',
'type' => CRM_Utils_Type::T_BOOLEAN,
'is_fields' => FALSE,
$individualFields = array(
$options['prefix'] . 'first_name' => array(
'name' => 'first_name',
- 'title' => ts($options['prefix_label'] . 'First Name'),
+ 'title' => $options['prefix_label'] . ts('First Name'),
'is_fields' => TRUE,
'is_filters' => TRUE,
'is_order_bys' => TRUE,
),
$options['prefix'] . 'middle_name' => array(
'name' => 'middle_name',
- 'title' => ts($options['prefix_label'] . 'Middle Name'),
+ 'title' => $options['prefix_label'] . ts('Middle Name'),
'is_fields' => TRUE,
),
$options['prefix'] . 'last_name' => array(
'name' => 'last_name',
- 'title' => ts($options['prefix_label'] . 'Last Name'),
+ 'title' => $options['prefix_label'] . ts('Last Name'),
'default_order' => 'ASC',
'is_fields' => TRUE,
),
$options['prefix'] . 'nick_name' => array(
'name' => 'nick_name',
- 'title' => ts($options['prefix_label'] . 'Nick Name'),
+ 'title' => $options['prefix_label'] . ts('Nick Name'),
'is_fields' => TRUE,
),
$options['prefix'] . 'gender_id' => array(
'name' => 'gender_id',
- 'title' => ts($options['prefix_label'] . 'Gender'),
+ 'title' => $options['prefix_label'] . ts('Gender'),
'options' => CRM_Contact_BAO_Contact::buildOptions('gender_id'),
'operatorType' => CRM_Report_Form::OP_MULTISELECT,
'alter_display' => 'alterGenderID',
'is_filters' => TRUE,
),
'birth_date' => array(
- 'title' => ts($options['prefix_label'] . 'Birth Date'),
+ 'title' => $options['prefix_label'] . ts('Birth Date'),
'operatorType' => CRM_Report_Form::OP_DATE,
'type' => CRM_Utils_Type::T_DATE,
'is_fields' => TRUE,
'is_filters' => TRUE,
),
'age' => array(
- 'title' => ts($options['prefix_label'] . 'Age'),
+ 'title' => $options['prefix_label'] . ts('Age'),
'dbAlias' => 'TIMESTAMPDIFF(YEAR, ' . $tableAlias . '.birth_date, CURDATE())',
'type' => CRM_Utils_Type::T_INT,
'is_fields' => TRUE,
),
$options['prefix'] . 'is_deceased' => array(
- 'title' => ts($options['prefix_label'] . 'Is deceased'),
+ 'title' => $options['prefix_label'] . ts('Is deceased'),
'name' => 'is_deceased',
'type' => CRM_Utils_Type::T_BOOLEAN,
'is_fields' => FALSE,
return $columns;
}
+ /**
+ * Store group bys into array - so we can check elsewhere what is grouped.
+ */
+ protected function storeGroupByArray() {
+
+ if (CRM_Utils_Array::value('group_bys', $this->_params) &&
+ is_array($this->_params['group_bys']) &&
+ !empty($this->_params['group_bys'])
+ ) {
+ foreach ($this->_columns as $tableName => $table) {
+ $table = $this->_columns[$tableName];
+ if (array_key_exists('group_bys', $table)) {
+ foreach ($table['group_bys'] as $fieldName => $fieldData) {
+ $field = $this->_columns[$tableName]['metadata'][$fieldName];
+ if (!empty($this->_params['group_bys'][$fieldName])) {
+ if (!empty($field['chart'])) {
+ $this->assign('chartSupported', TRUE);
+ }
+
+ if (!empty($table['group_bys'][$fieldName]['frequency']) &&
+ !empty($this->_params['group_bys_freq'][$fieldName])
+ ) {
+
+ switch ($this->_params['group_bys_freq'][$fieldName]) {
+ case 'FISCALYEAR':
+ $this->_groupByArray[$tableName . '_' . $fieldName . '_start'] = self::fiscalYearOffset($field['dbAlias']);
+
+ case 'YEAR':
+ $this->_groupByArray[$tableName . '_' . $fieldName . '_start'] = " {$this->_params['group_bys_freq'][$fieldName]}({$field['dbAlias']})";
+
+ default:
+ $this->_groupByArray[$tableName . '_' . $fieldName . '_start'] = "EXTRACT(YEAR_{$this->_params['group_bys_freq'][$fieldName]} FROM {$field['dbAlias']})";
+
+ }
+ }
+ else {
+ if (!in_array($field['dbAlias'], $this->_groupByArray)) {
+ $this->_groupByArray[$tableName . '_' . $fieldName] = $field['dbAlias'];
+ }
+ }
+ }
+ }
+
+ }
+ }
+ }
+ }
+
/**
* @param $options
*