]> git.nbdom.net Git - nb.git/commitdiff
add out to table
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Tue, 8 Mar 2016 17:40:28 +0000 (18:40 +0100)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Tue, 8 Mar 2016 17:40:28 +0000 (18:40 +0100)
lib/php/db/table.php
lib/php/out.php

index 3f8a261bb1e54ac773bc4759b61ec5a69e0f6620..5a1d3b180c52fb9fa1decd3cfe2b17f0719e9cf7 100644 (file)
@@ -1,6 +1,7 @@
 <?php
 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';
@@ -92,7 +93,7 @@ class Table extends nb {
    * return the sql to create the table
    *
    */
-  function sql() {
+  public function sql() {
     if (isset($this->sql)) return $this->sql;
 
     if (!preg_match('/^[\w_-]+$/',$this->name) ) {
@@ -168,7 +169,7 @@ GROUP BY sql.id,sql.table
    * @name (string) name of the field to return. Default: null
    * @return (array) return null where name does not exsts
    */
-  function fields($name=null) {
+  public function fields($name=null) {
 
     if ($this->fields === null) {
       $this->fields = array();
@@ -281,7 +282,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
     return $this->fields;
   }
 
-  function url_keys($values=null,$params=array(),$sep='&amp;') {
+  public function url_keys($values=null,$params=array(),$sep='&amp;') {
     if ($values === null) $values = $this->p();
     $url = is_array($params) ? $params : array($params);
 
@@ -299,7 +300,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
     return $url ? '?table='.$this->p('table').$sep.join($sep,$url) : '';
   }
 
-  function fields_keys(&$others=array()) {
+  public function fields_keys(&$others=array()) {
     
     $fields_keys = array();
 
@@ -315,7 +316,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
 
   }
 
-  function html_edit($values = null) {
+  public function html_edit($values = null) {
     if ($values === null) $values = $this->p();
     if (!is_array($values)) $values = array($values);
 
@@ -369,7 +370,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
 
   }
 
-  function url_list($k='',$v='') {
+  public function url_list($k='',$v='') {
 
     $params = array();
     $fields = ($this->p('action') == 'delete') ? array() : $this->fields();
@@ -398,7 +399,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
 
   }
 
-  function url_sort($name) {
+  public function url_sort($name) {
 
     # See: http://dev.w3.org/html5/html-author/charref
     $html = '';
@@ -424,7 +425,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
 
   }
 
-  function nav($count,$tot,$limit) {
+  public function nav($count,$tot,$limit) {
 
     $html = '';
 
@@ -456,7 +457,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
 
   }
 
-  function where($fields,$hvalues,$need_all_values=false) {
+  public function where($fields,$hvalues,$need_all_values=false) {
 
     // Construct where
     $where = array();
@@ -474,7 +475,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
 
   }
 
-  function where_criterias($values,$logic='') {
+  public function where_criterias($values,$logic='') {
     $having = $where = array();
     if (empty($logic)) $logic = 'AND';
 
@@ -549,7 +550,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
 
   }
 
-  function add_extras($extras) {
+  public function add_extras($extras) {
     $this->fields();
 
     foreach ($extras as $k => $v) {
@@ -567,7 +568,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
 
   }
 
-  function select_extras() {
+  public function select_extras() {
 
     if (!$this->extras) return '';
 
@@ -582,7 +583,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
   /******************************************************************
     Html Output
   ******************************************************************/
-  function rows($opt=array()) {
+  public function rows($opt=array()) {
     
     //
     // Select
@@ -618,12 +619,22 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
       : false
     ;
 
+    # Use the module out when format unknow
+    $out_conf = null;
+    if (!preg_match('/^text|csv|yaml|json|table|div$/',$format)) {
+      if (!($out_conf = out::$types[$format])) $this->bye("Unknow format `$format`");
+    }
+
     $count = 0;
     while ($row = $st->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT)) {
 
       if ($count === 0) {
-        if ($opt['is_html']) echo $this->html_nav_top();
-        echo $this->{"rows_begin_$format"}($this->fields());
+        if ($out_conf) {
+          echo out::begin($out_conf,$row);
+        } else {
+          if ($opt['is_html']) echo $this->html_nav_top();
+          echo $this->{"rows_begin_$format"}($this->fields());
+        }
       }
 
       $count++;
@@ -634,7 +645,11 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
         $count_fields++;
       }
 
-      echo $this->{"rows_rec_$format"}($row);
+      if ($out_conf) {
+        echo out::row($out_conf,$row);
+      } else {
+        echo $this->{"rows_rec_$format"}($row);
+      }
 
     }
 
@@ -663,12 +678,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
 
       }
 
-      if (!$query) {
-        $err = $this->db->conn->errorInfo();
-        $err[] = $sql;
-        err(join(' | ',$err));
-        return $err[0];
-      }
+      if (!$query) $this->err_sql($sql);
 
       $tot = $query->fetch(PDO::FETCH_COLUMN);
       $opt['tot'] = $tot;
@@ -681,16 +691,21 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
     if ($count === 0 and $this->p('header') === 'force') {
       echo $this->{"rows_begin_$format"}($this->fields());
     }
+
     if ($count) {
-      echo $this->{"rows_end_$format"}();
+      if ($out_conf) {
+        out::end($out_conf);
+      } else {
+        echo $this->{"rows_end_$format"}();
 
-      if ($opt['is_html']) {
+        if ($opt['is_html']) {
 
-        echo '<div class="nav bottom">'
-          .$this->nav($opt['count'],$opt['tot'],$opt['limit'])
-         .'</div>'.TABLE_EOL
-        ;
-       }
+          echo '<div class="nav bottom">'
+            .$this->nav($opt['count'],$opt['tot'],$opt['limit'])
+           .'</div>'.TABLE_EOL
+          ;
+         }
+      }
     } # < count
 
     $st->closeCursor();
@@ -701,12 +716,12 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
   /*-----------------------------------------------------------------
     Text
   -----------------------------------------------------------------*/
-  function rows_begin_text() {
+  public function rows_begin_text() {
     $this->_row_text = null;
     return '';
   }
 
-  function rows_rec_text(&$row) {
+  public function rows_rec_text(&$row) {
     $text = '';
     if ($this->_row_text === null) {
       $this->_row_text = true;
@@ -716,7 +731,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
     return $text.$row."\n";
   }
 
-  function rows_end_text() {
+  public function rows_end_text() {
     unset($this->_row_text);
     return '';
   }
@@ -724,7 +739,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
   /*-----------------------------------------------------------------
     Json
   -----------------------------------------------------------------*/
-  function rows_begin_json() {
+  public function rows_begin_json() {
     $this->_row_json = null;
     return '['.TABLE_EOL;
     return ''
@@ -733,7 +748,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
     .'['.TABLE_EOL;
   }
 
-  function rows_rec_json(&$row) {
+  public function rows_rec_json(&$row) {
     if ($this->_row_json === null) {
       $json = '';
       $this->_row_json = true;
@@ -743,7 +758,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
     return $json . json_encode($row);
   }
 
-  function rows_end_json() {
+  public function rows_end_json() {
     unset($this->_row_json);
     return TABLE_EOL.']'.TABLE_EOL;
   }
@@ -751,14 +766,14 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
   /*-----------------------------------------------------------------
     Yaml
   -----------------------------------------------------------------*/
-  function rows_begin_yaml() { return "---\n"; }
-  function rows_rec_yaml(&$row) { return $this->yaml_encode($row); }
-  function rows_end_yaml() { return ''; }
+  public function rows_begin_yaml() { return "---\n"; }
+  public function rows_rec_yaml(&$row) { return $this->yaml_encode($row); }
+  public function rows_end_yaml() { return ''; }
 
   /*-----------------------------------------------------------------
     Xml
   -----------------------------------------------------------------*/
-  function rows_begin_xml() {
+  public function rows_begin_xml() {
     return ''
       .'<?xml version="1.0" encoding="utf-8"?>'.TABLE_EOL #<?
       #.'<database name="'.$this->db->name.'" table="'.$this->name.'" type="'.$this->db->type.'">'.TABLE_EOL
@@ -766,7 +781,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
     ;
   }
 
-  function rows_rec_xml(&$row) {
+  public function rows_rec_xml(&$row) {
     $xml = '';
     $xml .= TABLE_INDENT."<row>".TABLE_EOL;
     foreach ($row as $k=>$v) {
@@ -780,31 +795,31 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
     return $xml;
   }
 
-  function rows_end_xml() {
+  public function rows_end_xml() {
     return '</rows>'.TABLE_EOL;
   }
 
   /*-----------------------------------------------------------------
     Csv
   -----------------------------------------------------------------*/
-  function rows_begin_csv($fields) {
+  public function rows_begin_csv($fields) {
 
     if ($this->p('header')==="0") return '';
     return join(TABLE_CSV_SEP,array_keys($fields))."\n";
   }
 
-  function rows_rec_csv(&$row) {
+  public function rows_rec_csv(&$row) {
     return join(TABLE_CSV_SEP,array_values($row))."\n";
   }
 
-  function rows_end_csv() {
+  public function rows_end_csv() {
     return '';
   }
 
   /*-----------------------------------------------------------------
     Html Table
   -----------------------------------------------------------------*/
-  function rows_begin_table($fields) {
+  public function rows_begin_table($fields) {
 
     $html = '';
 
@@ -829,7 +844,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
     return $html;
   }
 
-  function rows_rec_table(&$row) {
+  public function rows_rec_table(&$row) {
 
     $html = '<tr class="row">'.TABLE_EOL;
 
@@ -853,7 +868,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
     return $html;
   }
 
-  function rows_end_table($opt=array()) {
+  public function rows_end_table($opt=array()) {
     $html = '';
     $html .= '</tbody>'.TABLE_EOL;
     $html .= '</table>'.TABLE_EOL;
@@ -863,11 +878,11 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
   /*-----------------------------------------------------------------
     Html Div
   -----------------------------------------------------------------*/
-  function rows_begin_div() {
+  public function rows_begin_div() {
     return '<div class="rows '.$this->name.'">'.TABLE_EOL;
   }
 
-  function rows_rec_div(&$row) {
+  public function rows_rec_div(&$row) {
 
     $html = '';
 
@@ -892,14 +907,14 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
     return $html;
   }
 
-  function rows_end_div($opt=array()) {
+  public function rows_end_div($opt=array()) {
   }
 
-  function sql_name($value=null) {
+  public function sql_name($value=null) {
     return $this->db->sql_name($value === null ? $this->name : $value);
   }
 
-  function insert($hvalues) {
+  public function insert($hvalues) {
     $fields = $values = array();
 #var_dump($hvalues);
     foreach ($this->fields() as $name => $field) {
@@ -935,7 +950,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
     return $this->db->exec($sql);
   }
 
-  function update($hvalues) {
+  public function update($hvalues) {
     
     $keys = array();
     $keys_values = array();
@@ -998,7 +1013,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
 
   }
 
-  function delete($hvalues,&$e=null) {
+  public function delete($hvalues,&$e=null) {
     $keys = $this->fields_keys();
     // If no primary keys, we use all field
     if (empty($keys)) $keys = $this->fields();
@@ -1013,10 +1028,10 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
     return $this->db->exec($sql);
   }
 
-  function out($v,$head=false) { return $this->db->out($v,$head); }
-  function out2($v,$head=array()) { return $this->db->out2($v,$head); }
+  public function out($v,$head=false) { return $this->db->out($v,$head); }
+  public function out2($v,$head=array()) { return $this->db->out2($v,$head); }
 
-  function action() {
+  public function action() {
     $action = $this->p('action');
 
     if ($action == 'table.fields' or $action == 'fields') {
@@ -1075,7 +1090,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
     return false;
   }
 
-  function html_nav_top() {
+  public function html_nav_top() {
     if ($this->p('header')==="0") return '';
     $html = '';
 
@@ -1091,7 +1106,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
 
   }
 
-  function count() {
+  public function count() {
     
     if (isset($this->count)) return $this->count;
       $sql_count = $this->name;
@@ -1108,5 +1123,11 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
       return $this->count;
   }
 
+  public static function err_sql($sql) {
+    $err = $this->db->conn->errorInfo();
+    $err[] = $sql;
+    self::bye(join(' | ',$err));
+ }
+
 }
 ?>
index 40b4c43c4d305497926afb0f3e626ee540cde99c..c73ae219b56a7c50990848777c7edb9ea7607c49 100644 (file)
@@ -20,14 +20,14 @@ class Out extends Nb {
         'function' => 'out_csv',
       ),
       'labels' => array(
-        'enclose' => array("<center>".OUT_EOL,"</center>"),
+        'enclose' => array("<center>".OUT_EOL,"</center>".PHP_EOL),
         'tag_enclose' => 'labels',
         'tag' => 'label',
         'function' => 'out_tag',
         'head' => 'out_tag_head',
       ),
       'table' => array(
-        'enclose' => array("<table>".OUT_EOL,"</table>"),
+        'enclose' => array("<table>".OUT_EOL,"</table>".PHP_EOL),
         'tag_enclose' => 'tr',
         'tag_head' => 'th',
         'tag' => 'td',
@@ -35,7 +35,7 @@ class Out extends Nb {
         'head' => 'out_tag_head',
       ),
       'xml' => array(
-        'enclose' => array('<?xml version="1.0" encoding="utf-8"?>'.OUT_EOL."<rows>".OUT_EOL,"</rows>"),
+        'enclose' => array('<?xml version="1.0" encoding="utf-8"?>'.OUT_EOL."<rows>".OUT_EOL,"</rows>".PHP_EOL),
         'function' => 'out_xml',
       ),
       'yaml' => array(
@@ -62,6 +62,21 @@ class Out extends Nb {
     echo $conf['function']($row,$conf);
   }
 
+  public static function end($conf) {
+    if (!empty($conf['enclose'])) echo $conf['enclose'][1];
+  }
+
+  public static function begin($conf,&$head) {
+    if (!isset($conf['head'])) return;
+
+    if (!empty($conf['enclose'])) echo $conf['enclose'][0];
+    if (empty($head) and self::is_hash($data[0])) {
+      $head = array_keys($data[0]);
+    }
+    echo $conf['head']($head,$conf) . empty($conf['eol']) ? '' : $conf['eol'];
+
+  }
+
   public static function rows($type,&$data,$head=array()) {
     if (!isset(self::$types[$type])) self::bye("Unknow type: `$type`");
     $conf = self::$types[$type];
@@ -81,19 +96,7 @@ class Out extends Nb {
     echo $conf['enclose'][0];
 
     # Function head
-    if ($head !== false) {
-
-      if (isset($conf['head'])) {
-
-        $send = $head;
-        if (empty($head) and self::is_hash($data[0])) {
-          $head = array_keys($data[0]);
-        }
-        echo $conf['head']($head,$conf) . $conf['eol'];
-
-      }
-
-    }
+    if ($head !== false) self::begin($conf,$head);
 
     foreach ($data as $row) {
       $count++;
@@ -136,8 +139,12 @@ function out_csv(&$row,$o) {
 
 function out_csv_head(&$row,$o) {
   if (!is_array($row)) echo 0;
-  if (out::is_hash($row)) echo out_csv(array_keys($row),$o);
-  echo out_csv($row,$o);
+  if (out::is_hash($row)) {
+    $ar = array_keys($row);
+    echo out_csv($ar,$o);
+  } else {
+    echo out_csv($row,$o);
+  }
 }
 
 #function out_tag_head(&$row,$o) { return ''; }