<?php
require_once(dirname(__FILE__).'/../db.php');
require_once(dirname(__FILE__).'/../db/field.php');
+require_once(dirname(__FILE__).'/../out.php');
if (false and empty($_SERVER['DOCUMENT_ROOT'])) {
$_REQUEST['db'] = 'rent'; $name = 'tenant';
* return the sql to create the table
*
*/
- function sql() {
+ public function sql() {
if (isset($this->sql)) return $this->sql;
if (!preg_match('/^[\w_-]+$/',$this->name) ) {
* @name (string) name of the field to return. Default: null
* @return (array) return null where name does not exsts
*/
- function fields($name=null) {
+ public function fields($name=null) {
if ($this->fields === null) {
$this->fields = array();
return $this->fields;
}
- function url_keys($values=null,$params=array(),$sep='&') {
+ public function url_keys($values=null,$params=array(),$sep='&') {
if ($values === null) $values = $this->p();
$url = is_array($params) ? $params : array($params);
return $url ? '?table='.$this->p('table').$sep.join($sep,$url) : '';
}
- function fields_keys(&$others=array()) {
+ public function fields_keys(&$others=array()) {
$fields_keys = array();
}
- function html_edit($values = null) {
+ public function html_edit($values = null) {
if ($values === null) $values = $this->p();
if (!is_array($values)) $values = array($values);
}
- function url_list($k='',$v='') {
+ public function url_list($k='',$v='') {
$params = array();
$fields = ($this->p('action') == 'delete') ? array() : $this->fields();
}
- function url_sort($name) {
+ public function url_sort($name) {
# See: http://dev.w3.org/html5/html-author/charref
$html = '';
}
- function nav($count,$tot,$limit) {
+ public function nav($count,$tot,$limit) {
$html = '';
}
- function where($fields,$hvalues,$need_all_values=false) {
+ public function where($fields,$hvalues,$need_all_values=false) {
// Construct where
$where = array();
}
- function where_criterias($values,$logic='') {
+ public function where_criterias($values,$logic='') {
$having = $where = array();
if (empty($logic)) $logic = 'AND';
}
- function add_extras($extras) {
+ public function add_extras($extras) {
$this->fields();
foreach ($extras as $k => $v) {
}
- function select_extras() {
+ public function select_extras() {
if (!$this->extras) return '';
/******************************************************************
Html Output
******************************************************************/
- function rows($opt=array()) {
+ public function rows($opt=array()) {
//
// Select
: false
;
+ # Use the module out when format unknow
+ $out_conf = null;
+ if (!preg_match('/^text|csv|yaml|json|table|div$/',$format)) {
+ if (!($out_conf = out::$types[$format])) $this->bye("Unknow format `$format`");
+ }
+
$count = 0;
while ($row = $st->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT)) {
if ($count === 0) {
- if ($opt['is_html']) echo $this->html_nav_top();
- echo $this->{"rows_begin_$format"}($this->fields());
+ if ($out_conf) {
+ echo out::begin($out_conf,$row);
+ } else {
+ if ($opt['is_html']) echo $this->html_nav_top();
+ echo $this->{"rows_begin_$format"}($this->fields());
+ }
}
$count++;
$count_fields++;
}
- echo $this->{"rows_rec_$format"}($row);
+ if ($out_conf) {
+ echo out::row($out_conf,$row);
+ } else {
+ echo $this->{"rows_rec_$format"}($row);
+ }
}
}
- if (!$query) {
- $err = $this->db->conn->errorInfo();
- $err[] = $sql;
- err(join(' | ',$err));
- return $err[0];
- }
+ if (!$query) $this->err_sql($sql);
$tot = $query->fetch(PDO::FETCH_COLUMN);
$opt['tot'] = $tot;
if ($count === 0 and $this->p('header') === 'force') {
echo $this->{"rows_begin_$format"}($this->fields());
}
+
if ($count) {
- echo $this->{"rows_end_$format"}();
+ if ($out_conf) {
+ out::end($out_conf);
+ } else {
+ echo $this->{"rows_end_$format"}();
- if ($opt['is_html']) {
+ if ($opt['is_html']) {
- echo '<div class="nav bottom">'
- .$this->nav($opt['count'],$opt['tot'],$opt['limit'])
- .'</div>'.TABLE_EOL
- ;
- }
+ echo '<div class="nav bottom">'
+ .$this->nav($opt['count'],$opt['tot'],$opt['limit'])
+ .'</div>'.TABLE_EOL
+ ;
+ }
+ }
} # < count
$st->closeCursor();
/*-----------------------------------------------------------------
Text
-----------------------------------------------------------------*/
- function rows_begin_text() {
+ public function rows_begin_text() {
$this->_row_text = null;
return '';
}
- function rows_rec_text(&$row) {
+ public function rows_rec_text(&$row) {
$text = '';
if ($this->_row_text === null) {
$this->_row_text = true;
return $text.$row."\n";
}
- function rows_end_text() {
+ public function rows_end_text() {
unset($this->_row_text);
return '';
}
/*-----------------------------------------------------------------
Json
-----------------------------------------------------------------*/
- function rows_begin_json() {
+ public function rows_begin_json() {
$this->_row_json = null;
return '['.TABLE_EOL;
return ''
.'['.TABLE_EOL;
}
- function rows_rec_json(&$row) {
+ public function rows_rec_json(&$row) {
if ($this->_row_json === null) {
$json = '';
$this->_row_json = true;
return $json . json_encode($row);
}
- function rows_end_json() {
+ public function rows_end_json() {
unset($this->_row_json);
return TABLE_EOL.']'.TABLE_EOL;
}
/*-----------------------------------------------------------------
Yaml
-----------------------------------------------------------------*/
- function rows_begin_yaml() { return "---\n"; }
- function rows_rec_yaml(&$row) { return $this->yaml_encode($row); }
- function rows_end_yaml() { return ''; }
+ public function rows_begin_yaml() { return "---\n"; }
+ public function rows_rec_yaml(&$row) { return $this->yaml_encode($row); }
+ public function rows_end_yaml() { return ''; }
/*-----------------------------------------------------------------
Xml
-----------------------------------------------------------------*/
- function rows_begin_xml() {
+ public function rows_begin_xml() {
return ''
.'<?xml version="1.0" encoding="utf-8"?>'.TABLE_EOL #<?
#.'<database name="'.$this->db->name.'" table="'.$this->name.'" type="'.$this->db->type.'">'.TABLE_EOL
;
}
- function rows_rec_xml(&$row) {
+ public function rows_rec_xml(&$row) {
$xml = '';
$xml .= TABLE_INDENT."<row>".TABLE_EOL;
foreach ($row as $k=>$v) {
return $xml;
}
- function rows_end_xml() {
+ public function rows_end_xml() {
return '</rows>'.TABLE_EOL;
}
/*-----------------------------------------------------------------
Csv
-----------------------------------------------------------------*/
- function rows_begin_csv($fields) {
+ public function rows_begin_csv($fields) {
if ($this->p('header')==="0") return '';
return join(TABLE_CSV_SEP,array_keys($fields))."\n";
}
- function rows_rec_csv(&$row) {
+ public function rows_rec_csv(&$row) {
return join(TABLE_CSV_SEP,array_values($row))."\n";
}
- function rows_end_csv() {
+ public function rows_end_csv() {
return '';
}
/*-----------------------------------------------------------------
Html Table
-----------------------------------------------------------------*/
- function rows_begin_table($fields) {
+ public function rows_begin_table($fields) {
$html = '';
return $html;
}
- function rows_rec_table(&$row) {
+ public function rows_rec_table(&$row) {
$html = '<tr class="row">'.TABLE_EOL;
return $html;
}
- function rows_end_table($opt=array()) {
+ public function rows_end_table($opt=array()) {
$html = '';
$html .= '</tbody>'.TABLE_EOL;
$html .= '</table>'.TABLE_EOL;
/*-----------------------------------------------------------------
Html Div
-----------------------------------------------------------------*/
- function rows_begin_div() {
+ public function rows_begin_div() {
return '<div class="rows '.$this->name.'">'.TABLE_EOL;
}
- function rows_rec_div(&$row) {
+ public function rows_rec_div(&$row) {
$html = '';
return $html;
}
- function rows_end_div($opt=array()) {
+ public function rows_end_div($opt=array()) {
}
- function sql_name($value=null) {
+ public function sql_name($value=null) {
return $this->db->sql_name($value === null ? $this->name : $value);
}
- function insert($hvalues) {
+ public function insert($hvalues) {
$fields = $values = array();
#var_dump($hvalues);
foreach ($this->fields() as $name => $field) {
return $this->db->exec($sql);
}
- function update($hvalues) {
+ public function update($hvalues) {
$keys = array();
$keys_values = array();
}
- function delete($hvalues,&$e=null) {
+ public function delete($hvalues,&$e=null) {
$keys = $this->fields_keys();
// If no primary keys, we use all field
if (empty($keys)) $keys = $this->fields();
return $this->db->exec($sql);
}
- function out($v,$head=false) { return $this->db->out($v,$head); }
- function out2($v,$head=array()) { return $this->db->out2($v,$head); }
+ public function out($v,$head=false) { return $this->db->out($v,$head); }
+ public function out2($v,$head=array()) { return $this->db->out2($v,$head); }
- function action() {
+ public function action() {
$action = $this->p('action');
if ($action == 'table.fields' or $action == 'fields') {
return false;
}
- function html_nav_top() {
+ public function html_nav_top() {
if ($this->p('header')==="0") return '';
$html = '';
}
- function count() {
+ public function count() {
if (isset($this->count)) return $this->count;
$sql_count = $this->name;
return $this->count;
}
+ public static function err_sql($sql) {
+ $err = $this->db->conn->errorInfo();
+ $err[] = $sql;
+ self::bye(join(' | ',$err));
+ }
+
}
?>
'function' => 'out_csv',
),
'labels' => array(
- 'enclose' => array("<center>".OUT_EOL,"</center>"),
+ 'enclose' => array("<center>".OUT_EOL,"</center>".PHP_EOL),
'tag_enclose' => 'labels',
'tag' => 'label',
'function' => 'out_tag',
'head' => 'out_tag_head',
),
'table' => array(
- 'enclose' => array("<table>".OUT_EOL,"</table>"),
+ 'enclose' => array("<table>".OUT_EOL,"</table>".PHP_EOL),
'tag_enclose' => 'tr',
'tag_head' => 'th',
'tag' => 'td',
'head' => 'out_tag_head',
),
'xml' => array(
- 'enclose' => array('<?xml version="1.0" encoding="utf-8"?>'.OUT_EOL."<rows>".OUT_EOL,"</rows>"),
+ 'enclose' => array('<?xml version="1.0" encoding="utf-8"?>'.OUT_EOL."<rows>".OUT_EOL,"</rows>".PHP_EOL),
'function' => 'out_xml',
),
'yaml' => array(
echo $conf['function']($row,$conf);
}
+ public static function end($conf) {
+ if (!empty($conf['enclose'])) echo $conf['enclose'][1];
+ }
+
+ public static function begin($conf,&$head) {
+ if (!isset($conf['head'])) return;
+
+ if (!empty($conf['enclose'])) echo $conf['enclose'][0];
+ if (empty($head) and self::is_hash($data[0])) {
+ $head = array_keys($data[0]);
+ }
+ echo $conf['head']($head,$conf) . empty($conf['eol']) ? '' : $conf['eol'];
+
+ }
+
public static function rows($type,&$data,$head=array()) {
if (!isset(self::$types[$type])) self::bye("Unknow type: `$type`");
$conf = self::$types[$type];
echo $conf['enclose'][0];
# Function head
- if ($head !== false) {
-
- if (isset($conf['head'])) {
-
- $send = $head;
- if (empty($head) and self::is_hash($data[0])) {
- $head = array_keys($data[0]);
- }
- echo $conf['head']($head,$conf) . $conf['eol'];
-
- }
-
- }
+ if ($head !== false) self::begin($conf,$head);
foreach ($data as $row) {
$count++;
function out_csv_head(&$row,$o) {
if (!is_array($row)) echo 0;
- if (out::is_hash($row)) echo out_csv(array_keys($row),$o);
- echo out_csv($row,$o);
+ if (out::is_hash($row)) {
+ $ar = array_keys($row);
+ echo out_csv($ar,$o);
+ } else {
+ echo out_csv($row,$o);
+ }
}
#function out_tag_head(&$row,$o) { return ''; }