]> git.nbdom.net Git - nb.git/commitdiff
dbq css
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Wed, 18 Jan 2017 01:31:28 +0000 (01:31 +0000)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Wed, 18 Jan 2017 01:31:28 +0000 (01:31 +0000)
lib/php/db.php
lib/php/db/table.php
lib/php/nb.php
www/dbq/dbq.php
www/dbq/html/default.css
www/dbq/html/default.js
www/dbq/html/default.min.css

index ea6f62e464e5e7257d6bdb270a396885f27d97c3..0c184ec9fa57c4cbc6d07af05e8677b47c77c4a5 100644 (file)
@@ -150,6 +150,13 @@ class Db extends nb {
       unset($opt['tables']);
     }
 
+    # connect
+    $connect = false;
+    if (isset($opt['connect'])) {
+      $connect = (bool) $opt['connect'];
+      unset($opt['connect']);
+    }
+
     # Args into this
     foreach ($opt as $k=>$v) $this->$k = $v;
     #parent::__construct($opt);
@@ -171,6 +178,7 @@ class Db extends nb {
 
     # Pdo
     $this->connect_init();
+    if ($connect) $this->connect();
 
     # Params
     /*
@@ -208,9 +216,9 @@ class Db extends nb {
     if (empty($this->type)) $this->type = strtolower(preg_replace('/^([^:]+):.*$/','\1',$this->pdo));
     if (!$this->type) return false;
 
+    if ($v = $this->p('db.host')) $this->host = $v;
     if ($this->conf_type('use_path')) {
 
-      if ($p = $this->p('db.host')) $this->host = $p;
       if (empty($this->host)) $this->host = preg_replace('/^\w+:/','',$this->pdo);
 
       # Add file
@@ -232,8 +240,6 @@ class Db extends nb {
 # NB 26.10.16         }
 # NB 26.10.16       }
 
-      if ($p = $this->p('db.host')) $this->host = $p;
-
       # Add args to pdo
       foreach ([
         'host' => 'host',
@@ -252,8 +258,9 @@ class Db extends nb {
 
     }
 
-    # Title
+    # Defaults
     if (empty($this->title)) $this->title = prettyText($this->name);
+    if (empty($this->id) and !empty($this->name)) $this->id = preg_replace('/\W+/','',$this->name);
 
     # Row parser
     if (empty($this->row_parse_pre) and $this->conf_type('row_parse')) {
@@ -396,7 +403,7 @@ class Db extends nb {
   */
   public function table($name='',$params=[]) {
     if (!$name and !empty($this->default_table)) $name = $this->default_table;
-    if (!$name) bye('table name is empty');
+    if (!$name) $this->bye('table(): Missing table');
 
     if (empty($params) or empty($params['db'])) $params['db'] = $this;
 
@@ -1084,7 +1091,6 @@ class Db extends nb {
 
 #debug(array_keys($this->tables()));
     $new = []
-    #+( empty($this->id) ? [] : ['id' => $this->id])
     +[
       'id' => (empty($this->id) ? '' : $this->id),
       'name' => $this->name,
index 30a4b0a24ac457809e5db6a7752ded179407200e..3a1a5f18598a7a92719d12d86f18c900b3577010 100644 (file)
@@ -114,6 +114,7 @@ Class Table extends nb {
   public function create_temporary_rows() {
     // For static rows
     if(empty($this->rows)) return null;
+    if(!empty($this->rows) and empty($this->type)) $this->type = 'rows';
 
     $fields = array_keys($this->rows[0]);
     $sql_names = $this->sql_names($fields);
@@ -1398,7 +1399,7 @@ Class Table extends nb {
 
     $html = '';
 
-    $html .= '<table class="rows wp-list-table widefat fixed striped">'.NB_EOL;
+    $html .= '<table class="rows wp-list-table widefat striped">'.NB_EOL;
     #if (!empty($opt)) $html .= '<caption>' . $this->nav($opt['count'],$opt['tot'],$opt['limit']) . '</caption>' .NB_EOL;
 
     $this->__rows_table_attr = [];
@@ -1892,14 +1893,17 @@ Class Table extends nb {
       ;
 
       // Tables - see default.js if you change class
-      $r .= '<span class="label">';
-      $r .= '<label for="table">Tables</label>'.html_select_array(array_keys($this->db()->tables()),[
-        'html'       => 'class="tables" name="table" id="table"',
-        'selected'   => $this->name,
-        'prettyText' => true,
-        'sort'       => 'natcasesort',
-      ]);
-      $r .= '</span>';
+      $tables = array_keys($this->db()->tables());
+      if (count($tables)>1) {
+        $r .= '<span class="label">';
+        $r .= '<label for="table">Tables</label>'.html_select_array($tables,[
+          'html'       => 'class="tables" name="table" id="table"',
+          'selected'   => $this->name,
+          'prettyText' => true,
+          'sort'       => 'natcasesort',
+        ]);
+        $r .= '</span>';
+      }
 
       // Dbs - see default.js if you change class
       if (!empty($this->db()->dbs) and count($this->db()->dbs)>1) {
@@ -1925,7 +1929,7 @@ Class Table extends nb {
       $r .= '</span>';
 
       // Limit
-      if (!empty($this->db()->limits)) {
+      if (!empty($this->db()->limits) and count($this->db()->limits)>1) {
         $r .= '<span class="label">';
         $r .= '<label for="limit">Limit</label>'.html_select_array($this->db()->limits,[
           'html'       => 'class="limit" name="limit" id="limit"',
index 39ed3ab9b9e1e20b4062cb34292d72d11bdc3cac..49ac074630772fb70ffd3dc46a27c71bcc98ea37 100644 (file)
@@ -430,6 +430,7 @@ class NB {
   */
   public static function debug_backtrace_info($call_info) {
 
+    // Error into string
     $errorType = array (
       E_ERROR          => 'ERROR',
       E_WARNING        => 'WARNING',
@@ -451,10 +452,12 @@ class NB {
       E_DEPRECATED  => 'E_DEPRECATED ERROR',
       E_USER_DEPRECATED  => 'E_USER_DEPRECATED ERROR',
     );
+
     if (isset($call_info['type'])
       and isset($errorType[$call_info['type']])
-    ) $call_info['type'] = $errorType[$call_info['type']].'='.$call_info['type'];
-    #else ($call_info['type']) $call_info['type'] = $call_info['type'];
+    ) {
+      $call_info['type'] = $errorType[$call_info['type']].'='.$call_info['type'];
+    }
 
     // Default values
     if (isset($call_info['file']) and !empty($_SERVER['DOCUMENT_ROOT']))
index e446b19c59268fbcf952e728a7825b6a2d407f97..6924dae72ea3069b90279f0c53bacd9334f40e87 100644 (file)
@@ -2,6 +2,7 @@
 define('PRODUCTION',empty($_SERVER) or empty($_SERVER['PRODUCTION']) ? false : (bool)$_SERVER['PRODUCTION']);
 
 require_once(realpath(dirname(__FILE__).'/../../lib/php/nb.php'));
+require_once(NB_ROOT.'/lib/php/config.php');
 require_once(NB_ROOT.'/lib/php/http.php');
 require_once(NB_ROOT.'/lib/php/mime.php');
 
@@ -36,7 +37,9 @@ class DbQ extends nb {
   public $params_deep = []; # contain index in case if we decide to use a regexp
 
   # Db
-  public $limits = [];
+  #public $limits = [];
+  public $default_limit = '30';
+  public $limits = ['30','100','500','1000'];
   public $formats = [ 'html','csv','xml','json','yaml','sh','sql','php' ];
 
   # Page
@@ -94,6 +97,7 @@ class DbQ extends nb {
     $this->page = new Page([
       'css' => $this->css,
       'sep' => $this->sep_title,
+      #'body_class' => 'fixed',
     ]);
 
     if ($run) $this->run();
@@ -119,6 +123,7 @@ class DbQ extends nb {
   public function page($obj,$meth=null,$head=[],$fct=null) {
 
     // Defaults
+    if (empty($head)) $head = [];
     if (empty($this->default_limit)) $this->default_limit = $this->db->limits[0];
 
     // Write output
@@ -137,6 +142,8 @@ class DbQ extends nb {
         'param.action' => $this->params['action'],
         'param.args' => $this->params['args'],
         'param.deep' => $this->params_deep,
+        'text.add' => 'Add',
+        'text.clear' => 'Clear',
       ];
       $this->page->js_code = 'window._dbq = '.json_encode($this->json);
 
@@ -151,21 +158,27 @@ class DbQ extends nb {
     #if (is_scalar($obj)) {
     $conf = ['row_parse_post'=>$fct];
 
-    if(is_array($obj)) {
-      $this->db->out($obj,$head,$conf);
+    $rows = '';
+    if (is_array($obj)) {
+      $rows = $obj;
 
     } elseif (empty($meth)) {
       echo $obj;
 
-    } elseif(is_array($meth)) {
-      $this->db->out($meth,$head,$conf);
+    } elseif (is_array($meth)) {
+      $rows = $meth;
 
-    } else { # Objects Method
-      $out = $obj->$meth();
-      if (!empty($out)) $this->db->out($out,$head,$conf);
+    } elseif(is_scalar($meth)) { # Objects Method
+      $rows = $obj->$meth();
+      if (empty($this->params['table'])) $this->params['table'] = $meth;
+
+    } else {
+      $this->error("Page error");
 
     }
 
+    if (!empty($rows)) $this->db->out($rows,$head,$conf);
+
     if (empty($this->_nopage)) $this->page->end();
     exit;
   }
@@ -178,17 +191,19 @@ class DbQ extends nb {
       if (!$this->params['db']) $this->not_implemented('Params db missing');
       if (!isset($this->db)) $this->db = $this->db();
 
+      # From conf
       if (!is_array($connect)
         and $connect === true
         and isset($this->db->conf[$this->params['db']])
       ) {
         $connect = $this->db->conf[$this->params['db']];
+        if (empty($connect['name'])) $this->not_found('Unknown database: '.$this->params['db']);
       }
 
-      if (empty($connect['name'])) $this->not_found('Unknown database: '.$this->params['db']);
+      $connect['id']= $this->params['db'];
 
       $this->db->__construct($connect);
-      $this->id = $this->params['db'];
+      #$this->id = $this->params['db'];
       $this->db->connect();
       $this->db->base = '/'.$this->db->id;
       return $this->db;
@@ -196,7 +211,6 @@ class DbQ extends nb {
 
     // Re use
     if (!empty($this->db)) return $this->db;
-    require_once(NB_ROOT.'/lib/php/config.php');
     require_once(NB_ROOT.'/lib/php/db.php');
 
     // New
@@ -241,11 +255,12 @@ class DbQ extends nb {
     $opt = ($this->page->is('html')
       and ($this->perm >= self::VIEW)
       and $this->table->type() != 'sql'
+      and strpos(' table view',$this->table->type())
     ) ? [
       'row_parse_pre' => function(&$r){
         $GLOBALS['dbq_args'] = urlencode( join($this->param_args_sep,$this->table->fields_keys_values($r)) );
       },
-      'row_parse_post' => function(&$r){
+      'row_parse_post' => $this->table->type() ? 0 : function(&$r){
 
         $args = $GLOBALS['dbq_args'];
 
@@ -279,7 +294,7 @@ class DbQ extends nb {
   }
 
 
-  public function table() {
+  public function table($param=[]) {
 
     if (!in_array($this->params['table'],array_keys($this->db->tables()))) {
 
@@ -290,14 +305,14 @@ class DbQ extends nb {
 
     }
 
-    $this->table = $this->db->table($this->params['table'],[
+    $this->table = $this->db->table($this->params['table'],array_merge([
       'db' => $this->db,
       'show_hidden_params' => false,
       'show_buttons' => false,
       'show_header' => ($this->params['format'] == 'html'),
       #'params' => [],
       #'params' => [ 'limit' ],
-    ]);
+    ],$param));
 
     $this->table->base = $this->db->base.'/'.$this->table->name;
 
@@ -309,7 +324,7 @@ class DbQ extends nb {
 
     $title = array_filter(array_unique(array_slice(array_values($params),1)),
       function($v){
-        return(in_array($v,self::ACTIONS_NO_TITLE) ? '' : $v);
+        return(in_array($v,self::ACTIONS_NO_TITLE) ? '' : trim($v));
       }
     );
 
@@ -593,6 +608,29 @@ class DbQ extends nb {
     #return $this->page($rows);
   }
 
+  public function rows_table($name,$rows) {
+    $this->params['table'] = $name;
+    $this->params['db'] = ' ';
+
+    $this->db->conf[ $this->params['db'] ] = [
+      'name' => $this->params['table'],
+      'type' => 'sqlite',
+      'host' => ':memory:',
+      'type' => 'sqlite',
+      'tables' => [ $this->params['table'] => [
+          'rows' => $rows
+      ] ],
+    ];
+    $this->db->__construct($this->db->conf[ $this->params['db'] ]);
+    if (empty($this->db->conn)) $this->db(true);
+
+    if (empty($this->table)) $this->table();
+    $this->table->base = '';
+
+    $this->page($this,'table_rows');
+
+  }
+
   public function run_root() {
     $action = $this->params['db'];
     if (!$action and self::PARAM_DB_DEFAULT) $action = self::PARAM_DB_DEFAULT;
@@ -634,27 +672,9 @@ class DbQ extends nb {
       $this->page($this,'json');
 
     } elseif ($action == 'phpinfo') {
+      $this->rows_table($action,$this->phpinfo_rows());
       #$this->page(['phpinfo'=>$this->page->phpinfo(true)]);
       #$this->page($this->page->phpinfo());
-    if (0) {
-      $this->params['table'] = 'phpinfo';
-      /*
-      $this->params['db'] = 'mem';
-      $this->db = new Db([
-        'name' => 'mem',
-        'name' => 'id',
-        'type' => 'sqlite',
-        'host' => ':memory:',
-      ]);
-      */
-      $this->params['db'] = 'sys';
-      $this->db->table($this->params['table'],[
-        'rows' => $this->phpinfo_rows(),
-        'type' => 'table',
-      ]);
-      return true;
-      #$this->page($this,'table_rows');
-    }
       $this->page($this,'phpinfo_rows');
       #$this->phpinfo_rows();
 
@@ -716,18 +736,6 @@ class DbQ extends nb {
     $action = $this->params['action'];
     if (!$action and self::PARAM_DB_DEFAULT) $action = self::PARAM_DB_DEFAULT;
 
-    if (0) {
-      $this->table->name = 'zaza';
-      $this->table->rows = [
-        [
-          'name' => 'Name 1',
-        ],
-        [
-          'name' => 'Name 2',
-        ],
-      ];
-    }
-
 #bye([$this->params,$action]);
     if ($action == 'help') {
       $this->page($this->table,[
index 6496cea277e4b8573ffb601d79f0c7b70718487a..f222ff386d98e7e15df91e3c9edcbfbbf248b40c 100644 (file)
@@ -114,10 +114,10 @@ table.rows {
   border-collapse: collapse;
   box-shadow: 0 0 0 1px #DDDDDD;
   table-layout: fixed;
-  /*
-  display: inline-block;
+}
+
+table.rows.fixed {
   width: 100%;
-  */
 }
 
 table.rows tr:nth-child(even) td {
@@ -144,14 +144,12 @@ table.rows td .delete {
   white-space: nowrap;
 }
 
-/*
-table.rows td .code
+table.rows.fixed td
 {
   white-space: nowrap;
   text-overflow: ellipsis;
   overflow: hidden;
 }
-*/
 
 table.rows th {
   text-align: left;
index 15f9c5d436723cedbb17f1660921c9b19ec3d2a3..667c130559562a7a0cc8b4a4e181e6018e401206 100644 (file)
@@ -115,10 +115,26 @@ document.addEventListener("DOMContentLoaded", function() {
   });
   */
 
+  // No more than 100% width
+  document.querySelectorAll("table").forEach(function(e) {
+    if (parseInt(e.offsetWidth) > parseInt(window.innerWidth)) {
+      e.className = e.className.replace(/(^| +)fixed($| )/,'') + ' fixed'
+      e.className = e.className.trim();
+      //console.log(e.offsetWidth +'>'+ window.innerWidth+' '+e.className);
+    }
+  });
+
   document.querySelectorAll("form.menu input[type=submit]").forEach(function(e) {
     e.insertAdjacentHTML("afterend",''
-      +'&nbsp;<input type="button" class="clear '+e.className+'" onclick="form_clean(this.form)" value="Clear"/>'
-      +(window._dbq['table.base'] ? '&nbsp;<input type="button" class="add '+e.className+'" onclick="window.location=\''+window._dbq['table.base']+'add/\'" value="Add"/>' : '')
+      +'&nbsp;<input type="button" class="clear '+e.className+'" onclick="form_clean(this.form)"'
+        +' value="'+window._dbq['text.clear']+'"'
+      +' />'
+      +( window._dbq['table.base']
+        ? '&nbsp;<input type="button" class="add '+e.className+'"'
+          +' onclick="window.location=\''+window._dbq['table.base']+'add/\'"'
+          +' value="'+window._dbq['text.add']+'"/>'
+        : ''
+      )
     );
   });
 
index b34cefc6c7d4a92dd131c186cf23dfa53820dd3a..1d2ada4a62fb431a4eb3a67ab34bc12db77df318 100644 (file)
@@ -1 +1 @@
-html{font:90% 'Trebuchet MS',sans-serif;background-color:#EEE}body{display:table;margin-left:auto;margin-right:auto;padding-left:1em;padding-right:1em;color:#444}h1{margin:0 0 0.2em 0;font-size:180%;padding-bottom:0.3em;margin-bottom:0.5em;border-bottom:dashed 1px #444}a{color:inherit}p a,div > a,li a,td a{color:#E59B24}a,a:visited{text-decoration:none}ul{padding:0;margin:0}li{list-style:none;padding:0 0 0 0.3em}table{border-collapse:collapse}td,th{padding:0.2em 0.7em}th a.sort{padding:0 0.3em}textarea,select,input:not([type]),input[type='text'],input[type='password'],input[type='date'],input[type='email'],input[type='url'],input[type='']{border-radius:3px;border:solid 1px #ddd;padding:0.3em 0.4em}input[size]{max-width:70%}div.row div label:after,form label:after{content:':'}form label,form input,form select{vertical-align:middle;margin:0.2em 0.4em 0.2em 0}table.rows,div.row,ul.row,form.edit,.menu,.block,object,iframe,pre{border-radius:4px;border-spacing:0;padding:0.5em 0.2em;margin-bottom:0.5em;border:solid 1px #DDD;background-color:#FAFAFA}.rows{padding:0}table.rows{border-style:hidden;border-collapse:collapse;box-shadow:0 0 0 1px #DDD;table-layout:fixed;width:99%}table.rows tr:nth-child(even) td{background-color:#FEFEFE}ul.row li,div.row div,table.rows th,table.rows td{border-bottom:solid 1px #DDD}ul.row li,div.row div,table.rows tr:last-child td{border-bottom:none}table.rows tr > *{border-right:solid 1px #FEFEFE}table.rows td{padding:0.3em 0.3em;white-space:nowrap;text-overflow:ellipsis;overflow:hidden}table.rows th{text-align:left}div.row div{margin:0}ul.row li label,div.row div label{padding:0.1em 0.5em 0.1em 0;min-width:20%;display:inline-block}.menu,.nav,.rows,.buttons,td.button,.center{margin-left:auto;margin-right:auto;display:table}.button{font:inherit;display:inline-block;text-decoration:none;line-height:1.5em;cursor:pointer;border-radius:4px;white-space:nowrap;box-sizing:border-box;margin:0;padding:0 0.1em;cursor:pointer;color:#EEE;background-color:#666;padding:0.1em 0.3em;border:none}a:hover,.button:hover{opacity:0.8}form.menu{padding:0.5em;background-color:#FAFAFA}form.menu .button.add{float:right;margin-top:0.1em}form.menu .criterias{border-top:dashed 1px #DDD;margin-top:0.3em;padding-top:0.3em}form.menu .criterias span.label{border-right:solid 1px #fff;padding:0.2em 0.1em;margin:0.2em 0.2em 0 0;border-radius:3px}form.menu .criterias span.label:last-child{border-right:none}form.menu select:focus{max-width:auto}form.menu select{max-width:20%}form.menu .criterias input[type=text]:focus{width:auto}form.menu .criterias input[type=text]{width:4em}form.edit .fields label{width:25%;text-align:right;display:inline-block;margin-right:1em;vertical-align:top}form.edit .fields .label span{width:54%;display:inline-block;text-align:left}form.edit .fields input,form.edit .fields textarea,form.edit .fields select{width:65%}form.edit .fields label{width:25%;text-align:right;display:inline-block;margin-right:1em;vertical-align:top}form.edit .fields .label span{width:54%;display:inline-block;text-align:left}
\ No newline at end of file
+html{font:90% 'Trebuchet MS',sans-serif;background-color:#EEE}body{display:table;margin-left:auto;margin-right:auto;padding-left:1em;padding-right:1em;color:#444}h1{margin:0 0 0.2em 0;font-size:180%;padding-bottom:0.3em;margin-bottom:0.5em;border-bottom:dashed 1px #444}a{color:inherit}p a,div > a,li a,td a{color:#E59B24}a,a:visited{text-decoration:none}a:hover,.button:hover{opacity:0.8}ul{padding:0;margin:0}li{list-style:none;padding:0 0 0 0.3em}table{border-collapse:collapse}td,th{padding:0.2em 0.7em}th a.sort{padding:0 0.3em}textarea,select,input:not([type]),input[type='text'],input[type='password'],input[type='date'],input[type='email'],input[type='url'],input[type='']{border-radius:3px;border:solid 1px #DDD;padding:0.3em 0.4em;box-shadow:0 1px 1px #EEE}input[size]{max-width:70%}div.row div label:after,form label:after{content:':'}form label,form input,form select{vertical-align:middle;margin:0.2em 0.4em 0.2em 0}table.rows,div.row,ul.row,form.edit,.menu,.block,object,iframe,pre{border-radius:4px;border-spacing:0;padding:0.5em 0.2em;margin-bottom:0.5em;border:solid 1px #DDD;background-color:#FAFAFA;box-shadow:0 1px 2px #EEE}.rows{padding:0}table.rows{border-style:hidden;border-collapse:collapse;box-shadow:0 0 0 1px #DDD;table-layout:fixed}table.rows tr:nth-child(even) td{background-color:#FEFEFE}ul.row li,div.row div,table.rows th,table.rows td{border-bottom:solid 1px #DDD}ul.row li,div.row div,table.rows tr:last-child td{border-bottom:none}table.rows tr > *{border-right:solid 1px #FEFEFE}table.rows td{padding:0.3em 0.3em}table.rows td .view,table.rows td .delete{width:1%;white-space:nowrap}table.rows th{text-align:left}div.row div{margin:0}ul.row li label,div.row div label{padding:0.1em 0.5em 0.1em 0;min-width:20%;display:inline-block}.button{font:inherit;display:inline-block;text-decoration:none;line-height:1.5em;cursor:pointer;border-radius:4px;white-space:nowrap;box-sizing:border-box;margin:0;padding:0 0.1em;cursor:pointer;padding:0.1em 0.3em;border:none;color:#E59B24;background-color:#EEE;background:linear-gradient(to bottom right,#EEE,#DDD);margin-right:0.1em;box-shadow:0 1px 1px #BBB;font-weight:bold}.menu,.nav,.rows,.buttons,td.button,.center{margin-left:auto;margin-right:auto;display:table}form.menu{padding:0.5em;background-color:#FAFAFA}form.menu .button.add{float:right;margin-top:0.3ex;display:inline}form.menu .criterias{border-top:dashed 1px #DDD;margin-top:0.3em;padding-top:0.3em}form.menu .criterias span.label{border-right:solid 1px #fff;padding:0.2em 0.1em;margin:0.2em 0.2em 0 0;border-radius:3px}form.menu .criterias span.label:last-child{border-right:none}form.menu select:focus{max-width:auto}form.menu select{max-width:20%}form.menu .criterias input[type=text]:focus{width:auto}form.menu .criterias input[type=text]{width:4em}form.edit .fields label{width:25%;text-align:right;display:inline-block;margin-right:1em;vertical-align:top}form.edit .fields .label span{width:54%;display:inline-block;text-align:left}form.edit .fields input,form.edit .fields textarea,form.edit .fields select{width:65%}form.edit .fields label{width:25%;text-align:right;display:inline-block;margin-right:1em;vertical-align:top}form.edit .fields .label span{width:54%;display:inline-block;text-align:left}
\ No newline at end of file