public $name;
public $type = 'text';
public $default = null;
- private $autoincrement = false;
public $key = false;
public $table = null;
+ public $extra = null;
+
+ private $autoincrement = false;
function __construct($attr=array()) {
+ if (is_scalar($attr)) $attr = array('name'=>$attr);
foreach ($attr as $k => $v) { $this->$k = $v; }
}
function html_edit($value) {
+ $value = htmlspecialchars($value);
return '<label for="'.$this->name.'">'.htmlspecialchars(prettyText($this->name)).'</label>'
- .'<input name="'.$this->name.'" id="'.$this->name.'" value="'.htmlspecialchars($value).'" />'
+ .($this->extra
+ ? "<span class=\"field.extra\">$value</span>"
+ : '<input name="'.$this->name.'" id="'.$this->name.'" value="'.$value.'" type="text"/>'
+ )
.PHP_EOL;
}
}
function size() {
- return preg_match('/\((\d+)\)/',$this->type,$m) ? $m[1] : null;
+ if (! preg_match('/\((\d+)(?:,(\d+)\))?/',$this->type,$m) ) return null;
+ array_shift($m);
+ return count($m) == 1 ? $m[0] : $m;
+// NB 02.08.15 return preg_match('/\((\d+)\)/',$this->type,$m) ? $m[1] : null;
}
function where($values) {
or $v=='~'
) return null;
-// NB 03.07.15 $number = preg_match('/int|float|number|currency/',$this->type) ? 1 : 0;
$number = $this->numeric();
// Equal / Not Equal
// NB 27.07.15 return $sth->bindParam($name, $value, $this->pdo_type($value));
}
+ function out($value,$html_escape=false) {
+
+ static $sqlite = null; if ($sqlite === null) $sqlite = $this->db()->type == 'sqlite';
+
+ # Anoying !
+ if ($sqlite and preg_match('/^float\((?:\d+,)?(\d+)\)/',$this->type,$m)) {
+ $value = sprintf('%.'.$m[1].'f',$value);
+ }
+
+ if ($html_escape and empty($this->extra)) $row[$f] = htmlspecialchars($row[$f]);
+ return $value;
+ }
+
}
?>
$this->name = $name;
- if (@$opt['extras']) $this->add_extras($opt['extras']);
+ if (isset($opt['extras'])) $this->add_extras($opt['extras']);
return $this->fields();
}
foreach ($rows as $row) {
$field = array(
- 'name' => null,
+ 'name' => $row['name'],
'extra' => null,
- 'type' => null,
+ 'type' => strtolower($row['type']),
'default' => null,
'key' => null,
'autoincrement' => null,
);
- $field['name'] = $row['name'];
- $field['type'] = strtolower($row['type']);
-
if (isset($row['notnull'])) {
$field['null'] = $row['notnull'] == '0' ? 1 : 0;
$criteria = array();
foreach ( array_keys($this->fields()) as $k ) {
- $v = @$_REQUEST[$k];
+ $v = empty($_REQUEST[$k]) ? '' : $_REQUEST[$k];
$criteria[] = ''
. '<label>'.prettyText($k).':</label>'
foreach ($extras as $k => $v) {
- $this->fields[$k] = array(
+ $v = new Field( ( is_array($v) ? $v : array() ) + array(
+ 'name' => $k,
'type' => 'text',
- 'null' => 0,
'extra' => $v,
- );
+ ));
+ $this->fields[$k] = $v;
$this->extras[$k] = $v;
}
if (!$this->extras) return '';
- $select = array();
-
- foreach ($this->extras as $k => $v) {
-
- $select[] = "$v AS ".($this->db->type == 'pgsql'
- ? '"'.str_replace('"','\"',$k).'"'
- : $this->db->quote($k)
- );
- /*
- $select[] = "$v AS ".$this->db->quote($k);
- */
-
- /*
- $k = $this->db->quote($k);
- if ($this->db->type == 'pgsqpl') $k = str_replace(
- $select[] = "$v AS $k"
- */
-
+ $select = array(); foreach ($this->extras as $k => $v) {
+ $select[] = $v->extra." AS ".$v->quote($k,true);
}
return ','.join(',',$select);
foreach ($this->fields() as $f => $field) {
+ #echo '<pre>'; var_dump($field); echo '</pre>';
+ $row[$f] = $field->out($row[$f]);
+// NB 02.08.15 if ($escape and empty($this->extra)) $row[$f] = htmlspecialchars($row[$f]);
+
+ /*
if (!$field->extra) {
if ($escape) $row[$f] = htmlspecialchars($row[$f]);
}
if ($this->db->type == 'sqlite' and preg_match('/^float\((?:\d+,)?(\d+)\)/',$field->type,$m)) {
$row[$f] = sprintf('%.'.$m[1].'f',$row[$f]);
}
-
- if ($count_fields === 0) {
- #$row[$f] = '<a href="'.$count_fields.'">' . $row[$f] . '</a>';
- }
+ */
$count_fields++;
}
- if ($opt['is_html']) {
- array_unshift($row,'<a class="edit button" href="'.$this->url_keys($row,'action=edit').'">'.DB_HTML_EDIT.'</a>');
- array_push($row,'<a class="delete button" href="'.$this->url_keys($row,'action=delete').'">'.DB_HTML_DELETE.'</a>');
- }
-
echo $this->{"rows_rec_$format"}($row);
}
function rows_begin_table() {
$html = '';
- $html .= '<table class="rows '.$this->name.'">'.PHP_EOL;
+ $html .= '<table class="rows db '.$this->name.'">'.PHP_EOL;
$colspan = 0;
if (DB_HTML_EDIT) $colspan++;
. '</tr>'#.'</thead>'.PHP_EOL
;
- $html .= ''#'<tbody>'.PHP_EOL
- .'<tr class="'.$this->name.' row">'
- ;
+ $html .= '<tr class="head">';
if (DB_HTML_EDIT) $html .= '<th class="'.DB_HTML_EDIT.'"></th>';
}
function rows_rec_table($row) {
- $html = '<tr>';
+
+ array_unshift($row,'<a class="edit button" href="'.$this->url_keys($row,'action=edit').'">'.DB_HTML_EDIT.'</a>');
+ array_push($row,'<a class="delete button" href="'.$this->url_keys($row,'action=delete').'">'.DB_HTML_DELETE.'</a>');
+
+ $html = '<tr class="row">';
foreach ($row as $k => $v) {
$html .= '<td class="'.$k.'">'.$v.'</td>';
Html Div
-----------------------------------------------------------------*/
function rows_begin_div() {
- return '<div class="rows '.$this->name.'">'.DB_HTML_NAV_TOP.PHP_EOL;
+ return '<div class="rows db '.$this->name.'">'.DB_HTML_NAV_TOP.PHP_EOL;
}
function rows_rec_div($row) {
$html = '';
- $html .= '<ul>';
+
+ $html .= '<ul class="row">';
+
+ $html .= '<li class="buttons">';
+ $html .= '<a class="edit button" href="'.$this->url_keys($row,'action=edit').'">'.DB_HTML_EDIT.'</a>';
+ $html .= '<a class="delete button" href="'.$this->url_keys($row,'action=delete').'">'.DB_HTML_DELETE.'</a>';
+ $html .= '</li>';
foreach ($row as $k => $v) {
$html .= '<li>'
- .( $k == '0' ? '' : '<span class="k">'.$k.'</span>: ')
- .'<span class="v">'.$v.'</span>'
+ .( $k == '0' ? '' : '<label>'.prettyText($k).'</label>')
+ .'<span>'.$v.'</span>'
.'</li>';
}
$html .= '</ul>'.PHP_EOL;
+
+// NB 03.08.15 $html .= '<div class="buttons">';
+// NB 03.08.15 $html .= '<a class="edit button" href="'.$this->url_keys($row,'action=edit').'">'.DB_HTML_EDIT.'</a>';
+// NB 03.08.15 $html .= '<a class="delete button" href="'.$this->url_keys($row,'action=delete').'">'.DB_HTML_DELETE.'</a>';
+// NB 03.08.15 $html .= '</div>';
+
+
return $html;
}
- function rows_end_div() {
- return "</div>".PHP_EOL;
+ function rows_end_div($opt=array()) {
+ return '<div class="nav bottom">'.$this->nav($opt['count'],$opt['tot'],$opt['limit']).'</div>'
+ . "</div>".PHP_EOL
+ ;
}
function sql_name() {