From 7bd35491ad8a00ab856f629c4da58583e933a5fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20R=C5=BCanek?= Date: Thu, 5 Sep 2013 16:53:40 +0200 Subject: [PATCH] A new function to handle generation of SQL IN clause better. Usage: sqlin(table_name, values, or_null = false) --- lib/PhpReports/PhpReports.php | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/lib/PhpReports/PhpReports.php b/lib/PhpReports/PhpReports.php index 94bc7ba6..7ad062cf 100644 --- a/lib/PhpReports/PhpReports.php +++ b/lib/PhpReports/PhpReports.php @@ -42,11 +42,15 @@ public static function init($config = 'config/config.php') { new Twig_Loader_String() )); self::$twig = new Twig_Environment($loader); - self::$twig->addFunction('dbdate',new Twig_Function_Function('PhpReports::dbdate')); - + self::$twig->addFunction(new Twig_SimpleFunction('dbdate', 'PhpReports::dbdate')); + self::$twig->addFunction(new Twig_SimpleFunction('sqlin', 'PhpReports::generateSqlIN')); + + self::$twig->getFunctions(); + self::$twig_string = new Twig_Environment(new Twig_Loader_String()); - - FileSystemCache::$cacheDir = self::$config['cacheDir']; + self::$twig_string->addFunction(new Twig_SimpleFunction('sqlin', 'PhpReports::generateSqlIN')); + + FileSystemCache::$cacheDir = self::$config['cacheDir']; if(!isset($_SESSION['environment']) || !isset(self::$config['environments'][$_SESSION['environment']])) { $_SESSION['environment'] = array_shift(array_keys(self::$config['environments'])); @@ -108,8 +112,23 @@ public static function dbdate($time, $database=null, $format=null) { return $time; } - - public static function render($template, $macros) { + + public static function generateSqlIN($column, $values, $or_null = false) { + $sql = "$column IN ("; + foreach ($values as $value) { + $sql .= is_numeric($value) ? $value : "'".$value."'"; + if ($value !== end($values)) { + $sql .= ', '; + } + } + $sql .= ")"; + if ($or_null) { + $sql.= " OR $column IS NULL"; + } + return $sql; + } + + public static function render($template, $macros) { $default = array( 'base'=>self::$request->base, 'report_list_url'=>self::$request->base.'/',