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;
$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();
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;
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);
'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("<div class=\"rows\">".NB_EOL,"</div>".NB_EOL),
'tag_enclose' => 'div class="row"',
'tag_key' => 'label',
'row' => 'out_tag',
# NB 25.12.16 'head' => 'out_tag_head',
- ),
+ ],
- 'center' => array(
+ 'center' => [
'is_html' => true,
'enclose' => array("<center class=\"rows\">".NB_EOL,"</center>".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("<table class=\"rows widefat striped\">".NB_EOL,"</table>".NB_EOL),
'tag_enclose' => 'tr class="row"',
'tag' => 'td',
'row' => 'out_tag',
'head' => 'out_tag_head',
- ),
+ ],
- 'xml' => array(
+ 'xml' => [
'enclose' => array('<?xml version="1.0" encoding="utf-8"?'.'>'.NB_EOL."<rows>".NB_EOL,"</rows>".NB_EOL),
'eol' => self::p('eol',NB_EOL),
'row' => function (&$o,&$row) {
}
echo ($o['eol'] ? ' ' : '')."</row>".$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",""],
}
],
- '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' => ',',
- ),
+ ],
];
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_'];});
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;
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' => '',
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);
}
$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));