From: Nicolas Boisselier Date: Tue, 2 Oct 2018 22:18:27 +0000 (+0100) Subject: lib/php/db/table.php X-Git-Url: https://git.nbdom.net/?a=commitdiff_plain;h=1e55139a167db7250ab80bdd21ffa81802dbc9fa;p=nb.git lib/php/db/table.php --- diff --git a/lib/php/db/field.php b/lib/php/db/field.php index 8a8354e2..5d591356 100644 --- a/lib/php/db/field.php +++ b/lib/php/db/field.php @@ -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 .= ''.$this->out($value).''; - } 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 = ''.$media.''; + } + # 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.'' : ' 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 .= '
'.$media.'
'; - } + .$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; } diff --git a/lib/php/db/table.php b/lib/php/db/table.php index 65e60209..94d077c1 100644 --- a/lib/php/db/table.php +++ b/lib/php/db/table.php @@ -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 .= ""; - $h .= ''.NB_EOL; + $h .= ''.NB_EOL; } } @@ -603,7 +603,7 @@ Class Table extends nb { } // Form - echo '
'.NB_EOL; + echo ''.NB_EOL; echo '
'.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 .= '' .'' .''; @@ -698,7 +698,7 @@ Class Table extends nb { #$html .= ' '; # Desc - $sel = ( $this->p('orderby')=="$name desc") ? " sel" : ""; + $sel = ( $this->p('orderby')=="$name desc") ? " sel active" : ""; $html .= '' .'' .''; @@ -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)"; diff --git a/lib/php/mime.php b/lib/php/mime.php index 1650dbeb..3afba334 100644 --- a/lib/php/mime.php +++ b/lib/php/mime.php @@ -836,7 +836,7 @@ class Mime { return ''; } - 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 ''.htmlentities($v[1]).''; - },$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); } diff --git a/www/dbq/html/default.css b/www/dbq/html/default.css index 785a4748..e240ca98 100644 --- a/www/dbq/html/default.css +++ b/www/dbq/html/default.css @@ -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); +} diff --git a/www/dbq/html/default.min.css b/www/dbq/html/default.min.css index 024b21b0..27d853b9 100644 --- a/www/dbq/html/default.min.css +++ b/www/dbq/html/default.min.css @@ -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)}