From 0c7456c3291dda7120c438d7a7837bd83ae1c650 Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Fri, 18 Mar 2016 00:17:39 +0000 Subject: [PATCH] Bed ! --- lib/php/db.php | 6 +++++- lib/php/db/table.php | 26 +++++++++++--------------- lib/php/nb.php | 5 +++-- lib/php/out.php | 31 ++++++++++++++++++++++--------- 4 files changed, 41 insertions(+), 27 deletions(-) diff --git a/lib/php/db.php b/lib/php/db.php index e7a9be79..384b19ad 100644 --- a/lib/php/db.php +++ b/lib/php/db.php @@ -7,6 +7,7 @@ *****************************************************************************/ require_once(dirname(__FILE__).'/nb.php'); +require_once(dirname(__FILE__).'/out.php'); require_once(dirname(__FILE__).'/db/table.php'); require_once('out.php'); if (!defined('DB_HTML_FORM_BUTTONS')) define('DB_HTML_FORM_BUTTONS','' @@ -83,6 +84,9 @@ class Db extends nb { foreach ($db as $k=>$v) $this->$k = $v; } + #$this->formats += array_keys(out::$types); + foreach(out::$types as $t=>$v) if (!in_array($t,$this->formats)) $this->formats[] = $t; + # Pdo return $this->connect(); @@ -546,7 +550,7 @@ EOF; # NB 09.03.16 $head = array($head); # NB 09.03.16 } - out::rows($this->p('format','csv'),$rows,$head); + out::rows($this->p('format',out::php_cli() ? 'csv' : 'table'),$rows,$head); return true; } diff --git a/lib/php/db/table.php b/lib/php/db/table.php index 5c39aaef..2d16b843 100644 --- a/lib/php/db/table.php +++ b/lib/php/db/table.php @@ -3,18 +3,6 @@ require_once(dirname(__FILE__).'/../db.php'); require_once(dirname(__FILE__).'/../db/field.php'); require_once(dirname(__FILE__).'/../out.php'); -if (false and empty($_SERVER['DOCUMENT_ROOT'])) { - $_REQUEST['db'] = 'rent'; $name = 'tenant'; - $_REQUEST['db'] = 'rt'; $name = 'classes'; - $_REQUEST['db'] = 'mysql'; $name = 'addr'; - $_REQUEST['db'] = 'izi'; $name = 'zone'; - require_once('/opt/rent/www/config/db.php'); - print_r($Db->tables()); - $Table = $Db->table($name); - print_r($Table->fields()); - #print_r($Table->sql()); -} - define('TABLE_INDENT',NB_EOL ? "\t" : ""); define('TABLE_CSV_SEP',nb::p('sep') ? nb::p('sep') : "\t"); if (!defined('DB_HTML_EDIT')) define('DB_HTML_EDIT','Edit'); @@ -33,7 +21,7 @@ class Table extends nb { public $extras = array(); # hidden, sort, ... fields - public $params = array( 'table', 'limit', 'debug', 'action',); + public $params = array( 'table', 'limit', 'debug', 'action'); public $order_by = null; @@ -611,7 +599,8 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog. $st = $this->db->conn->prepare($sql); $st->execute(); - $format = ( $this->p('format') ? $this->p('format') : 'table' ); +# NB 17.03.16 $format = ( $this->p('format') ? $this->p('format') : 'table' ); + $format = $this->pdef('format','table'); if (!isset($opt['is_html'])) $opt['is_html'] = preg_match('/^(table|div)$/',$format) ? ( $this->p('header')!=="0" and $this->p('inc')!=="1") @@ -620,10 +609,17 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog. # Use the module out when format unknow $out_conf = null; - if ($this->p('out') or !preg_match('/^text|csv|yaml|json|table|div$/',$format)) { + if ($this->p('out') or !preg_match('/^('.join('|', + array( 'table','div','csv','xml','json','yaml' ) + ).')$/',$format)) + { + if (!($out_conf = out::$types[$format])) $this->bye("Unknow format `$format`"); + $this->params += array_values(out::$types); + if (empty($out_conf['enclose'])) $out_conf['enclose'] = array('',''); debug('Using out module!'); + } $count = 0; diff --git a/lib/php/nb.php b/lib/php/nb.php index c746897e..fe4c49f7 100644 --- a/lib/php/nb.php +++ b/lib/php/nb.php @@ -50,11 +50,12 @@ class nb { } /* - * Set a default value for param, if not sent + * Set a default's param value, if not set + * Return the value set or existing */ public static function pdef($name,$value) { if (preg_match('/^\s*$/',self::p($name))) return self::pset($name,$value); - return false; + return self::p($name); } /* diff --git a/lib/php/out.php b/lib/php/out.php index e4b6c80a..dbabf711 100644 --- a/lib/php/out.php +++ b/lib/php/out.php @@ -17,6 +17,15 @@ class Out extends Nb { 'head' => 'out_csv_head', 'function' => 'out_csv', ), + 'divs' => array( + 'enclose' => array("
".NB_EOL,"
".NB_EOL), + 'tag_enclose' => 'div class="row"', + #'tag_head' => 'span', + #'tag_head_enclose' => 'span class="row"', + 'tag' => 'div', + 'function' => 'out_tag', + 'head' => 'out_tag_head', + ), 'center' => array( 'enclose' => array("
".NB_EOL,"
".NB_EOL), 'tag_enclose' => 'div', @@ -25,8 +34,8 @@ class Out extends Nb { 'head' => 'out_tag_head', ), 'table' => array( - 'enclose' => array("".NB_EOL,"
".NB_EOL), - 'tag_enclose' => 'tr', + 'enclose' => array("".NB_EOL,"
".NB_EOL), + 'tag_enclose' => 'tr class="row"', 'tag_head' => 'th', 'tag' => 'td', 'function' => 'out_tag', @@ -53,7 +62,7 @@ class Out extends Nb { if (!empty($type) and !empty($data)) return $this->rows($type,$data,$head); } - public static function begin($o,$head,$data) { + public static function begin($o,$head,$data=array()) { if (!empty($o['enclose'])) echo $o['enclose'][0]; if (!isset($o['head'])) return; @@ -168,15 +177,21 @@ function out_csv_head(&$row,$o) { #function out_tag_head(&$row,$o) { return ''; } function out_tag_head(&$data,$o) { #return var_dump($row,true); return ''; - if (empty($o['tag']) and isset($o['tag_head'])) $o['tag'] = $o['tag_head']; + if (isset($o['tag_head'])) $o['tag'] = $o['tag_head']; $row = out::ar_first($data); if (!out::is_hash($row)) $row = $data; if (!is_array($row)) return 0; + + # For object if (out::is_hash($row)) $row = array_combine(array_keys($row),array_keys($row)); + + # Avoid: name + if (!out::is_hash($row)) $row = array_combine(array_values($row),array_values($row)); + #bye($row); out_tag($row,$o); } -function out_tag(&$row,$o) { +function out_tag(&$row,&$o) { #var_dump($row); return ''; if (isset($o['tag_enclose'])) echo (NB_EOL ? ' ' : '').'<'.$o['tag_enclose'].'>'.NB_EOL; @@ -191,14 +206,12 @@ function out_tag(&$row,$o) { } echo '' .(NB_EOL ? ' ' : '') - #.(isset($o['tag_enclose']) ? '<'.$o['tag_enclose'].'>' : '') ."<$k$class>$v" - #.(isset($o['tag_enclose']) ? '' : '') .NB_EOL ; } - if (isset($o['tag_enclose'])) echo (NB_EOL ? ' ' : '').''.NB_EOL; + if (isset($o['tag_enclose'])) echo (NB_EOL ? ' ' : '').''.NB_EOL; } function out_xml(&$row,$o) { @@ -213,7 +226,7 @@ function out_xml(&$row,$o) { /****************************************************************************/ #die("A NB_EOL B \n"); -if (empty($argv) or $argv[1] != 'test') return true; +if (empty($argv) or count($argv) < 2 or $argv[1] != 'test') return true; $data = array( 'a' => 1, 'b' => 2, -- 2.47.3