]> git.nbdom.net Git - nb.git/commitdiff
Bed !
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Fri, 18 Mar 2016 00:17:39 +0000 (00:17 +0000)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Fri, 18 Mar 2016 00:17:39 +0000 (00:17 +0000)
lib/php/db.php
lib/php/db/table.php
lib/php/nb.php
lib/php/out.php

index e7a9be79b3ce1d05dfa819afaef5a262b2001e07..384b19ad77d1e49f88d2df6a72fba8ee7693701d 100644 (file)
@@ -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;
   }
 
index 5c39aaef862b9d87c2e0153c22cdee02b810edb1..2d16b843bf32479578c1c37519b32e9f287c161b 100644 (file)
@@ -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;
index c746897ebd747e9bc241733769ab840454f9f367..fe4c49f7d75af12e8f97b005b8dee1b5d2af5ca7 100644 (file)
@@ -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);
   }
 
   /*
index e4b6c80ac5230c5daa598babdd7c4d37220d35bb..dbabf711a2e8cb8ebed0266c8f3c4f9edd89dbda 100644 (file)
@@ -17,6 +17,15 @@ class Out extends Nb {
         'head' => 'out_csv_head',
         'function' => 'out_csv',
       ),
+      'divs' => array(
+        'enclose' => array("<div class=\"rows\">".NB_EOL,"</div>".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("<center>".NB_EOL,"</center>".NB_EOL),
         'tag_enclose' => 'div',
@@ -25,8 +34,8 @@ class Out extends Nb {
         'head' => 'out_tag_head',
       ),
       'table' => array(
-        'enclose' => array("<table>".NB_EOL,"</table>".NB_EOL),
-        'tag_enclose' => 'tr',
+        'enclose' => array("<table class=\"rows\">".NB_EOL,"</table>".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: <th class="0">name</th>
+  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</$k>"
-      #.(isset($o['tag_enclose']) ? '</'.$o['tag_enclose'].'>' : '')
       .NB_EOL
     ;
   }
 
-  if (isset($o['tag_enclose'])) echo (NB_EOL ? '  ' : '').'</'.$o['tag_enclose'].'>'.NB_EOL;
+  if (isset($o['tag_enclose'])) echo (NB_EOL ? '  ' : '').'</'.explode(' ',$o['tag_enclose'])[0].'>'.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,