2009-01-17 04:36:58 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2013-06-12 16:29:04 +02:00
|
|
|
* EGroupware generalized SQL Storage Object with build in custom field support
|
2009-01-17 04:36:58 +01:00
|
|
|
*
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
* @package etemplate
|
|
|
|
* @link http://www.egroupware.org
|
|
|
|
* @author Ralf Becker <RalfBecker@outdoor-training.de>
|
2013-06-12 16:29:04 +02:00
|
|
|
* @copyright 2009-13 by RalfBecker@outdoor-training.de
|
2009-01-17 04:36:58 +01:00
|
|
|
* @version $Id$
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generalized SQL Storage Object with build in custom field support
|
2009-04-01 15:01:16 +02:00
|
|
|
*
|
2009-01-17 04:36:58 +01:00
|
|
|
* This class allows to display, search, order and filter by custom fields simply by replacing so_sql
|
2009-04-01 15:01:16 +02:00
|
|
|
* by it and adding custom field widgets to the eTemplates of an applications.
|
2009-01-17 04:36:58 +01:00
|
|
|
* It's inspired by the code from Klaus Leithoff, which does the same thing limited to addressbook.
|
|
|
|
*
|
|
|
|
* The schema of the custom fields table should be like (the lenght of the cf name is nowhere enfored and
|
|
|
|
* varies throughout eGW from 40-255, the value column from varchar(255) to longtext!):
|
2009-04-01 15:01:16 +02:00
|
|
|
*
|
2009-01-17 04:36:58 +01:00
|
|
|
* 'egw_app_extra' => array(
|
|
|
|
* 'fd' => array(
|
|
|
|
* 'prefix_id' => array('type' => 'int','precision' => '4','nullable' => False),
|
|
|
|
* 'prefix_name' => array('type' => 'string','precision' => '64','nullable' => False),
|
|
|
|
* 'prefix_value' => array('type' => 'text'),
|
|
|
|
* ),
|
|
|
|
* 'pk' => array('prefix_id','prefix_name'),
|
|
|
|
* 'fk' => array(),
|
|
|
|
* 'ix' => array(),
|
|
|
|
* 'uc' => array()
|
|
|
|
* )
|
|
|
|
*
|
|
|
|
* @package etemplate
|
|
|
|
* @subpackage api
|
|
|
|
* @author RalfBecker-AT-outdoor-training.de
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
*/
|
|
|
|
class so_sql_cf extends so_sql
|
|
|
|
{
|
2009-05-18 00:03:31 +02:00
|
|
|
/**
|
|
|
|
* Prefix used by the class
|
|
|
|
*/
|
2009-01-17 04:36:58 +01:00
|
|
|
const CF_PREFIX = '#';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* name of customefields table
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
var $extra_table;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* name of id column, defaults to the regular tables auto id
|
2009-04-01 15:01:16 +02:00
|
|
|
*
|
2009-01-17 04:36:58 +01:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
var $extra_id = '_id';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Name of key (cf name) column or just a postfix added to the table prefix
|
2009-04-01 15:01:16 +02:00
|
|
|
*
|
2009-01-17 04:36:58 +01:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
var $extra_key = '_name';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Name of value column or just a postfix added to the table prefix
|
2009-04-01 15:01:16 +02:00
|
|
|
*
|
2009-01-17 04:36:58 +01:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
var $extra_value = '_value';
|
|
|
|
|
|
|
|
var $extra_join;
|
|
|
|
var $extra_join_order;
|
|
|
|
var $extra_join_filter;
|
2009-04-01 15:01:16 +02:00
|
|
|
|
2009-08-10 11:12:35 +02:00
|
|
|
/**
|
|
|
|
* Does extra table has a unique index (over id and name)
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
var $extra_has_unique_index;
|
|
|
|
|
2009-01-17 04:36:58 +01:00
|
|
|
/**
|
|
|
|
* Custom fields of $app, read by the constructor
|
2009-04-01 15:01:16 +02:00
|
|
|
*
|
2009-01-17 04:36:58 +01:00
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
var $customfields;
|
|
|
|
|
2009-05-18 00:03:31 +02:00
|
|
|
/**
|
|
|
|
* Do we allow AND store multiple values for a cf (1:N) relations
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
var $allow_multiple_values = false;
|
|
|
|
|
2009-01-17 04:36:58 +01:00
|
|
|
/**
|
|
|
|
* constructor of the class
|
2009-04-01 15:01:16 +02:00
|
|
|
*
|
2009-01-17 04:36:58 +01:00
|
|
|
* Please note the different params compared to so_sql!
|
|
|
|
*
|
|
|
|
* @param string $app application name to load table schemas
|
|
|
|
* @param string $table name of the table to use
|
|
|
|
* @param string $extra_table name of the custom field table
|
|
|
|
* @param string $colum_prefix='' column prefix to automatic remove from the column-name, if the column name starts with it
|
|
|
|
* @param string $extra_key='_name' column name for cf name column (will be prefixed with colum prefix, if starting with _)
|
|
|
|
* @param string $extra_value='_value' column name for cf value column (will be prefixed with colum prefix, if starting with _)
|
|
|
|
* @param string $extra_id='_id' column name for cf id column (will be prefixed with colum prefix, if starting with _)
|
|
|
|
* @param egw_db $db=null database object, if not the one in $GLOBALS['egw']->db should be used, eg. for an other database
|
|
|
|
* @param boolean $no_clone=true can we avoid to clone the db-object, default yes (different from so_sql!)
|
|
|
|
* new code using appnames and foreach(select(...,$app) can set it to avoid an extra instance of the db object
|
2009-10-08 18:18:28 +02:00
|
|
|
* @param boolean $allow_multiple_values=false should we allow AND store multiple values (1:N relations)
|
|
|
|
* @param string $timestamp_type=null default null=leave them as is, 'ts'|'integer' use integer unix timestamps, 'object' use egw_time objects
|
2009-01-17 04:36:58 +01:00
|
|
|
*/
|
|
|
|
function __construct($app,$table,$extra_table,$column_prefix='',
|
|
|
|
$extra_key='_name',$extra_value='_value',$extra_id='_id',
|
2009-10-08 18:18:28 +02:00
|
|
|
$db=null,$no_clone=true,$allow_multiple_values=false,$timestamp_type=null)
|
2009-01-17 04:36:58 +01:00
|
|
|
{
|
|
|
|
// calling the so_sql constructor
|
2009-10-08 18:18:28 +02:00
|
|
|
parent::__construct($app,$table,$db,$column_prefix,$no_clone,$timestamp_type);
|
2009-04-01 15:01:16 +02:00
|
|
|
|
2009-05-18 00:03:31 +02:00
|
|
|
$this->allow_multiple_values = $allow_multiple_values;
|
2009-01-17 04:36:58 +01:00
|
|
|
$this->extra_table = $extra_table;
|
|
|
|
if (!$this->extra_id) $this->extra_id = $this->autoinc_id; // default to auto id of regular table
|
2009-04-01 15:01:16 +02:00
|
|
|
|
2009-01-17 04:36:58 +01:00
|
|
|
// if names from columns of extra table are only postfixes (starting with _), prepend column prefix
|
|
|
|
if (!($prefix=$column_prefix))
|
|
|
|
{
|
|
|
|
list($prefix) = explode('_',$this->autoinc_id);
|
|
|
|
}
|
|
|
|
elseif(substr($prefix,-1) == '_')
|
|
|
|
{
|
|
|
|
$prefix = substr($prefix,0,-1); // remove trailing underscore from column prefix parameter
|
|
|
|
}
|
|
|
|
foreach(array('extra_id','extra_key','extra_value') as $col)
|
|
|
|
{
|
|
|
|
$this->$col = $col_name = $$col;
|
|
|
|
if ($col_name[0] == '_') $this->$col = $prefix . $$col;
|
|
|
|
}
|
|
|
|
// some sanity checks, maybe they should be active only for development
|
|
|
|
if (!($extra_defs = $this->db->get_table_definitions($app,$extra_table)))
|
|
|
|
{
|
|
|
|
throw new egw_exception_wrong_parameter("extra table $extra_table is NOT defined!");
|
|
|
|
}
|
|
|
|
foreach(array('extra_id','extra_key','extra_value') as $col)
|
|
|
|
{
|
|
|
|
if (!$this->$col || !isset($extra_defs['fd'][$this->$col]))
|
|
|
|
{
|
|
|
|
throw new egw_exception_wrong_parameter("$col column $extra_table.{$this->$col} is NOT defined!");
|
|
|
|
}
|
|
|
|
}
|
2009-08-10 11:12:35 +02:00
|
|
|
// check if our extra table has a unique index (if not we have to delete the old values, as replacing does not work!)
|
|
|
|
$this->extra_has_unique_index = $extra_defs['pk'] || $extra_defs['uc'];
|
|
|
|
|
2009-01-17 04:36:58 +01:00
|
|
|
// setting up our extra joins, now we know table and column names
|
|
|
|
$this->extra_join = " LEFT JOIN $extra_table ON $table.$this->autoinc_id=$extra_table.$this->extra_id";
|
|
|
|
$this->extra_join_order = " LEFT JOIN $extra_table extra_order ON $table.$this->autoinc_id=extra_order.$this->extra_id";
|
|
|
|
$this->extra_join_filter = " JOIN $extra_table extra_filter ON $table.$this->autoinc_id=extra_filter.$this->extra_id";
|
2009-04-01 15:01:16 +02:00
|
|
|
|
2009-01-17 04:36:58 +01:00
|
|
|
$this->customfields = config::get_customfields($app);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read all customfields of the given id's
|
|
|
|
*
|
|
|
|
* @param int|array $ids one ore more id's
|
|
|
|
* @param array $field_names=null custom fields to read, default all
|
2009-05-18 00:03:31 +02:00
|
|
|
* @return array id => $this->cf_field(name) => value
|
2009-01-17 04:36:58 +01:00
|
|
|
*/
|
|
|
|
function read_customfields($ids,$field_names=null)
|
|
|
|
{
|
|
|
|
if (is_null($field_names)) $field_names = array_keys($this->customfields);
|
|
|
|
|
|
|
|
foreach((array)$ids as $key => $id)
|
|
|
|
{
|
|
|
|
if (!(int)$id && is_array($ids)) unset($ids[$key]);
|
|
|
|
}
|
|
|
|
if (!$ids || !$field_names) return array(); // nothing to do
|
|
|
|
|
2009-05-18 00:03:31 +02:00
|
|
|
$entries = array();
|
2009-01-17 04:36:58 +01:00
|
|
|
foreach($this->db->select($this->extra_table,'*',array(
|
|
|
|
$this->extra_id => $ids,
|
|
|
|
$this->extra_key => $field_names,
|
|
|
|
),__LINE__,__FILE__,false,'',$this->app) as $row)
|
|
|
|
{
|
2009-05-18 00:03:31 +02:00
|
|
|
$entry =& $entries[$row[$this->extra_id]];
|
2011-12-19 13:40:47 +01:00
|
|
|
if (!is_array($entry)) $entry = array();
|
2009-05-18 00:03:31 +02:00
|
|
|
$field = $this->get_cf_field($row[$this->extra_key]);
|
|
|
|
|
|
|
|
if ($this->allow_multiple_values && $this->is_multiple($row[$this->extra_key]))
|
|
|
|
{
|
|
|
|
$entry[$field][] = $row[$this->extra_value];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$entry[$field] = $row[$this->extra_value];
|
|
|
|
}
|
2009-01-17 04:36:58 +01:00
|
|
|
}
|
2009-05-18 00:03:31 +02:00
|
|
|
return $entries;
|
2009-01-17 04:36:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* saves custom field data
|
|
|
|
*
|
|
|
|
* @param array $data data to save (cf's have to be prefixed with self::CF_PREFIX = #)
|
2013-06-12 16:29:04 +02:00
|
|
|
* @param array $extra_cols=array()
|
2009-01-17 04:36:58 +01:00
|
|
|
* @return bool false on success, errornumber on failure
|
|
|
|
*/
|
2013-06-12 16:29:04 +02:00
|
|
|
function save_customfields($data, array $extra_cols=array())
|
2009-01-17 04:36:58 +01:00
|
|
|
{
|
2009-05-18 00:03:31 +02:00
|
|
|
foreach ((array)$this->customfields as $name => $options)
|
2009-01-17 04:36:58 +01:00
|
|
|
{
|
2009-05-18 00:03:31 +02:00
|
|
|
if (!isset($data[$field = $this->get_cf_field($name)])) continue;
|
2009-01-17 04:36:58 +01:00
|
|
|
|
|
|
|
$where = array(
|
2013-06-12 16:29:04 +02:00
|
|
|
$this->extra_id => isset($data[$this->autoinc_id]) ? $data[$this->autoinc_id] : $data[$this->db_key_cols[$this->autoinc_id]],
|
2009-05-18 00:03:31 +02:00
|
|
|
$this->extra_key => $name,
|
2009-01-17 04:36:58 +01:00
|
|
|
);
|
2009-05-18 00:03:31 +02:00
|
|
|
$is_multiple = $this->is_multiple($name);
|
2009-04-01 15:01:16 +02:00
|
|
|
|
2009-08-10 11:12:35 +02:00
|
|
|
// we explicitly need to delete fields, if value is empty or field allows multiple values or we have no unique index
|
|
|
|
if(empty($data[$field]) || $is_multiple || !$this->extra_has_unique_index)
|
2009-01-17 04:36:58 +01:00
|
|
|
{
|
2009-08-10 11:12:35 +02:00
|
|
|
$this->db->delete($this->extra_table,$where,__LINE__,__FILE__,$this->app);
|
|
|
|
if (empty($data[$field])) continue; // nothing else to do for empty values
|
2009-01-17 04:36:58 +01:00
|
|
|
}
|
2013-06-12 16:29:04 +02:00
|
|
|
foreach($is_multiple && !is_array($data[$field]) ? explode(',',$data[$field]) :
|
|
|
|
// regular custom fields (!$is_multiple) eg. addressbook store multiple values comma-separated
|
|
|
|
(array)(!$is_multiple && is_array($data[$field]) ? implode(',', $data[$field]) : $data[$field]) as $value)
|
2009-01-17 04:36:58 +01:00
|
|
|
{
|
2013-06-12 16:29:04 +02:00
|
|
|
if (!$this->db->insert($this->extra_table,array($this->extra_value => $value)+$extra_cols,$where,__LINE__,__FILE__,$this->app))
|
2009-05-18 00:03:31 +02:00
|
|
|
{
|
|
|
|
return $this->db->Errno;
|
|
|
|
}
|
2009-01-17 04:36:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false; // no error
|
|
|
|
}
|
2009-04-01 15:01:16 +02:00
|
|
|
|
2009-01-17 04:36:58 +01:00
|
|
|
/**
|
|
|
|
* merges in new values from the given new data-array
|
|
|
|
*
|
|
|
|
* reimplemented to also merge the customfields
|
|
|
|
*
|
|
|
|
* @param $new array in form col => new_value with values to set
|
|
|
|
*/
|
|
|
|
function data_merge($new)
|
|
|
|
{
|
|
|
|
parent::data_merge($new);
|
|
|
|
|
|
|
|
if ($this->customfields)
|
|
|
|
{
|
|
|
|
foreach($this->customfields as $name => $data)
|
|
|
|
{
|
2009-05-18 00:03:31 +02:00
|
|
|
if (isset($new[$field = $this->get_cf_field($name)]))
|
2009-01-17 04:36:58 +01:00
|
|
|
{
|
2009-05-18 00:03:31 +02:00
|
|
|
$this->data[$field] = $new[$field];
|
2009-01-17 04:36:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* reads row matched by key and puts all cols in the data array
|
2009-04-01 15:01:16 +02:00
|
|
|
*
|
2009-01-17 04:36:58 +01:00
|
|
|
* reimplented to also read the custom fields
|
|
|
|
*
|
|
|
|
* @param array $keys array with keys in form internalName => value, may be a scalar value if only one key
|
|
|
|
* @param string|array $extra_cols string or array of strings to be added to the SELECT, eg. "count(*) as num"
|
|
|
|
* @param string $join sql to do a join, added as is after the table-name, eg. ", table2 WHERE x=y" or
|
|
|
|
* @return array|boolean data if row could be retrived else False
|
|
|
|
*/
|
|
|
|
function read($keys,$extra_cols='',$join='')
|
|
|
|
{
|
|
|
|
if (!parent::read($keys,$extra_cols,$join))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (($id = (int)$this->data[$this->autoinc_id]) && $this->customfields &&
|
|
|
|
($cfs = $this->read_customfields($id)))
|
|
|
|
{
|
|
|
|
$this->data = array_merge($this->data,$cfs[$id]);
|
|
|
|
}
|
|
|
|
return $this->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* saves the content of data to the db
|
|
|
|
*
|
|
|
|
* reimplented to also save the custom fields
|
|
|
|
*
|
|
|
|
* @param array $keys if given $keys are copied to data before saveing => allows a save as
|
|
|
|
* @param string|array $extra_where=null extra where clause, eg. to check an etag, returns true if no affected rows!
|
|
|
|
* @return int|boolean 0 on success, or errno != 0 on error, or true if $extra_where is given and no rows affected
|
|
|
|
*/
|
|
|
|
function save($keys=null,$extra_where=null)
|
|
|
|
{
|
2009-08-10 18:32:45 +02:00
|
|
|
if (is_array($keys) && count($keys) && !isset($keys[0])) // allow to use an etag, eg array('etag=etag+1')
|
|
|
|
{
|
|
|
|
$this->data_merge($keys);
|
|
|
|
$keys = null;
|
|
|
|
}
|
|
|
|
$ret = parent::save($keys,$extra_where);
|
2009-04-01 15:01:16 +02:00
|
|
|
|
2009-01-17 04:36:58 +01:00
|
|
|
if ($ret == 0 && $this->customfields)
|
|
|
|
{
|
|
|
|
$this->save_customfields($this->data);
|
|
|
|
}
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* deletes row representing keys in internal data or the supplied $keys if != null
|
|
|
|
*
|
|
|
|
* reimplented to also delete the custom fields
|
|
|
|
*
|
2009-09-16 12:20:34 +02:00
|
|
|
* @param array|int $keys=null if given array with col => value pairs to characterise the rows to delete, or integer autoinc id
|
2009-07-13 10:17:41 +02:00
|
|
|
* @param boolean $only_return_ids=false return $ids of delete call to db object, but not run it (can be used by extending classes!)
|
2009-09-16 12:20:34 +02:00
|
|
|
* @return int|array affected rows, should be 1 if ok, 0 if an error or array with id's if $only_return_ids
|
2009-01-17 04:36:58 +01:00
|
|
|
*/
|
2009-07-13 10:17:41 +02:00
|
|
|
function delete($keys=null,$only_return_ids=false)
|
2009-01-17 04:36:58 +01:00
|
|
|
{
|
2009-08-28 09:14:39 +02:00
|
|
|
if ($this->customfields || $only_return_ids)
|
2009-01-17 04:36:58 +01:00
|
|
|
{
|
|
|
|
$query = parent::delete($keys,true);
|
|
|
|
// check if query contains more then the id's
|
|
|
|
if (!isset($query[$this->autoinc_id]) || count($query) != 1)
|
|
|
|
{
|
|
|
|
foreach($this->db->select($this->table_name,$this->autoinc_id,$query,__LINE__,__FILE__,false,'',$this->app) as $row)
|
|
|
|
{
|
|
|
|
$ids[] = $row[$this->autoinc_id];
|
|
|
|
}
|
|
|
|
if (!$ids) return 0; // no rows affected
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-09-16 12:20:34 +02:00
|
|
|
$ids = (array)$query[$this->autoinc_id];
|
2009-01-17 04:36:58 +01:00
|
|
|
}
|
2009-08-28 09:14:39 +02:00
|
|
|
if ($only_return_ids) return $ids;
|
2009-01-17 04:36:58 +01:00
|
|
|
$this->db->delete($this->extra_table,array($this->extra_id => $ids),__LINE__,__FILE__);
|
|
|
|
}
|
|
|
|
return parent::delete($keys);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* query rows for the nextmatch widget
|
2009-04-01 15:01:16 +02:00
|
|
|
*
|
2009-01-17 04:36:58 +01:00
|
|
|
* Reimplemented to also read the custom fields (if enabled via $query['selectcols']).
|
2009-04-01 15:01:16 +02:00
|
|
|
*
|
2009-01-17 04:36:58 +01:00
|
|
|
* Please note: the name of the nextmatch-customfields has to be 'customfields'!
|
|
|
|
*
|
|
|
|
* @param array $query with keys 'start', 'search', 'order', 'sort', 'col_filter'
|
|
|
|
* For other keys like 'filter', 'cat_id' you have to reimplement this method in a derived class.
|
|
|
|
* @param array &$rows returned rows/competitions
|
|
|
|
* @param array &$readonlys eg. to disable buttons based on acl, not use here, maybe in a derived class
|
|
|
|
* @param string $join='' sql to do a join, added as is after the table-name, eg. ", table2 WHERE x=y" or
|
|
|
|
* "LEFT JOIN table2 ON (x=y)", Note: there's no quoting done on $join!
|
|
|
|
* @param boolean $need_full_no_count=false If true an unlimited query is run to determine the total number of rows, default false
|
|
|
|
* @param mixed $only_keys=false, see search
|
2009-09-28 14:44:44 +02:00
|
|
|
* @param string|array $extra_cols=array()
|
2009-01-17 04:36:58 +01:00
|
|
|
* @return int total number of rows
|
|
|
|
*/
|
2009-09-28 14:44:44 +02:00
|
|
|
function get_rows($query,&$rows,&$readonlys,$join='',$need_full_no_count=false,$only_keys=false,$extra_cols=array())
|
2009-01-17 04:36:58 +01:00
|
|
|
{
|
2009-09-28 14:44:44 +02:00
|
|
|
parent::get_rows($query,$rows,$readonlys,$join,$need_full_no_count,$only_keys,$extra_cols);
|
2009-04-01 15:01:16 +02:00
|
|
|
|
2009-01-17 04:36:58 +01:00
|
|
|
$selectcols = $query['selectcols'] ? explode(',',$query['selectcols']) : array();
|
|
|
|
|
|
|
|
if ($rows && $this->customfields && (!$selectcols || in_array('customfields',$selectcols)))
|
|
|
|
{
|
|
|
|
$id2keys = array();
|
|
|
|
foreach($rows as $key => $row)
|
|
|
|
{
|
|
|
|
$id2keys[$row[$this->autoinc_id]] = $key;
|
|
|
|
}
|
|
|
|
// check if only certain cf's to show
|
|
|
|
foreach($selectcols as $col)
|
|
|
|
{
|
2009-05-18 00:03:31 +02:00
|
|
|
if ($this->is_cf($col)) $fields[] = $this->get_cf_name($col);
|
2009-01-17 04:36:58 +01:00
|
|
|
}
|
|
|
|
if (($cfs = $this->read_customfields(array_keys($id2keys),$fields)))
|
|
|
|
{
|
|
|
|
foreach($cfs as $id => $data)
|
|
|
|
{
|
|
|
|
$rows[$id2keys[$id]] = array_merge($rows[$id2keys[$id]],$data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $this->total;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* searches db for rows matching searchcriteria
|
2009-04-01 15:01:16 +02:00
|
|
|
*
|
2009-01-17 04:36:58 +01:00
|
|
|
* Reimplemented to search, order and filter by custom fields
|
|
|
|
*
|
2009-08-19 14:08:52 +02:00
|
|
|
* @param array|string $criteria array of key and data cols, OR string with search pattern (incl. * or ? as wildcards)
|
2009-05-03 20:36:38 +02:00
|
|
|
* @param boolean|string/array $only_keys=true True returns only keys, False returns all cols. or
|
2009-01-17 04:36:58 +01:00
|
|
|
* comma seperated list or array of columns to return
|
|
|
|
* @param string $order_by='' fieldnames + {ASC|DESC} separated by colons ',', can also contain a GROUP BY (if it contains ORDER BY)
|
2009-05-03 20:36:38 +02:00
|
|
|
* @param string|array $extra_cols='' string or array of strings to be added to the SELECT, eg. "count(*) as num"
|
2009-01-17 04:36:58 +01:00
|
|
|
* @param string $wildcard='' appended befor and after each criteria
|
|
|
|
* @param boolean $empty=false False=empty criteria are ignored in query, True=empty have to be empty in row
|
|
|
|
* @param string $op='AND' defaults to 'AND', can be set to 'OR' too, then criteria's are OR'ed together
|
|
|
|
* @param mixed $start=false if != false, return only maxmatch rows begining with start, or array($start,$num), or 'UNION' for a part of a union query
|
|
|
|
* @param array $filter=null if set (!=null) col-data pairs, to be and-ed (!) into the query without wildcards
|
|
|
|
* @param string $join='' sql to do a join, added as is after the table-name, eg. "JOIN table2 ON x=y" or
|
|
|
|
* "LEFT JOIN table2 ON (x=y AND z=o)", Note: there's no quoting done on $join, you are responsible for it!!!
|
|
|
|
* @param boolean $need_full_no_count=false If true an unlimited query is run to determine the total number of rows, default false
|
2009-05-03 20:36:38 +02:00
|
|
|
* @return array|NULL array of matching rows (the row is an array of the cols) or NULL
|
2009-01-17 04:36:58 +01:00
|
|
|
*/
|
|
|
|
function &search($criteria,$only_keys=True,$order_by='',$extra_cols='',$wildcard='',$empty=False,$op='AND',$start=false,$filter=null,$join='',$need_full_no_count=false)
|
|
|
|
{
|
2013-07-23 09:58:51 +02:00
|
|
|
//error_log(__METHOD__.'('.array2string(array_combine(array_slice(array('criteria','only_keys','order_by','extra_cols','wildcard','empty','op','start','filter','join','need_full_no_count'), 0, count(func_get_args())), func_get_args())).')');
|
2013-07-23 15:48:08 +02:00
|
|
|
if (!$this->customfields)
|
|
|
|
{
|
|
|
|
return parent::search($criteria,$only_keys,$order_by,$extra_cols,$wildcard,$empty,$op,$start,$filter,$join,$need_full_no_count);
|
|
|
|
}
|
2009-04-01 22:01:54 +02:00
|
|
|
if ($only_keys === false)
|
|
|
|
{
|
|
|
|
$only_keys = $this->table_name.'.*';
|
|
|
|
}
|
2009-08-19 14:08:52 +02:00
|
|
|
// if string given as criteria --> search in all (or $this->columns_to_search) columns including custom fields
|
|
|
|
if ($criteria && is_string($criteria))
|
2009-01-17 04:36:58 +01:00
|
|
|
{
|
2010-04-01 22:20:08 +02:00
|
|
|
$criteria = $this->search2criteria($criteria,$wildcard,$op);
|
2009-08-19 14:08:52 +02:00
|
|
|
}
|
|
|
|
if ($criteria && is_array($criteria))
|
|
|
|
{
|
2010-04-01 21:59:50 +02:00
|
|
|
$join .= $this->extra_join;
|
|
|
|
|
2009-08-19 14:08:52 +02:00
|
|
|
// check if we search in the custom fields
|
|
|
|
if (isset($criteria[$this->extra_value]))
|
|
|
|
{
|
|
|
|
if (($negate = $criteria[$this->extra_value][0] === '!'))
|
|
|
|
{
|
|
|
|
$criteria[$this->extra_value] = substr($criteria[$this->extra_value],1);
|
|
|
|
}
|
|
|
|
$criteria[] = $this->extra_table.'.'.$this->extra_value . ' ' .($negate ? 'NOT ' : '').
|
|
|
|
$this->db->capabilities[egw_db::CAPABILITY_CASE_INSENSITIV_LIKE]. ' ' .
|
|
|
|
$this->db->quote($wildcard.$criteria[$this->extra_value].$wildcard);
|
|
|
|
unset($criteria[$this->extra_value]);
|
|
|
|
}
|
2009-04-01 15:01:16 +02:00
|
|
|
// replace ambiguous auto-id with (an exact match of) table_name.autoid
|
|
|
|
if (isset($criteria[$this->autoinc_id]))
|
|
|
|
{
|
2009-08-25 14:48:04 +02:00
|
|
|
if ($criteria[$this->autoinc_id])
|
2009-04-01 15:01:16 +02:00
|
|
|
{
|
2009-08-25 14:48:04 +02:00
|
|
|
$criteria[] = $this->db->expression($this->table_name,$this->table_name.'.',
|
|
|
|
array($this->autoinc_id => $criteria[$this->autoinc_id]));
|
2009-04-01 15:01:16 +02:00
|
|
|
}
|
|
|
|
unset($criteria[$this->autoinc_id]);
|
|
|
|
}
|
2010-04-08 22:21:40 +02:00
|
|
|
// replace ambiguous column with (an exact match of) table_name.column
|
2010-04-15 16:56:37 +02:00
|
|
|
foreach($criteria as $name => $val)
|
2010-04-08 22:21:40 +02:00
|
|
|
{
|
|
|
|
$extra_columns = $this->db->get_table_definitions($app, $this->extra_table);
|
2010-04-20 09:39:23 +02:00
|
|
|
if(is_string($name) && $extra_columns['fd'][array_search($name, $this->db_cols)])
|
|
|
|
{
|
2010-04-15 16:56:37 +02:00
|
|
|
$criteria[] = $this->db->expression($this->table_name,$this->table_name.'.',array(
|
2010-04-08 22:21:40 +02:00
|
|
|
array_search($name, $this->db_cols) => $val,
|
|
|
|
));
|
2010-04-15 16:56:37 +02:00
|
|
|
unset($criteria[$name]);
|
2010-04-08 22:21:40 +02:00
|
|
|
}
|
2010-06-14 12:18:23 +02:00
|
|
|
elseif (is_string($name) && $this->is_cf($name))
|
|
|
|
{
|
2010-07-07 13:38:52 +02:00
|
|
|
if ($op != 'AND')
|
2010-06-14 12:18:23 +02:00
|
|
|
{
|
2010-07-07 13:38:52 +02:00
|
|
|
$name = substr($name, 1);
|
|
|
|
if (($negate = $criteria[$name][0] === '!'))
|
|
|
|
{
|
|
|
|
$val = substr($val,1);
|
|
|
|
}
|
|
|
|
$cfcriteria[] = '(' . $this->extra_table.'.'.$this->extra_value . ' ' .($negate ? 'NOT ' : '').
|
|
|
|
$this->db->capabilities[egw_db::CAPABILITY_CASE_INSENSITIV_LIKE]. ' ' .
|
|
|
|
$this->db->quote($wildcard.$val.$wildcard) . ' AND ' .
|
|
|
|
$this->extra_table.'.'.$this->extra_key . ' = ' . $this->db->quote($name) .
|
|
|
|
')';
|
|
|
|
unset($criteria[self::CF_PREFIX.$name]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// criteria operator is AND we remap the criteria to be transformed to filters
|
|
|
|
$filter[$name] = $val;
|
|
|
|
unset($criteria[$name]);
|
2010-06-14 12:18:23 +02:00
|
|
|
}
|
|
|
|
}
|
2010-04-08 22:21:40 +02:00
|
|
|
}
|
2010-07-07 13:38:52 +02:00
|
|
|
if ($cfcriteria && $op =='OR') $criteria[] = implode(' OR ',$cfcriteria);
|
2009-01-17 04:36:58 +01:00
|
|
|
}
|
2013-07-23 15:48:08 +02:00
|
|
|
if($only_keys === true)
|
|
|
|
{
|
2010-04-21 17:40:58 +02:00
|
|
|
// Expand to keys here, so table_name can be prepended below
|
|
|
|
$only_keys = array_values($this->db_key_cols);
|
|
|
|
}
|
2010-04-09 00:13:22 +02:00
|
|
|
// replace ambiguous column with (an exact match of) table_name.column
|
2010-04-20 09:39:23 +02:00
|
|
|
if(is_array($only_keys))
|
|
|
|
{
|
|
|
|
foreach($only_keys as $key => &$col)
|
|
|
|
{
|
2013-08-29 15:28:24 +02:00
|
|
|
if(is_numeric($key) && in_array($col, $this->db_cols, true))
|
2010-04-20 09:39:23 +02:00
|
|
|
{
|
|
|
|
$col = $this->table_name .'.'.array_search($col, $this->db_cols).' AS '.$col;
|
2010-04-09 00:13:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-01-17 04:36:58 +01:00
|
|
|
// check if we order by a custom field --> join cf table for given cf and order by it's value
|
2010-07-08 13:37:56 +02:00
|
|
|
if (strpos($order_by,self::CF_PREFIX) !== false)
|
2009-01-17 04:36:58 +01:00
|
|
|
{
|
2010-07-08 13:37:56 +02:00
|
|
|
// fields to order by, as cutomfields may have names with spaces, we examine each order by criteria
|
|
|
|
$fields2order = explode(',',$order_by);
|
|
|
|
foreach($fields2order as $k => $v)
|
|
|
|
{
|
|
|
|
if (strpos($v,self::CF_PREFIX) !== false)
|
|
|
|
{
|
|
|
|
// we found a customfield, so we split that part by space char in order to get Sorting Direction and Fieldname
|
|
|
|
$buff = explode(' ',trim($v));
|
|
|
|
$orderDir = array_pop($buff);
|
|
|
|
$key = trim(implode(' ',$buff));
|
|
|
|
$order_by = str_replace($v,'extra_order.'.$this->extra_value.' IS NULL,extra_order.'.$this->extra_value.' '.$orderDir,$order_by);
|
|
|
|
$join .= $this->extra_join_order.' AND extra_order.'.$this->extra_key.'='.$this->db->quote(substr($key,1));
|
|
|
|
}
|
|
|
|
}
|
2009-01-17 04:36:58 +01:00
|
|
|
}
|
|
|
|
// check if we filter by a custom field
|
2009-04-01 22:01:54 +02:00
|
|
|
if (is_array($filter))
|
2009-01-17 04:36:58 +01:00
|
|
|
{
|
2011-08-22 15:12:32 +02:00
|
|
|
$_cfnames = array_keys($this->customfields);
|
2009-04-01 22:01:54 +02:00
|
|
|
foreach($filter as $name => $val)
|
2009-01-17 04:36:58 +01:00
|
|
|
{
|
2009-04-01 22:01:54 +02:00
|
|
|
// replace ambiguous auto-id with (an exact match of) table_name.autoid
|
2010-05-28 13:08:36 +02:00
|
|
|
if (is_string($name) && $name == $this->autoinc_id)
|
2009-01-17 04:36:58 +01:00
|
|
|
{
|
2009-04-01 22:01:54 +02:00
|
|
|
if ((int)$filter[$this->autoinc_id])
|
|
|
|
{
|
2009-09-26 13:00:11 +02:00
|
|
|
$filter[] = $this->db->expression($this->table_name,$this->table_name.'.',array(
|
|
|
|
$this->autoinc_id => $filter[$this->autoinc_id],
|
|
|
|
));
|
2009-04-01 22:01:54 +02:00
|
|
|
}
|
|
|
|
unset($filter[$this->autoinc_id]);
|
|
|
|
}
|
2010-04-08 22:21:40 +02:00
|
|
|
// replace ambiguous column with (an exact match of) table_name.column
|
|
|
|
elseif (is_string($name) && $val!=null && in_array($name, $this->db_cols))
|
|
|
|
{
|
|
|
|
$extra_columns = $this->db->get_table_definitions($app, $this->extra_table);
|
2013-07-23 15:48:08 +02:00
|
|
|
if ($extra_columns['fd'][array_search($name, $this->db_cols)])
|
|
|
|
{
|
2010-04-08 22:21:40 +02:00
|
|
|
$filter[] = $this->db->expression($this->table_name,$this->table_name.'.',array(
|
|
|
|
array_search($name, $this->db_cols) => $val,
|
|
|
|
));
|
|
|
|
unset($filter[$name]);
|
|
|
|
}
|
|
|
|
}
|
2009-05-18 00:03:31 +02:00
|
|
|
elseif (is_string($name) && $this->is_cf($name))
|
2009-04-01 22:01:54 +02:00
|
|
|
{
|
|
|
|
if (!empty($val)) // empty -> dont filter
|
|
|
|
{
|
2009-08-10 17:31:49 +02:00
|
|
|
if ($val[0] === '!') // negative filter
|
|
|
|
{
|
|
|
|
$sql_filter = 'extra_filter.'.$this->extra_value.'!='.$this->db->quote(substr($val,1));
|
|
|
|
}
|
|
|
|
else // using egw_db::expression to allow to use array() with possible values or NULL
|
|
|
|
{
|
2010-04-20 09:39:23 +02:00
|
|
|
if($this->customfields[$this->get_cf_name($name)]['type'] == 'select' &&
|
2010-04-07 18:06:01 +02:00
|
|
|
$this->customfields[$this->get_cf_name($name)]['rows'] > 1)
|
|
|
|
{
|
|
|
|
// Multi-select - any entry with the filter value selected matches
|
|
|
|
$sql_filter = str_replace($this->extra_value,'extra_filter.'.
|
|
|
|
$this->extra_value,$this->db->expression($this->extra_table,array(
|
2010-05-26 17:44:09 +02:00
|
|
|
$this->db->concat("','",$this->extra_value,"','").' '.$this->db->capabilities[egw_db::CAPABILITY_CASE_INSENSITIV_LIKE].' '.$this->db->quote('%,'.$val.',%')
|
2010-04-07 18:06:01 +02:00
|
|
|
))
|
|
|
|
);
|
2010-07-07 16:11:33 +02:00
|
|
|
}
|
|
|
|
elseif ($this->customfields[$this->get_cf_name($name)]['type'] == 'text')
|
|
|
|
{
|
|
|
|
$sql_filter = str_replace($this->extra_value,'extra_filter.'.$this->extra_value,
|
|
|
|
$this->db->expression($this->extra_table,array(
|
|
|
|
$this->extra_value.' '.$this->db->capabilities[egw_db::CAPABILITY_CASE_INSENSITIV_LIKE].' '.$this->db->quote($wildcard.$val.$wildcard)
|
|
|
|
))
|
|
|
|
);
|
2010-04-07 18:06:01 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$sql_filter = str_replace($this->extra_value,'extra_filter.'.
|
|
|
|
$this->extra_value,$this->db->expression($this->extra_table,array($this->extra_value => $val)));
|
|
|
|
}
|
2009-08-10 17:31:49 +02:00
|
|
|
}
|
|
|
|
// need to use a LEFT JOIN for negative search or to allow NULL values
|
|
|
|
$need_left_join = $val[0] === '!' || strpos($sql_filter,'IS NULL') !== false ? ' LEFT ' : '';
|
|
|
|
$join .= str_replace('extra_filter','extra_filter'.$extra_filter,$need_left_join.$this->extra_join_filter.
|
2009-05-18 00:03:31 +02:00
|
|
|
' AND extra_filter.'.$this->extra_key.'='.$this->db->quote($this->get_cf_name($name)).
|
2009-08-10 17:31:49 +02:00
|
|
|
' AND '.$sql_filter);
|
2009-04-01 22:01:54 +02:00
|
|
|
++$extra_filter;
|
|
|
|
}
|
|
|
|
unset($filter[$name]);
|
|
|
|
}
|
2009-05-18 00:03:31 +02:00
|
|
|
elseif(is_int($name) && $this->is_cf($val)) // lettersearch: #cfname LIKE 's%'
|
2009-04-01 22:01:54 +02:00
|
|
|
{
|
2011-08-22 15:12:32 +02:00
|
|
|
$_cf = explode(' ',$val);
|
2013-06-12 16:29:04 +02:00
|
|
|
foreach($_cf as $ci => $cf_np)
|
2011-08-22 15:12:32 +02:00
|
|
|
{
|
|
|
|
// building cf_name by glueing parts together (, in case someone used whitespace in their custom field names)
|
|
|
|
$tcf_name = ($tcf_name?$tcf_name.' ':'').$cf_np;
|
2013-06-12 16:29:04 +02:00
|
|
|
// reacts on the first one found that matches an existing customfield, should be better then the old behavior of
|
2011-08-22 15:12:32 +02:00
|
|
|
// simply splitting by " " and using the first part
|
|
|
|
if ($this->is_cf($tcf_name) && ($cfn = $this->get_cf_name($tcf_name)) && array_search($cfn,(array)$_cfnames,true)!==false )
|
|
|
|
{
|
|
|
|
$cf = $tcf_name;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-01-17 04:36:58 +01:00
|
|
|
$join .= str_replace('extra_filter','extra_filter'.$extra_filter,$this->extra_join_filter.
|
2009-05-18 00:03:31 +02:00
|
|
|
' AND extra_filter.'.$this->extra_key.'='.$this->db->quote($this->get_cf_name($cf)).
|
2009-04-01 22:01:54 +02:00
|
|
|
' AND '.str_replace($cf,'extra_filter.'.$this->extra_value,$val));
|
2009-01-17 04:36:58 +01:00
|
|
|
++$extra_filter;
|
2009-04-01 22:01:54 +02:00
|
|
|
unset($filter[$name]);
|
2009-01-17 04:36:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-23 15:48:08 +02:00
|
|
|
// add DISTINCT as by joining custom fields for search a row can be returned multiple times
|
|
|
|
if ($join && strpos($join, $this->extra_join) !== false)
|
|
|
|
{
|
|
|
|
if (is_array($only_keys))
|
|
|
|
{
|
|
|
|
$only_keys = array_values($only_keys);
|
|
|
|
$only_keys[0] = 'DISTINCT '.($only_keys[0] != $this->autoinc_id ? $only_keys[0] :
|
|
|
|
$this->table_name.'.'.$this->autoinc_id.' AS '.$this->autoinc_id);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$only_keys = 'DISTINCT '.$only_keys;
|
|
|
|
}
|
|
|
|
}
|
2009-01-17 04:36:58 +01:00
|
|
|
return parent::search($criteria,$only_keys,$order_by,$extra_cols,$wildcard,$empty,$op,$start,$filter,$join,$need_full_no_count);
|
|
|
|
}
|
2009-04-01 15:01:16 +02:00
|
|
|
|
2010-04-01 21:59:50 +02:00
|
|
|
/**
|
|
|
|
* Return criteria array for a given search pattern
|
|
|
|
* Reimplemented to search custom fields
|
|
|
|
*
|
|
|
|
* @param string $pattern search pattern incl. * or ? as wildcard, if no wildcards used we append and prepend one!
|
|
|
|
* @param string &$wildcard='' on return wildcard char to use, if pattern does not already contain wildcards!
|
|
|
|
* @param string &$op='AND' on return boolean operation to use, if pattern does not start with ! we use OR else AND
|
|
|
|
* @param string $extra_col=null extra column to search
|
|
|
|
* @param array $search_cols=array() List of columns to search. If not provided, all columns in $this->db_cols will be considered
|
|
|
|
* @return array or column => value pairs
|
|
|
|
*/
|
|
|
|
public function search2criteria($pattern,&$wildcard='',&$op='AND',$extra_col=null, $search_cols = array())
|
|
|
|
{
|
|
|
|
// This function can get called multiple times. Make sure it doesn't re-process.
|
|
|
|
if (empty($pattern) || is_array($pattern)) return $pattern;
|
2010-05-26 17:44:09 +02:00
|
|
|
if(strpos($pattern, 'CAST(COALESCE(') !== false)
|
2010-04-01 21:59:50 +02:00
|
|
|
{
|
|
|
|
return $pattern;
|
|
|
|
}
|
|
|
|
|
|
|
|
$pattern = trim($pattern);
|
|
|
|
$filter = array();
|
|
|
|
if(!$search_cols)
|
|
|
|
{
|
|
|
|
$search_cols = $this->get_default_search_columns();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add in custom field column, if it is not already there
|
2013-07-23 15:48:08 +02:00
|
|
|
if($this->customfields && !in_array($this->extra_table.'.'.$this->extra_value, $search_cols))
|
2010-04-01 21:59:50 +02:00
|
|
|
{
|
|
|
|
$search_cols[] = $this->extra_table.'.'.$this->extra_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let parent deal with the normal stuff
|
|
|
|
$criteria = parent::search2criteria($pattern, $wildcard, $op, $extra_col, $search_cols);
|
|
|
|
|
|
|
|
return $criteria;
|
|
|
|
}
|
|
|
|
|
2009-05-18 00:03:31 +02:00
|
|
|
/**
|
|
|
|
* Function to test if $field is a custom field: check for the prefix
|
|
|
|
*
|
|
|
|
* @param string $field
|
|
|
|
* @return boolean true if $name is a custom field, false otherwise
|
|
|
|
*/
|
|
|
|
function is_cf($field)
|
|
|
|
{
|
|
|
|
return $field[0] == self::CF_PREFIX;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get name part from a custom field: remove the prefix
|
|
|
|
*
|
|
|
|
* @param string $field
|
|
|
|
* @return string name without prefix
|
|
|
|
*/
|
|
|
|
function get_cf_name($field)
|
|
|
|
{
|
|
|
|
return substr($field,1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the field-name from the name of a custom field: prepend the prefix
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @return string prefix-name
|
|
|
|
*/
|
|
|
|
function get_cf_field($name)
|
|
|
|
{
|
|
|
|
return self::CF_PREFIX.$name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if cf is stored as 1:N relation in DB and array in memory
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function is_multiple($name)
|
|
|
|
{
|
|
|
|
return $this->allow_multiple_values && in_array($this->customfields[$name]['type'],array('select','select-account')) &&
|
|
|
|
$this->customfields[$name]['rows'] > 1;
|
|
|
|
}
|
|
|
|
|
2009-01-17 04:36:58 +01:00
|
|
|
/**
|
|
|
|
* Prevent someone calling the old php4 so_sql constructor
|
|
|
|
*/
|
|
|
|
function so_sql()
|
|
|
|
{
|
|
|
|
throw new egw_exception_assertion_failed('use __construct()!');
|
|
|
|
}
|
|
|
|
}
|