]> git.nbdom.net Git - nb.git/commitdiff
lib/php/db/table.php
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Tue, 2 Oct 2018 22:18:27 +0000 (23:18 +0100)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Tue, 2 Oct 2018 22:18:27 +0000 (23:18 +0100)
lib/php/db/field.php
lib/php/db/table.php
lib/php/mime.php
www/dbq/html/default.css
www/dbq/html/default.min.css

index 8a8354e272890253766af73d513533cb80f90c6f..5d5913561820eb63772d00328bfcb893c0e2fcc6 100644 (file)
@@ -14,6 +14,7 @@ class field extends nb {
   public $extras;
   public $preffix = '';
   public $html_edit_hide_min_size = 500;
+  public $html_textarea_min_size = 300;
 
   public function __sleep() { return [
     'name',
@@ -56,7 +57,7 @@ class field extends nb {
 
   public function string() {
     if ($this->numeric()) return false;
-    if (strpos($this->type,'bool') !== false) return false;
+    if ($this->bool()) return false;
     return true;
   }
 
@@ -64,6 +65,10 @@ class field extends nb {
     return preg_match('/signed|(small|tiny)?int|float|decimal|numeric|number|currency/i',$this->type) ? true : false;
   }
 
+  public function bool() {
+    return (strpos($this->type,'bool') !== false);
+  }
+
   public function unsigned() {
     return ( stripos($this->type,'unsigned') !== false ? true : false );
   }
@@ -71,15 +76,19 @@ class field extends nb {
   public function html_type() {
     # See: http://www.w3schools.com/html/html_form_input_types.asp
     if (stripos($this->name,'password') !== false) return 'password" autocomplete="off';
-    if ($this->numeric() !== false) return 'number" step="any';
-    return 'text';
+    if ($this->numeric()) return 'number" step="any';
     # Html5 only
+    if (preg_match('/((^|_)tel|(^|_)mobile|(^|_)phone)/',$this->name)) return 'tel';
+    if (preg_match('/((^|_)month($|_))/',$this->name)) return 'month';
+    if (preg_match('/^passw*/',$this->name)) return 'tel';
     if (stripos($this->name,'mail') !== 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,'datetime') !== false) return 'datetime';
+    if (preg_match('/^(timestamp|datetime)/',$this->type)) return 'datetime';
     if (strpos($this->type,'time') !== false) return 'time';
     if (strpos($this->type,'date') !== false) return 'date';
+    return 'text';
   }
 
 # NB 14.09.18   public function sqlValue($value) {
@@ -90,9 +99,7 @@ class field extends nb {
   public function htmlValue($value,&$mime=null) {
     #if (strpos($this->type,'bool') !== false) re
 
-    if (strpos($this->type,'bool') !== false) {
-      return preg_match('/^(1|yes|on|true)/i',$value) ? 'Yes' : 'No';
-    }
+    if ($this->bool()) return preg_match('/^(1|yes|on|true)/i',$value) ? 'Yes' : 'No';
 
                $value = Mime::html($value,$value,$mime);
     return $value;
@@ -115,13 +122,16 @@ class field extends nb {
 
   private function _html_textarea($size=0) {
     #$size = ($this->size() and is_scalar($this->size())) ? $this->size() : 0;
-    return ( ($size>300) or preg_match('/^(\w*text|binary|blob|varchar)/i',$this->type) ) ? true : false;
+    #debug ( ( ($size>300) or preg_match('/^(\w*text|binary|blob|varchar)/i',$this->type) ) ? true : false );
+    return ( ($size >= $this->html_textarea_min_size) or preg_match('/^(\w*text|binary|blob|varchar)/i',$this->type) ) ? true : false;
        }
 
   public function html_edit($values,$type=null) {
 
     $size = ($this->size() and is_scalar($this->size())) ? $this->size() : 0;
                $tag = $this->_html_textarea($size) ? 'textarea' : 'input';
+               #debug((int)($this->_html_textarea($size)));
+    #$tag = ( ($size >= 300) or preg_match('/^(\w*text|binary|blob|varchar)/i',$this->type) ) ? 'textarea' : 'input';
 
     $html = '';
 
@@ -130,6 +140,7 @@ class field extends nb {
                if ($db = $this->db() and $fct=$db->conf_type('html_edit_multi')) $multi = $fct($this);
                $suff = $multi ? '[]' : '';
                if ($type === null) $type = $this->html_type();
+               #if (empty($type)) $type = 'text';
 
                foreach ((array)$values as $value) {
 
@@ -141,16 +152,34 @@ class field extends nb {
        if ($this->extras) {
        $h .= '<span class="extra '.$this->name.'">'.$this->out($value).'</span>';
 
-       } elseif (strpos($this->type,'bool') !== false) {
+       } elseif ($this->bool()) {
        $h .= $this->htmlYesNo($value);
 
        } else {
                                # NB 09.02.18: needed ? 
-                               if ($tag == 'textarea' and $type != 'text') $tag = 'input';
+# NB 02.10.18                          if ($tag == 'textarea' and $type != 'text') $tag = 'input';
 
                                # Trim CHAR type
                                if (preg_match('/^\s+$/',$value)) $value = '';
 
+                               # Html friendly
+                               if ($media = Mime::html($value,'',$mime,$media_type)) {
+
+                                       if (!empty($media_type)) {
+                                               $type = $media_type;
+                                               $tag = 'input';
+                                       }
+                                       #debug([$this->name,$type]);
+                                       if ($this->html_edit_hide_min_size
+                                               and $this->html_edit_hide_min_size <= mb_strlen($value)
+                                       ) {
+                                               $tag = 'input';
+                                               $type = 'hidden';
+                                       }
+
+                                       $media = '<span class="media">'.$media.'</span>';
+                               }
+
                                # Tag
        $h .= '<'.$tag
                .' name="'.$this->preffix.$this->name . ($multi ? '[]' : '') . '"'
@@ -160,19 +189,7 @@ class field extends nb {
                        . ($this->autoincrement ? ' readonly="readonly"' : '')
                        . ($this->null ? '' : ' required="required"')
                .($tag == 'textarea' ? '>'.$value.'</textarea>' : ' type="'.$type.'" value="'.htmlspecialchars($value).'" />')
-       ;
-
-                               # Html friendly
-                               if ($media = Mime::html($value,'',$mime)) {
-                                       if ($this->html_edit_hide_min_size
-                                               and $this->html_edit_hide_min_size <= mb_strlen($value)
-                                       ) {
-                                               $tag = 'input';
-                                               $type = 'hidden';
-                                       }
-                                       #$h .= $media;
-                                       $h .= '<div class="media">'.$media.'</div>';
-                               }
+       .$media;
 
        }
 
@@ -295,7 +312,7 @@ class field extends nb {
     } else { # Not a string
 
                        # NB 14.09.18: Ok with all databases types ? 
-       if (strpos($this->type,'bool') !== false) $value = preg_match('/^(f.*|no|0)?\s*$/i',$value) ? 'false' : 'true';
+       if ($this->bool()) $value = preg_match('/^(f.*|no|0)?\s*$/i',$value) ? 'false' : 'true';
 
       if (strcmp($value,'') == 0) $value = 'NULL';
     }
@@ -320,6 +337,13 @@ class field extends nb {
   }
   */
 
+       public function sql_where($value) {
+               #if ($this->bool()) $value = preg_match('/^(f.*|no|0)?\s*$/i',$value) ? 'false' : 'true';
+               #if ($this->bool()) return ( preg_match('/^(f.*|no|0)?\s*$/i',$value) ? ' NOT' : '' ) . $this->sql_name();
+               if ($this->bool()) return ( preg_match('/^(yes|1|on|t)/i',$value) ? '' : 'NOT ' ) . $this->sql_name();
+               return $this->sql_name() . '=' . $this->quote($value);
+       }
+
   public static function pdo2type($pdo) {
     if ($pdo === PDO::PARAM_BOOL) return 'bool';
     if ($pdo === PDO::PARAM_LOB) return 'blob';
@@ -331,7 +355,7 @@ class field extends nb {
   public function type2pdo() {
     if (func_get_args()>0 and func_get_arg(0) === null) return PDO::PARAM_NULL;
     if ($this->numeric()) return PDO::PARAM_INT;
-    if (strpos($this->type,'bool') !== false ) return PDO::PARAM_BOOL;
+    if ($this->bool()) return PDO::PARAM_BOOL;
     if (strpos($this->type,'blob') !== false ) return PDO::PARAM_LOB; # not a bug LOB
     return PDO::PARAM_STR;
   }
index 65e60209fde39e2c06738c705a2de86aa47763c9..94d077c1cfc4132193b4dbe838f2094cb29f3d8d 100644 (file)
@@ -510,7 +510,7 @@ Class Table extends nb {
                        if (!empty($ignore) and in_array($k,$ignore)) continue;
                        if (isset($v) or $k=='action') {
                                if (self::p('debug')) $h .= "<label>$k:</label>";
-                               $h .= '<input type="'.(self::p('debug')?'text':'hidden').'" name="'.$k.'" value="'.$v.'"/>'.NB_EOL;
+                               $h .= '<input type="'.(self::p('debug')?'text':'hidden').'" name="'.$k.'" value="'.$v.'" class="form-control"/>'.NB_EOL;
                        }
                }
 
@@ -603,7 +603,7 @@ Class Table extends nb {
                }
 
                // Form
-               echo '<form class="container db edit '.($add ? 'add ' : '' ).'form-table" method="post" action="'.$form_action.'">'.NB_EOL;
+               echo '<form class="container db-edit db edit '.($add ? 'add ' : '' ).'form-table" method="post" action="'.$form_action.'">'.NB_EOL;
                echo '<div class="fields insert-bottom">'.NB_EOL;
                if ($add or $row) {
 
@@ -687,7 +687,7 @@ Class Table extends nb {
                $title = $this->prettyText($name).': ';
 
                # Asc
-               $sel = ( $this->p('orderby')=="$name asc") ? " sel" : "";
+               $sel = ( $this->p('orderby')=="$name asc") ? " sel active" : "";
                $html .= '<a title="'.$title.'first In (asc)" class="sort asc'.$sel.'" href="'.$this->url_list('orderby',"$name asc").'">'
                        .'<span class="asc">&darr;</span>'
                .'</a>';
@@ -698,7 +698,7 @@ Class Table extends nb {
                #$html .= '&nbsp;';
 
                # Desc
-               $sel = ( $this->p('orderby')=="$name desc") ? " sel" : "";
+               $sel = ( $this->p('orderby')=="$name desc") ? " sel active" : "";
                $html .= '<a title="'.$title.'last In (desc)" class="sort desc'.$sel.'" href="'.$this->url_list('orderby',"$name desc").'">'
                        .'<span class="desc">&uarr;</span>'
                .'</a>';
@@ -769,7 +769,14 @@ Class Table extends nb {
                                if ($need_all_values) return null;
                                continue;
                        }
-                       $where[] = $field->sql_name() . '=' . $field->quote($hvalues[$k]);
+                       $where[] = $field->sql_where($hvalues[$k]);
+                       /*
+                       if ($field->bool()) {
+                               $where[] = $field->sql_name() . '=' . $field->quote($hvalues[$k]);
+                       } else {
+                               $where[] = $field->sql_name() . '=' . $field->quote($hvalues[$k]);
+                       }
+                        */
                }
 
                return empty($where) ? '' : ' WHERE ' . join(' AND ',$where);
@@ -845,7 +852,8 @@ Class Table extends nb {
                        } else {
 
                                // Integer
-                               if ($field->numeric()) {
+                               if ($field->bool()) {
+                               } elseif ($field->numeric()) {
                                        if (strtolower($v)=='null') $v = '0';
                                        #$k = "COLAESCE($k,0)";
 
@@ -867,6 +875,11 @@ Class Table extends nb {
                        } elseif ($match) {
                                if ($field->null) $k = 'COALESCE('.$name.','.$this->db()->quote('').")";
 
+                       } elseif (true and $field->bool()) {
+                               $k = $field->sql_where($v);
+                               $v = '';
+                               $equal = '';
+
                        } elseif ($field->numeric()) {
                                $name = $field->sql_name();
                                if ($field->null) $k = 'COALESCE('.$name.",0)";
index 1650dbeb48536281318386f1d80c594248beb39a..3afba334c25711b120c07d2c28806dce8efe9b4b 100644 (file)
@@ -836,7 +836,7 @@ class Mime {
                return '<img src="data:image/jpeg;base64,'.$v.'" />';
        }
 
-       public static function html(&$str,$default=null,&$mime=null) {
+       public static function html(&$str,$default=null,&$mime=null,&$input_type=null) {
 
                if (!$str) return $str;
 
@@ -852,14 +852,22 @@ class Mime {
                if ($mime == 'text/plain') {
 
                        # Videos
-                       if ($v = self::html_video($str)) return $v;
+                       if ($v = self::html_video($str)) {
+                               $input_type = 'url';
+                               return $v;
+                       }
 
                        # A HREF
                        elseif($v = preg_replace_callback(',^(\w+://\S+),',function($v){
                                return '<a target="_blank" href="'.$v[1].'">'.htmlentities($v[1]).'</a>';
-                       },$str) and $str != $v) return $v;
+                       },$str) and $str != $v)
+                       {
+                               $input_type = 'url';
+                               return $v;
+                       }
 
                } elseif (preg_match('@^image/@',$mime)) {
+                       $input_type = 'file';
                        return self::html_img_jpeg($str);
 
                }
index 785a47485aa4902d0a28d4ffcfa740f4d7d303a2..e240ca98ba99dd1d9c9cc8ccf629a5ea5179aeba 100644 (file)
@@ -45,11 +45,11 @@ h1, h1 a, h1 a:hover {
        font-weight: normal !important;;
 }
 
-.menu * {
+#db-menu-form_ * {
        font-size: 95%;
 }
 
-.menu .form-control {
+#db-menu-form .form-control {
        max-width: 5rem;
        height: 1.5rem;
        padding: 0 0.3rem;
@@ -57,12 +57,12 @@ h1, h1 a, h1 a:hover {
        margin-left: 0.2rem;
 }
 
-.menu .btn-group {
+#db-menu-form .btn-group {
        margin-left: 0.4rem;
        max-height: 2.5rem;
 }
 
-.edit .form-control + .media {
+.db-edit .form-control + .media {
        /*
        border:solid 1px red;
        display: inline-block;
@@ -104,3 +104,7 @@ h1, h1 a, h1 a:hover {
        text-shadow: none;
        background-color: transparent;
 }
+
+a.active, .active a {
+       color: var(--danger);
+}
index 024b21b0c3ecfaafc0af78b1aa5e8d190d0ea949..27d853b95eacb1f6b311d86afb7dc3e54a976e5b 100644 (file)
@@ -1 +1 @@
-@keyframes spin{0%{transform:rotate(0)}100%{transform:rotate(360deg)}}.loader{border:16px solid #fff;border-radius:50%;border-top:16px solid #000;padding:0;margin:0 auto 0 auto;width:8em;height:8em;animation:spin 2s linear infinite;display:block}body.loader{margin-top:8em}.loader *{display:none!important}h1{margin:0 0 .2em 0;font-size:140%;padding-bottom:.3em;margin-bottom:.5em;padding:.3em;background-color:var(--dark)}h1,h1 a,h1 a:hover{color:var(--light);font-weight:400!important}.menu *{font-size:95%}.menu .form-control{max-width:5rem;height:1.5rem;padding:0 .3rem;margin-top:.4rem;margin-left:.2rem}.menu .btn-group{margin-left:.4rem;max-height:2.5rem}.edit .form-control+.media{margin-top:.25rem}#db-menu-form,#page-bottom,.CENTER,.db-results,.db-rows,.pagination{margin-left:auto;margin-right:auto;width:auto;display:table}.pagination *{display:inline-block}.D-NONE,.db-field-delete label,.db-field-view label{display:none}.HIDE,.db-rows th.view{color:transparent;text-shadow:none;background-color:transparent}
+@keyframes spin{0%{transform:rotate(0)}100%{transform:rotate(360deg)}}.loader{border:16px solid #fff;border-radius:50%;border-top:16px solid #000;padding:0;margin:0 auto 0 auto;width:8em;height:8em;animation:spin 2s linear infinite;display:block}body.loader{margin-top:8em}.loader *{display:none!important}h1{margin:0 0 .2em 0;font-size:140%;padding-bottom:.3em;margin-bottom:.5em;padding:.3em;background-color:var(--dark)}h1,h1 a,h1 a:hover{color:var(--light);font-weight:400!important}#db-menu-form_ *{font-size:95%}#db-menu-form .form-control{max-width:5rem;height:1.5rem;padding:0 .3rem;margin-top:.4rem;margin-left:.2rem}#db-menu-form .btn-group{margin-left:.4rem;max-height:2.5rem}.db-edit .form-control+.media{margin-top:.25rem}#db-menu-form,#page-bottom,.CENTER,.db-results,.db-rows,.pagination{margin-left:auto;margin-right:auto;width:auto;display:table}.pagination *{display:inline-block}.D-NONE,.db-field-delete label,.db-field-view label{display:none}.HIDE,.db-rows th.view{color:transparent;text-shadow:none;background-color:transparent}.active a,a.active{color:var(--danger)}