Skip to content

Commit

Permalink
A new function to handle generation of SQL IN clause better.
Browse files Browse the repository at this point in the history
Usage: sqlin(table_name, values, or_null = false)
  • Loading branch information
uded committed Sep 5, 2013
1 parent 6b8ccad commit 7bd3549
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions lib/PhpReports/PhpReports.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']));
Expand Down Expand Up @@ -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.'/',
Expand Down

0 comments on commit 7bd3549

Please sign in to comment.