]> git.nbdom.net Git - nb.git/commitdiff
lib/php/nb.php
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Mon, 10 Sep 2018 02:58:55 +0000 (03:58 +0100)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Mon, 10 Sep 2018 02:58:55 +0000 (03:58 +0100)
lib/php/db.php
lib/php/db/table.php
lib/php/nb.php
lib/php/out/table.php
www/dbq/dbq.php

index ec64dddb50bdfbd1375e2d383293f4c02417de39..bec6a50392537ba571f9e1e3ba1038e8fc2ac874 100644 (file)
@@ -29,10 +29,10 @@ class Db extends nb {
        # PDO Connection
 # NB 22.09.16   public static $encoding = 'utf-8';
 
-# NB 22.09.16: TODO 
-       public static $table_rows_dump = [
-               'praser' => false,
-       ];
+# NB 10.09.18 # NB 22.09.16: TODO 
+# NB 10.09.18  public static $table_rows_dump = [
+# NB 10.09.18          'praser' => false,
+# NB 10.09.18  ];
 
        public $conn;
        public $pdo;
@@ -54,6 +54,7 @@ class Db extends nb {
        public $tables = [];
        public $types = [];
        public $conf = [];
+       static $dbq = []; # NB 10.09.18: For internal use in /etc/dbq* files 
        public $attach = []; # for sqlite
        public $row_parse_pre; # Function to call in table.rows()
        public $row_parse_post; # Function to call in table.rows()
@@ -273,7 +274,8 @@ class Db extends nb {
                                        if (!empty($this->$p)) $msg .= " $p=".$this->$p;
                                }
 
-                               $msg .= "\n  ".$e->getMessage();
+                               #$msg .= "\n  ".$e->getMessage();
+                               $msg .= ": ".$e->getMessage();
                                #throw new Exception($msg, (int)$e->getCode());
                                self::bye($msg);
                        }
@@ -729,11 +731,11 @@ class Db extends nb {
                if (empty($files)) return [];
 
                #
-               # Load all files into a $dbs if #files is not a hash
+               # Load all files into a self::$dbq if #files is not a hash
                #
 
                $DBQ = [];
-               $dbs = [];
+# NB 10.09.18          $dbs = [];
                $done = [];
                foreach ((array)$files as $file) {
 
@@ -743,24 +745,24 @@ class Db extends nb {
 
                        if (!is_readable($file)) continue;
                        if (preg_match('/\.(yaml|yml)$/i',$file) and ($yaml = self::yaml_parse_file($file))) {
-                               $dbs = self::ar_merge($dbs,$yaml);
+                               self::$dbq = self::ar_merge(self::$dbq,$yaml);
 
                        } elseif (preg_match('/\.php$/i',$file)) {
                                $DBQ = [];
                                require($file);
-                               if (!empty($DBQ)) $dbs = self::ar_merge($dbs,$DBQ);
+                               if (!empty($DBQ)) self::$dbq = self::ar_merge(self::$dbq,$DBQ);
 
                        }
 
                }
 
                unset($yaml,$DBQ);
-               if (!$dbs) return false;
+               if (!self::$dbq) return false;
 
                #
                # First iteration: Import database conf with key _import
                #
-               foreach ($dbs as $id=>$params) {
+               foreach (self::$dbq as $id=>$params) {
                        $params = (array)$params;
 
                        foreach ($params as $k => $v) {
@@ -769,46 +771,46 @@ class Db extends nb {
                                $import = is_array($v) ? $v : explode(',',$v);
                                foreach ($import as $v) {
                                        if ($id == $v) self::bye("Infinite loop: _import $id = $v");
-                                       if (empty($dbs[$v])) continue;
+                                       if (empty(self::$dbq[$v])) continue;
 
-                                       foreach ($dbs[$v] as $kk => $vv) {
-                                               if (!isset($params[$kk])) $dbs[$id][$kk] = $vv;
+                                       foreach (self::$dbq[$v] as $kk => $vv) {
+                                               if (!isset($params[$kk])) self::$dbq[$id][$kk] = $vv;
                                        }
                                }
 
-                               unset($dbs[$id][$k]);
+                               unset(self::$dbq[$id][$k]);
                        }
                }
 
-               $default = isset($dbs['_default']) ? $dbs['_default'] : [];
+               $default = isset(self::$dbq['_default']) ? self::$dbq['_default'] : [];
 
                #
                # Second iteration: Remove db starting with _
                #
-               foreach ($dbs as $db=>$params) { if (preg_match('/^_/',$db)) unset($dbs[$db]); }
+               foreach (self::$dbq as $db=>$params) { if (preg_match('/^_/',$db)) unset(self::$dbq[$db]); }
 
-               if (!$dbs) return false;
+               if (!self::$dbq) return false;
 
                #
                # Third iteration: Add missing and default
                #
-               foreach ($dbs as $db=>$params) {
-                       if (empty($params['name'])) $dbs[$db]['name'] = $db;
-                       if (empty($params['id'])) $dbs[$db]['id'] = $db;
+               foreach (self::$dbq as $db=>$params) {
+                       if (empty($params['name'])) self::$dbq[$db]['name'] = $db;
+                       if (empty($params['id'])) self::$dbq[$db]['id'] = $db;
 
                        # Ignore incomplete
                        if (empty($params['type'])) {
-                               unset($dbs[$db]);
+                               unset(self::$dbq[$db]);
                                continue;
                        }
 
-                       foreach ($default as $k=>$v) if (!isset($params[$k])) $dbs[$db][$k] = $v;
+                       foreach ($default as $k=>$v) if (!isset($params[$k])) self::$dbq[$db][$k] = $v;
                }
 
                #
                # Sort by `order`, min first
                #
-               uasort($dbs,function($a,$b){
+               uasort(self::$dbq,function($a,$b){
                        if (empty($a['order']) and empty($b['order'])) return strcmp($a['id'],$b['id']);
                        $a_ = !empty($a['order']) ? $a['order'] : 9999999;
                        $b_ = !empty($b['order']) ? $b['order'] : 9999999;
@@ -818,9 +820,11 @@ class Db extends nb {
                #
                # Return
                #
-               if (!$dbs) return false;
-               if ($first !== false) $first = self::ar_first($dbs);
-               return $dbs;
+               if (!self::$dbq) return false;
+               if ($first !== false) $first = self::ar_first(self::$dbq);
+
+               return self::$dbq;
+               #self::$dbq = array_merge(self::$dbq,$dbs);
        }
 
        /**
index 07a84eae07dfb61e15cb62a33f82608cd6223ee0..50d793fe348854fe5172c97ab1a3f9ab02ef5061 100644 (file)
@@ -1611,7 +1611,7 @@ Class Table extends nb {
 
                $html = '';
 
-               $html .= '<table class="rows wp-list-table widefat striped">'.NB_EOL;
+               $html .= '<table class="table table-bordered rows wp-list-table widefat striped">'.NB_EOL;
 
                if ($this->show_header) {
                        $html .= '<thead>'.NB_EOL;
index 167a5f465a4186b04f1dc51ebf7852e6224175f7..889abdc52bc95119b71f5034822d04bedc1fe997 100644 (file)
@@ -436,11 +436,16 @@ class NB {
                static $Spyc;
                if (!isset($Spyc)) {
 
-                       if (!function_exists('yaml_emit')) {
+                       # NB 10.09.18: Can't trust devian package php-yaml 
+                       if (true or !function_exists('yaml_emit')) {
                                require_once(NB_ROOT.'/lib/php/Spyc.php');
-                               function yaml_emit($data) { return Spyc::YAMLDump($data, false, false, true); }
-                               function yaml_parse_file($file) { return Spyc::YAMLLoad($file); }
-                               function yaml_parse($str) { return Spyc::YAMLLoadString($str); }
+                               function _yaml_emit($data) { return Spyc::YAMLDump($data, false, false, true); }
+                               function _yaml_parse_file($file) { return Spyc::YAMLLoad($file); }
+                               function _yaml_parse($str) { return Spyc::YAMLLoadString($str); }
+                       } else {
+                               function _yaml_emit($data) { return yaml_emit($data, false, false, true); }
+                               function _yaml_parse_file($file) { return yaml_parse_file($file); }
+                               function _yaml_parse($str) { return yaml_parse($str); }
                        }
 
                        $Spyc = true;
@@ -449,7 +454,7 @@ class NB {
 
        public static function yaml_parse_file($file) {
                self::yaml_init();
-               return yaml_parse_file($file);
+               return _yaml_parse_file($file);
        }
 
        /*
@@ -457,7 +462,7 @@ class NB {
         */
        public static function yaml_encode($row) {
                self::yaml_init();
-               $yaml = yaml_emit($row);
+               $yaml = _yaml_emit($row);
                $yaml = preg_replace('/^---\s*/','',$yaml);
                $yaml = preg_replace('/\n\.\.\.$/','',$yaml);
                $yaml = trim($yaml);
@@ -467,7 +472,7 @@ class NB {
 
        public static function yaml_decode($str) {
                self::yaml_init();
-               return yaml_parse($str);
+               return _yaml_parse($str);
        }
 
        /*
index 9b562d00266f42b55f79828346d4fcbc61250548..87fc90aaa0af7cb1b4b8cd204e0f4741707a0bc8 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 return [
   'is_html' => true,
-  'enclose' => array("<table class=\"rows widefat striped\">".NB_EOL,"</table>".NB_EOL),
+  'enclose' => array("<table class=\"table table-bordered rows widefat striped\">".NB_EOL,"</table>".NB_EOL),
   'tag_enclose' => 'tr class="row"',
   'tag_head' => 'th',
   'tag' => 'td',
index 8f36157f13342d5af09fd6f9c887d6ab28bd1ddc..da5aeca13440e76c7143197ad9eba655af7bf883 100644 (file)
@@ -94,6 +94,7 @@ class DbQ extends nb {
        public $page;
        public $table;
        public $db;
+       private $conf;
 
        public function __construct($opt=[]) {
                //
@@ -267,24 +268,27 @@ class DbQ extends nb {
                // Write output
                if ($this->expires and !preg_match('/^(dump|insert|update|replace|rm|vi)$/',$this->params['action'])) $this->page->expires = $this->expires;
                $this->page->headers_no_cache();
+
+               // Conf for js or info
+               $this->conf = [
+                       'db.base' => ( empty($this->db) ? '' : $this->db->base ),
+                       'table.base' => ( empty($this->table) ? '' : $this->table->base ),
+                       'default.format' => $this->format_html,
+                       'default.limit' => $this->default_limit,
+                       'param.format' => $this->params['format'],
+                       'param.db' => $this->params['db'],
+                       'param.table' => $this->params['table'],
+                       'param.action' => $this->params['action'],
+                       'param.args' => $this->params['args'],
+                       'param.deep' => $this->params_deep,
+                       'text.add' => 'Add New',
+                       'text.clear' => 'Clear',
+                       'perm' => $this->perm,
+                       'perms' => $this->perms,
+               ];
+
                if (empty($this->_nopage)) {
 
-                       $this->conf = [
-                               'db.base' => ( empty($this->db) ? '' : $this->db->base ),
-                               'table.base' => ( empty($this->table) ? '' : $this->table->base ),
-                               'default.format' => $this->format_html,
-                               'default.limit' => $this->default_limit,
-                               'param.format' => $this->params['format'],
-                               'param.db' => $this->params['db'],
-                               'param.table' => $this->params['table'],
-                               'param.action' => $this->params['action'],
-                               'param.args' => $this->params['args'],
-                               'param.deep' => $this->params_deep,
-                               'text.add' => 'Add New',
-                               'text.clear' => 'Clear',
-                               'perm' => $this->perm,
-                               'perms' => $this->perms,
-                       ];
                        $this->page->js_code = 'window._dbq = '.json_encode($this->conf);
 
                        if ($obj != 'logout') {
@@ -1192,7 +1196,6 @@ EOF;
                } elseif ($action == 'cryptkey') { $this->page($this->cryptkey());
                } elseif ($action == 'status') { $this->page($this,'status');
                } elseif ($action == 'conf') { $this->page($this,'conf');
-               } elseif ($action == 'conf') { $this->page($this,'conf');
                } elseif ($action == 'html') { $this->page(Mime::html($arg));
 
                } elseif ($action == 'types') {