From: Nicolas Boisselier Date: Sat, 9 Apr 2016 01:35:29 +0000 (+0100) Subject: Bed X-Git-Url: https://git.nbdom.net/?a=commitdiff_plain;h=1771fbd13d64aeb48ed7eaed34a79944edb37569;p=nb.git Bed --- diff --git a/etc/dbs.php b/etc/dbs.php index fc67de09..5982fa15 100644 --- a/etc/dbs.php +++ b/etc/dbs.php @@ -2,109 +2,6 @@ /* */ $CONF = array ( - 'rent' => array ( - 'order' => 1, - 'pdo' => 'sqlite:/opt/rent/rent.db', - 'title' => 'Rent', - 'notice' => 'Micro foncier 4BE', - 'default_table' => 'rent', - 'tables' => array ( - 'place' => array ( - 'replace' => array ( - 'id' => 'addr.addr', - ), - ), - 'rent' => array ( - 'order_by' => 'start desc', - 'replace' => array ( - 'idtenant' => 'tenant.name', - 'idplace' => 'addr.addr', - ), - 'extras' => array ( - 'total' => array ( - 'type' => 'float(8,2)', - 'extra' => 'ROUND(rent+charge,2)', - ), - 'rent_year' => array ( - 'type' => 'float(8,2)', - 'extra' => 'rent * (1 + ( strftime(\'%m\',end)+12*strftime(\'%Y\',end) ) - ( strftime(\'%m\',start)+12*strftime(\'%Y\',start) ))', - ), - ), - ), - ), - 'name' => 'rent', - ), - 'ui-mem' => array ( - 'pdo' => 'sqlite:/dev/shm/semantico.db', - 'title' => 'Semantico UI', - 'default_table' => 'node', - 'tables' => array ( - 'node' => array ( - 'order_by' => '(SELECT value FROM fact WHERE idnode=id AND name=\'date_install\') DESC', - 'replace_' => array ( - 'iddc' => 'dc.id', - ), - ), - ), - 'replace' => array ( - 'SELECT name FROM dc WHERE id=\'@id\'' => '.node td.iddc', - ), - 'name' => 'ui-mem', - ), - 'rt' => array ( - 'pdo' => 'pgsql:host=db.rt.semantico.net;port=5432;dbname=rtdb;user=rtuser;password=', - 'title' => 'RT', - 'password' => 'scrubchug', - 'name' => 'rt', - ), - 'ui' => array ( - 'pdo' => 'sqlite:/opt/semantico/product/releases/sem_ui/db/semantico.db', - 'title' => 'Semantico UI', - 'default_table' => 'node', - 'tables' => array ( - 'node' => array ( - 'order_by' => '(SELECT value FROM fact WHERE idnode=id AND name=\'date_install\') DESC', - 'replace_' => array ( - 'iddc' => 'dc.id', - ), - ), - ), - 'replace' => array ( - 'SELECT name FROM dc WHERE id=\'@id\'' => '.node td.iddc', - ), - 'name' => 'ui', - ), - 'postfix' => array ( - 'title' => 'Postfix on Izideal', - 'host' => 'media.izideal.vpn', - 'user' => 'postfix', - 'password' => 'dx7d50nfs', - 'type' => 'mysql', - 'name' => 'postfix', - ), - 'izidev' => array ( - 'host' => 'big.cascais.loc', - 'title' => 'Mysql Izi on Big', - 'password' => 'mpc60ax73', - 'default_table' => 'process', - 'user' => 'nico', - 'name' => 'izi', - 'tables' => array ( - 'site' => array ( - 'replace' => array ( - 'idlang' => 'lang.id', - ), - ), - ), - 'type' => 'mysql', - ), - 'wp' => array ( - 'host' => 'admin.izideal.vpn', - 'user' => 'nico', - 'password' => 'mpc60ax73', - 'type' => 'mysql', - 'name' => 'wp', - ), 'nb' => array ( 'host' => 'admin.izideal.vpn', 'user' => 'nico', @@ -119,66 +16,18 @@ $CONF = array ( 'idtenant' => 'tenant.name', 'idplace' => 'addr.addr', ), - 'columns' => array ( - 'total' => function(&$r) {$r['rent'] + $r['charge'];} - ), + 'call_rows' => function(&$r) { + if (0) $r['rent_year'] = $r['rent'] * (1 + + ( date('m',$r['end'])+12*date('Y',strtotime($r['end'])) ) + - + ( date('m',$r['start'])+12*date('Y',strtotime($r['start'])) ) + ); + #extra: "rent * (1 + ( strftime('%m',end)+12*strftime('%Y',end) ) - ( strftime('%m',start)+12*strftime('%Y',start) ))" + #$r['rent_year'] = $r['rent'] * (1 + ( date('m',strtotime($r['end']))+12 + $r['Total'] = $r['rent'] + $r['charge']; + } ), ), ), - 'puppetdb' => array ( - 'title' => 'Puppetdb', - 'host' => 'big', - 'type' => 'pgsql', - 'user' => 'puppetdb', - 'default_table' => 'view_hosts', - 'tables' => array ( - 'hosts' => array ( - 'extras' => array ( - 'last_compile' => array ( - 'type' => 'text', - 'extra' => 'to_char(hosts.last_compile, \'YYYY-MM-DD HH:mm:ss\'::text)', - ), - 'last_freshcheck' => array ( - 'type' => 'text', - 'extra' => 'to_char(hosts.last_freshcheck, \'YYYY-MM-DD HH:mm:ss\'::text)', - ), - 'updated_at' => array ( - 'type' => 'text', - 'extra' => 'to_char(hosts.updated_at, \'YYYY-MM-DD HH:mm:ss\'::text)', - ), - 'created_at' => array ( - 'type' => 'text', - 'extra' => 'to_char(hosts.created_at, \'YYYY-MM-DD HH:mm:ss\'::text)', - ), - 'test' => array ( - 'type' => 'int', - 'extra' => 1, - ), - ), - ), - ), - 'password' => 'puppetdb6073/!', - 'name' => 'puppetdb', - ), - 'izi' => array ( - 'title' => 'Mysql Izi on Admin', - 'default_table' => 'process', - 'host' => 'admin.izideal.vpn', - 'user' => 'izideal', - 'name' => 'izi', - 'tables' => array ( - 'site' => array ( - 'replace' => array ( - 'idlang' => 'lang.id', - ), - ), - ), - 'password' => 'dx7d50nfs', - 'type' => 'mysql', - ), - 'crypt' => array ( - 'pdo' => 'sqlite:/dev/shm/crypt.db', - 'name' => 'crypt', - ), ); ?> diff --git a/lib/js/nb.js b/lib/js/nb.js index 96e94d28..1585c40e 100644 --- a/lib/js/nb.js +++ b/lib/js/nb.js @@ -512,10 +512,23 @@ function NB() { $('

'+msg+'

').prependTo( "#nb-msg" ); } + this.reload_head_js = function() { + $("head script").each(function(){ + var oldScript = this.getAttribute("src"); + $(this).remove(); + var newScript; + newScript = document.createElement('script'); + newScript.type = 'text/javascript'; + newScript.src = oldScript; + document.getElementsByTagName("head")[0].appendChild(newScript); + }); + } + }; /* Statics */ +NB.reload_head_js = NB.prototype.reload_head_js NB.get_http = NB.prototype.get_http NB.dump = NB.prototype.dump NB.urlParam = NB.prototype.urlParam diff --git a/lib/php/db.php b/lib/php/db.php index 2ea82d19..aafd5fce 100644 --- a/lib/php/db.php +++ b/lib/php/db.php @@ -536,9 +536,8 @@ EOF; $h = array_replace_recursive($h,$yaml); } elseif (preg_match('/\.php$/i',$file)) { - #global $VAR1; - require($file); - if (!empty($CONF)) $h = $CONF; + #global $CONF; + require($file); if (!empty($CONF)) $h = array_replace_recursive($h,$CONF); } diff --git a/lib/php/db/field.php b/lib/php/db/field.php index 15d8cf9f..b2b53d5e 100644 --- a/lib/php/db/field.php +++ b/lib/php/db/field.php @@ -11,11 +11,13 @@ $DB_FIELD_TYPES = array( class field { private $table; public $name; - public $type = 'text'; + #public $type = 'text'; + public $type; public $null = true; public $key = false; + #public $value; public $default; - public $autoincrement = false; + public $autoincrement; public $extra; public function __sleep() { return array( @@ -38,7 +40,21 @@ class field { } public function numeric() { - return preg_match('/int|float|number|currency/',$this->type) ? true : false; + return preg_match('/signed|(snall )?int|float|decimal|numeric|number|currency/i',$this->type) ? true : false; + } + + public function html_type() { + # See: http://www.w3schools.com/html/html_form_input_types.asp + if (strpos($this->name,'password') !== false) return 'password'; + if ($this->numeric() !== false) return 'number'; + return 'text'; + # Html5 only + if (strpos($this->name,'email') !== false) return 'email'; # no safari + if (strpos($this->name,'color') !== false) return 'color'; # no safari, no ie + # NOT READY + if (strpos($this->type,'datetime') !== false) return 'datetime'; + if (strpos($this->type,'time') !== false) return 'time'; + if (strpos($this->type,'date') !== false) return 'date'; } public function html_edit($value) { @@ -46,7 +62,13 @@ class field { return '
' .($this->extra ? ''.$this->out($value).'' - : '' + : 'size() and is_scalar($size) ? ' size="'.$size.'"' : '') + .' type="'.$this->html_type().'"' + .'/>' ) .'
'.NB_EOL; @@ -64,6 +86,7 @@ class field { public function size() { if (! preg_match('/\((\d+)(?:,(\d+)\))?/',$this->type,$m) ) return null; array_shift($m); +#debug($m); return count($m) == 1 ? $m[0] : $m; // NB 02.08.15 return preg_match('/\((\d+)\)/',$this->type,$m) ? $m[1] : null; } @@ -169,7 +192,11 @@ class field { # See: http://php.net/manual/en/pdostatement.bindparam.php if ($name === null) $name = ':'.$this->name; #debug($this->name . " | $name | $value | $this->type | " . $this->size()."
"); - return $sth->bindParam($name, $value, $this->type2pdo($value), $this->size()); + if (strpos($this->type,'float') !== false) { + $value = strval($value); + return $sth->bindParam($name, $value, PDO::PARAM_STR); + } + return $sth->bindParam($name, $value, $this->type2pdo($this->type), $this->size()); // NB 27.07.15 return $sth->bindParam($name, $value, $this->type2pdo($value)); } diff --git a/lib/php/db/table.php b/lib/php/db/table.php index d4950d4c..c13ffb7e 100644 --- a/lib/php/db/table.php +++ b/lib/php/db/table.php @@ -22,6 +22,7 @@ Class Table extends nb { # NB 28.03.16 public $fields_keys; public $replace = array(); # replace by javascript public $extras = array(); + public $call_rows; # Function to call in rows() public $count; public $engine; public $created; @@ -38,13 +39,12 @@ Class Table extends nb { $opt = $name; #$name = $opt['name']; $name = isset($opt['name']) ? $opt['name'] : ''; - if (!empty($opt['name'])) $this->name = $opt['name']; - - } elseif(!empty($name)) { - $this->name = $name; } + if(!empty($name)) $opt['name'] = $this->name = $name; + #debug($opt); + #unset($opt['db']); bye($opt); // Extras - We dont want to affect $this @@ -86,7 +86,7 @@ Class Table extends nb { #$temp = '_'.substr(md5($this->name),0,6); $temp = DB_TABLE_QUERY_NAME; #bye("CREATE TEMPORARY VIEW $temp AS $this->name"); - $this->db()->conn->query("CREATE TEMPORARY VIEW $temp AS $this->name"); + $this->db()->conn->query("CREATE TEMPORARY TABLE $temp AS $this->name"); $this->name = $temp; } elseif (preg_match('/\b(\.import|LOAD DATA|COPY|INSERT|REPLACE|DELETE|TRUNCATE|CREATE|DROP|ALERT)\b/',$this->name)) { @@ -256,9 +256,12 @@ Class Table extends nb { public function html_edit($values = null) { if ($values === null) $values = $this->p(); if (!is_array($values)) $values = array($values); + $fields = array_filter($this->fields(),function($v){return empty($v->extra);}); + #$fields = $this->fields(); + #debug($fields); $add = false; - $where = $this->where($this->fields(),$values); + $where = $this->where($fields,$values); if (empty($where)) { $where = ' WHERE 1=0'; $add = true; @@ -280,7 +283,7 @@ Class Table extends nb { if ( $add or ($row = $st->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT))) { $count ++; - foreach ($this->fields() as $name => $field) { + foreach ($fields as $name => $field) { if ($add and !preg_match('/^(null)?$/',$field->default)) $row[$name] = $field->default; elseif(!isset($row[$name])) $row[$name] = ''; echo $field->html_edit($row[$name] @@ -572,8 +575,8 @@ Class Table extends nb { $st->execute(); # Fields + $fields = array(); if (!DB_TABLE_QUERY_NAME) { - $fields = array(); foreach ($this->db()->fields($st) as $f) { $fields[$f->name] = $f; } $this->fields = $fields; } @@ -615,6 +618,10 @@ Class Table extends nb { $count++; + if (!empty($this->call_rows)) { + $call = $this->call_rows; $call($row); + } + if ($opt['is_html'] and !$out_conf) { foreach ($row as $k=>$v) { if (!isset($this->extras[$k])) $row[$k] = out::format($v); @@ -627,6 +634,16 @@ Class Table extends nb { if ($count === 1) { if ($opt['is_html']) echo $this->html_rows_top(); + if ($this->call_rows) { + foreach (array_keys($row) as $name) { + if (!$this->fields($name)) { + $this->fields[$name] = new Field($name); + $this->call = true; + #$fields[$name] = $this->fields[$name]; + } + } + } + if ($out_conf) { out::head($out_conf,$this->fields(),array($row)); @@ -875,7 +892,8 @@ Class Table extends nb { foreach ($fields as $name => $f) { #$html .= ''.($this->p('buttons')==='0' ? $f : $this->url_sort($f)).''.NB_EOL; - $html .= ''.($this->p('buttons')==='0' ? $this->prettyText($name) : $this->url_sort($name)).''.NB_EOL; + $sort = $this->p('buttons')!=='0' and empty($f->call); + $html .= ''.(!$sort ? $this->prettyText($name) : $this->url_sort($name)).''.NB_EOL; } if ($this->p('buttons')!=='0' and DB_HTML_DELETE) $html .= ''.NB_EOL; $html .= ''.NB_EOL.''.NB_EOL; @@ -891,14 +909,14 @@ Class Table extends nb { $html = ''.NB_EOL; - if ($this->p('buttons')!=='0') $html .= ''.$buttons[0].''.NB_EOL; + if ($this->p('buttons')!=='0') $html .= ''.$buttons[0].''.NB_EOL; foreach ($row as $k => $v) { if (isset($this->extras[$k])) $k .= " extra"; $html .= ''.$v.''.NB_EOL; } - if ($this->p('buttons')!=='0') $html .= ''.$buttons[1].''.NB_EOL; + if ($this->p('buttons')!=='0') $html .= ''.$buttons[1].''.NB_EOL; $html .= ''.NB_EOL; @@ -1289,7 +1307,7 @@ Class Table extends nb { // Tables - see default.js if you change class $r .= ''; $r .= ''.html_select_array(array_keys($this->db()->tables()),array( - 'html' => 'class="tables" id="table" name="table" id="table"', + 'html' => 'class="tables" name="table" id="table"', 'selected' => $this->p('table'), 'prettyText' => true, ));