From: Nicolas Boisselier Date: Tue, 6 Feb 2018 04:55:32 +0000 (+0000) Subject: lib/php/db/table.php X-Git-Url: https://git.nbdom.net/?a=commitdiff_plain;h=c1b34c5d53a5389dd704f68f501be6e599405615;p=nb.git lib/php/db/table.php --- diff --git a/etc/dbq/ldap.php b/etc/dbq/ldap.php index f4e127be..0e42d772 100644 --- a/etc/dbq/ldap.php +++ b/etc/dbq/ldap.php @@ -51,14 +51,8 @@ $filter = '(&('.$filter.')(userPassword=*))'; $DBQ['ldap'] = $GLOBALS['DBQ_LDAP'] + [ 'type' => 'ldap', 'tables' => [ - $filter => [ - #'fields' => ['dn','objectClass'], - 'fields' => [ - 'dn' => (new Field(['name'=>'dn','key'=>true])), - 'objectClass' => (new Field(['name'=>'objectClass'])), - ], - 'type' => 'table', - ], + 'objectClass=*' => [ 'fields' => ['!dn','objectClass'], 'type' => 'table'], + $filter => [ 'fields' => ['!dn','objectClass'], 'type' => 'table'], ], 'default_table' => $filter, ]; diff --git a/lib/php/db/field.php b/lib/php/db/field.php index 72d93f1d..9d88b899 100644 --- a/lib/php/db/field.php +++ b/lib/php/db/field.php @@ -110,34 +110,39 @@ class field extends nb { } - public function html_edit($value,$type=null) { + public function html_edit($values,$type=null) { $size = ($this->size() and is_scalar($this->size())) ? $this->size() : 0; $tag = ( ($size>$this->textarea_size) or preg_match('/^(\w*text|binary|blob)/i',$this->type) ) ? 'textarea' : 'input'; $html = ''; - if ($this->extras) { - $html = ''.$this->out($value).''; - } elseif (strpos($this->type,'bool') !== false) { - $html .= $this->htmlYesNo($value); + foreach ((array)$values as $value) { + $html .= '
'; - } else { - $html = '<'.$tag - .' name="'.$this->preffix.$this->name.'"' - .' id="'.$this->name.'"' - .( ($size and $tag != 'textarea') ? ' size="'.$size.'"' : '') - #.' value="'.$value.'"' - .($tag == 'textarea' ? '>'.$value.'' : ' type="' - .($type ? $type : $this->html_type()) - .'" value="'.$value.'" />') - ; + if ($this->extras) { + $html .= ''.$this->out($value).''; - } + } elseif (strpos($this->type,'bool') !== false) { + $html .= $this->htmlYesNo($value); + + } else { + $html .= '<'.$tag + .' name="'.$this->preffix.$this->name.'"' + # NB 06.02.18 .' id="'.$this->name.'"' + .( ($size and $tag != 'textarea') ? ' size="'.$size.'"' : '') + #.' value="'.$value.'"' + .($tag == 'textarea' ? '>'.$value.'' : ' type="' + .($type ? $type : $this->html_type()) + .'" value="'.$value.'" />') + ; + + } + + $html .= '
'.NB_EOL; + } - return '
' - .$html - .'
'.NB_EOL; + return $html; } diff --git a/lib/php/db/table.php b/lib/php/db/table.php index a95222d3..7c20b523 100644 --- a/lib/php/db/table.php +++ b/lib/php/db/table.php @@ -543,9 +543,19 @@ Class Table extends nb { if (!is_array($values)) $values = [$values]; if (!$add) { - $sql = $this->sql_edit($values,$add); - $st = $this->db()->conn->prepare($sql); - $st->execute(); + if ($fct = $this->db()->conf_type('html_edit_row')) { + } else { + $fct = function (&$table,&$values) use ($add) { + $sql = $table->sql_edit($values,$add); + $st = $table->db()->conn->prepare($sql); + $st->execute(); + $row = $st->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT); + $st->closeCursor(); + return $row; + }; + } + $row = $fct($this,$values); + $table = $this; } // Params @@ -583,7 +593,8 @@ Class Table extends nb { $flat[] = $k . '=' . urlencode($v); } if (NB_P_GET) { - $form_action .= ( strpos('?',$form_action) === false ? '?' : '' ) . join('&',$flat); +# NB 06.02.18 $form_action .= ( strpos('?',$form_action) === false ? '?' : '' ) . join('&',$flat); + $form_action .= ( strpos($form_action,'?') === false ? '?' : '' ) . join('&',$flat); $form_hidden = ''; } } @@ -591,11 +602,10 @@ Class Table extends nb { // Form echo '
'.NB_EOL; echo '
'.NB_EOL; - $count = 0; - if ( $add or ($row = $st->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT))) { + #if ( $add or ($row = $st->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT))) { + if ( $add or $row) { if (!empty($row)) $this->db()->table_row_decrypt($this,$row); - $count ++; foreach ($this->fields() as $name => $field) { @@ -851,13 +861,19 @@ Class Table extends nb { } - private function add_fields($fields) { + public function add_fields($fields) { foreach ($fields as $k => $v) { + $key = false; if (is_scalar($v)) { + if (strpos($v,'!') === 0) { + $v = substr($v,1); + $key = true; + } $this->fields[$v] = new Field([ 'name' => $v, 'type' => 'text', 'table' => $this, + 'key' => $key, ]); } else { @@ -1000,13 +1016,7 @@ Class Table extends nb { // // Fields filter // - if ($this->fields_only) { - $new_row = []; - foreach ($this->fields_only as $k) { - if (isset($row[$k])) $new_row[$k] = $row[$k]; - } - $row = $new_row; - } + $this->fields_only($row); // // Format @@ -1072,11 +1082,7 @@ Class Table extends nb { # Fields # $opt['fields'] = $fields = $this->fields(); - if ($this->fields_only) { - foreach ($fields as $k=>$v) { - if (!in_array($k,$this->fields_only)) unset($fields[$k]); - } - } + $this->fields_only($fields); # # Build query @@ -1098,13 +1104,13 @@ Class Table extends nb { if ($nosql) { $sql = empty($this->sql) ? $this->name : $this->sql; + $fct = $this->db()->conf_type('rows_prepare'); if ($fct) $st = $fct($this,$sql); + if (!$fct = $this->db()->conf_type('rows_fetch')) { $fct = function() use($st) { return $st->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT); }; } - #$fct = function() { return false; }; - #$fct = function() use($st) { return $st->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT); }; } else { @@ -1759,6 +1765,7 @@ Class Table extends nb { } public function update($post,&$info=[]) { + if ($fct = $this->db()->conf_type('update')) return $fct($this,$post); $keys = []; $keys_values = []; @@ -2363,5 +2370,15 @@ Class Table extends nb { die($msg); } + public function fields_only(&$row,$fields=null) { + if ($fields === null) $fields = $this->fields_only; + if (empty($fields)) return; + $new_row = []; + foreach ($fields as $k) { + if (isset($row[$k])) $new_row[$k] = isset($row[$k]) ? $row[$k] : null; + } + $row = $new_row; + } + } # < Class ?> diff --git a/lib/php/db/types/ldap.php b/lib/php/db/types/ldap.php index bba01a51..4f422ddb 100644 --- a/lib/php/db/types/ldap.php +++ b/lib/php/db/types/ldap.php @@ -7,6 +7,18 @@ $DB_TYPES['ldap'] = [ 'quote_name' => '', 'quote' => function($v) { return $v; }, 'nosql' => true, + '_connect' => function($db) { + require_once(NB_ROOT.'/lib/php/ldap.php'); + + $db->ldap = new Ldap([ + 'host' => $this->host, + 'user' => $this->user, + 'password' => $this->password, + 'base' => $this->base, + ]); + + return $db->ldap->connect(); + }, #'prepare' => function($sql) { return $this->ldap->prepare($sql); }, 'rows_prepare' => function($table,$sql) { $fields = (empty($table->fields_only) ? array_keys($table->fields()) : $table->fields_only); @@ -20,19 +32,20 @@ $DB_TYPES['ldap'] = [ if (!$row) return []; $fields = (empty($table->fields_only) ? array_keys($table->fields()) : $table->fields_only); $row = $table->db()->ldap->ar_filter_keys($row,$fields); + $table->fields_only($row,$fields); # reorder by fields return $row; }, - '_connect' => function($db) { - require_once(NB_ROOT.'/lib/php/ldap.php'); - - $db->ldap = new Ldap([ - 'host' => $this->host, - 'user' => $this->user, - 'password' => $this->password, - 'base' => $this->base, + 'html_edit_row' => function(&$table,&$values) { + $table->db()->ldap->prepare([ + 'base' => $values['dn'], ]); - - return $db->ldap->connect(); + $row = $table->db()->ldap->fetch(); + #debug($row); + return $row; + }, + 'update' => function(&$table,&$values) { + $count = (int)$table->db()->ldap->update($values['dn']); + return [ 'count' => $count, ]; }, ]; ?> diff --git a/lib/php/ldap.php b/lib/php/ldap.php index 7b6f79bc..3fd484bb 100644 --- a/lib/php/ldap.php +++ b/lib/php/ldap.php @@ -138,15 +138,16 @@ class Ldap { /// DB / PDO compatible public function setAttribute($k,$v) { } - public function prepare($sql,$o=[]) { - if (is_array($sql)) { - $o = $sql; - $sql = $o['filter']; + public function prepare($filter,$o=[]) { + if (is_array($filter)) { + $o = $filter; +# NB 06.02.18 if (isset($o['filter'])) $filter = $o['filter']; + $filter = isset($o['filter']) ? $o['filter'] : 'objectClass=*'; } + $o['filter'] = $filter; if (!isset($o['base'])) $o['base'] = $this->base; #if (!isset($o['filter'])) $o = array_merge($o,[ 'filter' => 'objectClass=*', 'attrs' => ['dn','objectClass'], 'dn' => true ]); - $o['filter'] = $sql; if (!isset($o['attrs'])) $o['attrs'] = []; if (!is_array($o['attrs'])) $o['attrs'] = [$o['attrs']]; if (!isset($o['limit'])) $o['limit'] = 0;