]> git.nbdom.net Git - nb.git/commitdiff
update, add, delete
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Tue, 14 Jul 2015 10:11:13 +0000 (11:11 +0100)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Tue, 14 Jul 2015 10:11:13 +0000 (11:11 +0100)
lib/css/db.css
lib/js/tools.js
lib/php/db.php
lib/php/db/table.php

index c0c421d5d0be5737f48ac81d83ea8c3301d76ae2..c30e33cff79869580afc5afa67a4115f50f8536f 100644 (file)
@@ -94,23 +94,3 @@ td.email,
 td.phone {
   white-space: nowrap;
 }
-
-.button {
-       display: inline-block;
-       text-decoration: none;
-       font-size: 13px;
-       line-height: 26px;
-       height: 28px;
-       margin: 0;
-       padding: 0 10px 1px;
-       cursor: pointer;
-       border-width: 1px;
-       border-style: solid;
-       -webkit-appearance: none;
-       -webkit-border-radius: 3px;
-       border-radius: 3px;
-       white-space: nowrap;
-       -webkit-box-sizing: border-box;
-       -moz-box-sizing: border-box;
-       box-sizing: border-box;
-}
index 3a277bc89d3f5f3fe9ff6dd02deb63cae8d2819e..b63eb722a257764869c5f751116a866973306084 100644 (file)
@@ -3,7 +3,17 @@
  *
  * Date: 28.03.15
  */
-function Tools() {
+function NB() {
+
+  this.form_clear = function(form) {
+    var e;
+    for(i=0;i<form.length;i++) {
+      e = form.elements[i];
+      if (e.style.display == 'none') continue;
+      if (e.type != 'text') continue;
+      e.value = ''
+    }
+  };
 
   this.debug = function(test) {
     console.log(test);
index 9f412248d89e3a982500c247797ec82bca5200cd..b616b62903220a663c71203c9edfd0431504a967 100644 (file)
@@ -10,7 +10,12 @@ require_once(dirname(__FILE__).'/functions.php');
 require_once(dirname(__FILE__).'/db/table.php');
 require_once(dirname(__FILE__).'/db/field.php');
 if (!defined('DB_ERR_PRINT')) define('DB_ERR_PRINT',false);
-if (!defined('DB_HTML_BUTTON_ADD')) define('DB_HTML_BUTTON_ADD','<input type="submit" class="button" name="edit" value="Add"/>');
+if (!defined('DB_HTML_FORM_BUTTONS')) define('DB_HTML_FORM_BUTTONS',''
+  .'<input type="submit" class="button" value="GO"/>'
+  .'<a class="button" href="#" onclick="return nb.form_clear(this.parentElement)">X</a>'
+  .'<input type="submit" class="button" name="edit" value="Add"/>'
+);
+if (!defined('DB_HTML_BUTTON_ADD'))    define('DB_HTML_BUTTON_ADD','<input type="submit" class="button" name="edit" value="Add"/>');
 if (!defined('DB_HTML_BUTTON_SUBMIT')) define('DB_HTML_BUTTON_SUBMIT','<input type="submit" class="button" value="GO"/>');
 
 class db {
@@ -190,7 +195,7 @@ EOF;
   function html_menu($table=null) {
 
     $html = ' onchange="this.parentElement.submit()"';
-    $r = '<form class="db menu border rad" method="get">'.PHP_EOL;
+    $r = '<form class="db menu border rad" method="get" action="">'.PHP_EOL;
 
     if (!empty($table)) foreach ( array_keys($table->fields()) as $k ) {
 
@@ -229,8 +234,7 @@ EOF;
     ));
 
     // Bye
-    $r .= DB_HTML_BUTTON_ADD;
-    $r .= DB_HTML_BUTTON_SUBMIT;
+    $r .= DB_HTML_FORM_BUTTONS;
     $r .= '</form>'.PHP_EOL;
     return $r;
   }
index 8e4cec999e8a8265214253060b5a6c59063f53c3..2087edb5d312ec51979005421e837df6a732fad6 100644 (file)
@@ -1,6 +1,7 @@
 <?php
 require_once(dirname(__FILE__).'/../functions.php');
 if (!defined('DB_HTML_EDIT')) define('DB_HTML_EDIT','Edit');
+if (!defined('DB_HTML_DELETE')) define('DB_HTML_DELETE','Delete');
 if (!defined('DB_HTML_NAV_TOP')) define('DB_HTML_NAV_TOP','<div align="center" class="nav" id="nav_top"></div>'.PHP_EOL);
 class table {
 
@@ -118,17 +119,17 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
     return $this->fields;
   }
 
-  function url_edit($values=null,$sep='&amp;') {
+  function url_keys($values=null,$params=array(),$sep='&amp;') {
     if ($values === null) $values = $this->db->p();
-    $url_edit = array();
+    $url = is_array($params) ? $params : array($params);
 
     foreach ($this->fields_keys() as $name => $spec) {
-      $url_edit[] = $name . '=' .urlencode($values[$name]);
+      if (isset($values[$name]) and strcmp($values[$name],'')!=0)
+        $url[] = $name . '=' .urlencode($values[$name])
+      ;
     }
 
-    $url_edit[] = 'edit=1';
-    return $url_edit ? '?table='.$this->db->p('table').$sep.join($sep,$url_edit) : '';
-
+    return $url ? '?table='.$this->db->p('table').$sep.join($sep,$url) : '';
   }
 
   function fields_keys(&$others=array()) {
@@ -185,7 +186,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
     echo ''
       .'<div class="db buttons">'
       .'<input type="submit" name="update"/>'
-      .'<input type="text" name="table" value="'.$this->db->p('table').'"/>'
+      .'<input type="hidden" name="table" value="'.$this->db->p('table').'"/>'
       .'<input type="reset" />'
       .( empty($_SERVER['HTTP_REFERER']) ? '' : '<input type="button" onclick="document.location=document.referrer" value="Cancel" />')
       .'</div>'.PHP_EOL
@@ -340,8 +341,9 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
 
   }
 
-  function where_criterias($values,$logic='AND') {
+  function where_criterias($values,$logic='') {
     $having = $where = array();
+    if (empty($logic)) $logic = 'AND';
 
     foreach ($this->fields() as $k => $spec) {
 
@@ -411,8 +413,8 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
     }
 
     $sql = '';
-    if ($where) $sql .= ' WHERE '.join(' '. $logic.' ',$where);
-    if ($having) $sql .= ' HAVING '.join(' '. $logic.' ',$having);
+    if ($where) $sql .= ' WHERE '.join(" $logic ",$where);
+    if ($having) $sql .= ' HAVING '.join(" $logic ",$having);
     return $sql;
 
   }
@@ -533,7 +535,11 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
 
       }
 
-      if ($opt['is_html']) array_unshift($row,'<a class="edit button" href="'.$this->url_edit($row,'&amp;').'">'.DB_HTML_EDIT.'</a>');
+      if ($opt['is_html']) {
+        array_unshift($row,'<a class="edit button" href="'.$this->url_keys($row,'edit=1').'">'.DB_HTML_EDIT.'</a>');
+        array_push($row,'<a class="delete button" href="'.$this->url_keys($row,'delete=1').'">'.DB_HTML_DELETE.'</a>');
+      }
+
       echo $this->{"rows_rec_$format"}($row);
 
     }
@@ -645,17 +651,18 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
 
     $colspan = 0;
     if (DB_HTML_EDIT) $colspan++;
+    if (DB_HTML_DELETE) $colspan++;
     if (DB_HTML_NAV_TOP) $html .= '<thead><tr>'
       . '<td colspan="'.(count($this->fields())+$colspan).'">' . DB_HTML_NAV_TOP . '</td>'
       . '</tr></thead>'.PHP_EOL
     ;
 
     $html .= '<tr class="'.$this->name.' row bold">';
-    $html .= '<th class="'.DB_HTML_EDIT.'"></th>';
+    if (DB_HTML_EDIT) $html .= '<th class="'.DB_HTML_EDIT.'"></th>';
     foreach (array_keys($this->fields()) as $f) {
       $html .= '<th class="'.$f.'">'.$this->url_sort($f).'</th>';
     }
-
+    if (DB_HTML_DELETE) $html .= '<th class="'.DB_HTML_DELETE.'"></th>';
     $html .= '</tr>'.PHP_EOL.'<tbody>'.PHP_EOL;
 
     return $html;
@@ -757,11 +764,11 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
   }
 
   function delete($hvalues) {
-    $where = str_replace(' LIKE ','=',$this->where_criterias($values));
+    $where = str_replace(' LIKE ','=',$this->where_criterias($hvalues));
     if (!$where) return 0;
     $sql = 'DELETE FROM ' . $this->sql_name() . $where;
     debug($sql);
-    $execute = $this->db->conn($sql);
+    $execute = $this->db->query($sql);
     if (!$execute) return err('PDO::errorInfo(): ' .join(' ', $this->db->conn->errorInfo()) .PHP_EOL);
     return $execute;
   }