diff --git a/classes/report_types/MysqlReportType.php b/classes/report_types/MysqlReportType.php index 3cd0b191..b1674106 100644 --- a/classes/report_types/MysqlReportType.php +++ b/classes/report_types/MysqlReportType.php @@ -86,16 +86,22 @@ public static function closeConnection(&$report) { } public static function getVariableOptions($params, &$report) { - $query = 'SELECT DISTINCT '.$params['column'].' FROM '.$params['table']; - - if(isset($params['where'])) { - $query .= ' WHERE '.$params['where']; - } + if (isset($params['query'])) { + $query = $params['query']; + } else if (isset($params['column']) && isset($params['table'])) { + $query = 'SELECT DISTINCT ' . $params['column'] . ' FROM ' . $params['table']; + + if (isset($params['where'])) { + $query .= ' WHERE ' . $params['where']; + } - if(isset($params['order']) && in_array($params['order'], array('ASC', 'DESC')) ) { - $query .= ' ORDER BY '.$params['column'].' '.$params['order']; + if (isset($params['order']) && in_array($params['order'], array('ASC', 'DESC'))) { + $query .= ' ORDER BY ' . $params['column'] . ' ' . $params['order']; + } } - + + if(empty($query)) return array(); + $result = mysql_query($query, $report->conn); if(!$result) { @@ -105,11 +111,16 @@ public static function getVariableOptions($params, &$report) { $options = array(); if(isset($params['all'])) $options[] = 'ALL'; - - while($row = mysql_fetch_assoc($result)) { - $options[] = $row[$params['column']]; + + if (isset($params['column'])) { + while ($row = mysql_fetch_assoc($result)) { + $options[] = $row[$params['column']]; + } + } else { + while ($row = mysql_fetch_array($result)) { + $options[] = $row[0]; + } } - return $options; } diff --git a/classes/report_types/PhpReportType.php b/classes/report_types/PhpReportType.php index 99dc3f8d..a823c630 100644 --- a/classes/report_types/PhpReportType.php +++ b/classes/report_types/PhpReportType.php @@ -27,7 +27,33 @@ public static function openConnection(&$report) { public static function closeConnection(&$report) { } - + + public static function getVariableOptions($params, &$report) + { + + if (is_array($params)) { + + // support for dynamic select type using database_options in php reports + // only supporting mysql currently: + + if (stristr($params['type'], 'mysql')) { + + // create a clone of our report for a mysql connection to not pollute + // the original object. This may not be necessary. + $reportMysql = clone $report; + + // Connect to the database since this we are using a different type. + // This also allows for setting the database with the varaible string. + $reportMysql->options['Database'] = isset($params['database']) ? $params['database'] : 'mysql'; + MysqlReportType::openConnection($reportMysql); + + return MysqlReportType::getVariableOptions($params, $reportMysql); + } + } + + return array(); + } + public static function run(&$report) { $eval = "macros as $key=>$value) {