]> git.nbdom.net Git - nb.git/commitdiff
out
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Wed, 9 Mar 2016 00:23:21 +0000 (00:23 +0000)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Wed, 9 Mar 2016 00:23:21 +0000 (00:23 +0000)
etc/profile.d/envs
etc/profile.d/functions
lib/php/db/table.php
lib/php/nb.php
lib/php/out.php

index e09048f024483aec908706718ab4255d82121be5..b03fc08ae4240fa2945a04aaf1bc9413e1a2cace 100644 (file)
@@ -12,6 +12,7 @@
 [ -z "$UID" ] && UID=`id -u`
 [ -z "$USER" ] && USER=`whoami`
 [ -z "$OSTYPE" ] && OSTYPE=`uname|tr [:upper:] [:lower:]`
+[ -z "PWD" ] && PWD=`pwd`
 
 #
 # PATHS
index c3dc62d8f126913591b85902fc683c1cf867b7f5..2b5977ca7a07bb18204bd01a8141da67c1a529e6 100644 (file)
@@ -476,3 +476,24 @@ END {
 }
 ' $noheader
 }
+
+mail_open_relay() {
+  shell_help "Usage: $FUNCNAME MAIL_SERVER" "$@" && return;
+  cat <<EOF | nc $@ 25
+helo client.server.com
+mail from: rockyjr@vsnl.com
+rcpt to: vivek@nixcraft.in
+quit
+EOF
+}
+
+shell_push_arg() {
+  local ret="$1"
+  local to_var="$2"
+  case "$1" in
+    #) echo "Escape: $i" ;;
+    *[^[:alpha:]./_-]*) ret='"'${1//\"/\\\"}'"' ;;
+  esac
+  [ -z "$to_var" ] && echo "$ret" && return
+  eval "$to_var=\"\$$to_var \$ret\"; $to_var=\${$to_var## };"
+}
index 5a1d3b180c52fb9fa1decd3cfe2b17f0719e9cf7..bd7e7afcaf364fd8ac7992b5085ecf9b2ca8a7ee 100644 (file)
@@ -621,23 +621,37 @@ 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 (!preg_match('/^text|csv|yaml|json|table|div$/',$format)) {
+    if ($this->p('out') or !preg_match('/^text|csv|yaml|json|table|div$/',$format)) {
       if (!($out_conf = out::$types[$format])) $this->bye("Unknow format `$format`");
+      if (empty($out_conf['enclose'])) $out_conf['enclose'] = array('','');
+      debug('Using out module!');
     }
 
     $count = 0;
     while ($row = $st->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT)) {
 
-      if ($count === 0) {
+      $count++;
+
+      #
+      # Head
+      #
+      if ($count === 1) {
+        if ($opt['is_html']) echo $this->html_nav_top();
+
         if ($out_conf) {
-          echo out::begin($out_conf,$row);
+          out::begin($out_conf,$this->fields());
+
         } else {
-          if ($opt['is_html']) echo $this->html_nav_top();
           echo $this->{"rows_begin_$format"}($this->fields());
         }
+
       }
+      
+      #
+      # Row
+      #
+      if ($out_conf and $count !== 1) out::row_end($out_conf);
 
-      $count++;
       $count_fields = 0;
 
       foreach ($this->fields() as $f => $field) {
@@ -646,7 +660,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
       }
 
       if ($out_conf) {
-        echo out::row($out_conf,$row);
+        out::row($out_conf,$row);
       } else {
         echo $this->{"rows_rec_$format"}($row);
       }
@@ -656,56 +670,62 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
     $opt['tot'] = $count;
     $opt['count'] = $count;
     $opt['limit'] = $limit;
-    if ($count and $opt['is_html']) {
-      //
-      // Tot
-      //
-      if (!$where and !$limit) {
-        debug("Table.rows(): Not using count(*)",1);
-        $query = $this->db->conn->query("SELECT $count");
-
-      } elseif ($select_count) {
-        $query = $this->db->conn->query('SELECT FOUND_ROWS()');
-
-      } elseif ($where) {
-        $sql_count = $sql;
-        $sql_count = preg_replace('/ (ORDER|LIMIT) .*?$/s','',$sql_count);
-        $sql_count = preg_replace('/^SELECT .*FROM/s','SELECT count(*) FROM ',$sql_count);
-        $query = $this->db->conn->query($sql_count);
 
-      } else {
-        $query = $this->db->conn->query('SELECT count(*) FROM '.$this->sql_name());
+    if ($count) {
 
-      }
+      if ($opt['is_html']) {
+        //
+        // Tot
+        //
+        if (!$where and !$limit) {
+          debug("Table.rows(): Not using count(*)",1);
+          $query = $this->db->conn->query("SELECT $count");
 
-      if (!$query) $this->err_sql($sql);
+        } elseif ($select_count) {
+          $query = $this->db->conn->query('SELECT FOUND_ROWS()');
 
-      $tot = $query->fetch(PDO::FETCH_COLUMN);
-      $opt['tot'] = $tot;
-      #if (!$tot) return;
+        } elseif ($where) {
+          $sql_count = $sql;
+          $sql_count = preg_replace('/ (ORDER|LIMIT) .*?$/s','',$sql_count);
+          $sql_count = preg_replace('/^SELECT .*FROM/s','SELECT count(*) FROM ',$sql_count);
+          $query = $this->db->conn->query($sql_count);
 
-      $opt['count'] = $count;
-      $opt['limit'] = $limit;
-    }
+        } else {
+          $query = $this->db->conn->query('SELECT count(*) FROM '.$this->sql_name());
 
-    if ($count === 0 and $this->p('header') === 'force') {
-      echo $this->{"rows_begin_$format"}($this->fields());
-    }
+        }
 
-    if ($count) {
-      if ($out_conf) {
-        out::end($out_conf);
-      } else {
-        echo $this->{"rows_end_$format"}();
+        if (!$query) $this->err_sql($sql);
 
-        if ($opt['is_html']) {
+        $tot = $query->fetch(PDO::FETCH_COLUMN);
+        $opt['tot'] = $tot;
+        #if (!$tot) return;
 
-          echo '<div class="nav bottom">'
-            .$this->nav($opt['count'],$opt['tot'],$opt['limit'])
-           .'</div>'.TABLE_EOL
-          ;
-         }
+        $opt['count'] = $count;
+        $opt['limit'] = $limit;
       }
+
+      if ($count === 0 and $this->p('header') === 'force') {
+        echo $this->{"rows_begin_$format"}($this->fields());
+      }
+
+      if ($count) {
+        if ($out_conf) {
+          out::end($out_conf);
+        } else {
+          echo $this->{"rows_end_$format"}();
+
+          if ($opt['is_html']) {
+
+            echo '<div class="nav bottom">'
+              .$this->nav($opt['count'],$opt['tot'],$opt['limit'])
+             .'</div>'.TABLE_EOL
+            ;
+           }
+        }
+
+      } # < is_html
+
     } # < count
 
     $st->closeCursor();
index 5603ecbdf221d10add4735079b9583714fe56695..1eefc13979c5721d47efd45d64bab6028f06745b 100644 (file)
@@ -326,8 +326,7 @@ class nb {
        * @return BOOL
        */
        public static function is_hash(&$arr) {
-    if (is_scalar($arr)) return false;
-               //debug (array_values($arr));
+    if (!is_array($arr)) return false;
                foreach ($arr as $k=>$v) {
       return ($k === 0) ? false : true;
                }
index c73ae219b56a7c50990848777c7edb9ea7607c49..51f3afefb1cec9b70b8421298aa1f0cb1a3601a4 100644 (file)
@@ -19,9 +19,9 @@ class Out extends Nb {
         'head' => 'out_csv_head',
         'function' => 'out_csv',
       ),
-      'labels' => array(
+      'center' => array(
         'enclose' => array("<center>".OUT_EOL,"</center>".PHP_EOL),
-        'tag_enclose' => 'labels',
+        'tag_enclose' => 'div',
         'tag' => 'label',
         'function' => 'out_tag',
         'head' => 'out_tag_head',
@@ -44,7 +44,7 @@ class Out extends Nb {
       ),
       'json' => array(
         'enclose' => array('['.OUT_EOL,OUT_EOL.']'),
-        'function' => 'json_encode',
+        'function.ext' => 'json_encode',
         'eol' => OUT_EOL,
         'rec' => ',',
       ),
@@ -55,26 +55,42 @@ class Out extends Nb {
     if (!empty($type) and !empty($data)) return $this->rows($type,$data,$head);
   }
 
-  public static function row($conf,&$row) {
+  public static function begin($o,$head) {
+    if (!empty($o['enclose'])) echo $o['enclose'][0];
+    if (!isset($o['head'])) return;
 
-    #if (isset($conf['eol'])) $row = rtrim($row,$conf['eol']);
-    if (!isset($conf['function'])) return;
-    echo $conf['function']($row,$conf);
-  }
+    if ($head === false) return;
+    if (empty($head) and self::is_hash($data[0])) {
+      $head = array_keys($data[0]);
+    }
+    echo $o['head']($head,$o) . empty($o['eol']) ? '' : $o['eol'];
 
-  public static function end($conf) {
-    if (!empty($conf['enclose'])) echo $conf['enclose'][1];
   }
 
-  public static function begin($conf,&$head) {
-    if (!isset($conf['head'])) return;
+  public static function row($o,&$row) {
 
-    if (!empty($conf['enclose'])) echo $conf['enclose'][0];
-    if (empty($head) and self::is_hash($data[0])) {
-      $head = array_keys($data[0]);
+    #if (isset($o['eol'])) $row = rtrim($row,$o['eol']);
+    #bye($o['function']);
+    if (isset($o['function.ext'])) {
+      echo $o['function.ext']($row);
+      return true;
     }
-    echo $conf['head']($head,$conf) . empty($conf['eol']) ? '' : $conf['eol'];
+    if (isset($o['function'])) {
+      echo $o['function']($row,$o);
+      return true;
+    }
+    #if (!isset($o['sep'])) $o['sep'] = "\t"; out_csv($row,$o);
+    out_tag($row,$o);
+    return false;
+  }
 
+  public static function row_end($o) {
+    if (!empty($o['rec'])) echo $o['rec'];
+    if (!empty($o['eol'])) echo $o['eol'];
+  }
+
+  public static function end($o) {
+    if (!empty($o['enclose'])) echo $o['enclose'][1];
   }
 
   public static function rows($type,&$data,$head=array()) {
@@ -93,12 +109,11 @@ class Out extends Nb {
     $tot = count($data);
     $count = 0;
 
-    echo $conf['enclose'][0];
-
     # Function head
-    if ($head !== false) self::begin($conf,$head);
+    self::begin($conf,$head);
 
     foreach ($data as $row) {
+      if ($count>0) out::row_end($conf);
       $count++;
 
       # Transform simple array into hash
@@ -117,11 +132,9 @@ class Out extends Nb {
       self::row($conf,$row);
       #echo "   ($count)";
 
-      if (!empty($conf['rec']) and $count<$tot) echo $conf['rec'];
-      if (!empty($conf['eol'])) echo $conf['eol'];
     }
 
-    print $conf['enclose'][1];
+    if ($count) self::end($conf);
   }
 
 } Out::types() ; # < Class
@@ -151,7 +164,6 @@ function out_csv_head(&$row,$o) {
 function out_tag_head(&$data,$o) {
 #return var_dump($row,true); return '';
   $o['tag'] = isset($o['tag_head']) ? $o['tag_head'] : $o['tag'];
-  #echo out_tag($row,$o); return true;
   $row = out::ar_first($data);
   if (!out::is_hash($row)) $row = $data;
   #if (!is_array($row)) return out_csv(array_fill(0,count($row),'?'));