From 955bca1570e778d28ad9a7fc8e624d66293ad18e Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Fri, 16 Sep 2016 16:41:02 +0200 Subject: [PATCH] fix bug on actions tables --- etc/dbs.php | 13 +++++++------ etc/dbs/nb.php | 18 ++---------------- etc/dbs/owncloud.php | 6 +++--- lib/php/db.php | 12 ++++++++---- lib/php/db/table.php | 29 ++++++++++++++++++++--------- lib/php/nb.php | 12 ++++++++++++ 6 files changed, 52 insertions(+), 38 deletions(-) diff --git a/etc/dbs.php b/etc/dbs.php index 7f6460b3..a9e352f6 100644 --- a/etc/dbs.php +++ b/etc/dbs.php @@ -58,6 +58,7 @@ if ($DIR_SQLITE) { 'title' => 'created by dbs.php', '_import' => $name ]; + #if (!empty($CONF[$name])) $CONF[$id] = array_merge_recursive($CONF[$name],$CONF[$id]); } } @@ -83,9 +84,7 @@ $CONF['all'] = [ ], ]; -#debug($CONF['postfix']); foreach ($CONF as $id => $db) { - #debug($db); if (0 or empty($db['type'] ) or $db['type']!='sqlite' @@ -94,26 +93,28 @@ foreach ($CONF as $id => $db) { #or !is_readable($fname) or !is_readable($db['host']) ) continue; - #debug(basename($db['host'],'.db')); $fname = basename($db['host'],'.db'); $CONF['all']['types']['exec'][] = "ATTACH DATABASE '".$db['host']."' as ".$fname.""; conf_merge($CONF[$id],$CONF['all']); } -conf_merge($CONF['_nb'],$CONF['all']); conf_merge($CONF['nb'],$CONF['all']); +return 1; + function conf_merge(&$c1,&$c2) { - if (!empty($c1['tables'])) + if (!empty($c1['tables'])) { #debug('zaza'); #$c2['tables'] = array_merge($c2['tables'],$c1['tables']); #$c2['tables'] #$c1['tables'] + if (empty($c1['tables'])) $c1['tables'] = []; foreach ($c1['tables'] as $k=>$v) { + #debug($k); $c2['tables'][$k] = $v; } - ; + } foreach ([ 'default_table', diff --git a/etc/dbs/nb.php b/etc/dbs/nb.php index 4a27e852..c2de8764 100644 --- a/etc/dbs/nb.php +++ b/etc/dbs/nb.php @@ -4,23 +4,8 @@ $CONF['nb'] = array( 'host' => 'admin.izideal.vpn', 'type' => 'mysql', 'name' => 'nb', - #'order' => ($LOCAL_DB ? null : 1), - #'order' => 1, - '_import' => ['_nb','_nico'], -); -if (strpos(php_uname("n"),'ovh.net')!==false) $CONF['nb']['order'] = 1; + '_import' => ['_nico'], -/***************************************************************************** -if ($LOCAL_DB and !empty($CONF['nb-sqlite']) and isset($CONF['nb']['order'])) { - $CONF['nb-sqlite']['order'] = isset($CONF['nb']['order']) ? $CONF['nb']['order'] : 1; - unset($CONF['nb']['order']); -} -*****************************************************************************/ -#die(print_r(posix_getpwnam('nico'),true)); -#die(print_r(posix_getpwuid(posix_getuid()),true)); -#die(print_r(pathinfo('~/.gitconfig'),true)); -#die(file_exists(realpath('~/.gitconfig')) ? 'yes' : 'no'); -$CONF['_nb'] = array( 'title' => 'Nb', 'notice' => 'Micro foncier 4BE', 'default_table' => 'rent', @@ -79,6 +64,7 @@ $CONF['_nb'] = array( ), ); +if (strpos(php_uname("n"),'ovh.net')!==false) $CONF['nb']['order'] = 1; return 1; function tenant_doc(&$r) { return rent_doc('tenant',$r); } diff --git a/etc/dbs/owncloud.php b/etc/dbs/owncloud.php index 2d4a7fab..70abbaa7 100644 --- a/etc/dbs/owncloud.php +++ b/etc/dbs/owncloud.php @@ -5,9 +5,9 @@ $CONF['owncloud'] = [ 'type' => 'mysql', 'host' => 'admin.izideal.vpn', #'default_table' => 'oc_users', - 'default_table' => 'last_files', + 'default_table' => 'oc_last_files', 'tables' => [ - 'view_contact' => [ + 'oc_contact' => [ #'sql' => "SELECT * FROM oc_contacts_cards_properties", 'sql' => "SELECT c.id" #.",org.value as org" @@ -31,7 +31,7 @@ $CONF['owncloud'] = [ , 'row_parse_pre' => 'owncloud_row_parse', ], - 'last_files' => [ + 'oc_last_files' => [ 'sql' => "SELECT path,mtime FROM oc_filecache WHERE path NOT LIKE 'files_versions/%' ORDER BY fileid DESC LIMIT 10", 'row_parse_pre' => function(&$r) { $r['mtime'] = date ("Y-m-d H:i:s",$r['mtime']); diff --git a/lib/php/db.php b/lib/php/db.php index b8cf5bc5..a1a167b7 100644 --- a/lib/php/db.php +++ b/lib/php/db.php @@ -88,6 +88,8 @@ class Db extends nb { # Tables if (isset($opt['tables'])) { + #debug($opt['tables']); + #foreach ($opt['tables'] as $name=>$param) debug($name); foreach ($opt['tables'] as $name=>$param) $this->table($name,$param); unset($opt['tables']); } @@ -310,7 +312,7 @@ class Db extends nb { */ public function table($name,$params=[]) { #if (!array_key_exists($name,$this->tables)) { - if (!$name) bye('empty name'); + if (!$name) bye('table name is empty'); if (empty($this->tables[$name])) { $this->tables[$name] = new Table($name,['db'=>$this]+$params); } elseif ($params) { @@ -414,9 +416,6 @@ class Db extends nb { #if (!isset($this->tables)) { $this->_tables = true; -# NB 09.09.16 $sql = $this->type('tables',true,$this->type); -# NB 09.09.16 if (is_callable($sql)) $sql = $sql($this); -# NB 09.09.16 $sql = $this->unvar($sql); $sql = $this->method('tables'); foreach ($this->conn->query($sql,PDO::FETCH_ASSOC) as $row) { @@ -456,6 +455,7 @@ class Db extends nb { } public function action($action,$table=null) { + #debug(($this->tables)); $available = [ ['(db.)?help','This help'], ['(db.)?ls','List databases from conf'], @@ -489,7 +489,9 @@ class Db extends nb { #if ($count = $this->p('count','')) $this->pset('count',1); $engine = $this->p('engine',''); + #var_dump ($this->tables()); foreach ($this->tables() as $t) { + #debug($t->name); if (!empty($name) and !$this->str_match($t->name,$name)) continue; if (!empty($type) and !$this->str_match($t->type,$type)) continue; if (!empty($count) and !$this->str_match($t->count,$count)) continue; @@ -498,6 +500,8 @@ class Db extends nb { $rows[] = $t->status(); } + #bye($rows); + $rows = self::array_fill_assoc($rows); usort($rows,function($a,$b) { return strcmp($a['name'],$b['name']); }); $return = $this->out($rows); diff --git a/lib/php/db/table.php b/lib/php/db/table.php index a856b80e..2c3ce138 100644 --- a/lib/php/db/table.php +++ b/lib/php/db/table.php @@ -23,8 +23,8 @@ Class Table extends nb { public $type; # table, view, sql public $sql; # NB 28.03.16 public $fields_keys; - public $replace = array(); # replace by javascript - public $extras = array(); + public $replace = []; # replace by javascript + public $extras = []; public $row_parse_pre; # Function to call in rows() public $row_parse_post; # Function to call in rows() public $count; @@ -33,14 +33,14 @@ Class Table extends nb { public static $is_admin = true; # hidden, sort, ... fields - public static $params = array( 'db', 'table', 'limit', 'debug', 'action' + public static $params = [ 'db', 'table', 'limit', 'debug', 'action' , 'page', 'paged' # wordpress - ); + ]; public $fields; public $indexes; - function __construct($name,$opt=array()) { + function __construct($name,$opt=[]) { if (!is_scalar($name)) { $opt = $name; @@ -1352,6 +1352,7 @@ Class Table extends nb { public function out($v,$head=array()) { return $this->db()->out($v,$head); } public function infos() { + debug('TODEL'); return ['TODEL']; # TODEL - NB 07.09.16 $this->type(); @@ -1678,17 +1679,26 @@ Class Table extends nb { } public function status($key=null) { + #if ($this->type!='table' and $this->type=!'view') { } if (!isset($this->status)) { - $this->status = []; + $this->create_temporary(); - $sql = $this->db()->type('tables'); - if (is_callable($sql)) $sql = $sql($this->db()); - #$sql .= ' AND name='.$this->db()->quote($this->name); + $sql = $this->db()->method('tables'); $sql = "SELECT * FROM ($sql) t WHERE name=".$this->db()->quote($this->name); $sth = $this->db()->conn->query($sql,PDO::FETCH_ASSOC); if (!empty($sth)) $this->status = $sth->fetch(); + if (empty($this->status)) $this->status = []; + # Add to status array + foreach ([ + 'name', + 'type', + ] as $k) { + if (!empty($this->$k) and empty($this->status[$k])) $this->status[$k] = $this->$k; + } + + # Add from status array foreach ([ 'engine', 'created', @@ -1707,6 +1717,7 @@ Class Table extends nb { if (is_array($this->status[$k])) $this->status[$k] = count($this->status[$k]); } + #debug([$this->name,$this->status,$this->sql()]); if (!empty($key)) return ( empty($this->status[$key]) ? '' : $this->status[$key] ); return $this->status; } diff --git a/lib/php/nb.php b/lib/php/nb.php index 0b446678..e349f95d 100644 --- a/lib/php/nb.php +++ b/lib/php/nb.php @@ -775,5 +775,17 @@ class NB { return $v ? $v[0] : ''; } + public static function array_fill_assoc($rows) { + $keys = []; + foreach($rows as $k=>$v) { + $keys = array_merge_recursive(array_keys($v),$keys); + } + foreach($rows as $k=>$v) { + foreach ($keys as $key) { + if (empty($v[$key])) $rows[$k][$key] = ''; + } + } + return $rows; + } } # < Class ?> -- 2.47.3