From: Nicolas Boisselier Date: Tue, 27 Dec 2016 19:20:33 +0000 (+0000) Subject: dbq X-Git-Url: https://git.nbdom.net/?a=commitdiff_plain;h=bed17862cee0642e4bd3c7ae0f6016b4eb244cdb;p=nb.git dbq --- diff --git a/lib/php/db/table.php b/lib/php/db/table.php index 0939f196..14683115 100644 --- a/lib/php/db/table.php +++ b/lib/php/db/table.php @@ -945,7 +945,7 @@ Class Table extends nb { $opt['use_out'] = false; if ($this->p('out') or !preg_match('/^('.join('|', - [ '','template','table','sql','div','wp' ] # local + [ '','template','table','sql','_div','wp' ] # local ).')$/',$format)) { @@ -1347,13 +1347,14 @@ Class Table extends nb { #if (!empty($opt)) $html .= '' . $this->nav($opt['count'],$opt['tot'],$opt['limit']) . '' .NB_EOL; $this->__rows_table_attr = []; - $this->__rows_table_keys = []; +# NB 27.12.16 $this->__rows_table_keys = []; foreach ($fields as $name => $f) { $this->__rows_table_attr[$name] = '' . ' class="' . $name . ($f->key?' key':'') . (isset($this->extras[$name])?' extra':'') . '"' ; - if ($f->key) $this->__rows_table_keys[] = $name; +# NB 27.12.16 if ($f->key) $this->__rows_table_keys[] = $name; } +# NB 27.12.16 if (empty($this->__rows_table_keys)) $this->__rows_table_keys = array_keys($fields); if ($this->show_header) { $html .= ''.NB_EOL; @@ -1393,13 +1394,13 @@ Class Table extends nb { $buttons = $this->html_row_buttons($row); - $id = []; - foreach ($this->__rows_table_keys as $k) { - $id[] = $row[$k]; - } - $id = empty($id) ? '' : ' '.'__'.join('__',$id).'__'; - - $html = ''.NB_EOL; +# NB 27.12.16 $id = []; +# NB 27.12.16 foreach ($this->__rows_table_keys as $k) { +# NB 27.12.16 $id[] = $row[$k]; +# NB 27.12.16 } +# NB 27.12.16 $id = empty($id) ? '' : ' '.'__'.join('__',$id).'__'; +# NB 27.12.16 $html = ''.NB_EOL; + $html = ''.NB_EOL; if ($this->buttons() and !empty($buttons[0])) $html .= ''.$buttons[0].''.NB_EOL; @@ -1415,7 +1416,8 @@ Class Table extends nb { } public function rows_end_table() { - unset($this->__rows_table_attr,$this->__rows_table_keys); + unset($this->__rows_table_attr); +# NB 27.12.16 unset($this->__rows_table_keys); $html = ''; $html .= ''.NB_EOL; $html .= ''.NB_EOL; diff --git a/lib/php/nb.php b/lib/php/nb.php index 972d768b..54749a0d 100644 --- a/lib/php/nb.php +++ b/lib/php/nb.php @@ -591,18 +591,14 @@ class NB { if (in_array($name,get_class_methods($class))) $rows = $class->$name(); elseif (in_array($name,get_class_vars($class_name))) $rows = $class->$name; else self::bye("Unknown action: `$action`"); - #else $rows = $class->$name; + if (is_array($rows) and !$rows) return $rows; if (!$rows) return false; if ($rows === true) return true; - #bye($rows); if (is_scalar($rows)) { - #echo "$rows\n"; $rows = array($name => $rows); - #return true; } - #debug($action.' '.$class_name.' '.$name); out::rows(self::p('format',self::php_cli() ? 'csv' : 'table'),$rows,(is_scalar($rows) ? $name : [])); return true; } diff --git a/lib/php/out.php b/lib/php/out.php index 77fe9956..16712b01 100644 --- a/lib/php/out.php +++ b/lib/php/out.php @@ -67,8 +67,8 @@ Class Out extends Nb { if ($size) { $align = $size>0 ? STR_PAD_LEFT : STR_PAD_RIGHT; $size = abs($size); - if ($count>1) $size -= mb_strlen($o['sep'],out::$charset); - $v = out::mb_str_pad($v, $size, ' ',$align); + if ($count>1) $size -= mb_strlen($o['sep'],self::$charset); + $v = self::mb_str_pad($v, $size, ' ',$align); } return $v; @@ -162,7 +162,7 @@ Class Out extends Nb { 'row' => function (&$o,&$row) { echo ($o['eol'] ? ' ' : '')."".$o['eol']; foreach ($row as $k => $v) { - echo ($o['eol'] ? ' ' : '')."<$k>".$o['eol']; + echo ($o['eol'] ? ' ' : '')."<$k>".$o['eol']; } echo ($o['eol'] ? ' ' : '')."".$o['eol']; }, @@ -184,7 +184,7 @@ Class Out extends Nb { 'yaml' => [ 'enclose' => ["---\n",""], 'row' => function (&$o,&$row) { - $yaml = '- '.out::yaml_encode($row); + $yaml = '- '.self::yaml_encode($row); $yaml = preg_replace("/^(?!-)/m"," ",$yaml); echo $yaml; } @@ -256,7 +256,6 @@ Class Out extends Nb { } public static function type_call($k,&$o,&$row=[]) { - static $methods; if (!isset($o[$k])) { @@ -277,6 +276,11 @@ Class Out extends Nb { $call = $o[$k]; echo $call($o,$row); + # Internal method + } elseif(is_scalar($o[$k]) and preg_match('/^out_tag|out_human/',$o[$k])) { + $f = $o[$k]; + echo self::$f($o,$row); + # String } elseif(is_scalar($o[$k])) { echo $o[$k]; @@ -344,7 +348,7 @@ Class Out extends Nb { # Assume html/xml tag style } elseif (isset($o['tag'])) { - $ret = out_tag($o,$row); + $ret = self::out_tag($o,$row); } else { # txt @@ -446,7 +450,7 @@ Class Out extends Nb { */ public static function row_parse(&$row,$o=[]) { foreach ($row as $k => $v) { - $row[$k] = out::format($v,null,$o); + $row[$k] = self::format($v,null,$o); } if (isset($o['row_parse_post'])) { $o['row_parse_post']($row); @@ -553,7 +557,7 @@ Class Out extends Nb { if (!empty($o['quote'])) $v = $o['quote'] . str_replace($o['quote'],$o['quote_escape'].$o['quote'],$v) . $o['quote']; - $values[] = str_replace("\n",'\\n',out::scalar($v)); + $values[] = str_replace("\n",'\\n',self::scalar($v)); } if (!empty($o['return'])) return join($o['sep'],$values); @@ -576,164 +580,160 @@ Class Out extends Nb { ); } -} Out::init() ; # < Class -/*--------------------------------------------------------------------------*/ -// Functions -/*--------------------------------------------------------------------------*/ - -// -// Tag -// -function out_tag_head($o,&$data) { - if (isset($o['tag_head'])) $o['tag'] = $o['tag_head']; - $row = out::ar_first($data); - if (!out::is_hash($row)) $row = $data; - if (!is_array($row)) return 0; - - # For object - if (out::is_hash($row)) $row = array_combine(array_keys($row),array_keys($row)); - - # Avoid: name - if (!out::is_hash($row)) $row = array_combine(array_values($row),array_values($row)); - out_tag($o,$row); -} - -function out_tag(&$o,&$row) { - if (isset($o['tag_enclose'])) echo (NB_EOL ? ' ' : '').'<'.$o['tag_enclose'].'>'.NB_EOL; - - out::row_parse($row,$o); - foreach ($row as $k => $v) { - - # Inside tag, ex: label - if (isset($o['tag_key'])) { - $v = '<'.$o['tag_key'].'>'.$k.''.$v; - } - - # Tag = o[tag] or key - if (isset($o['tag'])) { - $class = is_numeric($k) ? '' : ' class="'.$k.'"'; - $k = $o['tag']; - } else { - $class = ''; - } - - echo '' - .(NB_EOL ? ' ' : '') - ."<$k$class>$v" - .NB_EOL - ; + // + // Tag + // + public static function out_tag_head($o,&$data) { + if (isset($o['tag_head'])) $o['tag'] = $o['tag_head']; + $row = self::ar_first($data); + if (!self::is_hash($row)) $row = $data; + if (!is_array($row)) return 0; + + # For object + if (self::is_hash($row)) $row = array_combine(array_keys($row),array_keys($row)); + + # Avoid: name + if (!self::is_hash($row)) $row = array_combine(array_values($row),array_values($row)); + self::out_tag($o,$row); } - if (isset($o['tag_enclose'])) echo (NB_EOL ? ' ' : '').''.NB_EOL; -} + public static function out_tag(&$o,&$row) { + if (isset($o['tag_enclose'])) echo (NB_EOL ? ' ' : '').'<'.$o['tag_enclose'].'>'.NB_EOL; -// -// Human -// -function out_human_head(&$o,$head) { - #$line = "a\tb\tc\t\t\tg"; bye(explode("\t",$line)); - - if (!out::header()) return ''; - $o['_human'] = [ - 'head' => 1, - ]; - if (out::is_hash($head)) $head = array_keys($head); - return out_human($o,$head); -} + self::row_parse($row,$o); + foreach ($row as $k => $v) { -function out_human(&$o,&$row) { + # Inside tag, ex: label + if (isset($o['tag_key'])) { + $v = '<'.$o['tag_key'].'>'.$k.''.$v; + } - # Init options - if (empty($o['_human'])) $o['_human'] = []; - if (!isset($o['_human']['len'])) { - $o['_human']['len'] = []; + # Tag = o[tag] or key + if (isset($o['tag'])) { + $class = is_numeric($k) ? '' : ' class="'.$k.'"'; + $k = $o['tag']; + } else { + $class = ''; + } - $o['_human']['sep'] = $o['sep']; - $o['sep'] = "\t"; + echo '' + .(NB_EOL ? ' ' : '') + ."<$k$class>$v" + .NB_EOL + ; + } - $o['_human']['eol'] = $o['eol']; - $o['eol'] = ""; + if (isset($o['tag_enclose'])) echo (NB_EOL ? ' ' : '').''.NB_EOL; + } - $o['tmpfile'] = tmpfile(); + // + // Human + // + public static function out_human_head(&$o,$head) { + #$line = "a\tb\tc\t\t\tg"; bye(explode("\t",$line)); - # no csv echo call - $o['return'] = true; - $o['_human']['len'] = []; + if (!self::header()) return ''; + $o['_human'] = [ + 'head' => 1, + ]; + if (self::is_hash($head)) $head = array_keys($head); + return self::out_human($o,$head); } - $line = out::row_csv($o,$row); - $line = str_replace('\n',' ',$line); + public static function out_human(&$o,&$row) { - # Search max length string - $i = 0; - foreach (explode($o['sep'],$line) as $v) { - $len = mb_strlen($v,out::$charset); + # Init options + if (empty($o['_human'])) $o['_human'] = []; + if (!isset($o['_human']['len'])) { + $o['_human']['len'] = []; - if (0 - or empty($o['_human']['len'][$i]) - or $len > $o['_human']['len'][$i] - ) $o['_human']['len'][$i] = $len; + $o['_human']['sep'] = $o['sep']; + $o['sep'] = "\t"; - $i++; - } + $o['_human']['eol'] = $o['eol']; + $o['eol'] = ""; - if (!isset($o['tmpfile'])) { - echo "\n"; - out::bye("Option tmpfile is missing, wrong uage of this funtion!"); - } + $o['tmpfile'] = tmpfile(); - fwrite($o['tmpfile'],$line."\n"); + # no csv echo call + $o['return'] = true; + $o['_human']['len'] = []; + } -} + $line = self::row_csv($o,$row); + $line = str_replace('\n',' ',$line); -function out_human_end(&$o) { - $len = $o['_human']['len']; - fseek($o['tmpfile'], 0); + # Search max length string + $i = 0; + foreach (explode($o['sep'],$line) as $v) { + $len = mb_strlen($v,self::$charset); + + if (0 + or empty($o['_human']['len'][$i]) + or $len > $o['_human']['len'][$i] + ) $o['_human']['len'][$i] = $len; - $sep_line = ''; - if (out::p('sep_line') !== '0') { + $i++; + } - foreach ($len as $i) { - $sep_line .= $o['corner'].str_repeat($o['line'],$i+strlen($o['_human']['sep'])-1); + if (!isset($o['tmpfile'])) { + echo "\n"; + self::bye("Option tmpfile is missing, wrong uage of this funtion!"); } - $sep_line .= $o['corner'].$o['_human']['eol']; + + fwrite($o['tmpfile'],$line."\n"); + } - $header = empty($o['_human']['head']) ? 0 : 1; - $count = 0; - while (($line = fgets($o['tmpfile'])) !== false) { + public static function out_human_end(&$o) { + $len = $o['_human']['len']; + fseek($o['tmpfile'], 0); - $values = []; - $i = 0; - foreach (explode($o['sep'],rtrim($line,"\n")) as $v) { - $values[] = out::mb_str_pad($v, $len[$i], ' ', STR_PAD_RIGHT); - $i++; + $sep_line = ''; + if (self::p('sep_line') !== '0') { + + foreach ($len as $i) { + $sep_line .= $o['corner'].str_repeat($o['line'],$i+strlen($o['_human']['sep'])-1); + } + $sep_line .= $o['corner'].$o['_human']['eol']; } - # header - if (!$count) echo $sep_line; + $header = empty($o['_human']['head']) ? 0 : 1; + $count = 0; + while (($line = fgets($o['tmpfile'])) !== false) { - if ($header or $count>0) echo '' - . ltrim($o['_human']['sep']) - . join($o['_human']['sep'],$values) - . rtrim($o['_human']['sep']) - .$o['_human']['eol']; + $values = []; + $i = 0; + foreach (explode($o['sep'],rtrim($line,"\n")) as $v) { + $values[] = self::mb_str_pad($v, $len[$i], ' ', STR_PAD_RIGHT); + $i++; + } - if ($header and !$count) echo $sep_line; - $count++; - } + # header + if (!$count) echo $sep_line; + + if ($header or $count>0) echo '' + . ltrim($o['_human']['sep']) + . join($o['_human']['sep'],$values) + . rtrim($o['_human']['sep']) + .$o['_human']['eol']; + + if ($header and !$count) echo $sep_line; + $count++; + } - fclose($o['tmpfile']); - unset($o['_human']); + fclose($o['tmpfile']); + unset($o['_human']); - if ($header) $count--; - if (!$count) return; + if ($header) $count--; + if (!$count) return; - echo $sep_line; - if ($header) echo "$count Records\n"; + echo $sep_line; + if ($header) echo "$count Records\n"; -} -# < Functions + } + +} Out::init() ; # < Class /****************************************************************************/ #die("A NB_EOL B \n"); @@ -757,5 +757,5 @@ $data = array( */ #$data = array( array('a'=>'A'), array('b'=>'B'), array('c'=>'C') ); $field = array(); -$o = out::rows(!empty($argv[2]) ? $argv[2] : 'csv',$data,$field); +$o = self::rows(!empty($argv[2]) ? $argv[2] : 'csv',$data,$field); ?> diff --git a/www/dbq/dbq.php b/www/dbq/dbq.php index 4db08e7a..1fc658d5 100644 --- a/www/dbq/dbq.php +++ b/www/dbq/dbq.php @@ -245,13 +245,13 @@ class DbQ extends nb { $args = $GLOBALS['dbq_args']; - $r = ['@' => join(' ',[ + $r = ['Vew' => join(' ',[ 'View' ]) ] + $r; if ($this->perm < self::DELETE) return; $rm = $this->table->base.'rm/'.$args; - $r['-'] = '' + $r['Delete'] = '' .'
' .'Delete' .$this->form_hidden($r)