]> git.nbdom.net Git - nb.git/commitdiff
lib/php/db/table.php
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Tue, 6 Feb 2018 04:55:32 +0000 (04:55 +0000)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Tue, 6 Feb 2018 04:55:32 +0000 (04:55 +0000)
etc/dbq/ldap.php
lib/php/db/field.php
lib/php/db/table.php
lib/php/db/types/ldap.php
lib/php/ldap.php

index f4e127be0513af1aafa7feabcf397445573bd518..0e42d772d940d1d0fad3b1112233f53fe481ecb1 100644 (file)
@@ -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,
 ];
index 72d93f1d1dceceacf8cd3f0ec5ade27673f82256..9d88b899c3a282bef25942bb40ae46b18000d7dc 100644 (file)
@@ -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 = '<span class="extra" id="'.$this->name.'">'.$this->out($value).'</span>';
 
-    } elseif (strpos($this->type,'bool') !== false) {
-      $html .= $this->htmlYesNo($value);
+               foreach ((array)$values as $value) {
+       $html .= '<div class="label '.$this->name.'"><label for="'.$this->name.'">'.htmlspecialchars(prettyText($this->name)).'</label>';
 
-    } else {
-      $html = '<'.$tag
-        .' name="'.$this->preffix.$this->name.'"'
-        .' id="'.$this->name.'"'
-        .( ($size and $tag != 'textarea') ? ' size="'.$size.'"' : '')
-        #.' value="'.$value.'"'
-        .($tag == 'textarea' ? '>'.$value.'</textarea>' : ' type="'
-          .($type ? $type : $this->html_type())
-        .'" value="'.$value.'" />')
-      ;
+       if ($this->extras) {
+       $html .= '<span class="extra '.$this->name.'">'.$this->out($value).'</span>';
 
-    }
+       } 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.'</textarea>' : ' type="'
+               .($type ? $type : $this->html_type())
+               .'" value="'.$value.'" />')
+       ;
+
+       }
+
+       $html .= '</div>'.NB_EOL;
+               }
 
-    return '<div class="label '.$this->name.'"><label for="'.$this->name.'">'.htmlspecialchars(prettyText($this->name)).'</label>'
-      .$html
-    .'</div>'.NB_EOL;
+               return $html;
 
   }
 
index a95222d3be97f0b098e25b848da8beb463ddc3d6..7c20b523d5fe1601a7aa995edef75206dd741220 100644 (file)
@@ -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('&amp;',$flat); 
+# NB 06.02.18                          $form_action .= ( strpos('?',$form_action) === false ? '?' : '' ) . join('&amp;',$flat); 
+                               $form_action .= ( strpos($form_action,'?') === false ? '?' : '' ) . join('&amp;',$flat); 
                                $form_hidden = '';
                        }
                }
@@ -591,11 +602,10 @@ Class Table extends nb {
                // Form
                echo '<form class="db edit form-table" method="post" action="'.$form_action.'">'.NB_EOL;
                echo '<div class="fields">'.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
 ?>
index bba01a51003aaba919c58bf53c5b60826e05ade5..4f422ddb706411518db49ca9c4e562c6b58cbdb1 100644 (file)
@@ -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, ];
        },
 ];
 ?>
index 7b6f79bc8c7a37177fe70cfda54ac133514ef257..3fd484bb52b918f683b6f3af1c2a56f88cb72bae 100644 (file)
@@ -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;