self::$params = $opt['params']; unset($opt['params']);
}
+ // Fields
+ if (isset($opt['fields'])) {
+ $this->add_fields($opt['fields']);
+ $this->_fields = 1;
+ unset($opt['fields']);
+ }
+
// Db / Connection
if (isset($opt['db'])) {
$this->db(is_object($opt['db']) ? $opt['db'] : new Db($opt['db']));
}
- public function create_temporary_rows() {
+ private function create_temporary_rows() {
// For static rows
if(empty($this->rows)) return null;
if(!empty($this->rows) and empty($this->type)) $this->type = 'rows';
* Create temporary if needed
*
*/
- public function create_temporary() {
+ private function create_temporary() {
if (!empty($this->_create_temporary)) return;
$this->_create_temporary = true;
*/
public function fields($name=null) {
- $this->create_temporary();
-
if (!isset($this->_fields)) {
+ $this->create_temporary();
+
$this->_fields = true;
# Extras fields at the end
return $this->fields;
}
- public function url_keys($values=null,$params=[],$preff='?',$sep='&') {
+ private function url_keys($values=null,$params=[],$preff='?',$sep='&') {
if ($values === null) $values = $this->p();
$url = is_array($params) ? $params : [$params];
return $url ? $preff.join($sep,$url) : '';
}
- public function fields_keys_values($values) {
+ private function fields_keys_values($values) {
$keys = $this->fields_keys();
if (empty($keys)) $keys = $this->fields();
return $ret;
}
- public function fields_keys(&$others=[]) {
+ private function fields_keys(&$others=[]) {
$fields_keys = [];
return html_edit($values,$form_action,true);
}
- public function sql_edit($values = null,&$add=false) {
+ private function sql_edit($values = null,&$add=false) {
$where = $this->where($this->fields(),$values);
if (empty($where)) {
$where = ' WHERE 1=0';
}
- public function url_list($k='',$v='') {
+ private function url_list($k='',$v='') {
$params = [];
$fields = ($this->p('action') == 'delete') ? [] : $this->fields();
}
- public function url_sort($name) {
+ private function url_sort($name) {
if (!$this->show_url_sort or !($f=$this->field($name)) or !empty($f->dyn) or $f->is_encrypt()) return self::prettyText($name);
#debug($f);
}
- public function where($fields,$hvalues,$need_all_values=false) {
+ private function where($fields,$hvalues,$need_all_values=false) {
// Construct where
$where = [];
}
- public function where_criterias($values,$logic='') {
+ private function where_criterias($values,$logic='') {
$having = $where = [];
if (empty($logic)) $logic = 'AND';
}
-# NB 01.02.18 private function add_fields($fields) {
-# NB 01.02.18 foreach ($fields as $k => $v) {
-# NB 01.02.18
-# NB 01.02.18 if (is_scalar($v)) {
-# NB 01.02.18 $this->fields[] = new Field([
-# NB 01.02.18 'name' => $k,
-# NB 01.02.18 'type' => 'text',
-# NB 01.02.18 'table' => $this,
-# NB 01.02.18 ]);
-# NB 01.02.18
-# NB 01.02.18 } else {
-# NB 01.02.18 $this->fields[$k] = $v;
-# NB 01.02.18
-# NB 01.02.18 }
-# NB 01.02.18
-# NB 01.02.18 }
-# NB 01.02.18 }
+ private function add_fields($fields) {
+ foreach ($fields as $k => $v) {
+ if (is_scalar($v)) {
+ $this->fields[$v] = new Field([
+ 'name' => $v,
+ 'type' => 'text',
+ 'table' => $this,
+ ]);
+
+ } else {
+ $this->fields[$k] = $v;
+
+ }
+ }
+ }
+
private function add_extras($extras) {
if ($this->p('extras') === '0') return false;
}
- public function select_extras() {
+ private function select_extras() {
if (empty($this->extras)) return '';
return ','.join(',',$select);
}
- public function rows_count() {
- $opt = [ 'count' => 1 ];
- return $this->db()->row($this->rows_sql($opt));
- }
+# NB 04.02.18 public function rows_count() {
+# NB 04.02.18 $opt = [ 'count' => 1 ];
+# NB 04.02.18 return $this->db()->row($this->rows_sql($opt));
+# NB 04.02.18 }
public function rows_sql(&$opt=[]) {
/*-----------------------------------------------------------------
No Out
-----------------------------------------------------------------*/
- public function rows_begin_($fields,&$o) {
+ private function rows_begin_($fields,&$o) {
$o['sep'] = '';
$o['var'] = [
'head' => array_keys($fields),
'rows' => [],
];
}
-
- public function rows_rec_($row,&$o) {
- $o['var']['rows'][] = $row;
- }
-
- public function rows_end_() {
- }
+ private function rows_rec_($row,&$o) { $o['var']['rows'][] = $row; }
+ private function rows_end_() { }
/*-----------------------------------------------------------------
Template
-----------------------------------------------------------------*/
- public function rows_begin_template($fields,&$o) {
+ private function rows_begin_template($fields,&$o) {
$id = $this->idtemplate();
$id = preg_replace('/[^\w\._-]/','',$id);
# NB 02.12.16 require $o['file'];
}
- public function rows_rec_template($row,&$o) {
+ private function rows_rec_template($row,&$o) {
$i = 0;
foreach ($row as $k=>$v) {
$row[$i] = $v;
# NB 02.12.16 $ROW = $row;
}
- public function rows_end_template() {
+ private function rows_end_template() {
$HEAD = &$this->__fields;
$ROWS = &$this->__rows;
$ROW = &$ROWS[0];;
/*-----------------------------------------------------------------
Sql
-----------------------------------------------------------------*/
- public function rows_begin_sql() {
+ private function rows_begin_sql() {
return '';
return "\n-- ".$this->name."\n";
}
- public function rows_rec_sql(&$row,&$opt) {
+ private function rows_rec_sql(&$row,&$opt) {
$keys = $values = [];
foreach ($row as $k=>$v) {
return $sql.NB_EOL;
}
- public function rows_end_sql() {
+ private function rows_end_sql() {
return '';
}
/*-----------------------------------------------------------------
Text
-----------------------------------------------------------------*/
- public function rows_begin_text() {
+ private function rows_begin_text() {
$this->_row_text = null;
return '';
}
- public function rows_rec_text(&$row) {
+ private function rows_rec_text(&$row) {
$text = '';
if ($this->_row_text === null) {
$this->_row_text = true;
return $text.$row."\n";
}
- public function rows_end_text() {
+ private function rows_end_text() {
unset($this->_row_text);
return '';
}
/*-----------------------------------------------------------------
Json
-----------------------------------------------------------------*/
- public function rows_begin_json() {
+ private function rows_begin_json() {
$this->_row_json = null;
return '['.NB_EOL;
return ''
.'['.NB_EOL;
}
- public function rows_rec_json(&$row) {
+ private function rows_rec_json(&$row) {
if ($this->_row_json === null) {
$json = '';
$this->_row_json = true;
return $json . json_encode($row);
}
- public function rows_end_json() {
+ private function rows_end_json() {
unset($this->_row_json);
return NB_EOL.']'.NB_EOL;
}
/*-----------------------------------------------------------------
Xml
-----------------------------------------------------------------*/
- public function rows_begin_xml() {
+ private function rows_begin_xml() {
return ''
.'<?xml version="1.0" encoding="utf-8"?>'.NB_EOL #<?
.'<rows name="'.$this->name.'" database="'.$this->db()->name.'" database-type="'.$this->db()->type.'">'.NB_EOL
;
}
- public function rows_rec_xml(&$row) {
+ private function rows_rec_xml(&$row) {
$xml = '';
$xml .= TABLE_INDENT."<row>".NB_EOL;
foreach ($row as $k=>$v) {
return $xml;
}
- public function rows_end_xml() {
+ private function rows_end_xml() {
return '</rows>'.NB_EOL;
}
- public function zaza() { return [ ['A','B'], ['a','bb'] ]; }
-
/*-----------------------------------------------------------------
Csv
-----------------------------------------------------------------*/
- public function rows_begin_csv($fields) {
+ private function rows_begin_csv($fields) {
if (self::p('rows_head_char')!=='') echo self::p('rows_head_char');
if (!$this->show_header) return '';
return join(TABLE_CSV_SEP,array_keys($fields))."\n";
}
- public function rows_rec_csv(&$row) {
+ private function rows_rec_csv(&$row) {
return str_replace("\n",'|',join(TABLE_CSV_SEP,array_values($row)))."\n";
}
- public function rows_end_csv() {
+ private function rows_end_csv() {
return '';
}
/*-----------------------------------------------------------------
Html Table
-----------------------------------------------------------------*/
- public function rows_begin_table($fields) {
+ private function rows_begin_table($fields) {
$html = '';
return $html;
}
- public function rows_rec_table(&$row,&$opt) {
+ private function rows_rec_table(&$row,&$opt) {
$buttons = $this->html_row_buttons($row);
return $html;
}
- public function rows_end_table() {
+ private function rows_end_table() {
unset($this->__rows_table_attr);
$html = '';
$html .= '</tbody>'.NB_EOL;
/*-----------------------------------------------------------------
Html Div
-----------------------------------------------------------------*/
- public function rows_begin_div() {
+ private function rows_begin_div() {
return '<div class="rows search-results">'.NB_EOL;
}
- public function rows_rec_div(&$row) {
+ private function rows_rec_div(&$row) {
$html = '';
return $html;
}
- public function rows_end_div() {
+ private function rows_end_div() {
return '</div>'.NB_EOL;
}
- public function sql_names($value) {
+ public function sql_names($values) {
$new = [];
- foreach ($value as $k=>$v) {
+ foreach ($values as $k=>$v) {
$new[$k] = $this->sql_name($v);
}
return $new;
return $this->db()->sql_name($value === null ? $this->name : $value);
}
- private function _post2sql($post) {
- $keys = [];
- $keys_values = [];
- $fields = [];
- $fields_values = [];
- foreach ($this->fields() as $name => $field) {
-
- if ($field->extras) continue;
-
- $value = $post[$this->ppreff().$name];
-
- if ($field->key) {
- $keys[] = $name;
-
- if (!isset($post[$this->key_preff.$name])) {
- if ($field->key) $this->bye("Missing `$name`!");
- continue;
- }
-
- if (isset($post[$this->key_preff.$name])) {
- $keys_values[] = $post[$this->key_preff.$name];
-
- } else {
- $keys_values[] = $value;
- }
-
- } else {
-
- $fields[] = $name;
- $fields_values[] = $value;
-
- }
- }
-
- return [
- 'keys' => $keys,
- 'keys_values' => $keys_values,
- 'fields' => $fields,
- 'fields_values' => $fields_values,
- 'all_keys' => array_combine($keys,$keys_values),
- 'all_fields' => array_combine($fields,$fields_values),
- ];
- }
+# NB 04.02.18 private function _post2sql($post) {
+# NB 04.02.18 $keys = [];
+# NB 04.02.18 $keys_values = [];
+# NB 04.02.18 $fields = [];
+# NB 04.02.18 $fields_values = [];
+# NB 04.02.18 foreach ($this->fields() as $name => $field) {
+# NB 04.02.18
+# NB 04.02.18 if ($field->extras) continue;
+# NB 04.02.18
+# NB 04.02.18 $value = $post[$this->ppreff().$name];
+# NB 04.02.18
+# NB 04.02.18 if ($field->key) {
+# NB 04.02.18 $keys[] = $name;
+# NB 04.02.18
+# NB 04.02.18 if (!isset($post[$this->key_preff.$name])) {
+# NB 04.02.18 if ($field->key) $this->bye("Missing `$name`!");
+# NB 04.02.18 continue;
+# NB 04.02.18 }
+# NB 04.02.18
+# NB 04.02.18 if (isset($post[$this->key_preff.$name])) {
+# NB 04.02.18 $keys_values[] = $post[$this->key_preff.$name];
+# NB 04.02.18
+# NB 04.02.18 } else {
+# NB 04.02.18 $keys_values[] = $value;
+# NB 04.02.18 }
+# NB 04.02.18
+# NB 04.02.18 } else {
+# NB 04.02.18
+# NB 04.02.18 $fields[] = $name;
+# NB 04.02.18 $fields_values[] = $value;
+# NB 04.02.18
+# NB 04.02.18 }
+# NB 04.02.18 }
+# NB 04.02.18
+# NB 04.02.18 return [
+# NB 04.02.18 'keys' => $keys,
+# NB 04.02.18 'keys_values' => $keys_values,
+# NB 04.02.18 'fields' => $fields,
+# NB 04.02.18 'fields_values' => $fields_values,
+# NB 04.02.18 'all_keys' => array_combine($keys,$keys_values),
+# NB 04.02.18 'all_fields' => array_combine($fields,$fields_values),
+# NB 04.02.18 ];
+# NB 04.02.18 }
public function replace($hvalues,&$info=[]) {
# NB 12.12.17: When REPLACE is not supported where call a personalized function to modify $sql
return false;
}
- public function html_row_buttons(&$row) {
+ private function html_row_buttons(&$row) {
if (!$this->show_buttons or empty(self::$params)) return [];
return [
(!DB_HTML_EDIT ? '' : '<a class="edit button" href="'.$this->url_keys($row,'action=edit').'">'.DB_HTML_EDIT.'</a>'.NB_EOL),
];
}
- public function html_rows_top() {
+ private function html_rows_top() {
if (self::p('replace') === '0') return '';
$html = '';
}
public function count() {
-
if (isset($this->count)) return $this->count;
-
- $sql_count = $this->name;
-
- # We could use it instead of query, but wont be cached ! $this->create_temporary();
- if ($this->type == 'sql' and $this->name !== DB_TABLE_QUERY_NAME) {
- if (!$this->sql) return;
-
- $sql_count = "SELECT COUNT(*) FROM ($this->sql) _table_count" ;
- #debug([$this->name,$sql_count]);
-# NB 08.08.17 if (preg_match('/SELECT.*SELECT|UNION/i',$this->sql)) {
-# NB 08.08.17 $sql_count = "SELECT COUNT(*) FROM ($this->sql) _table_count" ;
-# NB 08.08.17 } else {
-# NB 08.08.17 $sql_count = "SELECT COUNT(*) FROM ".preg_replace('/^SELECT .*?\s+FROM\s+/','',$this->sql);
-# NB 08.08.17 bye($sql_count);
-# NB 08.08.17 }
-
- } elseif (preg_match('/^[\w_-]+$/',$sql_count)) {
- $sql_count = "SELECT count(*) FROM ".$this->sql_name($sql_count);
-
- } else {
- $sql_count = preg_replace('/ (ORDER|LIMIT) .*?$/s','',$sql_count);
- $sql_count = preg_replace('/^SELECT .*FROM/s','SELECT count(*) FROM ',$sql_count);
-
- }
-
- $this->count = (int)$this->db()->row($sql_count);
+ $opt = [ 'count' => 1 ];
+ $this->count = $this->db()->row($this->rows_sql($opt));
return $this->count;
+# NB 04.02.18
+# NB 04.02.18 if (isset($this->count)) return $this->count;
+# NB 04.02.18
+# NB 04.02.18 $sql_count = $this->name;
+# NB 04.02.18
+# NB 04.02.18 # We could use it instead of query, but wont be cached ! $this->create_temporary();
+# NB 04.02.18 if ($this->type == 'sql' and $this->name !== DB_TABLE_QUERY_NAME) {
+# NB 04.02.18 if (!$this->sql) return;
+# NB 04.02.18
+# NB 04.02.18 $sql_count = "SELECT COUNT(*) FROM ($this->sql) _table_count" ;
+# NB 04.02.18
+# NB 04.02.18 } elseif (preg_match('/^[\w_-]+$/',$sql_count)) {
+# NB 04.02.18 $sql_count = "SELECT count(*) FROM ".$this->sql_name($sql_count);
+# NB 04.02.18
+# NB 04.02.18 } else {
+# NB 04.02.18 $sql_count = preg_replace('/ (ORDER|LIMIT) .*?$/s','',$sql_count);
+# NB 04.02.18 $sql_count = preg_replace('/^SELECT .*FROM/s','SELECT count(*) FROM ',$sql_count);
+# NB 04.02.18
+# NB 04.02.18 }
+# NB 04.02.18
+# NB 04.02.18 $this->count = (int)$this->db()->row($sql_count);
+# NB 04.02.18 return $this->count;
}
public function serialize() {
$this->fields();
- #$this->count();
return serialize($this);
}