]> git.nbdom.net Git - nb.git/commitdiff
class field
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Fri, 3 Jul 2015 15:47:03 +0000 (16:47 +0100)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Fri, 3 Jul 2015 15:47:03 +0000 (16:47 +0100)
lib/php/db.php

index 5f4410f9f6ad5234883bee0a8d6a332916eef01f..556b486747d16303522f423d3140cd378870fbe7 100644 (file)
@@ -11,17 +11,17 @@ if (!defined('DB_HTML_EDIT')) define('DB_HTML_EDIT','Edit');
 
 class db {
 
-  var $tables = array();
-  var $conn;
-  var $type;
-  var $help_criterias = array(
+  public $tables = array();
+  public $conn;
+  public $type;
+  public $help_criterias = array(
     ' * or % for wildcar',
     ' ! to negate',
     ' ~ for regex',
     ' combine criterias with OR/AND',
   );
 
-  function db($pdo) {
+  function __construct($pdo) {
     $this->conn = new PDO($pdo);
     $this->type = strtolower(preg_replace('/^([^:]+):.*$/','\1',$pdo));
 
@@ -242,19 +242,19 @@ EOF;
 
 class table {
 
-  var $name;
-  var $db;
-  var $sql;
-  var $fields = array();
-  var $fields_keys = array();
-  var $extras = array();
-  var $params = array(
+  public $name;
+  public $db;
+  public $sql;
+  public $fields = array();
+  public $fields_keys = array();
+  public $extras = array();
+  public $params = array(
     'table',
     'limit',
     'debug',
   );
 
-  function table($name,$opt=array()) {
+  function __construct($name,$opt=array()) {
 
     // Connection
     if (@$opt['db']) {
@@ -452,16 +452,15 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
       $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;
+        $field = new field($name,$attr);
+        $field->html_edit(array_key_exists($name,$row) ? $row[$name] : $attr['default']);
+// NB 03.07.15         continue;
+// NB 03.07.15 
+// NB 03.07.15         $value = array_key_exists($name,$row) ? $row[$name] : $attr['default'];
+// NB 03.07.15 
+// NB 03.07.15         echo '<label for="'.$name.'">'.htmlspecialchars($name).'</label>'
+// NB 03.07.15           .'<input name="'.$name.'" id="'.$name.'" value="'.htmlspecialchars($value).'" />'
+// NB 03.07.15         .PHP_EOL;
       }
     }
 
@@ -470,7 +469,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
       .'<input type="submit" name="update"/>'
       .'<input type="text" name="table" value="'.$this->db->p('table').'"/>'
       .'<input type="reset" />'
-      .($_SERVER['HTTP_REFERER'] ? '<input type="button" onclick="document.location=document.referrer" value="Cancel" />' : '')
+      .( empty($_SERVER['HTTP_REFERER']) ? '' : '<input type="button" onclick="document.location=document.referrer" value="Cancel" />')
       .'</div>'.PHP_EOL
     .'</form>'.PHP_EOL;
 
@@ -560,6 +559,15 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
     if ($next<$tot) echo '&nbsp;<span class="prev"><a href="'.$this->url_params('limit',"$next,$y").'">&gt;&gt;</a></span>';
 
     echo '</div>'.PHP_EOL;
+    static $js = null;
+    if ($js === null) {
+      echo '<script type="text/javascript"><!-- '.PHP_EOL
+        ."var rb = document.getElementById('nav_bottom'); var rt = document.getElementById('nav_top'); if (rb && rt) rt.innerHTML = rb.innerHTML;\n"
+      . ' --></script>'.PHP_EOL
+      ;
+      $js = '1';
+    }
+
   }
 
   function form_criterias($opt=array()) {
@@ -608,6 +616,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
     echo join(''.PHP_EOL,$criteria);
 
     echo '</form>'.PHP_EOL;
+
   }
 
   function where_criterias($values,$logic='AND') {
@@ -615,10 +624,13 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
 
     foreach ($this->fields() as $k => $spec) {
 
+      $field = new field($k,$spec);
+
       // No empty values
       $v = @$values[$k];
       if (strcmp($v,'')==0 or $v=='!' or $v=='~') continue;
-      $number = preg_match('/int|float|number|currency/',$spec['type']) ? 1 : 0;
+// NB 03.07.15       $number = preg_match('/int|float|number|currency/',$spec['type']) ? 1 : 0;
+      $number = $field->is_num();
 
       // Equal / Not Equal
       $equal = '=';
@@ -968,5 +980,31 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
 
 }
 
+class field {
+
+  public $name;
+  public $extras = array();
+  public $type = 'text';
+  public $default = null;
+  public $key = false;
+
+  function __construct($name,$attr=array()) {
+    foreach ($attr as $k => $v) { $this->$k = $v; }
+  }
+
+  function is_num() {
+    return preg_match('/int|float|number|currency/',$this->type) ? 1 : 0;
+  }
+
+  function html_edit($value) {
+
+    echo '<label for="'.$this->name.'">'.htmlspecialchars($this->name).'</label>'
+    .'<input name="'.$this->name.'" id="'.$this->name.'" value="'.htmlspecialchars($value).'" />'
+    .PHP_EOL;
+
+  }
+
+}
+
 return;
 ?>