$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,
];
}
- 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;
}
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
$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 = '';
}
}
// 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) {
}
- 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 {
//
// 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
# 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
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 {
}
public function update($post,&$info=[]) {
+ if ($fct = $this->db()->conf_type('update')) return $fct($this,$post);
$keys = [];
$keys_values = [];
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
?>
'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);
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, ];
},
];
?>
/// 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;