/*****************************************************************************
Class DB
- Render different output format ex: html, xml, yaml, csv
+ Render different output format example: html, xml, yaml, csv
from any database
*****************************************************************************/
if (!defined('DB_HTML_EDIT')) define('DB_HTML_EDIT','Edit');
if (!defined('DEBUG')) define('DEBUG',0);
-if (!function_exists('err')) {
- function err($err) {
- die( is_scalar($err) ? $err : print_r($err) );
- }
-}
-
class db {
var $tables = array();
} else {
- @$GLOBALS['PAGE']['is_text'] = false;
return false;
}
- @$GLOBALS['PAGE']['is_text'] = true;
return true;
}
#$_REQUEST['op'] = strtoupper(@$_REQUEST['op']) == 'AND' ? 'AND' : 'OR';
$_REQUEST['op'] = strtoupper(@$_REQUEST['op']) == 'OR' ? 'OR' : 'AND';
- @$GLOBALS['PAGE']['js_code'] = 'var sem_ui_tables = ['.join(',',ar_map('str_quote($a)',$tables)).']; var sem_ui_table = '.str_quote(@$_REQUEST['table']).';';
-
}
function html_menu() {
return $this->fields();
}
- function fields() {
+ /*
+ * Function db.page
+ *
+ * print any pages according to params
+ *
+ * @param (array)
+ */
+ function page($Page,$param=null) {
+ if ($param === null) $param = $_REQUEST;
+ elseif (!is_array($param)) $param = array('name' => $param);
+
+ foreach (array(
+ 'action' => 'list',
+ ) as $k => $v) {
+ }
+
+ $Page->title = prettyText($this->name);
+
+ #if ($Page->is('html')) $this->form_criterias();
+
+ if (@$_REQUEST['format']) $this->print_header($_REQUEST['format']);
+ if (@$_REQUEST['format']) {
+ $this->rows(array('format' => @$_REQUEST['format']));
+ return 0;
+ }
+
+ #die($Page->content_type);
+ $Page->begin();
+ echo '<div id="msg"></div>'.PHP_EOL;
+ echo '<div id="db">'.PHP_EOL;
+ #$Page->debug($this->tables());
+
+
+ echo $this->html_menu();
+
+ /*
+ Rows
+ */
+ if (@$this) $this->rows(array(
+ 'format' => @$_REQUEST['format'],
+ ));
+
+ // END
+ echo '</div>'.PHP_EOL;
+ #$Page->out($Db->query2v("SELECT 1"));
+ $Page->end();
+ return 0;
+ }
+
+ /*
+ * Function db.fields
+ *
+ * return all or one fields from a table
+ *
+ * @name (string) name of the field to return. Default: null
+ * @return (array) return null where name does not exsts
+ */
+ function fields($name=null) {
if (!$this->fields) {
}
+ if ($name !== null ) {
+ if (!isset($this->fields[$name])) return null;
+ return $this->fields[$name];
+ }
+
return $this->fields;
}
return $this;
}
- function debug($msg,$level=0) {
- if ($level and $level>DEBUG) return;
- $msg = is_scalar($msg) ? $msg : print_r($msg,true);
-
- if (!@$GLOBALS['PAGE']['is_text']) {
- echo '<pre class="debug">'
- .(@$_SERVER['HTTP_HOST'] ? htmlentities($msg) : $msg)
- .'</pre>'.PHP_EOL;
- return;
- }
- echo "DEBUG: $msg\n";
-
- }
+ function debug($msg,$level=0) { return debug($msg,$level); }
function url_params($k='',$v='') {
$params = array();
- foreach (array_merge(
- $this->params,
- array_keys($this->fields())
- ) as $f) {
+ foreach (array_merge( $this->params, array_keys($this->fields()) ) as $f) {
if (@strcmp($_REQUEST[$f],'')==0) continue;
$params[$f] = $_REQUEST[$f];
return $_txt2md->text($txt);
}
-function benchmark($function=null,$limit=1000000) {
- global $_benchmark;
-
- if ($_benchmark === null) {
- $_benchmark = array('.' => microtime(true));
- }
-
- if ($function === null) {
- $prev = $_benchmark['.'];
- foreach ($_benchmark as $lib => $sec) {
- if ($lib === '.') continue;
- printf("%-30s %s\n",$lib,($sec-$prev));
- $prev = $sec;
- }
- return;
- }
-
- for ($i=0; $i<$limit; $i++) {
- $function();
- }
-
- $_benchmark[$function] = microtime(true);
-}
-
function bye($msg='') {
if ($msg) err($msg);
exit;
function err($msg) {
$msg = is_scalar($msg) ? $msg : print_r($msg,true);
- echo( !@$GLOBALS['PAGE']['is_text'] ? '<pre class="err">'.$msg.'</pre>' : "ERR: $msg").PHP_EOL;
+ echo( preg_match('/ml/i',get_header('Content-type')) ? '<pre class="err">'.$msg.'</pre>' : "ERR: $msg").PHP_EOL;
}
function debug($msg,$level=0) {
if ($level and $level>DEBUG) return;
-
$msg = is_scalar($msg) ? $msg : print_r($msg,true);
- if (!@$GLOBALS['PAGE']['is_text']) {
+
+ if (preg_match('/ml/i',get_header('Content-type'))) {
echo '<pre class="debug">'
.(@$_SERVER['HTTP_HOST'] ? htmlentities($msg) : $msg)
.'</pre>'.PHP_EOL;
+
} else {
echo "DEBUG: $msg\n";
+
}
}
return $new_argv;
}
+
+function benchmark($function=null,$limit=1000000) {
+ global $_benchmark;
+
+ if ($_benchmark === null) {
+ $_benchmark = array('.' => microtime(true));
+ }
+
+ if ($function === null) {
+ $prev = $_benchmark['.'];
+ echo "Iteration: $limit\n";
+ foreach ($_benchmark as $lib => $sec) {
+ if ($lib === '.') continue;
+ printf("%-30s %s\n",$lib,($sec-$prev));
+ $prev = $sec;
+ }
+ return;
+ }
+
+ for ($i=0; $i<$limit; $i++) {
+ $function();
+ }
+
+ $_benchmark[$function] = microtime(true);
+}
+
+function get_header($name,$value=null) {
+ $v = array_map(create_function('$a',
+ #'return $a;'
+ 'return( preg_match("/^'.$name.':\s+(\S+)/i",$a,$m) ? $m[1] : null );'
+ ),headers_list());
+
+ return $v ? $v[0] : null;
+}
?>