egroupware/resources/inc/class.so_resources.inc.php

151 lines
4.9 KiB
PHP
Raw Normal View History

2005-02-03 17:42:20 +01:00
<?php
/**************************************************************************\
* eGroupWare - resources - Resource Management System *
* http://www.egroupware.org *
* Written by Cornelius Weiss [nelius@gmx.net] *
2005-02-17 18:29:07 +01:00
* and Lukas Weiss [ichlukas@gmx.de] *
2005-02-03 17:42:20 +01:00
* ----------------------------------------------- *
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU General Public License as published by the *
* Free Software Foundation; either version 2 of the License, or (at your *
* option) any later version. *
\**************************************************************************/
2005-02-17 12:23:00 +01:00
/* $Id: */
2005-02-03 17:42:20 +01:00
class so_resources
{
function so_resources()
{
$this->db = $GLOBALS['phpgw']->db;
$this->rs_table = 'egw_resources';
}
/*!
@function search
@abstract searches db for rows matching searchcriteria and categories
2005-02-17 10:53:46 +01:00
@autor Cornelius Wei<EFBFBD> <egw@von-und-zu-weiss.de>
2005-02-03 17:42:20 +01:00
@discussion '*' is replaced with sql-wildcard '%'
@param array $criteria array of key => value for search. (or'ed together)
@param array $cats array of cat_id => cat_name to be searched
@param &array $data reference of data array with cols to return in first row ( key => '')
@param string $order_by fieldnames + {ASC|DESC} separated by colons ','
@param int $offset row to start from, default 0
@param int $num_rows number of rows to return (optional), default -1 = all, 0 will use $GLOBALS['phpgw_info']['user']['preferences']['common']['maxmatchs']
@return int number of matching rows
*/
function search($criteria,$cats,&$data,$order_by='',$offset=false,$num_rows=-1)
{
$select = implode(',',array_keys($data[0]));
foreach($criteria as $col => $value)
{
$where .= ($where ? " OR " : " ( " ). $col . ((strstr($value,'*') || strstr($value,'*')) ?
" LIKE '" . strtr(str_replace('_','\\_',addslashes($value)),'*?','%_') ."'":
"='" .$value."'");
}
$where .= " ) ";
foreach ((array)$cats as $cat_id => $cat_name)
{
$wherecats .= ($wherecats ? " OR " : " AND ( " ) .'cat_id' . "='".$cat_id."'";
}
$wherecats .= $wherecats ? " ) " : "";
2005-02-03 17:42:20 +01:00
$this->db->query( 'SELECT '.$select." FROM ".$this->rs_table." WHERE ".$where.$wherecats.
($order_by != '' ? " ORDER BY $order_by" : ''),__LINE__,__FILE__);
$nr = $this->db->nf();
if($offset > 0)
{
$this->db->seek($offset-1);
}
if($num_rows==0)
{
$num_rows = $GLOBALS['phpgw_info']['user']['preferences']['common']['maxmatchs'];
}
for ($n = 1; $this->db->next_record() && $n!=$num_rows+1; ++$n)
{
$data[$n] = $this->db->row();
}
return $nr;
}
/*!
@function get_value
@abstract gets the value of $key from resource of $id
2005-02-17 10:53:46 +01:00
@autor Cornelius Wei<EFBFBD> <egw@von-und-zu-weiss.de>
2005-02-03 17:42:20 +01:00
@param string $key key of value to get
@param int $id resource id
@return mixed value of key and resource, false if key or id not found.
*/
function get_value($key,$id)
{
if($this->db->query( "SELECT ". $key . " FROM ".$this->rs_table." WHERE id = ".$id,__LINE__,__FILE__))
{
$this->db->next_record();
(array)$value = $this->db->row();
return $value[$key];
}
return false;
}
2005-02-17 16:27:43 +01:00
function delete($id)
{
$this->db->delete($this->rs_table,$id,__LINE__,__FILE__);
return true;
}
2005-02-03 17:42:20 +01:00
/*!
@function read
@abstract reads a resource exept binary datas
2005-02-17 10:53:46 +01:00
@autor Cornelius Wei<EFBFBD> <egw@von-und-zu-weiss.de>
2005-02-03 17:42:20 +01:00
@param int $id resource id
@return array with key => value or false if not found
*/
function read($id)
{
$tabledef = $this->db->metadata($table=$this->rs_table,$full=false);
foreach($tabledef as $n => $fielddef)
{
if(!$fielddef['binary'])
{
$readfields[$n] = $fielddef['name'];
}
}
if($this->db->query( "SELECT ". implode(',',$readfields) . " FROM ".$this->rs_table." WHERE id = ".$id,__LINE__,__FILE__))
{
$this->db->next_record();
return $this->db->row();
}
return false;
}
2005-02-12 16:49:38 +01:00
/*!
@function save
@abstract saves a resource including binary datas
2005-02-17 10:53:46 +01:00
@autor Cornelius Wei<EFBFBD> <egw@von-und-zu-weiss.de>
2005-02-12 16:49:38 +01:00
@param array $resource key => value
2005-02-15 17:26:21 +01:00
@return mixed id of resource if all right, false if fale
2005-02-12 16:49:38 +01:00
*/
function save($resource)
{
$where = array('id' => $resource['id']);
2005-02-14 15:55:45 +01:00
2005-02-14 13:03:06 +01:00
$tabledef = $this->db->metadata($table=$this->rs_table,$full=false);
foreach($tabledef as $n => $fielddef)
{
2005-02-14 15:55:45 +01:00
if(isset($resource[$fielddef['name']]))
{
$data[$fielddef['name']] = $resource[$fielddef['name']];
}
elseif($resource['id'] > 0) //we need to reload old data! bug in db::update?
{
$data[$fielddef['name']] = $this->get_value($fielddef['name'],$resource['id']);
}
2005-02-14 13:03:06 +01:00
}
2005-02-15 17:26:21 +01:00
return $this->db->insert($this->rs_table,$data,$where,__LINE__,__FILE__) ? $this->db->get_last_insert_id($this->rs_table, 'id') : false;
2005-02-12 16:49:38 +01:00
}
2005-02-03 17:42:20 +01:00
}