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);
# Pdo
$this->connect_init();
+ if ($connect) $this->connect();
# Params
/*
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
# NB 26.10.16 }
# NB 26.10.16 }
- if ($p = $this->p('db.host')) $this->host = $p;
-
# Add args to pdo
foreach ([
'host' => 'host',
}
- # 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')) {
*/
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;
#debug(array_keys($this->tables()));
$new = []
- #+( empty($this->id) ? [] : ['id' => $this->id])
+[
'id' => (empty($this->id) ? '' : $this->id),
'name' => $this->name,
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);
$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 = [];
;
// 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) {
$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"',
*/
public static function debug_backtrace_info($call_info) {
+ // Error into string
$errorType = array (
E_ERROR => 'ERROR',
E_WARNING => 'WARNING',
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']))
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');
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
$this->page = new Page([
'css' => $this->css,
'sep' => $this->sep_title,
+ #'body_class' => 'fixed',
]);
if ($run) $this->run();
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
'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);
#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;
}
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;
// 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
$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'];
}
- public function table() {
+ public function table($param=[]) {
if (!in_array($this->params['table'],array_keys($this->db->tables()))) {
}
- $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;
$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));
}
);
#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;
$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();
$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,[
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 {
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;
});
*/
+ // 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",''
- +' <input type="button" class="clear '+e.className+'" onclick="form_clean(this.form)" value="Clear"/>'
- +(window._dbq['table.base'] ? ' <input type="button" class="add '+e.className+'" onclick="window.location=\''+window._dbq['table.base']+'add/\'" value="Add"/>' : '')
+ +' <input type="button" class="clear '+e.className+'" onclick="form_clean(this.form)"'
+ +' value="'+window._dbq['text.clear']+'"'
+ +' />'
+ +( window._dbq['table.base']
+ ? ' <input type="button" class="add '+e.className+'"'
+ +' onclick="window.location=\''+window._dbq['table.base']+'add/\'"'
+ +' value="'+window._dbq['text.add']+'"/>'
+ : ''
+ )
);
});
-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