]> git.nbdom.net Git - nb.git/commitdiff
db.edit
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Wed, 10 Jun 2015 14:27:39 +0000 (15:27 +0100)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Wed, 10 Jun 2015 14:27:39 +0000 (15:27 +0100)
lib/php/db.php
lib/php/functions.php

index 5dff605a02f593fc4a536091c016030b30c83a9b..7f784342609d2aa8860adfcc22adc72d0b019806 100644 (file)
@@ -6,6 +6,9 @@
   from any database
 
 *****************************************************************************/
+require_once(dirname(__FILE__).'/functions.php');
+if (!defined('DEBUG')) define('DEBUG',0);
+
 if (!function_exists('err')) {
   function err($err) {
     die( is_scalar($err) ? $err : print_r($err) );
@@ -227,7 +230,7 @@ class table {
 
     if (@$opt['extras']) $this->add_extras($opt['extras']);
 
-    return array();
+    return $this->fields();
   }
 
   function fields() {
@@ -311,13 +314,39 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
 
   }
 
-  function edit($ids) {
-    if (!is_array($ids)) $ids = array($ids);
+  function edit($values = null) {
+    if ($values === null) $values = $_REQUEST;
+    if (!is_array($values)) $values = array($values);
 
-    return $this->fields_keys();
     $sql = "SELECT *" . $this->select_extras();
-    $sql .= " FROM $this->name".$this->where_criterias();
+    $sql .= " FROM $this->name".$this->where_criterias($values);
+    $sql .= " LIMIT 1";
     $this->sql = $sql;
+
+    $this->debug($sql,1);
+    $st = $this->db->conn->prepare($sql);
+    $st->execute();
+
+    $count = 0;
+    if ($row = $st->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT)) {
+      $count ++;
+
+      foreach ($this->fields() as $name => $attr) {
+
+        if (array_key_exists($name,$row)) {
+          $value = $row[$name];  
+        } else {
+          $value = $attr['default'];  
+        }
+
+        echo '<label for="'.$name.'">'.htmlspecialchars($name).'</label>'
+          .'<input name="'.$name.'" id="'.$name.'" value="'.htmlspecialchars($value).'" />'
+        .PHP_EOL;
+      }
+    }
+
+    $this->row = $row;
+    return $this;
   }
 
   function debug($msg,$level=0) {
@@ -467,13 +496,13 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
     echo '</form>'.PHP_EOL;
   }
 
-  function where_criterias() {
+  function where_criterias($values,$logic='AND') {
     $having = $where = array();
 
     foreach ($this->fields() as $k => $spec) {
 
       // No empty values
-      $v = @$_REQUEST[$k];
+      $v = @$values[$k];
       if (strcmp($v,'')==0 or $v=='!' or $v=='~') continue;
 
       // Equal / Not Equal
@@ -489,7 +518,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
 
       // Text
       } elseif (preg_match('/text|char|blob/',$spec['type'])
-        or !preg_match('/^[\d+\.]$/',$v) # text char in search
+        or !preg_match('/^\d+(\.\d*)?$/',$v) # text criteria
       ) {
 
         if (strtolower($v)=='null') $v = '';
@@ -515,8 +544,10 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
 
       if ($this->db->type == 'mysql' and $spec['extra']) {
         $having[] = "$k$equal$v";
+
       } elseif ($this->db->type == 'pgsql' and $spec['extra']) {
         $where[] = $this->extras[$k]."$equal$v";
+
       } else {
         $where[] = "$k$equal$v";
       }
@@ -524,8 +555,8 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
     }
 
     $sql = '';
-    if ($where) $sql .= ' WHERE '.join(' '. @$_REQUEST['op'].' ',$where);
-    if ($having) $sql .= ' HAVING '.join(' '. @$_REQUEST['op'].' ',$having);
+    if ($where) $sql .= ' WHERE '.join(' '. $logic.' ',$where);
+    if ($having) $sql .= ' HAVING '.join(' '. $logic.' ',$having);
     return $sql;
 
   }
@@ -583,7 +614,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
     // Select
     //
     $sql = "SELECT *" . $this->select_extras();
-    $sql .= " FROM $this->name".$this->where_criterias();
+    $sql .= " FROM $this->name".$this->where_criterias($_REQUEST,$_REQUEST['op']);
     $this->sql = $sql;
     #$this->debug($sql);
     $this->debug($sql,1);
index a7cbc4f93ac9a846bba733280cd2551a44e687d8..2abe2103a17bef697aa995b2627d16375341bec6 100644 (file)
@@ -95,5 +95,43 @@ function cmd2str($cmd,$write=null) {
 
        }
 
+}
+
+function argv2request() {
+/******************************************************************************
+# NB 14.07.11 - Put argv options into _REQUEST. Return argv without options
+******************************************************************************/
+
+  global $argv;
+
+  $new_argv = array();
+  for ($i=1;$i<count($argv);$i++) {
+
+    if (preg_match('/^([\w_\-\.]+)=(.+)\s*$/',$argv[$i],$f)) {
+      $k = $f[1];
+      $v = $f[2];
+
+      // Array
+      if (preg_match('/^(.*)\[\]$/',$k,$p)) {
+        #bye($p);
+        if (($_REQUEST[$p[1]]==NULL) or !in_array($v,$_REQUEST[$p[1]])) $_REQUEST[$p[1]][] = $v;
+
+      } else {
+         $_REQUEST[$k] = $v;
+
+      }
+
+    } else {
+
+      if (preg_match('/^-(h|-?help)$/',$argv[$i])) $_REQUEST['help'] = 1;
+      else $new_argv[] = $argv[$i];
+
+    }
+
+  }
+
+  return $new_argv;
+
 }
 ?>