from any database
*****************************************************************************/
+require_once(dirname(__FILE__).'/functions.php');
+if (!defined('DEBUG')) define('DEBUG',0);
+
if (!function_exists('err')) {
function err($err) {
die( is_scalar($err) ? $err : print_r($err) );
if (@$opt['extras']) $this->add_extras($opt['extras']);
- return array();
+ return $this->fields();
}
function fields() {
}
- function edit($ids) {
- if (!is_array($ids)) $ids = array($ids);
+ function edit($values = null) {
+ if ($values === null) $values = $_REQUEST;
+ if (!is_array($values)) $values = array($values);
- return $this->fields_keys();
$sql = "SELECT *" . $this->select_extras();
- $sql .= " FROM $this->name".$this->where_criterias();
+ $sql .= " FROM $this->name".$this->where_criterias($values);
+ $sql .= " LIMIT 1";
$this->sql = $sql;
+
+ $this->debug($sql,1);
+ $st = $this->db->conn->prepare($sql);
+ $st->execute();
+
+ $count = 0;
+ if ($row = $st->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT)) {
+ $count ++;
+
+ foreach ($this->fields() as $name => $attr) {
+
+ if (array_key_exists($name,$row)) {
+ $value = $row[$name];
+ } else {
+ $value = $attr['default'];
+ }
+
+ echo '<label for="'.$name.'">'.htmlspecialchars($name).'</label>'
+ .'<input name="'.$name.'" id="'.$name.'" value="'.htmlspecialchars($value).'" />'
+ .PHP_EOL;
+ }
+ }
+
+ $this->row = $row;
+ return $this;
}
function debug($msg,$level=0) {
echo '</form>'.PHP_EOL;
}
- function where_criterias() {
+ function where_criterias($values,$logic='AND') {
$having = $where = array();
foreach ($this->fields() as $k => $spec) {
// No empty values
- $v = @$_REQUEST[$k];
+ $v = @$values[$k];
if (strcmp($v,'')==0 or $v=='!' or $v=='~') continue;
// Equal / Not Equal
// Text
} elseif (preg_match('/text|char|blob/',$spec['type'])
- or !preg_match('/^[\d+\.]$/',$v) # text char in search
+ or !preg_match('/^\d+(\.\d*)?$/',$v) # text criteria
) {
if (strtolower($v)=='null') $v = '';
if ($this->db->type == 'mysql' and $spec['extra']) {
$having[] = "$k$equal$v";
+
} elseif ($this->db->type == 'pgsql' and $spec['extra']) {
$where[] = $this->extras[$k]."$equal$v";
+
} else {
$where[] = "$k$equal$v";
}
}
$sql = '';
- if ($where) $sql .= ' WHERE '.join(' '. @$_REQUEST['op'].' ',$where);
- if ($having) $sql .= ' HAVING '.join(' '. @$_REQUEST['op'].' ',$having);
+ if ($where) $sql .= ' WHERE '.join(' '. $logic.' ',$where);
+ if ($having) $sql .= ' HAVING '.join(' '. $logic.' ',$having);
return $sql;
}
// Select
//
$sql = "SELECT *" . $this->select_extras();
- $sql .= " FROM $this->name".$this->where_criterias();
+ $sql .= " FROM $this->name".$this->where_criterias($_REQUEST,$_REQUEST['op']);
$this->sql = $sql;
#$this->debug($sql);
$this->debug($sql,1);
}
+}
+
+function argv2request() {
+/******************************************************************************
+# NB 14.07.11 - Put argv options into _REQUEST. Return argv without options
+******************************************************************************/
+
+ global $argv;
+
+ $new_argv = array();
+
+ for ($i=1;$i<count($argv);$i++) {
+
+ if (preg_match('/^([\w_\-\.]+)=(.+)\s*$/',$argv[$i],$f)) {
+ $k = $f[1];
+ $v = $f[2];
+
+ // Array
+ if (preg_match('/^(.*)\[\]$/',$k,$p)) {
+ #bye($p);
+ if (($_REQUEST[$p[1]]==NULL) or !in_array($v,$_REQUEST[$p[1]])) $_REQUEST[$p[1]][] = $v;
+
+ } else {
+ $_REQUEST[$k] = $v;
+
+ }
+
+ } else {
+
+ if (preg_match('/^-(h|-?help)$/',$argv[$i])) $_REQUEST['help'] = 1;
+ else $new_argv[] = $argv[$i];
+
+ }
+
+ }
+
+ return $new_argv;
+
}
?>