[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Phpgroupware-cvs] old/brewer/inc/class.std.inc.php, 1.1
From: |
nomail |
Subject: |
[Phpgroupware-cvs] old/brewer/inc/class.std.inc.php, 1.1 |
Date: |
Thu, 30 Dec 2004 08:50:59 +0100 |
Update of /old/brewer/inc
Added Files:
Branch:
class.std.inc.php
date: 2004/12/30 07:50:59; author: skwashd; state: Exp;
Log Message:
keep a historic record of the app
=====================================================================
<?php
/**************************************************************************\
* phpGroupWare - Standard DB Query Class *
* This file written by Miles Lott <address@hidden> *
* Copyright (C) 2001 Miles Lott *
* -------------------------------------------------------------------------*
* This library is part of the phpGroupWare API *
* http://www.phpgroupware.org/api *
* ------------------------------------------------------------------------ *
* This library is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, *
* or any later version. *
* This library is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* See the GNU Lesser General Public License for more details. *
* You should have received a copy of the GNU Lesser General Public License *
* along with this library; if not, write to the Free Software Foundation, *
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
\**************************************************************************/
/* $Id: class.std.inc.php,v 1.1 2004/12/30 07:50:59 skwashd Exp $ */
/*
This is a simplified query class for simple table lists and
for fetching single rows of data. Typical use;
$tbldef = array(
'phpgw_some_table', array(
'id' => 0,
'name' => '',
'some' => '',
'other' => '',
'info' => ''
)
);
$obj = CreateObject('appname.std',$tbldef);
$obj->debug = True;
$obj->qfields = 'id,name,other';
$obj->qstr = 'php';
$local = $obj->get_list();
...
var_dump($obj->data);
var_dump($local);
...
single row or list data is returned, but is also set
within the
var, $data.
By default, array data is serialized within set(). Also,
checks are done in get() and get_list() to automatically
unserialize data. This helps to make this not very efficient,
but the goal here is simplicity for list data, etc. The
(de)serialization
is switchable.
*/
class std
{
/* DB Object */
var $db = '';
/* Table name */
var $table = '';
/* get or set the current working row id */
var $id = 0;
/* total records as set by get_list() */
var $total = 0;
/* global debug flag */
var $debug = False;
/* Fields for the table */
var $fields = array();
/* Your working data - set to be a copy of $fields at class
instantiation,
but is then overwritten by the result rowset. */
var $data = array();
/* Comma-delimited string of fields to query (optional,
overrides
the complete field listing) */
var $qfields = '';
/* string for search, (A query may also be passed to get_list(),
in which case this is ignored.) */
var $qstr = '';
function std($tbldef='')
{
/* check for tablename */
if (is_array($tbldef))
{
/* Set the table name */
$this->table = $tbldef[0];
if(is_array($tbldef[1]))
{
/* Set the field list */
$this->_debug('<br>new class, setting
$fields to:' . $tbldef[1]);
$this->fields = $tbldef[1];
/* Make a copy */
$this->data = $this->fields;
}
}
}
function get($id=0,$unserial=True)
{
$fields = $this->_get_fields();
$fieldstr = implode(',',$fields);
if(!$id)
{
$id = $this->id;
}
$sql = 'SELECT ' . $fieldstr . ' FROM ' . $this->table
. ' WHERE id=' . $id;
$this->_debug('<br>get(): ' . $sql);
$GLOBALS['phpgw']->db->query($sql,__LINE__,__FILE__);
$GLOBALS['phpgw']->db->next_record();
@reset($fields);
while(list($a,$b) = each($fields))
{
$c = unserialize($GLOBALS['phpgw']->db->f($b));
$d = gettype($c);
if(($d == 'array' || $d == 'object') &&
$unserial)
{
$e = $c;
}
else
{
$e = $GLOBALS['phpgw']->db->f($b);
}
$this->data[$GLOBALS['phpgw']->db->f('id')][$b]
= $e;
}
return $this->data;
}
function set($id=0,$serial=True)
{
$fields = $this->_get_fields();
$fieldstr = implode(',',$fields);
if($id)
{
$sql = 'UPDATE ' . $this->table . ' SET ';
while(list($a,$b) = each($this->data))
{
if($a != 'id' && !is_int($a))
{
if(is_array($b) && $serial)
{
$b = serialize($c);
}
$sql .= $a . "='" . $b . "',";
}
}
$sql = substr($sql,0,-1) . ' WHERE id=' . $id;
}
else
{
$values = "'" . implode("','",$this->data) .
"'";
$sql = 'INSERT INTO ' . $this->table . '(' .
$fieldstr . ') VALUES (' . $values . ')';
}
$ret =
$GLOBALS['phpgw']->db->query($sql,__LINE__,__FILE__);
return $ret;
}
function
get_list($start=0,$query='',$sort='ASC',$order='',$limit=True,$unserial=True)
{
$fields = $this->_get_fields();
$fieldstr = implode(',',$fields);
if($query || $this->qstr)
{
if($query)
{
$querystr = $this->_makequery($query);
}
elseif($this->qstr)
{
$querystr =
$this->_makequery($this->qstr);
}
}
$sql = 'SELECT ' . $fieldstr . ' FROM ' . $this->table
. $querystr;
if($sort && $order)
{
$sort = ' ORDER BY ' . $order . ' ' . $sort . '
';
}
else
{
$sort = '';
}
if($limit)
{
$qfunc =
"\$GLOBALS['phpgw']->db->limit_query(\"" . $sql . $sort . '",' . $start .
',__LINE__,__FILE__);';
}
else
{
$qfunc = "\$GLOBALS['phpgw']->db->query(\"" .
$sql . $sort . '",__LINE__,__FILE__);';
}
$GLOBALS['phpgw']->db->query($sql,__LINE__,__FILE__);
$this->total = $GLOBALS['phpgw']->db->num_rows();
eval($qfunc);
$this->_debug('<br>get_list(): ' . $sql);
if($GLOBALS['phpgw']->db->num_rows())
{
$this->data = array();
}
while ($GLOBALS['phpgw']->db->next_record())
{
@reset($fields);
while(list($a,$b) = @each($fields))
{
$c =
unserialize($GLOBALS['phpgw']->db->f($b));
$d = gettype($c);
if(($d == 'array' || $d == 'object') &&
$unserial)
{
$e = $c;
}
else
{
$e =
$GLOBALS['phpgw']->db->f($b);
}
$this->_debug('<br>get_list()$this->data[' . $GLOBALS['phpgw']->db->f('id') .
'][' . $b . '] is : ',$e);
$this->data[$GLOBALS['phpgw']->db->f('id')][$b] = $e;
}
}
return $this->data;
}
/* Return a selectbox */
function formatted_list($id=0,$name='',$java=True)
{
if(!$name)
{
return False;
}
$list = $this->get_list(0,'','ASC','',False);
if($java)
{
$jselect = ' onChange="this.form.submit();"';
}
$select = "\n" .'<select name="newval[' . $name . ']"'
. $jselect . ">\n";
$select .= '<option value="0">' . lang('Please Select')
. '</option>'."\n";
while(list($key,$val) = each($list))
{
$select .= '<option value="' . $val['id'] . '"';
if($val['id'] == $id)
{
$select .= ' selected';
}
$select .= '>' . $val['name'] .
'</option>'."\n";
}
$select .= '</select>'."\n";
$select .= '<noscript><input type="submit"
name="style_select" value="{lang_select}"></noscript>' . "\n";
return $select;
}
/*
Functions for internal use only below...
*/
function _get_fields()
{
if($this->qfields)
{
$tmp = explode(',',$this->qfields);
@reset($tmp);
while(list($key,$val) = @each($tmp))
{
$fields[] = $val;
}
}
else
{
@reset($this->fields);
while(list($key,$val) = each($this->fields))
{
$fields[] = $key;
}
}
return $fields;
}
/* Yes, this sucks, but it at least works... */
function _makequery($query='')
{
if(!$query || $this->qstr)
{
return;
}
elseif($query)
{
$look = $query;
}
else
{
$look = $this->qstr;
}
$s = ' WHERE (';
$fields = $this->_get_fields();
while(list($key,$val) = each($fields))
{
if($val != 'id')
{
$s .= $val . " LIKE '%$look%' OR ";
}
}
$s = substr($s,0,-4);
$s .= ')';
return $s;
}
function _debug($err,$var='')
{
if(!$err)
{
return;
}
if($this->debug)
{
echo $err . ' ';
if($var)
{
var_dump($var);
}
}
}
}
?>
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Phpgroupware-cvs] old/brewer/inc/class.std.inc.php, 1.1,
nomail <=