]> git.nbdom.net Git - nb.git/commitdiff
fix bug
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Fri, 2 Dec 2016 10:35:27 +0000 (10:35 +0000)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Fri, 2 Dec 2016 10:35:27 +0000 (10:35 +0000)
bin/dbq
lib/php/db.php
lib/php/db/dbq.php [new file with mode: 0644]
lib/php/db/index.php
lib/php/db/page.php
lib/php/db/table.php

diff --git a/bin/dbq b/bin/dbq
index d1533afacbab58cd081e8129c7d24c49586ebeec..72c74ef34671ff37768ccef6aedf37230d94bced 100755 (executable)
--- a/bin/dbq
+++ b/bin/dbq
@@ -109,7 +109,7 @@ if ($ENV{$UC_NAME.'_PARAMS'}) {
 #
 #-e $ROOT_DIR.'/lib/php/db/index.php' && die 'OK';
 my $url = ($ENV{$UC_NAME.'_URL'} ? $ENV{$UC_NAME.'_URL'} :
-  ( -e $ROOT_DIR.'/lib/php/db/index.php' ? $ROOT_DIR.'/lib/php/db/index.php' : '/opt/rent/www/index.php' )
+  ( -e $ROOT_DIR.'/lib/php/db/dbq.php' ? $ROOT_DIR.'/lib/php/db/dbq.php' : '' )
 );
 if (!$Opt{ssh} and @ARGV and ($ARGV[0] =~ m|^\w+://| or -e $ARGV[0] )) {
   $url = shift @ARGV;
index 9f1f6ae52ef450fbedd15b1a168ea1119ce1dd80..cde8f1ce9b102f0d564a782c25bf361aab08f3ea 100644 (file)
@@ -561,16 +561,15 @@ class Db extends nb {
 
   public function action($action,$table=null) {
     #debug(($this->tables));
-    $available = self::$action_help;
     $actions = explode(',',$action);
-    $this->pdef('format',($this->php_cli() ? 'csv' : ''));
+# NB 02.12.16     $this->pdef('format',($this->php_cli() ? 'csv' : ''));
     $rows = [];
     $return = false;
 
     foreach($actions as $action) {
 
         if ($action == 'db.help') {
-          $this->out($available,['action','description']);
+          $this->out(self::$action_help,['action','description']);
           $return = true;
 
         } elseif ($action == 'db.exec') {
@@ -810,10 +809,11 @@ class Db extends nb {
     global $Db, $Table;
     if (isset($Table)) self::bye("Table.init(): GLOBALS['Table'] already exists !");
     if (isset($Db)) self::bye("Db.init(): GLOBALS['Db'] already exists !");
+
     $Db = new self();
     $Db->pinit();
-
     if (empty($Db)) return false;
+
     $db = $Db->conf_search_db($conf);
     if ($Db->p('localFile')) {
       foreach ($Db->localFile($db['name']) as $k=>$v) {
@@ -823,9 +823,7 @@ class Db extends nb {
 
     # Connection
     $Db->__construct($db);
-# NB 21.09.16     if (empty($Db->_no_connect)) {
     $Db->connect();
-# NB 21.09.16     }
 
     /*
       Table
@@ -834,6 +832,7 @@ class Db extends nb {
       !self::p('table') and
       !preg_match('/^(table\.\w+|rows|insert|edit|delete|update)$/',self::p('action'))
     ) {
+
     } else {
 
       # Search default_table
@@ -1252,6 +1251,17 @@ class Db extends nb {
     return $row;
   }
 
+  public static function content_type2format($content_type) {
+    if (empty($content_type)) $content_type = self::client_content_type();
+    switch (self::mime2ext($content_type)) {
+      case "html":
+        return 'table';
+      case "txt":
+        return 'human';
+      default:
+        return 'csv';
+    }
+  }
   #public function default_table() { return $this->default_table; }
 } # < Class
 
diff --git a/lib/php/db/dbq.php b/lib/php/db/dbq.php
new file mode 100644 (file)
index 0000000..21be543
--- /dev/null
@@ -0,0 +1,12 @@
+<?php
+#!/usr/bin/env php
+/**
+* @copyright (C) 2016 Nicolas Boisselier
+* @author Nicolas Boisselier nicolas.boisselier@gmail.com
+*
+* lib/php/db/dbq.php
+*/
+require_once(realpath(dirname(__FILE__).'/init.php'));
+$Db->pdef('format',$Db->content_type2format());
+return $Db->action($Db->p('action'),$Table);
+?>
index 9ba43caf4301a8e9ae156164be9834b0a38db6e2..2fb5bf6495564abbe2193fe556bad1333062955a 100755 (executable)
@@ -8,22 +8,11 @@
 */
 #require_once(realpath(dirname(__FILE__).'/page.php')); return;
 require_once(realpath(dirname(__FILE__).'/init.php'));
-
 if(defined('DB_NO_ACTION') and DB_NO_ACTION) return true;
 
-# Happend when include from a function
+# Needed when include from a function
 if (!isset($Db)) global $Db, $Table;
 
-switch ($Db->mime2ext($Db->client_content_type())) {
-  case "html":
-    $Db->pdef('format','table');
-    break;
-  case "txt":
-    $Db->pdef('format','human');
-    break;
-  default:
-    #bye($type);
-    $Db->pdef('format','json');
-}
+$Db->pdef('format',$Db->content_type2format());
 return $Db->action($Db->p('action'),$Table);
 ?>
index 422d6f0dfecb83a9cc9cf6a08a22575f12347d44..c0ac38129068bd0f4392315643de2c4ca30cd13d 100644 (file)
@@ -2,6 +2,7 @@
 require_once(dirname(__FILE__).'/init.php');
 require_once(dirname(__FILE__).'/../page.php');
 
+
 $Page = new Page([
   'title' => ($Db->title ? $Db->title : Db::prettyText($Db->name)),
   'css' => [
@@ -26,18 +27,7 @@ $Page = new Page([
 
     function($page){
       global $Db, $Table;
-      #$type = $page->mime2ext($page->content_type());
-      switch ($page->mime2ext($page->content_type())) {
-        case "html":
-          $page->pdef('format','table');
-          break;
-        case "txt":
-          $page->pdef('format','human');
-          break;
-        default:
-          #bye($type);
-          $page->pdef('format','csv');
-      }
+      $Db->pdef('format',$Db->content_type2format($page->content_type()));
       if (!$page->action()) return $Db->action($Db->p('action'),$Table);
     },
 
index c51e88b785ab5625fdafdd5ff492e33503e509e8..705d5b95cd056742c7bc9da19ff0d13fbc231005 100644 (file)
@@ -1493,7 +1493,9 @@ Class Table extends nb {
   }
 
   public function action($action=null) {
-    if ($action === null) $action = $this->p('action');
+    #if (empty($action)) return;
+    #if ($action === null) $action = $this->p('action');
+    #debug($action);
 
     if ($action == 'table.fields') {
       $rows = array_values($this->object2array($this->fields()));
@@ -1525,7 +1527,8 @@ Class Table extends nb {
 
     } elseif ($action == 'table.rows') {
       $this->db()->print_header($this->p('format'));
-      $this->rows(); return true;
+      $this->rows();
+      return true;
 
     } elseif ($action == 'table.info') {
       return $this->out($this->info());
@@ -1848,7 +1851,10 @@ Class Table extends nb {
   }
 
   public function template($id=null) {
-    if (empty($id) and !( $id=self::p('table.template.id') ) ) self::bye('Wrong parameter!');
+    if (empty($id)
+      and !( $id=self::p('table.template.id') )
+      and !( $id=self::p('idtemplate') )
+    ) self::bye('Wrong parameter!');
 
     $id = preg_replace('/[^\w\._-]/','',$id);