]> git.nbdom.net Git - nb.git/commitdiff
improvment
authornboisselier <nicolas.boisselier@gmail.com>
Wed, 1 Jul 2015 00:03:02 +0000 (01:03 +0100)
committernboisselier <nicolas.boisselier@gmail.com>
Wed, 1 Jul 2015 00:03:02 +0000 (01:03 +0100)
lib/php/db.php
lib/php/functions.php
lib/php/page.php

index eba3955d8c1464b7001e89aea06c654073094e91..d01fba5330765979b09be6446b501dcb5dba77e4 100644 (file)
@@ -2,7 +2,7 @@
 /*****************************************************************************
 
   Class DB
-  Render different output format ex: html, xml, yaml, csv
+  Render different output format example: html, xml, yaml, csv
   from any database
 
 *****************************************************************************/
@@ -10,12 +10,6 @@ require_once(dirname(__FILE__).'/functions.php');
 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();
@@ -166,11 +160,9 @@ EOF;
 
     } else {
 
-      @$GLOBALS['PAGE']['is_text'] = false;
       return false;
     }
 
-    @$GLOBALS['PAGE']['is_text'] = true;
     return true;
   }
 
@@ -193,8 +185,6 @@ EOF;
     #$_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() {
@@ -281,7 +271,64 @@ class table {
     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) {
 
@@ -343,6 +390,11 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
 
     }
 
+    if ($name !== null ) {
+      if (!isset($this->fields[$name])) return null;
+      return $this->fields[$name];
+    }
+
     return $this->fields;
   }
 
@@ -419,28 +471,13 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
     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];
index c306eed0d865ad1a28da2b4307256842214e084f..9dea64b382547889ba9804faf71c7e80af9c8d5e 100644 (file)
@@ -91,30 +91,6 @@ function txt2md($txt) {
   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;
@@ -126,19 +102,21 @@ function warn ($msg) {
 
 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";
+
   }
 
 }
@@ -242,4 +220,38 @@ function argv2request() {
   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;
+}
 ?>
index d973db837ed95938362de7cf53986f1f9dbaef25..9d279a672c4a408ab0d4ffd6e96e614486c42e59 100644 (file)
@@ -211,6 +211,21 @@ class Page {
 
   }
 
+  /*
+   * Function client_content_type
+   * Return the head Accept from the client
+   */
+  function client_content_type() {
+    echo split(',', getallheaders()['Accept'])[0];
+  }
+
+  function header($name,$value=null) {
+
+    if ($value === null) return get_header($name);
+    header("$name: $value");
+
+  }
+
   function headers() {
 
     #header('Content-type: ' . $this->content_type);