]> git.nbdom.net Git - nb.git/commitdiff
Bed
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Sat, 9 Apr 2016 01:35:29 +0000 (02:35 +0100)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Sat, 9 Apr 2016 01:35:29 +0000 (02:35 +0100)
etc/dbs.php
lib/js/nb.js
lib/php/db.php
lib/php/db/field.php
lib/php/db/table.php

index fc67de096350cc4ffd2232dbc5783efafaaa0361..5982fa15727910285788e999cc4fbb15d778292f 100644 (file)
@@ -2,109 +2,6 @@
 /*
 */
 $CONF = array (
-  'rent' => array (
-    'order' => 1,
-    'pdo' => 'sqlite:/opt/rent/rent.db',
-    'title' => 'Rent',
-    'notice' => 'Micro foncier 4BE',
-    'default_table' => 'rent',
-    'tables' => array (
-      'place' => array (
-        'replace' => array (
-          'id' => 'addr.addr',
-        ),
-      ),
-      'rent' => array (
-        'order_by' => 'start desc',
-        'replace' => array (
-          'idtenant' => 'tenant.name',
-          'idplace' => 'addr.addr',
-        ),
-        'extras' => array (
-          'total' => array (
-            'type' => 'float(8,2)',
-            'extra' => 'ROUND(rent+charge,2)',
-          ),
-          'rent_year' => array (
-            'type' => 'float(8,2)',
-            'extra' => 'rent * (1 + ( strftime(\'%m\',end)+12*strftime(\'%Y\',end) ) -  ( strftime(\'%m\',start)+12*strftime(\'%Y\',start) ))',
-          ),
-        ),
-      ),
-    ),
-    'name' => 'rent',
-  ),
-  'ui-mem' => array (
-    'pdo' => 'sqlite:/dev/shm/semantico.db',
-    'title' => 'Semantico UI',
-    'default_table' => 'node',
-    'tables' => array (
-      'node' => array (
-        'order_by' => '(SELECT value FROM fact WHERE idnode=id AND name=\'date_install\') DESC',
-        'replace_' => array (
-          'iddc' => 'dc.id',
-        ),
-      ),
-    ),
-    'replace' => array (
-      'SELECT name FROM dc WHERE id=\'@id\'' => '.node td.iddc',
-    ),
-    'name' => 'ui-mem',
-  ),
-  'rt' => array (
-    'pdo' => 'pgsql:host=db.rt.semantico.net;port=5432;dbname=rtdb;user=rtuser;password=',
-    'title' => 'RT',
-    'password' => 'scrubchug',
-    'name' => 'rt',
-  ),
-  'ui' => array (
-    'pdo' => 'sqlite:/opt/semantico/product/releases/sem_ui/db/semantico.db',
-    'title' => 'Semantico UI',
-    'default_table' => 'node',
-    'tables' => array (
-      'node' => array (
-        'order_by' => '(SELECT value FROM fact WHERE idnode=id AND name=\'date_install\') DESC',
-        'replace_' => array (
-          'iddc' => 'dc.id',
-        ),
-      ),
-    ),
-    'replace' => array (
-      'SELECT name FROM dc WHERE id=\'@id\'' => '.node td.iddc',
-    ),
-    'name' => 'ui',
-  ),
-  'postfix' => array (
-    'title' => 'Postfix on Izideal',
-    'host' => 'media.izideal.vpn',
-    'user' => 'postfix',
-    'password' => 'dx7d50nfs',
-    'type' => 'mysql',
-    'name' => 'postfix',
-  ),
-  'izidev' => array (
-    'host' => 'big.cascais.loc',
-    'title' => 'Mysql Izi on Big',
-    'password' => 'mpc60ax73',
-    'default_table' => 'process',
-    'user' => 'nico',
-    'name' => 'izi',
-    'tables' => array (
-      'site' => array (
-        'replace' => array (
-          'idlang' => 'lang.id',
-        ),
-      ),
-    ),
-    'type' => 'mysql',
-  ),
-  'wp' => array (
-    'host' => 'admin.izideal.vpn',
-    'user' => 'nico',
-    'password' => 'mpc60ax73',
-    'type' => 'mysql',
-    'name' => 'wp',
-  ),
   'nb' => array (
     'host' => 'admin.izideal.vpn',
     'user' => 'nico',
@@ -119,66 +16,18 @@ $CONF = array (
           'idtenant' => 'tenant.name',
           'idplace' => 'addr.addr',
         ),
-        'columns' => array (
-          'total' => function(&$r) {$r['rent'] + $r['charge'];}
-        ),
+        'call_rows' => function(&$r) {
+          if (0) $r['rent_year'] = $r['rent'] * (1 +
+            ( date('m',$r['end'])+12*date('Y',strtotime($r['end'])) )
+            - 
+            ( date('m',$r['start'])+12*date('Y',strtotime($r['start'])) )
+          );
+          #extra: "rent * (1 + ( strftime('%m',end)+12*strftime('%Y',end) ) -  ( strftime('%m',start)+12*strftime('%Y',start) ))"
+          #$r['rent_year'] = $r['rent'] * (1 + ( date('m',strtotime($r['end']))+12
+          $r['Total'] = $r['rent'] + $r['charge'];
+        }
       ),
     ),
   ),
-  'puppetdb' => array (
-    'title' => 'Puppetdb',
-    'host' => 'big',
-    'type' => 'pgsql',
-    'user' => 'puppetdb',
-    'default_table' => 'view_hosts',
-    'tables' => array (
-      'hosts' => array (
-        'extras' => array (
-          'last_compile' => array (
-            'type' => 'text',
-            'extra' => 'to_char(hosts.last_compile, \'YYYY-MM-DD HH:mm:ss\'::text)',
-          ),
-          'last_freshcheck' => array (
-            'type' => 'text',
-            'extra' => 'to_char(hosts.last_freshcheck, \'YYYY-MM-DD HH:mm:ss\'::text)',
-          ),
-          'updated_at' => array (
-            'type' => 'text',
-            'extra' => 'to_char(hosts.updated_at, \'YYYY-MM-DD HH:mm:ss\'::text)',
-          ),
-          'created_at' => array (
-            'type' => 'text',
-            'extra' => 'to_char(hosts.created_at, \'YYYY-MM-DD HH:mm:ss\'::text)',
-          ),
-          'test' => array (
-            'type' => 'int',
-            'extra' => 1,
-          ),
-        ),
-      ),
-    ),
-    'password' => 'puppetdb6073/!',
-    'name' => 'puppetdb',
-  ),
-  'izi' => array (
-    'title' => 'Mysql Izi on Admin',
-    'default_table' => 'process',
-    'host' => 'admin.izideal.vpn',
-    'user' => 'izideal',
-    'name' => 'izi',
-    'tables' => array (
-      'site' => array (
-        'replace' => array (
-          'idlang' => 'lang.id',
-        ),
-      ),
-    ),
-    'password' => 'dx7d50nfs',
-    'type' => 'mysql',
-  ),
-  'crypt' => array (
-    'pdo' => 'sqlite:/dev/shm/crypt.db',
-    'name' => 'crypt',
-  ),
 );
 ?>
index 96e94d280fe4503bba7bdb8f0be965a8b988a86e..1585c40e1295248947b6a402fa30413aaaa0b212 100644 (file)
@@ -512,10 +512,23 @@ function NB() {
     $('<p>'+msg+'</p>').prependTo( "#nb-msg" );
   }
 
+  this.reload_head_js = function() {
+               $("head script").each(function(){
+                       var oldScript = this.getAttribute("src");
+                       $(this).remove();
+                       var newScript;
+                       newScript = document.createElement('script');
+                       newScript.type = 'text/javascript';
+                       newScript.src = oldScript;
+                       document.getElementsByTagName("head")[0].appendChild(newScript);
+               });
+  }
+
 };
 /*
   Statics
 */
+NB.reload_head_js = NB.prototype.reload_head_js
 NB.get_http = NB.prototype.get_http
 NB.dump = NB.prototype.dump
 NB.urlParam = NB.prototype.urlParam
index 2ea82d19a6186fcbfc67aec4e5c6eb43b283d27b..aafd5fce396ba11290c54a4059a8a8003ca52cb4 100644 (file)
@@ -536,9 +536,8 @@ EOF;
         $h = array_replace_recursive($h,$yaml);
 
       } elseif (preg_match('/\.php$/i',$file)) {
-        #global $VAR1;
-        require($file);
-        if (!empty($CONF)) $h = $CONF;
+        #global $CONF;
+        require($file); if (!empty($CONF)) $h = array_replace_recursive($h,$CONF);
 
       }
 
index 15d8cf9fd3fd52be625eafd7970544928b8aa08a..b2b53d5e3e3071a6aefb0953592d326a0adc1116 100644 (file)
@@ -11,11 +11,13 @@ $DB_FIELD_TYPES = array(
 class field {
   private $table;
   public $name;
-  public $type = 'text';
+  #public $type = 'text';
+  public $type;
   public $null = true;
   public $key = false;
+  #public $value;
   public $default;
-  public $autoincrement = false;
+  public $autoincrement;
   public $extra;
 
   public function __sleep() { return array(
@@ -38,7 +40,21 @@ class field {
   }
 
   public function numeric() {
-    return preg_match('/int|float|number|currency/',$this->type) ? true : false;
+    return preg_match('/signed|(snall )?int|float|decimal|numeric|number|currency/i',$this->type) ? true : false;
+  }
+
+  public function html_type() {
+    # See: http://www.w3schools.com/html/html_form_input_types.asp
+    if (strpos($this->name,'password') !== false) return 'password';
+    if ($this->numeric() !== false) return 'number';
+    return 'text';
+    # Html5 only
+    if (strpos($this->name,'email') !== false) return 'email'; # no safari
+    if (strpos($this->name,'color') !== false) return 'color'; # no safari, no ie
+    # NOT READY
+    if (strpos($this->type,'datetime') !== false) return 'datetime';
+    if (strpos($this->type,'time') !== false) return 'time';
+    if (strpos($this->type,'date') !== false) return 'date';
   }
 
   public function html_edit($value) {
@@ -46,7 +62,13 @@ class field {
     return '<div class="label '.$this->name.'"><label for="'.$this->name.'">'.htmlspecialchars(prettyText($this->name)).'</label>'
       .($this->extra
         ? '<span class="extra" id="'.$this->name.'">'.$this->out($value).'</span>'
-        : '<input name="'.$this->name.'" id="'.$this->name.'" value="'.$value.'" type="text"/>'
+        : '<input'
+          .' name="'.$this->name.'"'
+          .' id="'.$this->name.'"'
+          .' value="'.$value.'"'
+          .($size=$this->size() and is_scalar($size) ? ' size="'.$size.'"' : '')
+          .' type="'.$this->html_type().'"'
+          .'/>'
       )
     .'</div>'.NB_EOL;
 
@@ -64,6 +86,7 @@ class field {
   public function size() {
     if (! preg_match('/\((\d+)(?:,(\d+)\))?/',$this->type,$m) ) return null;
     array_shift($m);
+#debug($m);
     return count($m) == 1 ? $m[0] : $m;
 // NB 02.08.15     return preg_match('/\((\d+)\)/',$this->type,$m) ? $m[1] : null;
   }
@@ -169,7 +192,11 @@ class field {
     # See: http://php.net/manual/en/pdostatement.bindparam.php
     if ($name === null) $name = ':'.$this->name;
 #debug($this->name . " | $name | $value | $this->type | " .  $this->size()."<br/>");
-    return $sth->bindParam($name, $value, $this->type2pdo($value), $this->size());
+    if (strpos($this->type,'float') !== false) {
+      $value = strval($value);
+      return $sth->bindParam($name, $value, PDO::PARAM_STR);
+    }
+    return $sth->bindParam($name, $value, $this->type2pdo($this->type), $this->size());
 // NB 27.07.15     return $sth->bindParam($name, $value, $this->type2pdo($value));
   }
 
index d4950d4cb81490760dc053a2db049f8aa2b59006..c13ffb7ea3864c276c62e76b1d5ca30236938d3e 100644 (file)
@@ -22,6 +22,7 @@ Class Table extends nb {
 # NB 28.03.16   public $fields_keys;
   public $replace = array(); # replace by javascript
   public $extras = array();
+  public $call_rows; # Function to call in rows()
   public $count;
   public $engine;
   public $created;
@@ -38,13 +39,12 @@ Class Table extends nb {
       $opt = $name;
       #$name = $opt['name'];
       $name = isset($opt['name']) ? $opt['name'] : '';
-      if (!empty($opt['name'])) $this->name = $opt['name'];
-
-    } elseif(!empty($name)) {
-      $this->name = $name;
 
     }
 
+    if(!empty($name)) $opt['name'] = $this->name = $name;
+    #debug($opt);
+
     #unset($opt['db']); bye($opt);
 
     // Extras - We dont want to affect $this
@@ -86,7 +86,7 @@ Class Table extends nb {
       #$temp = '_'.substr(md5($this->name),0,6);
       $temp = DB_TABLE_QUERY_NAME;
       #bye("CREATE TEMPORARY VIEW $temp AS $this->name");
-      $this->db()->conn->query("CREATE TEMPORARY VIEW $temp AS $this->name");
+      $this->db()->conn->query("CREATE TEMPORARY TABLE $temp AS $this->name");
       $this->name = $temp;
 
     } elseif (preg_match('/\b(\.import|LOAD DATA|COPY|INSERT|REPLACE|DELETE|TRUNCATE|CREATE|DROP|ALERT)\b/',$this->name)) {
@@ -256,9 +256,12 @@ Class Table extends nb {
   public function html_edit($values = null) {
     if ($values === null) $values = $this->p();
     if (!is_array($values)) $values = array($values);
+    $fields = array_filter($this->fields(),function($v){return empty($v->extra);});
+    #$fields = $this->fields();
+    #debug($fields);
 
     $add = false;
-    $where = $this->where($this->fields(),$values);
+    $where = $this->where($fields,$values);
     if (empty($where)) {
       $where = ' WHERE 1=0';
       $add = true;
@@ -280,7 +283,7 @@ Class Table extends nb {
     if ( $add or ($row = $st->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT))) {
       $count ++;
 
-      foreach ($this->fields() as $name => $field) {
+      foreach ($fields as $name => $field) {
         if ($add and !preg_match('/^(null)?$/',$field->default)) $row[$name] = $field->default;
         elseif(!isset($row[$name])) $row[$name] = '';
         echo $field->html_edit($row[$name]
@@ -572,8 +575,8 @@ Class Table extends nb {
     $st->execute();
 
     # Fields
+    $fields = array();
     if (!DB_TABLE_QUERY_NAME) {
-      $fields = array();
       foreach ($this->db()->fields($st) as $f) { $fields[$f->name] = $f; }
       $this->fields = $fields;
     }
@@ -615,6 +618,10 @@ Class Table extends nb {
 
       $count++;
 
+      if (!empty($this->call_rows)) {
+        $call = $this->call_rows; $call($row);
+      }
+
       if ($opt['is_html'] and !$out_conf) {
         foreach ($row as $k=>$v) {
           if (!isset($this->extras[$k])) $row[$k] = out::format($v);
@@ -627,6 +634,16 @@ Class Table extends nb {
       if ($count === 1) {
         if ($opt['is_html']) echo $this->html_rows_top();
 
+        if ($this->call_rows) {
+          foreach (array_keys($row) as $name) {
+            if (!$this->fields($name)) {
+              $this->fields[$name] = new Field($name);
+              $this->call = true;
+              #$fields[$name] = $this->fields[$name];
+            }
+          }
+        }
+
         if ($out_conf) {
           out::head($out_conf,$this->fields(),array($row));
 
@@ -875,7 +892,8 @@ Class Table extends nb {
 
       foreach ($fields as $name => $f) {
         #$html .= '<th class="'.$f.'">'.($this->p('buttons')==='0' ? $f : $this->url_sort($f)).'</th>'.NB_EOL;
-        $html .= '<th class="'.$name.($f->key?' key':'').'">'.($this->p('buttons')==='0' ? $this->prettyText($name) : $this->url_sort($name)).'</th>'.NB_EOL;
+        $sort = $this->p('buttons')!=='0' and empty($f->call);
+        $html .= '<th class="'.$name.($f->key?' key':'').'">'.(!$sort ? $this->prettyText($name) : $this->url_sort($name)).'</th>'.NB_EOL;
       }
       if ($this->p('buttons')!=='0' and DB_HTML_DELETE) $html .= '<th class="delete"></th>'.NB_EOL;
       $html .= '</tr>'.NB_EOL.'</thead>'.NB_EOL;
@@ -891,14 +909,14 @@ Class Table extends nb {
 
     $html = '<tr class="row">'.NB_EOL;
 
-    if ($this->p('buttons')!=='0') $html .= '<td class="action">'.$buttons[0].'</a></td>'.NB_EOL;
+    if ($this->p('buttons')!=='0') $html .= '<td class="action">'.$buttons[0].'</td>'.NB_EOL;
 
     foreach ($row as $k => $v) {
       if (isset($this->extras[$k])) $k .= " extra";
       $html .= '<td class="'.$k.'">'.$v.'</td>'.NB_EOL;
     }
 
-    if ($this->p('buttons')!=='0') $html .= '<td class="action">'.$buttons[1].'</a></td>'.NB_EOL;
+    if ($this->p('buttons')!=='0') $html .= '<td class="action">'.$buttons[1].'</td>'.NB_EOL;
 
     $html .= '</tr>'.NB_EOL;
 
@@ -1289,7 +1307,7 @@ Class Table extends nb {
     // Tables - see default.js if you change class
     $r .= '<span class="label">';
     $r .= '<label for="table">Tables</label>'.html_select_array(array_keys($this->db()->tables()),array(
-      'html'       => 'class="tables" id="table" name="table" id="table"',
+      'html'       => 'class="tables" name="table" id="table"',
       'selected'   => $this->p('table'),
       'prettyText' => true,
     ));