From: Nicolas Boisselier Date: Thu, 23 Nov 2017 02:25:25 +0000 (+0000) Subject: www/dbq/dbq.php X-Git-Url: https://git.nbdom.net/?a=commitdiff_plain;h=e8876cd227ef86776a1eef7143e8e8121f75d1ff;p=nb.git www/dbq/dbq.php --- diff --git a/lib/php/db/table.php b/lib/php/db/table.php index d277110d..a87ddc66 100644 --- a/lib/php/db/table.php +++ b/lib/php/db/table.php @@ -511,14 +511,8 @@ Class Table extends nb { return html_edit($values,$form_action,true); } - public function html_edit($values = null,$form_action='?',$add=false) { - if ($values === null) $values = $this->p(); - if (!is_array($values)) $values = [$values]; - - $fields = $this->fields(); - $keys = $this->fields_keys(); - - $where = $this->where($fields,$values); + public function sql_edit($values = null,&$add=false) { + $where = $this->where($this->fields(),$values); if (empty($where)) { $where = ' WHERE 1=0'; $add = true; @@ -529,8 +523,20 @@ Class Table extends nb { $sql = "SELECT *" . $this->select_extras(); $sql .= " FROM ".$this->sql_name().$where; if ($this->type == 'sql' and !empty($this->sql)) $sql = $this->sql.$where; - $this->debug(preg_replace('/(,|FROM|WHERE|HAVING|GROUP|ORDER)/i',"\n\\1",$sql),1); + + return $sql; + } + + public function html_edit($values = null,$form_action='?',$add=false) { + if ($values === null) $values = $this->p(); + if (!is_array($values)) $values = [$values]; + +# NB 23.11.17 $fields = $this->fields(); +# NB 23.11.17 $keys = $this->fields_keys(); + + $sql = $this->sql_edit($values,$add); + $st = $this->db()->conn->prepare($sql); $st->execute(); @@ -583,7 +589,7 @@ Class Table extends nb { if (!empty($row)) $this->db()->table_row_decrypt($this,$row); $count ++; - foreach ($fields as $name => $field) { + foreach ($this->fields() as $name => $field) { if ($add and !preg_match('/^(null)?$/',strtolower($field->default))) { $row[$name] = $field->default; diff --git a/lib/php/out.php b/lib/php/out.php index 19e5d87b..583d149d 100644 --- a/lib/php/out.php +++ b/lib/php/out.php @@ -4,9 +4,11 @@ require_once(dirname(__FILE__).'/nb.php'); Class Out extends Nb { public static $charset = 'utf-8'; + protected static $types; protected static $type; protected static $header = true; + public static function init() { self::$header = (bool)self::p('header',self::$header); @@ -118,7 +120,7 @@ Class Out extends Nb { 'php_print_r' => ['row' => function(&$o,&$r) {print_r($r);},], 'php_var_export' => ['row' => function(&$o,&$r) {var_export($r);},], - 'div' => array( + 'div' => [ 'is_html' => true, 'enclose' => array("
".NB_EOL,"
".NB_EOL), 'tag_enclose' => 'div class="row"', @@ -126,18 +128,18 @@ Class Out extends Nb { 'tag_key' => 'label', 'row' => 'out_tag', # NB 25.12.16 'head' => 'out_tag_head', - ), + ], - 'center' => array( + 'center' => [ 'is_html' => true, 'enclose' => array("
".NB_EOL,"
".NB_EOL), 'tag_enclose' => 'div class="row"', 'tag' => 'div', 'row' => 'out_tag', # NB 25.12.16 'head' => 'out_tag_head', - ), + ], - 'table' => array( + 'table' => [ 'is_html' => true, 'enclose' => array("".NB_EOL,"
".NB_EOL), 'tag_enclose' => 'tr class="row"', @@ -145,9 +147,9 @@ Class Out extends Nb { 'tag' => 'td', 'row' => 'out_tag', 'head' => 'out_tag_head', - ), + ], - 'xml' => array( + 'xml' => [ 'enclose' => array(''.NB_EOL."".NB_EOL,"".NB_EOL), 'eol' => self::p('eol',NB_EOL), 'row' => function (&$o,&$row) { @@ -157,20 +159,20 @@ Class Out extends Nb { } echo ($o['eol'] ? ' ' : '')."".$o['eol']; }, - ), + ], - 'txt' => array( + 'txt' => [ 'col' => self::p('col',' : '), 'sep' => self::p('sep',"\n"), 'eol' => self::p('eol',"--\n"), - ), + ], - 'csv' => array( + 'csv' => [ 'sep' => self::punescape('sep',"\t"), 'quote' => self::p('quote',''), 'quote_escape' => self::punescape('quote_escape','\\'), 'eol' => self::punescape('eol',"\n"), - ), + ], 'yaml' => [ 'enclose' => ["---\n",""], @@ -181,12 +183,12 @@ Class Out extends Nb { } ], - 'json' => array( + 'json' => [ 'enclose' => array('['.self::p('eol',NB_EOL),']'), 'row' => function(&$o,&$row) { return json_encode($row); }, 'eol' => self::p('eol',NB_EOL), 'rec' => ',', - ), + ], ]; @@ -212,9 +214,9 @@ Class Out extends Nb { return parent::__construct($opt); } - public static function charset($set=null) { if (!empty($set)) self::$charset = $set; return self::$charset; } - public static function type($set=null) { if (!empty($set)) self::$type = $set; return self::$type; } - public static function header($set=null) { if (!empty($set)) self::$header = $set; return self::$header; } + public static function charset($set=null) { if (isset($set)) self::$charset = $set; return self::$charset; } + public static function type($set=null) { if (isset($set)) self::$type = $set; return self::$type; } + public static function header($set=null) { if (isset($set)) self::$header = $set; return self::$header; } public static function types($type=null) { if ($type === '_web_') return array_filter(self::$types,function($v){return !isset($v['_web_']) or (bool)$v['_web_'];}); diff --git a/www/dbq/dbq.php b/www/dbq/dbq.php index 5fa86d8e..c7dbb7d5 100644 --- a/www/dbq/dbq.php +++ b/www/dbq/dbq.php @@ -7,6 +7,9 @@ require_once(NB_ROOT.'/lib/php/http.php'); require_once(NB_ROOT.'/lib/php/mime.php'); class DbQ extends nb { + + const ACTIONS_NO_TITLE = ['ls','vi']; + const PARAM_DB_DEFAULT = 'ls'; const ADMIN = 9; const DELETE = 4; @@ -32,10 +35,9 @@ class DbQ extends nb { public $uri; public $uri_params; - public $param_args_sep = '|'; - public $param_exp_value = '[\w\._:-]{2,100}'; - const ACTIONS_NO_TITLE = ['ls','vi']; - const PARAM_DB_DEFAULT = 'ls'; + private $param_args_sep = '|'; + private $param_exp_value = '[\w\._:-]{2,100}'; + public $params = [ 'format' => '', 'db' => '', @@ -265,19 +267,54 @@ class DbQ extends nb { return $this->db; } - public function table_html_add() { - $this->table_html_edit(true); + public function add() { + $this->vi(true); } - public function table_html_edit($add=false) { + private function vi_extract_fields() { + $sep = $this->param_args_sep . $this->param_args_sep; + #$sep ='@'; + + $fields = []; + if (strpos($this->params['args'],$sep) !== false) { + list($fields,$this->params['args']) = explode($sep, $this->params['args']); + $fields = explode(',',$fields); + } + + return $fields; + } + + public function vi($add=false) { $all = []; + $keys = $this->table->fields_keys($all); if (!$keys) $keys = $all; $keys = array_keys($keys); + $fields = $this->vi_extract_fields(); + $values = $add ? array_fill(0,count($keys),'') : explode($this->param_args_sep,$this->params['args']); + $values = array_combine($keys,$values); + + # NB 23.11.17: Handle format for /vi/ + if (true and $this->params['format'] != $this->format_html) { + $row = $this->db->query2h($this->table->sql_edit($values)); + if ($fields) { + foreach ($row as $k=>$v) { + if (!in_array($k,$fields)) unset($row[$k]); + } + } + + #$this->db->out->header(false); + $this->db->out($row); + #out::header(false); + #out::rows($this->params['format'],$row); + #bye($this->db->out->header()); + #print_r($values); + return; + } - $this->table->html_edit(array_combine($keys,$values), + $this->table->html_edit($values, $this->table->base . '/' . ($add ? 'insert' : 'update' . '/' . urlencode($this->params['args'])) . '/' ,$add); } @@ -997,10 +1034,10 @@ EOF; $this->page($this->table->status()+$this->table->status(['fields'])); } elseif ($action == 'add' and $this->perm >= self::WRITE) { - $this->page($this,'table_html_add'); + $this->page($this,'add'); } elseif ($action == 'vi' and $this->perm >= self::READ) { - $this->page($this,'table_html_edit'); + $this->page($this,'vi'); } elseif ($action == 'insert' and $this->perm >= self::WRITE) { if (!$this->table->insert($_POST,$info)) $this->error('insert: '.print_r($info,true));