From f2c72fcc2448dbc7868e292f0ea25da7fe181242 Mon Sep 17 00:00:00 2001 From: graham Date: Fri, 31 Jul 2015 13:16:04 -0700 Subject: [PATCH 1/4] Adding the ability to have variable parameters (select) that are pulled from mysql in Php report types. --- classes/report_types/PhpReportType.php | 151 +++++++++++++++++-------- 1 file changed, 106 insertions(+), 45 deletions(-) diff --git a/classes/report_types/PhpReportType.php b/classes/report_types/PhpReportType.php index 99dc3f8d..df2d88c2 100644 --- a/classes/report_types/PhpReportType.php +++ b/classes/report_types/PhpReportType.php @@ -1,85 +1,146 @@ raw_query = "report."\n".trim($report->raw_query); - +class PhpReportType extends ReportTypeBase +{ + public static function init(&$report) + { + $report->raw_query = "report . "\n" . trim($report->raw_query); + //if there are any included reports, add it to the top of the raw query - if(isset($report->options['Includes'])) { + if (isset($report->options['Includes'])) { $included_code = ''; - foreach($report->options['Includes'] as &$included_report) { - $included_code .= "\n".trim($included_report->raw_query).""; + foreach ($report->options['Includes'] as &$included_report) { + $included_code .= "\n" . trim($included_report->raw_query) . ""; } - - if($included_code) $included_code.= "\n"; - + + if ($included_code) $included_code .= "\n"; + $report->raw_query = $included_code . $report->raw_query; - + //make sure the raw query has a closing PHP tag at the end //this makes sure it will play nice as an included report - if(!preg_match('/\?>\s*$/',$report->raw_query)) $report->raw_query .= "\n?>"; + if (!preg_match('/\?>\s*$/', $report->raw_query)) $report->raw_query .= "\n?>"; } } - - public static function openConnection(&$report) { - + + public static function openConnection(&$report) + { + } - - public static function closeConnection(&$report) { - + + public static function closeConnection(&$report) + { + } - - public static function run(&$report) { + + public static function run(&$report) + { $eval = "macros as $key=>$value) { - $value = var_export($value,true); - - $eval .= "\n".'$'.$key.' = '.$value.';'; + foreach ($report->macros as $key => $value) { + $value = var_export($value, true); + + $eval .= "\n" . '$' . $key . ' = ' . $value . ';'; } - $eval .= "\n?>".$report->raw_query; - + $eval .= "\n?>" . $report->raw_query; + $config = PhpReports::$config; - + //store in both $database and $environment for backwards compatibility $database = PhpReports::$config['environments'][$report->options['Environment']]; $environment = $database; - + $report->options['Query'] = $report->raw_query; - - $parts = preg_split('/<\?php \/\*(BEGIN|END) (INCLUDED REPORT|REPORT MACROS)\*\/ \?>/',$eval); + + $parts = preg_split('/<\?php \/\*(BEGIN|END) (INCLUDED REPORT|REPORT MACROS)\*\/ \?>/', $eval); $report->options['Query_Formatted'] = ''; $code = htmlentities(trim(array_pop($parts))); $linenum = 1; - foreach($parts as $part) { - if(!trim($part)) continue; + foreach ($parts as $part) { + if (!trim($part)) continue; //get name of report - $name = preg_match("|//REPORT: ([^\n]+)\n|",$part,$matches); + $name = preg_match("|//REPORT: ([^\n]+)\n|", $part, $matches); - if(!$matches) { + if (!$matches) { $name = "Variables"; - } - else { + } else { $name = $matches[1]; } - $report->options['Query_Formatted'] .= '
'; - $report->options['Query_Formatted'] .= "
".htmlentities(trim($part))."
"; + $report->options['Query_Formatted'] .= '
'; + $report->options['Query_Formatted'] .= "
" . htmlentities(trim($part)) . "
"; $report->options['Query_Formatted'] .= "
"; - $linenum += count(explode("\n",trim($part))); + $linenum += count(explode("\n", trim($part))); } - - $report->options['Query_Formatted'] .= '
'.$code.'
'; + + $report->options['Query_Formatted'] .= '
' . $code . '
'; ob_start(); - eval('?>'.$eval); + eval('?>' . $eval); $result = ob_get_contents(); ob_end_clean(); $result = trim($result); - + $json = json_decode($result, true); - if($json === NULL) throw new Exception($result); - + if ($json === NULL) throw new Exception($result); + return $json; } + + 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')) { + + 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($query)) return array(); + + // open a connection to mysql: + + $reportMysql = $report; + $reportMysql->options['Database'] = isset($params['database']) ? $params['database'] : 'mysql'; + MysqlReportType::openConnection($reportMysql); + $result = mysql_query($query, $reportMysql->conn); + if (!$result) { + throw new Exception("Unable to get variable options: " . mysql_error()); + } + + $options = array(); + + if (isset($params['all'])) $options[] = 'ALL'; + + 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; + } + } + + return array(); + } } + From b5fb2b9c2bf7db18a2609753205e527ba3466b53 Mon Sep 17 00:00:00 2001 From: graham Date: Fri, 31 Jul 2015 13:21:59 -0700 Subject: [PATCH 2/4] Added the ability to do arbitrary queries when using database_options in mysql. --- classes/report_types/MysqlReportType.php | 35 ++++++++++++++++-------- 1 file changed, 23 insertions(+), 12 deletions(-) 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; } From 871b73f5ff4ce1511b0166e2dadb2c6d335d7eae Mon Sep 17 00:00:00 2001 From: graham Date: Fri, 31 Jul 2015 14:42:21 -0700 Subject: [PATCH 3/4] Cleaned up whitespaces. --- classes/report_types/PhpReportType.php | 151 ++++++++++++------------- 1 file changed, 73 insertions(+), 78 deletions(-) diff --git a/classes/report_types/PhpReportType.php b/classes/report_types/PhpReportType.php index df2d88c2..a9d076c5 100644 --- a/classes/report_types/PhpReportType.php +++ b/classes/report_types/PhpReportType.php @@ -1,90 +1,31 @@ raw_query = "report . "\n" . trim($report->raw_query); - +class PhpReportType extends ReportTypeBase { + public static function init(&$report) { + $report->raw_query = "report."\n".trim($report->raw_query); + //if there are any included reports, add it to the top of the raw query - if (isset($report->options['Includes'])) { + if(isset($report->options['Includes'])) { $included_code = ''; - foreach ($report->options['Includes'] as &$included_report) { - $included_code .= "\n" . trim($included_report->raw_query) . ""; + foreach($report->options['Includes'] as &$included_report) { + $included_code .= "\n".trim($included_report->raw_query).""; } - - if ($included_code) $included_code .= "\n"; - + + if($included_code) $included_code.= "\n"; + $report->raw_query = $included_code . $report->raw_query; - + //make sure the raw query has a closing PHP tag at the end //this makes sure it will play nice as an included report - if (!preg_match('/\?>\s*$/', $report->raw_query)) $report->raw_query .= "\n?>"; + if(!preg_match('/\?>\s*$/',$report->raw_query)) $report->raw_query .= "\n?>"; } } - - public static function openConnection(&$report) - { - - } - - public static function closeConnection(&$report) - { - + + public static function openConnection(&$report) { + } - - public static function run(&$report) - { - $eval = "macros as $key => $value) { - $value = var_export($value, true); - - $eval .= "\n" . '$' . $key . ' = ' . $value . ';'; - } - $eval .= "\n?>" . $report->raw_query; - - $config = PhpReports::$config; - - //store in both $database and $environment for backwards compatibility - $database = PhpReports::$config['environments'][$report->options['Environment']]; - $environment = $database; - - $report->options['Query'] = $report->raw_query; - - $parts = preg_split('/<\?php \/\*(BEGIN|END) (INCLUDED REPORT|REPORT MACROS)\*\/ \?>/', $eval); - $report->options['Query_Formatted'] = ''; - $code = htmlentities(trim(array_pop($parts))); - $linenum = 1; - foreach ($parts as $part) { - if (!trim($part)) continue; - - //get name of report - $name = preg_match("|//REPORT: ([^\n]+)\n|", $part, $matches); - - if (!$matches) { - $name = "Variables"; - } else { - $name = $matches[1]; - } - - $report->options['Query_Formatted'] .= '
'; - $report->options['Query_Formatted'] .= "
" . htmlentities(trim($part)) . "
"; - $report->options['Query_Formatted'] .= "
"; - $linenum += count(explode("\n", trim($part))); - } - - $report->options['Query_Formatted'] .= '
' . $code . '
'; - - ob_start(); - eval('?>' . $eval); - $result = ob_get_contents(); - ob_end_clean(); - - $result = trim($result); - - $json = json_decode($result, true); - if ($json === NULL) throw new Exception($result); - - return $json; + + public static function closeConnection(&$report) { + } public static function getVariableOptions($params, &$report) @@ -142,5 +83,59 @@ public static function getVariableOptions($params, &$report) return array(); } -} + public static function run(&$report) { + $eval = "macros as $key=>$value) { + $value = var_export($value,true); + + $eval .= "\n".'$'.$key.' = '.$value.';'; + } + $eval .= "\n?>".$report->raw_query; + + $config = PhpReports::$config; + + //store in both $database and $environment for backwards compatibility + $database = PhpReports::$config['environments'][$report->options['Environment']]; + $environment = $database; + + $report->options['Query'] = $report->raw_query; + + $parts = preg_split('/<\?php \/\*(BEGIN|END) (INCLUDED REPORT|REPORT MACROS)\*\/ \?>/',$eval); + $report->options['Query_Formatted'] = ''; + $code = htmlentities(trim(array_pop($parts))); + $linenum = 1; + foreach($parts as $part) { + if(!trim($part)) continue; + + //get name of report + $name = preg_match("|//REPORT: ([^\n]+)\n|",$part,$matches); + + if(!$matches) { + $name = "Variables"; + } + else { + $name = $matches[1]; + } + + $report->options['Query_Formatted'] .= '
'; + $report->options['Query_Formatted'] .= "
".htmlentities(trim($part))."
"; + $report->options['Query_Formatted'] .= "
"; + $linenum += count(explode("\n",trim($part))); + } + + $report->options['Query_Formatted'] .= '
'.$code.'
'; + + ob_start(); + eval('?>'.$eval); + $result = ob_get_contents(); + ob_end_clean(); + + $result = trim($result); + + $json = json_decode($result, true); + if($json === NULL) throw new Exception($result); + + return $json; + } +} From 171ebf7223bd0bb416336d09e6b1ebcf570ee51b Mon Sep 17 00:00:00 2001 From: graham Date: Mon, 3 Aug 2015 09:14:18 -0700 Subject: [PATCH 4/4] Simplified logic around variable mysql options in reports of type php. --- classes/report_types/PhpReportType.php | 42 ++++---------------------- 1 file changed, 6 insertions(+), 36 deletions(-) diff --git a/classes/report_types/PhpReportType.php b/classes/report_types/PhpReportType.php index a9d076c5..a823c630 100644 --- a/classes/report_types/PhpReportType.php +++ b/classes/report_types/PhpReportType.php @@ -38,46 +38,16 @@ public static function getVariableOptions($params, &$report) if (stristr($params['type'], 'mysql')) { - if (isset($params['query'])) { - $query = $params['query']; - } else if (isset($params['column']) && isset($params['table'])) { - $query = 'SELECT DISTINCT ' . $params['column'] . ' FROM ' . $params['table']; + // create a clone of our report for a mysql connection to not pollute + // the original object. This may not be necessary. + $reportMysql = clone $report; - 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($query)) return array(); - - // open a connection to mysql: - - $reportMysql = $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); - $result = mysql_query($query, $reportMysql->conn); - if (!$result) { - throw new Exception("Unable to get variable options: " . mysql_error()); - } - - $options = array(); - - if (isset($params['all'])) $options[] = 'ALL'; - 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; + return MysqlReportType::getVariableOptions($params, $reportMysql); } }