2001-01-25 04:03:45 +01:00
|
|
|
<?php
|
2006-06-13 06:16:38 +02:00
|
|
|
/**
|
|
|
|
* API - contacts service provided by the addressbook application
|
|
|
|
*
|
|
|
|
* @link http://www.egroupware.org
|
|
|
|
* @author Ralf Becker <RalfBecker-AT-outdoor-training.de>
|
|
|
|
* @package api
|
|
|
|
* @copyright (c) 2006 by Ralf Becker <RalfBecker-AT-outdoor-training.de>
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
* @version $Id$
|
|
|
|
*/
|
2006-04-24 22:55:16 +02:00
|
|
|
|
|
|
|
require_once(EGW_INCLUDE_ROOT.'/addressbook/inc/class.bocontacts.inc.php');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* contacts service provided by the addressbook application
|
|
|
|
*
|
|
|
|
* @package api
|
|
|
|
* @author Ralf Becker <RalfBecker-AT-outdoor-training.de>
|
|
|
|
* @copyright (c) 2006 by Ralf Becker <RalfBecker-AT-outdoor-training.de>
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
*/
|
|
|
|
class contacts extends bocontacts
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @deprecated since 1.3 use total
|
|
|
|
* @var int $total_records
|
|
|
|
*/
|
|
|
|
var $total_records;
|
2006-04-26 23:24:50 +02:00
|
|
|
/**
|
|
|
|
* @deprecated since 1.3 use contact_fields
|
|
|
|
* @var array $stock_contact_fields
|
|
|
|
*/
|
|
|
|
var $stock_contact_fields = array();
|
2006-04-24 22:55:16 +02:00
|
|
|
|
2006-04-26 16:01:10 +02:00
|
|
|
/**
|
|
|
|
* constructor calling the constructor of the extended class
|
|
|
|
*/
|
2006-04-24 22:55:16 +02:00
|
|
|
function contacts($contact_app='addressbook')
|
2001-05-21 11:12:10 +02:00
|
|
|
{
|
2006-04-24 22:55:16 +02:00
|
|
|
$this->bocontacts($contact_app);
|
|
|
|
|
|
|
|
$this->total_records =& $this->total;
|
2006-04-26 23:24:50 +02:00
|
|
|
$this->stock_contact_fields = array_keys($this->contact_fields);
|
2003-10-25 12:14:25 +02:00
|
|
|
}
|
2006-04-24 22:55:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* reads contacts matched by key and puts all cols in the data array
|
|
|
|
*
|
2006-04-26 16:01:10 +02:00
|
|
|
* reimplemented from bocontacts to call the old read function, if more then one param
|
|
|
|
*
|
2006-04-24 22:55:16 +02:00
|
|
|
* @param int/string $contact_id
|
|
|
|
* @return array/boolean contact data or false on error
|
|
|
|
*/
|
|
|
|
function read($contact_id)
|
2003-10-25 12:14:25 +02:00
|
|
|
{
|
2006-04-24 22:55:16 +02:00
|
|
|
if (func_num_args() > 1) // calling the old / depricated read function
|
2003-10-25 12:14:25 +02:00
|
|
|
{
|
2006-04-24 22:55:16 +02:00
|
|
|
$args = func_get_args();
|
|
|
|
return call_user_func_array(array(&$this,'old_read'),$args);
|
2003-10-25 12:14:25 +02:00
|
|
|
}
|
2006-04-24 22:55:16 +02:00
|
|
|
return parent::read($contact_id);
|
|
|
|
}
|
2003-10-25 12:14:25 +02:00
|
|
|
|
2006-04-24 22:55:16 +02:00
|
|
|
/**
|
2006-04-26 16:01:10 +02:00
|
|
|
* Deprecated methods for compatibility with the old contacts class
|
2006-04-24 22:55:16 +02:00
|
|
|
*
|
|
|
|
* They will be removed after one release, so dont use them in new code!!!
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Searches for contacts meating certain criteria and evtl. return only a range of them
|
|
|
|
*
|
|
|
|
* This method was named read in eGW 1.0 and 1.2
|
|
|
|
*
|
|
|
|
* @deprecated since 1.3 use search() instead
|
|
|
|
* @param int $start=0 starting number of the range, if $limit != 0
|
|
|
|
* @param int $limit=0 max. number of entries to return, 0=all
|
2006-06-16 06:47:22 +02:00
|
|
|
* @param array $fields=null fields to return or null for all stock fields, fields are either in the keys or values
|
2006-04-24 22:55:16 +02:00
|
|
|
* @param string $query='' search pattern or '' for none
|
|
|
|
* @param string $filter='' filters with syntax like <name>=<value>,<name2>=<value2>,<name3>=!'' for not empty
|
|
|
|
* @param string $sort='' sorting: ASC or DESC
|
|
|
|
* @param string $order='' column to order, default ('') n_family,n_given,email ASC
|
|
|
|
* @param int $lastmod=-1 return only values modified after given timestamp, default (-1) return all
|
|
|
|
* @param string $cquery='' return only entries starting with given character, default ('') all
|
|
|
|
* @return array of contacts
|
|
|
|
*/
|
|
|
|
function old_read($start=0,$limit=0,$fields=null,$query='',$filter='',$sort='',$order='', $lastmod=-1,$cquery='')
|
|
|
|
{
|
2007-05-18 19:47:51 +02:00
|
|
|
//error_log("contacts::old_read($start,$limit,".print_r($fields,true).",$query,'$filter','$sort','$order',$lastmod,$cquery)");
|
2006-04-24 22:55:16 +02:00
|
|
|
//echo "<p>contacts::old_read($start,$limit,".print_r($fields,true).",$query,'$filter','$sort','$order',$lastmod,$cquery)</p>\n";
|
2006-04-26 16:01:10 +02:00
|
|
|
$sfilter = array();
|
2006-05-17 06:15:58 +02:00
|
|
|
if ($filter)
|
2004-03-22 16:34:32 +01:00
|
|
|
{
|
2006-05-17 06:15:58 +02:00
|
|
|
foreach(explode(',',$filter) as $expr)
|
|
|
|
{
|
|
|
|
list($col,$value) = explode('=',$expr);
|
|
|
|
|
2006-06-14 21:00:16 +02:00
|
|
|
if ($col == 'access') // text field access is renamed to private and using boolean 0/1
|
|
|
|
{
|
|
|
|
$col = 'private';
|
|
|
|
$value = $value == 'private';
|
|
|
|
}
|
2006-05-17 06:15:58 +02:00
|
|
|
$sfilter[$col] = $value;
|
|
|
|
}
|
2004-03-22 16:34:32 +01:00
|
|
|
}
|
2006-04-24 22:55:16 +02:00
|
|
|
if ($lastmod != -1)
|
2003-10-25 12:14:25 +02:00
|
|
|
{
|
2006-04-24 22:55:16 +02:00
|
|
|
$sfilter[] = 'contact_modified > '.(int)$lastmod;
|
2003-10-25 12:14:25 +02:00
|
|
|
}
|
2006-06-16 06:47:22 +02:00
|
|
|
static $old2new = array('fn' => 'n_fn','bday' => 'bday');
|
|
|
|
|
2006-04-24 22:55:16 +02:00
|
|
|
if (is_array($fields))
|
2003-10-25 12:14:25 +02:00
|
|
|
{
|
2006-06-16 06:47:22 +02:00
|
|
|
$fields2 = array_values($fields);
|
|
|
|
// check if the fields are in the keys with values true or 1
|
|
|
|
$fields = $fields2[0] === true || $fields2[0] === 1 ? array_keys($fields) : $fields2;
|
|
|
|
|
|
|
|
|
|
|
|
foreach($old2new as $old => $new)
|
|
|
|
{
|
|
|
|
if (($key = array_search($old,$fields)) !== false)
|
|
|
|
{
|
|
|
|
$fields[$key] = $new;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elseif (is_string($fields))
|
|
|
|
{
|
|
|
|
$fields = explode(',',$fields);
|
2003-10-25 12:14:25 +02:00
|
|
|
}
|
2006-06-16 06:47:22 +02:00
|
|
|
if (!$order) $order = $fields ? $fields[0] : 'org_name,n_family,n_given';
|
2007-04-30 07:42:05 +02:00
|
|
|
if ($order && strpos($order,'_') === false) $order = 'contact_'.$order;
|
2006-06-16 06:47:22 +02:00
|
|
|
|
2006-04-26 16:01:10 +02:00
|
|
|
//echo '<p>contacts::search('.($cquery ? $cquery.'*' : $query).','.print_r($fields,true).",'$order $sort','','".($cquery ? '' : '%')."',false,'OR',".(!$limit ? 'false' : "array($start,$limit)").",".print_r($sfilter,true).");</p>\n";
|
|
|
|
$rows =& $this->search($cquery ? $cquery.'*' : $query,$fields,$order.($sort ? ' '.$sort : ''),'',
|
|
|
|
$cquery ? '' : '%',false,'OR',!$limit ? false : array((int)$start,(int)$limit),$sfilter);
|
2006-04-24 22:55:16 +02:00
|
|
|
|
2006-04-26 16:01:10 +02:00
|
|
|
// return the old birthday format
|
2006-06-16 06:47:22 +02:00
|
|
|
if ($rows && (is_null($fields) || array_intersect($old2new,$fields)))
|
2003-10-25 12:14:25 +02:00
|
|
|
{
|
2006-04-24 22:55:16 +02:00
|
|
|
foreach($rows as $n => $row)
|
2003-10-25 12:14:25 +02:00
|
|
|
{
|
2006-06-16 06:47:22 +02:00
|
|
|
foreach($old2new as $old => $new)
|
|
|
|
{
|
|
|
|
if (isset($row[$new])) $rows[$n][$old] = $row[$new];
|
|
|
|
|
|
|
|
if (isset($row['bday']))
|
|
|
|
{
|
|
|
|
list($y,$m,$d) = explode('-',$row['bday']);
|
|
|
|
$rows[$n]['bday'] = sprintf('%d/%d/%04d',$m,$d,$y);
|
|
|
|
}
|
|
|
|
}
|
2003-10-25 12:14:25 +02:00
|
|
|
}
|
|
|
|
}
|
2006-04-24 22:55:16 +02:00
|
|
|
return $rows;
|
|
|
|
}
|
2003-10-25 12:14:25 +02:00
|
|
|
|
2006-04-24 22:55:16 +02:00
|
|
|
/**
|
|
|
|
* read a single entry
|
|
|
|
*
|
|
|
|
* @deprecated since 1.3 use read() instead
|
|
|
|
* @param int $id
|
|
|
|
* @param string $fields='' we always return all fields now
|
|
|
|
* @return array/boolean contact or false on failure
|
|
|
|
*/
|
|
|
|
function read_single_entry($id,$fields='')
|
|
|
|
{
|
|
|
|
return $this->read($id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* add a contact
|
|
|
|
*
|
|
|
|
* @deprecated since 1.3 use save() instead
|
|
|
|
* @param int $owner owner of the entry
|
|
|
|
* @param array $fields contains access, cat_id and tif if their param is null
|
|
|
|
* @param string $access=null 'private' or 'public'
|
|
|
|
* @param int $cat_id=null
|
|
|
|
* @param string $tid=null 'n'
|
|
|
|
* @return array/boolean contact or false on failure
|
|
|
|
*/
|
|
|
|
function add($owner,$fields,$access=NULL,$cat_id=NULL,$tid=NULL)
|
|
|
|
{
|
|
|
|
// access, cat_id and tid can be in $fields now or as extra params
|
|
|
|
foreach(array('access','cat_id','tid') as $extra)
|
2003-10-25 12:14:25 +02:00
|
|
|
{
|
2006-04-24 22:55:16 +02:00
|
|
|
if (!is_null($$extra))
|
2004-01-17 04:21:21 +01:00
|
|
|
{
|
2006-04-24 22:55:16 +02:00
|
|
|
$fields[$extra] = $$extra;
|
2004-01-17 04:21:21 +01:00
|
|
|
}
|
2003-10-25 12:14:25 +02:00
|
|
|
}
|
2006-04-24 22:55:16 +02:00
|
|
|
if(empty($fields['tid']))
|
2003-10-25 12:14:25 +02:00
|
|
|
{
|
2006-04-24 22:55:16 +02:00
|
|
|
$fields['tid'] = 'n';
|
2003-10-25 12:14:25 +02:00
|
|
|
}
|
2006-04-24 22:55:16 +02:00
|
|
|
$fields['private'] = (int) $fields['access'] == 'private';
|
|
|
|
unset($fields['id']); // in case it's set
|
2003-10-25 12:14:25 +02:00
|
|
|
|
2006-04-24 22:55:16 +02:00
|
|
|
return !$this->save($fields) ? $fields['id'] : false;
|
|
|
|
}
|
2003-10-25 12:14:25 +02:00
|
|
|
|
2006-04-24 22:55:16 +02:00
|
|
|
/**
|
|
|
|
* update a contact
|
|
|
|
*
|
|
|
|
* @deprecated since 1.3 use save() instead
|
|
|
|
* @param int $id id of the entry
|
|
|
|
* @param int $owner owner of the entry
|
|
|
|
* @param array $fields contains access, cat_id and tif if their param is null
|
|
|
|
* @param string $access=null 'private' or 'public'
|
|
|
|
* @param int $cat_id=null
|
|
|
|
* @param string $tid=null 'n'
|
|
|
|
* @return array/boolean contact or false on failure
|
|
|
|
*/
|
|
|
|
function update($id,$owner,$fields,$access=NULL,$cat_id=NULL,$tid=NULL)
|
|
|
|
{
|
|
|
|
// access, cat_id and tid can be in $fields now or as extra params
|
|
|
|
foreach(array('access','cat_id','tid') as $extra)
|
2003-10-25 12:14:25 +02:00
|
|
|
{
|
2006-04-24 22:55:16 +02:00
|
|
|
if (!is_null($$extra))
|
2003-10-25 12:14:25 +02:00
|
|
|
{
|
2006-04-24 22:55:16 +02:00
|
|
|
$fields[$extra] = $$extra;
|
2003-10-25 12:14:25 +02:00
|
|
|
}
|
|
|
|
}
|
2006-04-24 22:55:16 +02:00
|
|
|
if(empty($fields['tid']))
|
2003-10-25 12:14:25 +02:00
|
|
|
{
|
2006-04-24 22:55:16 +02:00
|
|
|
$fields['tid'] = 'n';
|
2003-10-25 12:14:25 +02:00
|
|
|
}
|
2006-04-24 22:55:16 +02:00
|
|
|
$fields['private'] = (int) $fields['access'] == 'private';
|
|
|
|
$fields['id'] = $id;
|
2003-10-25 12:14:25 +02:00
|
|
|
|
2006-04-24 22:55:16 +02:00
|
|
|
return $id && !$this->save($fields);
|
2001-05-21 11:12:10 +02:00
|
|
|
}
|
2006-04-24 22:55:16 +02:00
|
|
|
}
|