<?php
require_once(realpath(dirname(__FILE__).'/../config.php'));
require_once(realpath(dirname(__FILE__).'/../db.php'));
-$DB_CONFS = array_merge([]
+if (empty($_SERVER['HOME'])) $_SERVER['HOME'] = '';
+$DB_CONFS = array_merge([],
# ::ROOT_DIR
- ,[
+ [
Db::ROOT_DIR.'/etc/dbq.yml',
Db::ROOT_DIR.'/etc/dbq.yaml',
Db::ROOT_DIR.'/etc/dbq.php',
- ]
- ,(array)glob(Db::ROOT_DIR.'/etc/dbq/*.yml')
- ,(array)glob(Db::ROOT_DIR.'/etc/dbq/*.yaml')
- ,(array)glob(Db::ROOT_DIR.'/etc/dbq/*.php')
+ ],
+ (array)glob(Db::ROOT_DIR.'/etc/dbq/*.yml'),
+ (array)glob(Db::ROOT_DIR.'/etc/dbq/*.yaml'),
+ (array)glob(Db::ROOT_DIR.'/etc/dbq/*.php'),
# /etc
- ,[
+ [
'/etc/dbq.yml',
'/etc/dbq.yaml',
'/etc/dbq.php',
- ]
- ,(array)glob('/etc/dbq/*.yml')
- ,(array)glob('/etc/dbq/*.yaml')
- ,(array)glob('/etc/dbq/*.php')
+ ],
+ (array)glob('/etc/dbq/*.yml'),
+ (array)glob('/etc/dbq/*.yaml'),
+ (array)glob('/etc/dbq/*.php'),
# ~/
- ,[
- '~/.dbq.yml',
- '~/.dbq.yaml',
- '~/.dbq.php',
- ]
- ,(array)glob($_ENV['HOME'].'/.dbq/*.yml')
- ,(array)glob($_ENV['HOME'].'/.dbq/*.yaml')
- ,(array)glob($_ENV['HOME'].'/.dbq/*.php')
-);
+ [
+ $_SERVER['HOME'].'/.dbq.yml',
+ $_SERVER['HOME'].'/.dbq.yaml',
+ $_SERVER['HOME'].'/.dbq.php',
+ ],
+ (array)glob($_SERVER['HOME'].'/.dbq/*.yml'),
+ (array)glob($_SERVER['HOME'].'/.dbq/*.yaml'),
+ (array)glob($_SERVER['HOME'].'/.dbq/*.php'),
+ #] ),
+[]);
#bye($DB_CONFS);
$DB_CONFS = Db::conf_load($DB_CONFS);
require_once(realpath(dirname(__FILE__).'/../db.php'));
require_once(realpath(dirname(__FILE__).'/../db/field.php'));
require_once(realpath(dirname(__FILE__).'/../out.php'));
+$DB_TABLE_QUERY_ID = 0;
+function __table_define() {
+ define('TABLE_INDENT',NB_EOL ? "\t" : "");
+ define('TABLE_CSV_SEP',Table::p('sep',"\t"));
-define('TABLE_INDENT',NB_EOL ? "\t" : "");
-define('TABLE_CSV_SEP',nb::p('sep') ? nb::p('sep') : "\t");
+ if (!defined('TABLE_TEMPLATE')) define('TABLE_TEMPLATE',dirname(__FILE__).'/../../../share/templates');
-if (!defined('DB_HTML_EDIT')) define('DB_HTML_EDIT','Edit');
-if (!defined('DB_HTML_DELETE')) define('DB_HTML_DELETE','Delete');
+ if (!defined('DB_HTML_EDIT')) define('DB_HTML_EDIT','Edit');
+ if (!defined('DB_HTML_DELETE')) define('DB_HTML_DELETE','Delete');
-# Create a temporary table when table is a SELECT
-if (!defined('DB_TABLE_QUERY_NAME')) define('DB_TABLE_QUERY_NAME','_query_');
-$DB_TABLE_QUERY_ID = 0;
+ # Create a temporary table when table is a SELECT
+ #if (!defined('DB_TABLE_QUERY_NAME'))
+ define('DB_TABLE_QUERY_NAME','_query_');
+}
Class Table extends nb {
if ($opt_by_val !== null) $opt = $opt_by_val;
- $format = $this->p('format');
- if (!$format) bye("Parameter `format` missing!");
+ #if (isset($opt['format']) and empty($opt['format'])) {
+ if (isset($opt['format']) and $opt['format']==='') {
+ $format = '';
+ } else {
+
+ $format = empty($opt['format']) ? $this->p('format') : $opt['format'];
+ if (!$format) bye("Parameter `format` missing!");
+ }
list($sql,$where,$limit,$select_count) = $this->rows_sql($opt);
$st = $this->db()->conn->prepare($sql);#,[PDO::ATTR_ERRMODE => PDO::ERRMODE_SILENT]);
$st->execute();
+ if (!empty($opt['query'])) return $st;
#
# Fields
#
$out_conf = null;
$opt['use_out'] = false;
+
if ($this->p('out') or !preg_match('/^('.join('|',
- [ 'table','sql','div','wp','_csv','_xml','_json','_yaml' ] # local
+ [ '','template','table','sql','div','wp','_csv','_xml','_json','_yaml' ] # local
).')$/',$format))
{
$opt['use_out'] = true;
if (!($out_conf = out::types($format))) $this->bye("Unknow format `$format`");
- out::type($format);
- self::$params += array_values(out::types());
+ $out_conf = array_merge($opt,$out_conf);
+
+ # Function name should be format
+ out::type($format); self::$params += array_values(out::types());
if (empty($out_conf['enclose'])) $out_conf['enclose'] = ['',''];
debug('Using out module!',3);
$call = null;
$call = $this->rows_parsers($row,$opt);
- /*
- if ($parser) $this->db()->table_row_decrypt($this,$row);
- if ($parser and !empty($this->db()->row_parse)) {
- $call = $this->db()->row_parse; $call($row);
- }
-
- if ($parser and !empty($this->row_parse_pre)) {
- $call = $this->row_parse_pre; $call($row);
- }
-
- if ($opt['is_html'] and !$out_conf) {
- foreach ($row as $k=>$v) {
- if (!isset($this->extras[$k])) $row[$k] = out::format($v);
- }
-
- }
-
- if ($parser and !empty($this->row_parse_post)) {
- $call = $this->row_parse_post; $call($row);
- }
- */
-
- #
- # Close previous
- #
-# NB 27.09.16 if ($out_conf and $count !== 1) out::row_end($out_conf);
#
# Fields filter
if ($call) {
foreach (array_keys($row) as $name) {
-# NB 19.11.16 if (!$this->fields($name)) {
if (!isset($fields[$name])) {
$fields[$name] = new Field($name);
$fields[$name]->dyn = true;
-# NB 19.11.16 $this->fields[$name] = $fields[$name];
}
}
}
if ($out_conf) {
-# NB 19.11.16 out::head($out_conf,array_keys($this->fields()),[$row]);
out::head($out_conf,array_keys($fields),[$row]);
} else {
-# NB 19.11.16 echo $this->{"rows_begin_$format"}($this->fields(),$opt);
echo $this->{"rows_begin_$format"}($fields,$opt);
}
if ($out_conf) {
out::row($out_conf,$row);
+
} else {
echo $this->{"rows_rec_$format"}($row,$opt);
}
} # < is_html
- if ($count === 0 and $this->p('header') === 'force') {
-# NB 19.11.16 echo $this->{"rows_begin_$format"}($this->fields());
- echo $this->{"rows_begin_$format"}($fields);
+ if ($count === 0 and (
+ $this->p('header') === 'force'
+ or !$format
+ )) {
+ echo $this->{"rows_begin_$format"}($fields,$opt);
}
if ($out_conf) {
return $count;
}
+ /*-----------------------------------------------------------------
+ No Out
+ -----------------------------------------------------------------*/
+ public function rows_begin_($fields,&$o) {
+ $o['sep'] = '';
+ $o['var'] = [
+ 'head' => array_keys($fields),
+ 'rows' => [],
+ ];
+ }
+
+ public function rows_rec_($row,&$o) {
+ $o['var']['rows'][] = $row;
+ }
+
+ public function rows_end_() {
+ }
+
+ /*-----------------------------------------------------------------
+ Template
+ -----------------------------------------------------------------*/
+ public function rows_begin_template($fields,&$o) {
+ if (empty($id) and !( $id=self::p('table.template.id') ) ) self::bye('Wrong parameter!');
+
+ $id = preg_replace('/[^\w\._-]/','',$id);
+
+ $file = TABLE_TEMPLATE.'/'.$id.'.php';
+ if (!is_readable($file)) return false; #self::bye("Wrong id `$id`");
+ $o['file'] = $file;
+ $HEAD = array_keys($fields);
+ $ROWS = [];
+ $ROW = [];
+ require $o['file'];
+ }
+
+ public function rows_rec_template($row,&$o) {
+ $HEAD = [];
+ $ROWS = [$row];
+ $ROW = $row;
+ require $o['file'];
+ }
+
+ public function rows_end_template() {
+ unset($this->__file);
+ unset($this->__fields);
+ }
+
/*-----------------------------------------------------------------
Wordpress
-----------------------------------------------------------------*/
} elseif ($this->p('format') and !preg_match('/^(table|div)$/',$this->p('format'))) {
- $this->rows($dummy,['format' => $this->p('format')]);
+ $opt = ['format' => $this->p('format')];
+ $this->rows($opt);
return true;
} elseif ($action == 'edit') {
return $this->status;
}
+ public function template($id=null) {
+ if (empty($id) and !( $id=self::p('table.template.id') ) ) self::bye('Wrong parameter!');
+
+ $id = preg_replace('/[^\w\._-]/','',$id);
+
+ $file = TABLE_TEMPLATE.'/'.$id.'.php';
+ if (!is_readable($file)) return false; #self::bye("Wrong id `$id`");
+
+ $opt = [ 'format'=> '' ];
+ $this->rows($opt);
+
+ $HEAD = &$opt['var']['head'];
+ $ROWS = &$opt['var']['rows'];
+ $ROW = &$opt['var']['rows'][0];
+ #$opt['var']['rows']=[];
+
+ $_REQUEST_BAK = $_REQUEST;
+ $_REQUEST = array_merge($_REQUEST,$ROW);
+
+ $ex = require $file;
+
+ $_REQUEST = $_REQUEST_BAK;
+
+ #return $ex;
+ }
} # < Class
+__table_define();
?>