]> git.nbdom.net Git - nb.git/commitdiff
Bed
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Fri, 23 Dec 2016 00:37:43 +0000 (00:37 +0000)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Fri, 23 Dec 2016 00:37:43 +0000 (00:37 +0000)
lib/php/db.php
lib/php/db/table.php
lib/php/out.php
www/dbq/dbq.php

index e47ef864f561f64782b5836672e62d0ba7df7006..2dc61595169c9192e7f9990ba4fd862b00bacaae 100644 (file)
@@ -176,7 +176,7 @@ class Db extends nb {
     }
 
     # Add formats from module out
-    foreach($this->out->types() as $t=>$v) if (!in_array($t,$this->formats)) $this->formats[] = $t;
+    foreach($this->out->types('_web_') as $t=>$v) if (!in_array($t,$this->formats)) $this->formats[] = $t;
 
     # Pdo
     $this->connect_init();
index d11842c9c32a0c448cac8bd2c173bf43f9bbecc4..d3116fbf6cea7915a59698c1045df5756b56bb03 100644 (file)
@@ -22,7 +22,6 @@ Class Table extends nb {
   public $name;
   public $type; # table, view, sql
   public $sql;
-# NB 28.03.16   public $fields_keys;
   public $replace = []; # replace by javascript
   public $extras = [];
   public $row_parse_pre; # Function to call in rows()
@@ -96,7 +95,11 @@ Class Table extends nb {
     foreach ($opt as $k => $v) { $this->$k = $v; }
 
     # NB 22.12.16: TODEL 
-    if (!isset($this->show_header)) $this->show_header = (bool)$this->p('header');
+    $this->show_header =
+      (bool)$this->p('header',$this->show_header)
+    ;
+    # TODE FOR out::
+    $this->pdef('header',$this->show_header);
 
   }
 
@@ -887,6 +890,7 @@ Class Table extends nb {
     # Run query
     #
     $this->create_temporary();
+    $this->db()->out->header($this->show_header);
 
     if ($opt_by_val !== null) $opt = $opt_by_val;
 
index 468c35002fa3c70ee173b1731d404f9660ed4e1c..5ce0c058610de1c0418432149f3758739c518fea 100644 (file)
@@ -6,10 +6,16 @@ Class Out extends Nb {
   public static $charset = 'utf-8';
   protected static $types;
   protected static $type;
+  protected static $header = true;
   public static function init() {
 
+    self::$header =
+      (bool)self::p('header',self::$header)
+    ;
     self::$types = [
 
+      'sql' => [], # Code still in db/table.php !
+
       'human' => [
         'sep' => self::p('sep'," | "),
         'corner' => self::p('corner',"+"),
@@ -20,9 +26,8 @@ Class Out extends Nb {
         'end' => 'out_human_end',
       ],
 
-      'sql' => [], # Code still in db/table.php !
-
       'cust' => [
+        '_web_' => false,
         'cust' => self::p('cust',"CHANGE ME"),
         'at' => self::p('at','@'),
         'open' => self::p('open',''),
@@ -218,11 +223,13 @@ Class Out extends Nb {
   public static function charset($set=null) { if (!empty($set)) self::$charset = $set; return self::$charset; }
 
   public static function types($type=null) {
+    if ($type === '_web_') return array_filter(self::$types,function($v){return !isset($v['_web_']) or (bool)$v['_web_'];});
     if (!empty($type)) return(empty(self::$types[$type]) ? null : self::$types[$type] );
     return self::$types;
   }
 
   public static function type($set=null) { if (!empty($set)) self::$type = $set; return self::$type; }
+  public static function header($set=null) { if (!empty($set)) self::$header = $set; return self::$header; }
 
   public static function scalar($v) {
     if ($v === null) return '';
@@ -283,7 +290,6 @@ Class Out extends Nb {
   }
 
   public static function head(&$o,$head,$data=[]) {
-    $o['header'] = (self::p('header')==='0') ? false : true;
     $o['out_count'] = 0;
 
     # Format $head
@@ -317,7 +323,7 @@ Class Out extends Nb {
 
     if (isset($o['enclose'])) echo $o['enclose'][0];
     if (0
-      or !$o['header']
+      or !self::header()
       or $head === false
       #or !isset($o['head'])
     ) return;
@@ -506,12 +512,11 @@ Class Out extends Nb {
   }
 
   public static function row_txt(&$o,&$row) {
-    $header = (self::p('header')==='0') ? false : true;
     if (!isset($o['col'])) $o['col'] = '';
     self::row_parse($row,$o);
     foreach ($row as $k => $v) {
 
-      if (!$header) {
+      if (!self::header()) {
         echo $v.$o['sep'];
         continue;
       }
@@ -628,7 +633,7 @@ function out_tag(&$o,&$row) {
 function out_human_head(&$o,$head) {
   #$line = "a\tb\tc\t\t\tg"; bye(explode("\t",$line));
 
-  if (out::p('header')==='0') return '';
+  if (!out::header()) return '';
   $o['_human'] = [
     'head' => 1,
   ];
index 92a384b7a2c3ba1c8adde9daf8b5fc6345574582..ede271a1dd65c904ab2e85382b6ceddbd5ac928b 100644 (file)
@@ -3,40 +3,28 @@ require_once(realpath(dirname(__FILE__).'/../../lib/php/nb.php'));
 require_once(NB_ROOT.'/lib/php/http.php');
 require_once(NB_ROOT.'/lib/php/mime.php');
 
-# Minimum permission access 0 = readonly
-define('DBQ_PERM',(int)(isset($_SERVER['DBQ_PERM']) ? $_SERVER['DBQ_PERM'] : 9));
-
-define('DBQ_HUMAN_UA_EXP',(string)(isset($_SERVER['DBQ_HUMAN_UA_EXP'])
-  ? $_SERVER['DBQ_HUMAN_UA_EXP']
-  : '/^\S+\s+.Windows|iPhone|Android|Macintosh|Linux/')
-);
-
-define('DBQ_PARAM_DEFAULT',(string)(isset($_SERVER['DBQ_PARAM_DEFAULT']) ? $_SERVER['DBQ_PARAM_DEFAULT'] : 'ls'));
-define('DBQ_PARAM_FORMAT',(string)(isset($_SERVER['DBQ_PARAM_FORMAT']) ? $_SERVER['DBQ_PARAM_FORMAT'] : ''));
-
-define('DBQ_CSS',(string)(isset($_SERVER['DBQ_CSS'])
-  ? $_SERVER['DBQ_CSS']
-  : '/default.css')
-);
-
-define('DBQ_TITLE',(string)(isset($_SERVER['DBQ_TITLE'])
-  ? $_SERVER['DBQ_TITLE']
-  : 'Dbq')
+/*
+  Default action
+*/
+define('DBQ_PARAM_DEFAULT',(string)(isset($_SERVER['DBQ_PARAM_DEFAULT'])
+  ? $_SERVER['DBQ_PARAM_DEFAULT']
+  : 'ls')
 );
 
 class DbQ extends nb {
-
-  public $perm = DBQ_PERM;
+  
+  public $perm = 9; // Minimum permission access 0 = readonly
   const ADMIN = 9;
   const DELETE = 4;
   const WRITE = 3;
   const VIEW = 1;
 
-  public $title = DBQ_TITLE;
+  public $title = 'Dbq';
   public $sep = ' / ';
 
   const HTML_FORMAT = 'table';
   const CLI_FORMAT = 'human';
+  public $human_ua_exp = '/^\S+\s+.Windows|iPhone|Android|Macintosh|Linux/';
   public $formats = [
     'table',
     'csv',
@@ -50,7 +38,7 @@ class DbQ extends nb {
   const PARAM_EXP = '[\w\._:-]{2,100}';
   const PARAM_DB_DEFAULT = DBQ_PARAM_DEFAULT;
   public $params = [
-    'format' => DBQ_PARAM_FORMAT,
+    'format' => '',
     'db' => self::PARAM_DB_DEFAULT,
     'table' => 'ls',
     'action' => 'ls',
@@ -60,20 +48,8 @@ class DbQ extends nb {
 
   public $run = false; # call run() when __contruct
 
-# NB 10.12.16   public $_colors = [
-# NB 10.12.16     'body' => '#ddd',
-# NB 10.12.16     'body-background' => '#009B9C',
-# NB 10.12.16 
-# NB 10.12.16     #'button' => '#ddd',
-# NB 10.12.16     #'button-background' => '#3C3C3B',
-# NB 10.12.16     'button' => '#444',
-# NB 10.12.16     'button-background' => '#aaa',
-# NB 10.12.16     'button-border' => '#888',
-# NB 10.12.16   ];
-  public $colors = [
-  ];
-
-  public $css = DBQ_CSS;
+  public $css = '/default.css';
+  public $js = '/default.js';
   public $ext;
 
   public $uri;
@@ -85,17 +61,31 @@ class DbQ extends nb {
   public $db;
 
   public function __construct($opt=[]) {
-    #debug(self::VIEW);
-
     //
     // Pre defaults values
 
+    // Envs -> var
+    foreach ([
+      'perm',
+      'title',
+      'css',
+      'js',
+      'human_ua_exp'
+    ] as $k) {
+      $env = 'DBQ_'.strtoupper($k);
+      if (!empty($_SERVER[$env])) $this->$k = $_SERVER[$env];
+    }
     // Envs -> params
     foreach ([
+      'format',
+      'db',
+      'table',
     ] as $k) {
       $env = 'DBQ_PARAM_'.strtoupper($k);
-      if (!empty($_SERVER[$env])) $this->env = $_SERVER[$env];
+      if (!empty($_SERVER[$env])) $this->params[$k] = $_SERVER[$env];
     }
+    /*
+    */
 
     //
     // Init
@@ -115,116 +105,16 @@ class DbQ extends nb {
     if ($this->run) $this->run();
   }
 
-  public function page_colors() {
-    if (!empty($this->colors)) {
-      if (empty($this->colors['body-border'])) $this->colors['body-border'] = $this->colors['body'];
-      if (empty($this->colors['button'])) $this->colors['button'] = $this->colors['body'];
-      if (empty($this->colors['button-background'])) $this->colors['button-background'] = $this->colors['body-background'];
-      if (empty($this->colors['button-border'])) $this->colors['button-border'] = $this->colors['body-border'];
-      $this->page->css_code = trim('
-body, .button {
-  color: '.$this->colors['body'].';
-  background-color: '.$this->colors['body-background'].';
-}
-table.rows,
-div.rows,
-.menu,
-.block,
-form.edit,
-object, iframe, pre
-{
-  border-color: '.$this->colors['body'].';
-}
-table.rows th, table.rows td { border-color: '.$this->colors['body-border'].'; }
-.button {
-  color: '.$this->colors['button'].';
-  border-color: '.$this->colors['button-border'].';
-  background-color: '.$this->colors['button-background'].';
-}
-');
-    }
-  }
-
   public function page($obj,$meth=null,$head=[],$fct=null) {
 
     if (empty($this->_nopage)) {
       // Write output
-      $this->page_colors();
       $this->page->headers_no_cache();
+      $this->page->js = $this->js;
       $this->page->js_code = trim('
-function form_submit_clean(form) {
-  form_clean(form);
-  form.submit();
-}
-
-function form_clean(form) {
-    var e;
-    for(i=0;i<form.length;i++) {
-      e = form.elements[i];
-      if (e.style.display == "none") continue;
-      if (e.type != "text") continue;
-      e.value = ""
-    }
-}
-
-function form_submit_clean(f) {
-  var i = 0;
-  var url = "";
-  var action = f.getAttribute("action");
-  var method = f.getAttribute("method");
-  if (method != "get") return true;
-
-  //if (typeof(action) == "undefined") action = "";
-
-  for(i=0;i<f.length;i++) {
-    var p = f[i];
-    if (!p.name) continue;
-    if (p.value=="") continue;
-    if (p.value=="undefined") continue;
-// NB 02.09.13                 url += (url ? "&" : "?") + escape(p.name) + "=" + escape(p.value);
-    url += (url ? "&" : "?") + encodeURI(p.name) + "=" + encodeURI(p.value);
-  }
-
-  url = (action == "?" ? "" : action) + url;
-
-  //document.location = url;
-
-  //if (url == window.location) return false;
-  window.location = url;
-  return false;
-}
-
-document.addEventListener("DOMContentLoaded", function() {
-
-  for (var e of document.querySelectorAll("form")) {
-    e.onsubmit = function() {
-      return form_submit_clean(this);
-    };
-  }
-  document.getElementById("table").removeAttribute("name");
-  document.getElementById("format").removeAttribute("name");
-
-  for (var e of document.querySelectorAll(".menu select.tables, .menu select.format")) {
-    e.onchange = function() {
-      var format = document.getElementById("format").value;
-      if (format == "'.self::HTML_FORMAT.'") format = "html";
-      window.location =  "./" + document.getElementById("table").value + "." + format;
-      return false;
-    };
-  }
-
-  for (var e of document.querySelectorAll(".menu select.limit")) {
-    e.onchange = function() {
-      this.form.submit();
-      return false;
-    };
-  }
-
-  if (document.querySelector(".nav.bottom")) {
-    document.querySelector("table.rows").insertAdjacentHTML("beforebegin","<div class=\"nav top\">"+document.querySelector(".nav.bottom").innerHTML+"</div>");
-  }
-
-});
+window._dbq = {
+  "HTML_FORMAT": "'.self::HTML_FORMAT.'"
+};
       ');
 
       $this->page->begin();
@@ -474,9 +364,12 @@ document.addEventListener("DOMContentLoaded", function() {
   public function not_found($admin_msg='') {
     header('Content-type: text/plain');
     $msg = '404 Not Found';
+
     if (empty($_SERVER['SERVER_PROTOCOL'])) $_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.0';
+
     header($_SERVER['SERVER_PROTOCOL'].' '.$msg);
     echo "$msg\n";
+
     if (!empty($admin_msg) and $this->perm >= self::ADMIN) echo "$admin_msg\n";
     exit;
   }
@@ -484,9 +377,12 @@ document.addEventListener("DOMContentLoaded", function() {
   public function error($admin_msg='') {
     header('Content-type: text/plain');
     $msg = '500 Internal Server Error';
+
     if (empty($_SERVER['SERVER_PROTOCOL'])) $_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.0';
+
     header($_SERVER['SERVER_PROTOCOL'].' '.$msg);
     echo "$msg\n";
+
     if (!empty($admin_msg) and $this->perm >= self::ADMIN) echo "$admin_msg\n";
     exit;
   }
@@ -548,10 +444,7 @@ document.addEventListener("DOMContentLoaded", function() {
   public function init() {
 
     // Format first !
-    if (!empty(DBQ_PARAM_FORMAT)) {
-      $format = DBQ_PARAM_FORMAT;
-
-    } else if ($this->params['format']) {
+    if ($this->params['format']) {
       $format = $this->params['format'];
 
     } else if ($content_type = self::client_content_type() and ( $format=Mime::toExt($content_type) )) {
@@ -559,7 +452,7 @@ document.addEventListener("DOMContentLoaded", function() {
 
     } else {
       $format = 
-        (!empty($_SERVER['HTTP_USER_AGENT']) and preg_match(DBQ_HUMAN_UA_EXP,$_SERVER['HTTP_USER_AGENT']))
+        (!empty($_SERVER['HTTP_USER_AGENT']) and preg_match($this->human_ua_exp,$_SERVER['HTTP_USER_AGENT']))
           ? 'html'
           : self::CLI_FORMAT
       ;
@@ -573,17 +466,18 @@ document.addEventListener("DOMContentLoaded", function() {
     $this->page->title = join($this->sep,$title);
     $this->page->nav = $nav;
 
-    if ($format == 'html') $format = self::HTML_FORMAT;
-    $this->params['format'] = $format;
-    $this->db->format = $format;
-
     if (!empty($this->formats)) $this->db->formats = $this->formats;
 
     // Then content type
     if (empty($content_type)) $content_type = Mime::fromExt($format==self::CLI_FORMAT ? 'txt' : $format);
-    #if (empty($content_type)) $content_type = 'text/plain';
+    if (empty($content_type)) $content_type = 'text/plain';
     if ($content_type) $this->page->content_type($content_type);
 
+    // Affect values to objects
+    if ($format == 'html') $format = self::HTML_FORMAT;
+    $this->params['format'] = $format;
+    $this->db->format = $format;
+
   }
 
   public function page_phpinfo() {