}
}
}
+ 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;
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
*