new API contacts class using the contacts service of addressbook:

- it extends the bocontacts class of addressbook and
- adds some depricated methods to add basic support for the old contacts methods: read, read_single_entry, add and update
This commit is contained in:
Ralf Becker 2006-04-24 20:55:16 +00:00
parent 2315bfc199
commit 53b47fed8d
3 changed files with 213 additions and 2465 deletions

View File

@ -1,583 +1,221 @@
<?php
/**************************************************************************\
* eGroupWare API - Contact Management Shared Routines *
* Written by Joseph Engo <jengo@phpgroupware.org> *
* and Miles Lott <milosch@groupwhere.org> *
* and Bettina Gille <ceb@phpgroupware.org> *
* View and manipulate contact records *
* Copyright (C) 2001, 2002 Joseph Engo, Miles Lott, Bettina Gille *
* ------------------------------------------------------------------------ *
* This library is part of the eGroupWare API *
* http://www.egroupware.org *
* ------------------------------------------------------------------------ *
* 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$ */
/**************************************************************************\
* eGroupWare - contacts service provided by the addressbook application *
* http://www.egroupware.org *
* Written and (c) 2006 Ralf Becker <RalfBecker-AT-outdoor-training.de> *
* ------------------------------------------------------------------------ *
* 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. *
\**************************************************************************/
if (!isset($GLOBALS['egw_info']['server']['contact_repository']))
/* $Id$ */
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;
function contacts($contact_app='addressbook')
{
$GLOBALS['egw_info']['server']['contact_repository'] = 'sql';
$this->bocontacts($contact_app);
$this->total_records =& $this->total;
}
require_once(EGW_API_INC . '/class.contacts_'.$GLOBALS['egw_info']['server']['contact_repository'] . '.inc.php');
class contacts extends contacts_
/**
* reads contacts matched by key and puts all cols in the data array
*
* @param int/string $contact_id
* @return array/boolean contact data or false on error
*/
function read($contact_id)
{
function contacts()
if (func_num_args() > 1) // calling the old / depricated read function
{
$this->contacts_(); // call constructor of extended class
}
/*!
@function check_perms
@abstract checks if user has the necessary permissions on a contact
@syntax check_perms($rights,$needed,$addr=False)
@param $rights integer the rights the user has / grants from the owner of the contact, only used if $addr not given
@param $needed integer PHPGW_ACL_{READ|EDIT|DELETE}
@param $addr mixed contact-array or contact-id, if False rights have to be supplyed in $rights
*/
function check_perms($rights,$needed,$addr=False)
{
//echo "<p>contacts::check_perms($rights,$needed,".print_r($addr,True).")";
if ($addr !== False) // addr-record or id given
{
if(@is_array($addr))
{
if (isset($addr['rights']))
{
$rights = $addr['rights'];
}
elseif (isset($addr['owner']))
{
$rights = $this->grants[$addr['owner']];
}
else
{
$id = (int)(isset($addr['id']) ? $addr['id'] : $addr['ab_id']);
}
}
else
{
$id = (int)$addr;
}
if (isset($id))
{
$addr = $this->read_single_entry($id,array('owner' => 'owner'));
$rights = @$addr[0]['rights'];
//echo "addr($id)=<pre>".print_r($addr[0],True)."</pre>\n";
}
}
$ret = !!((int)$rights & $needed);
//echo " rights=$rights, id=$id => ".($ret?'True':'False')."</p>\n";
//echo "grants=<pre>".print_r($this->grants,True)."</pre>\n";
return $ret;
}
/**
* Get the the person data what you want. Wrapper function to stay compatible with egroupware.
*
* @author Lars Kneschke <lars@kneschke.de>
* @param array $fields The fields that you can see from person
* @param integer $limit Limit of records that you want
* @param integer $ofset Ofset of record that you want start
* @param string $orderby The field which you want order
* @param string $sort ASC | DESC depending what you want
* @param mixed $criteria All criterias what you want
* @param mixed $criteria_token same like $criteria but builded<br>with sql_criteria class, more powerfull
* @return array with records
*/
function get_persons($_fields, $start='', $limit='', $orderby='', $sort='', $_criteria='', $token_criteria='')
{
// transform fields from phpgw to egw structure
foreach($_fields as $fieldValue)
{
switch($fieldValue)
{
case 'per_first_name':
$fields['n_given']='n_given';
case 'per_last_name':
$fields['n_family']='n_family';
default:
$fields[$fieldValue]=$fieldValue;
}
}
// transform criteria from phpgw to egw structure
if(is_array($_criteria))
{
foreach($_criteria as $criteriaKey => $criteriaValue)
{
if($GLOBALS['egw_info']['server']['contact_repository'] == 'ldap')
{
switch($criteriaKey)
{
case 'contact_id':
$criteria['uid']=$criteriaValue;
break;
default:
$criteria[$criteriaKey] = $criteria[$criteriaValue];
break;
}
}
else
{
switch($criteriaKey)
{
case 'contact_id':
$criteria['id']=$criteriaValue;
break;
default:
$criteria[$criteriaKey] = $criteria[$criteriaValue];
break;
}
}
}
}
$entries = $this->read($start,$limit,$fields,$criteria,'',$sort,$orderby);
// transform entries from egw to phpgw structure
if(is_array($entries))
{
foreach($entries as $entryKey => $entryValue)
{
$entryValue['per_first_name'] = $entryValue['n_given'];
$entryValue['per_last_name'] = $entryValue['n_family'];
$entryValue['contact_id'] = $entryValue['id'];
$entries[$entryKey] = $entryValue;
}
}
return $entries;
}
function split_stock_and_extras($fields)
{
settype($fields, 'array');
foreach($fields as $field => $value)
{
/* Depending on how the array was built, this is needed. */
if (is_int($field))
{
$field = $value;
}
if(@is_int($value))
{
$value = $field;
}
if ($this->stock_contact_fields[$field])
{
$stock_fields[$field] = $value;
$stock_fieldnames[$field] = $this->stock_contact_fields[$field];
}
elseif (!isset($this->non_contact_fields[$field]))
{
$extra_fields[$field] = $value;
}
}
return array($stock_fields,$stock_fieldnames,$extra_fields);
}
function loop_addslashes($fields)
{
$absf = $this->stock_contact_fields;
foreach($absf as $t => $nul)
{
$ta[] = $this->db->db_addslashes($fields[$t]);
}
return $ta;
}
/* This will take an array or integer */
function delete($id)
{
if(@is_array($id))
{
foreach($id as $nul => $t_id)
{
$this->delete_($t_id);
}
}
else
{
$this->delete_($id);
}
}
function asc_sort($a,$b)
{
echo "<br>A:'".$a."' B:'".$b;
if($a[1] == $b[1])
{
return 0;
}
return ($a[1]>$b[1])?1:-1;
}
function desc_sort($a,$b)
{
echo "<br>A:'".$a."' B:'".$b;
if($a[1]==$b[1])
{
return 0;
}
return ($a[1]<$b[1])?1:-1;
}
/**
* To be used in usort()
*
* compares two 2-dimensional arrays a and b.
* The first dimension holds the key, the array is sorted by.
* The second dimension holds the data, that's actually string-compared.
*
* @author Carsten Wolff <wolffc@egroupware.org>
* @param array $fields The fields, the array is to be sorted by. Use of multiple keys is for subsorting.
* @param string $order ASC | DESC ascending or descending order
* @param array $a one item
* @param array $b the other item
* @return integer -1 | 0 | 1 equals: a is smaller | the same | larger than b.
*/
function _cmp($fields, $order, $a, $b)
{
foreach ($fields as $field)
{
$result = strcasecmp($a[$field][0], $b[$field][0]);
if ($result == 0)
{
continue;
}
if ($order == "DESC")
{
return -$result;
}
else
{
return $result;
}
}
return 0;
}
function formatted_address($id, $business = True, $afont = '', $asize = '2')
{
$t = CreateObject('phpgwapi.Template',$GLOBALS['egw']->common->get_tpl_dir('addressbook'));
$s = CreateObject('phpgwapi.sbox');
$fields = array(
'n_given' => 'n_given',
'n_family' => 'n_family',
'title' => 'title',
'org_name' => 'org_name',
'org_unit' => 'org_unit',
'adr_one_street' => 'adr_one_street',
'adr_one_locality' => 'adr_one_locality',
'adr_one_postalcode' => 'adr_one_postalcode',
'adr_one_region' => 'adr_one_region',
'adr_one_countryname' => 'adr_one_countryname',
'adr_two_street' => 'adr_two_street',
'adr_two_locality' => 'adr_two_locality',
'adr_two_postalcode' => 'adr_two_postalcode',
'adr_two_region' => 'adr_two_region',
'adr_two_countryname' => 'adr_two_countryname'
);
list($address) = $this->read_single_entry($id,$fields);
foreach($address as $k => $val)
{
$address[$k] = $GLOBALS['egw']->strip_html($val);
}
if ($address['title'])
{
$title = $address['title'] . '&nbsp;';
}
if ($business)
{
if ($address['org_name'])
{
$company = $address['org_name'];
}
else
{
$company = $title . $address['n_given'] . '&nbsp;' . $address['n_family'];
}
$street = $address['adr_one_street'];
$city = $address['adr_one_locality'];
$zip = $address['adr_one_postalcode'];
$state = $address['adr_one_region'];
$country = $address['adr_one_countryname'];
}
else
{
$company = $title . $address['n_given'] . '&nbsp;' . $address['n_family'];
$street = $address['adr_two_street'];
$city = $address['adr_two_locality'];
$zip = $address['adr_two_postalcode'];
$state = $address['adr_two_region'];
$country = $address['adr_two_countryname'];
}
if (! $country)
{
$country = $GLOBALS['egw_info']['user']['preferences']['common']['country'];
}
if (file_exists(PHPGW_SERVER_ROOT . SEP . 'addressbook' . SEP . 'templates' . SEP .'default' . SEP . 'format_' . strtolower($country) . '.tpl'))
{
$a = $t->set_file(array('address_format' => 'format_' . strtolower($country) . '.tpl'));
}
else
{
$a = $t->set_file(array('address_format' => 'format_us.tpl'));
}
if (!$afont)
{
$afont = $GLOBALS['egw_info']['theme']['font'];
}
$a .= $t->set_var('font',$afont);
$a .= $t->set_var('fontsize',$asize);
$a .= $t->set_var('company',$company);
$a .= $t->set_var('department',$address['org_unit']);
$a .= $t->set_var('street',$street);
$a .= $t->set_var('city',$city);
$a .= $t->set_var('zip',$zip);
$a .= $t->set_var('state',$state);
if ($country != $GLOBALS['egw_info']['user']['preferences']['common']['country'])
{
$countryname = $s->get_full_name($country);
$a .= $t->set_var('country',lang($countryname));
}
$a .= $t->fp('out','address_format');
return $a;
}
function formatted_address_full($id, $business = True, $afont = '', $asize = '2')
{
$t = CreateObject('phpgwapi.Template',$GLOBALS['egw']->common->get_tpl_dir('addressbook'));
$s = CreateObject('phpgwapi.sbox');
$fields = array(
'n_given' => 'n_given',
'n_family' => 'n_family',
'title' => 'title',
'org_name' => 'org_name',
'org_unit' => 'org_unit',
'adr_one_street' => 'adr_one_street',
'adr_one_locality' => 'adr_one_locality',
'adr_one_postalcode' => 'adr_one_postalcode',
'adr_one_region' => 'adr_one_region',
'tel_work' => 'tel_work',
'tel_fax' => 'tel_fax',
'email' => 'email',
'url' => 'url',
'adr_one_countryname' => 'adr_one_countryname',
'adr_two_street' => 'adr_two_street',
'adr_two_locality' => 'adr_two_locality',
'adr_two_postalcode' => 'adr_two_postalcode',
'adr_two_region' => 'adr_two_region',
'adr_two_countryname' => 'adr_two_countryname',
'tel_home' => 'tel_home',
'email_home' => 'email_home'
);
list($address) = $this->read_single_entry($id,$fields);
foreach($address as $k => $val)
{
$address[$k] = $GLOBALS['egw']->strip_html($val);
}
if($address['title'])
{
$title = $address['title'] . '&nbsp;';
}
if($business)
{
if($address['org_name'])
{
$company = $address['org_name'];
}
else
{
$company = $title . $address['n_given'] . '&nbsp;' . $address['n_family'];
}
$street = $address['adr_one_street'];
$city = $address['adr_one_locality'];
$zip = $address['adr_one_postalcode'];
$state = $address['adr_one_region'];
$country = $address['adr_one_countryname'];
$tel = $address['tel_work'];
$email = $address['email'];
}
else
{
$company = $title . $address['n_given'] . '&nbsp;' . $address['n_family'];
$street = $address['adr_two_street'];
$city = $address['adr_two_locality'];
$zip = $address['adr_two_postalcode'];
$state = $address['adr_two_region'];
$country = $address['adr_two_countryname'];
$tel = $address['tel_home'];
$email = $address['email_home'];
}
if(!$country)
{
$country = $GLOBALS['egw_info']['user']['preferences']['common']['country'];
}
if(file_exists(PHPGW_SERVER_ROOT . SEP . 'addressbook' . SEP . 'templates' . SEP .'default' . SEP . 'full_format_' . strtolower($country) . '.tpl'))
{
$a = $t->set_file(array('address_format' => 'full_format_' . strtolower($country) . '.tpl'));
}
else
{
$a = $t->set_file(array('address_format' => 'full_format_us.tpl'));
}
if(!$afont)
{
$afont = $GLOBALS['egw_info']['theme']['font'];
}
$a .= $t->set_var('font',$afont);
$a .= $t->set_var('fontsize',$asize);
$a .= $t->set_var('lang_url',lang('url'));
$a .= $t->set_var('lang_email',lang('email'));
$a .= $t->set_var('lang_fax',lang('fax number'));
$a .= $t->set_var('lang_fon',lang('phone number'));
$a .= $t->set_var('company',$company);
$a .= $t->set_var('department',$address['org_unit']);
$a .= $t->set_var('street',$street);
$a .= $t->set_var('city',$city);
$a .= $t->set_var('zip',$zip);
$a .= $t->set_var('state',$state);
$a .= $t->set_var('email',$email);
$a .= $t->set_var('tel',$tel);
$a .= $t->set_var('fax',$address['tel_fax']);
$a .= $t->set_var('url',$address['url']);
if($country != $GLOBALS['egw_info']['user']['preferences']['common']['country'])
{
$countryname = $s->get_full_name($country);
$a .= $t->set_var('country',lang($countryname));
}
$a .= $t->fp('out','address_format');
return $a;
}
function formatted_address_line($id, $business = True, $afont = '', $asize = '2')
{
$t = CreateObject('phpgwapi.Template',$GLOBALS['egw']->common->get_tpl_dir('addressbook'));
$s = CreateObject('phpgwapi.sbox');
$fields = array(
'n_given' => 'n_given',
'n_family' => 'n_family',
'title' => 'title',
'org_name' => 'org_name',
'adr_one_street' => 'adr_one_street',
'adr_one_locality' => 'adr_one_locality',
'adr_one_postalcode' => 'adr_one_postalcode',
'adr_one_region' => 'adr_one_region',
'adr_one_countryname' => 'adr_one_countryname',
'adr_two_street' => 'adr_two_street',
'adr_two_locality' => 'adr_two_locality',
'adr_two_postalcode' => 'adr_two_postalcode',
'adr_two_region' => 'adr_two_region',
'adr_two_countryname' => 'adr_two_countryname'
);
list($address) = $this->read_single_entry($id,$fields);
foreach($address as $k => $val)
{
$address[$k] = $GLOBALS['egw']->strip_html($val);
}
if($address['title'])
{
$title = $address['title'] . '&nbsp;';
}
if($business)
{
if($address['org_name'])
{
$company = $address['org_name'];
}
else
{
$company = $title . $address['n_given'] . '&nbsp;' . $address['n_family'];
}
$street = $address['adr_one_street'];
$city = $address['adr_one_locality'];
$zip = $address['adr_one_postalcode'];
$state = $address['adr_one_region'];
$country = $address['adr_one_countryname'];
}
else
{
$company = $title . $address['n_given'] . '&nbsp;' . $address['n_family'];
$street = $address['adr_two_street'];
$city = $address['adr_two_locality'];
$zip = $address['adr_two_postalcode'];
$state = $address['adr_two_region'];
$country = $address['adr_two_countryname'];
}
if(!$country)
{
$country = $GLOBALS['egw_info']['user']['preferences']['common']['country'];
}
if(file_exists(PHPGW_SERVER_ROOT . SEP . 'addressbook' . SEP . 'templates' . SEP .'default' . SEP . 'line_format_' . strtolower($country) . '.tpl'))
{
$a = $t->set_file(array('address_format' => 'line_format_' . strtolower($country) . '.tpl'));
}
else
{
$a = $t->set_file(array('address_format' => 'line_format_us.tpl'));
}
if(!$afont)
{
$afont = $GLOBALS['egw_info']['theme']['font'];
}
$a .= $t->set_var('font',$afont);
$a .= $t->set_var('fontsize',$asize);
$a .= $t->set_var('company',$company);
$a .= $t->set_var('street',$street);
$a .= $t->set_var('city',$city);
$a .= $t->set_var('zip',$zip);
$a .= $t->set_var('state',$state);
if($country != $GLOBALS['egw_info']['user']['preferences']['common']['country'])
{
$countryname = $s->get_full_name($country);
$a .= $t->set_var('country','&nbsp;°&nbsp;' . lang($countryname));
}
$a .= $t->fp('out','address_format');
return $a;
$args = func_get_args();
return call_user_func_array(array(&$this,'old_read'),$args);
}
return parent::read($contact_id);
}
?>
/**
* searches db for rows matching searchcriteria
*
* '*' and '?' are replaced with sql-wildcards '%' and '_'
*
* @param array/string $criteria array of key and data cols, OR a SQL query (content for WHERE), fully quoted (!)
* @param boolean/string $only_keys=true True returns only keys, False returns all cols. comma seperated list of keys to return
* @param string $order_by='' fieldnames + {ASC|DESC} separated by colons ',', can also contain a GROUP BY (if it contains ORDER BY)
* @param string/array $extra_cols='' string or array of strings to be added to the SELECT, eg. "count(*) as num"
* @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)
* @param array $filter=null if set (!=null) col-data pairs, to be and-ed (!) into the query without wildcards
* @return array of matching rows (the row is an array of the cols) or False
*/
/* function &search($criteria,$only_keys=True,$order_by='',$extra_cols='',$wildcard='',$empty=False,$op='AND',$start=false,$filter=null)
{
//echo "<p>contacts::search(".print_r($criteria,true).",'$only_keys','$order_by','$extra_cols','$wildcard','$empty','$op','$start')</p>\n";
return parent::regular_search($criteria,$only_keys,$order_by,$extra_cols,$wildcard,$empty,$op,$start,$filter);
}*/
/**
* Deprecated methods and class vars for compatibility with the old contacts class
*
* 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
* @param array $fields=null fields to return or null for all stock fields
* @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='')
{
//echo "<p>contacts::old_read($start,$limit,".print_r($fields,true).",$query,'$filter','$sort','$order',$lastmod,$cquery)</p>\n";
$criteria = $sfilter = array();
if ($cquery) $query = $cquery.'*';
if ($query)
{
foreach(array_merge($this->columns_to_search,$this->account_extra_search) as $col)
{
$criteria[$col] = $query['search'];
}
}
foreach(explode(',',$filter) as $expr)
{
list($col,$value) = explode('=',$expr);
$sfilter[$col] = $value;
}
if ($lastmod != -1)
{
$sfilter[] = 'contact_modified > '.(int)$lastmod;
}
if ($order && !strstr($order,'_')) $order = 'contact_'.$order;
if (!$order) $order = 'org_name';
if (is_array($fields))
{
$fields = array_values($fields);
}
//echo '<p>contacts::search('.print_r($criteria,true).','.print_r($fields,true).",'$order $sort','','$wildcard',false,'OR',".(!$limit ? 'false' : "array($start,$limit)").",".print_r($sfilter,true).");</p>\n";
$rows =& $this->search($criteria,$fields,$order.($sort ? ' '.$sort : ''),'',$wildcard,false,'OR',
!$limit ? false : array((int)$start,(int)$limit),$sfilter);
// fix the old birthday format
if ($rows && in_array('bday',$fields))
{
foreach($rows as $n => $row)
{
list($y,$m,$d) = explode('-',$row['bday']);
$rows[$n]['bday'] = sprintf('%d/%d/%04d',$m,$d,$y);
}
}
return $rows;
}
/**
* 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)
{
if (!is_null($$extra))
{
$fields[$extra] = $$extra;
}
}
if(empty($fields['tid']))
{
$fields['tid'] = 'n';
}
$fields['private'] = (int) $fields['access'] == 'private';
unset($fields['id']); // in case it's set
return !$this->save($fields) ? $fields['id'] : false;
}
/**
* 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)
{
if (!is_null($$extra))
{
$fields[$extra] = $$extra;
}
}
if(empty($fields['tid']))
{
$fields['tid'] = 'n';
}
$fields['private'] = (int) $fields['access'] == 'private';
$fields['id'] = $id;
return $id && !$this->save($fields);
}
}

File diff suppressed because it is too large Load Diff

View File

@ -1,768 +0,0 @@
<?php
/**************************************************************************\
* eGroupWare API - Contacts manager for SQL *
* Reworked with new DB-functions by RalfBecker-AT-outdoor-training.de *
* This file written by Joseph Engo <jengo@phpgroupware.org> *
* and Miles Lott <milosch@groupwhere.org> *
* View and manipulate contact records using SQL *
* Copyright (C) 2001 Joseph Engo *
* ------------------------------------------------------------------------ *
* This library is part of the eGroupWare API *
* http://www.egroupware.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 *
\**************************************************************************/
/**
* This class provides a contact database scheme.
* It attempts to be based on the vcard 2.1 standard, with mods as needed
* to make for more reasonable sql storage.
* Note that changes here must also work in the LDAP version.
* Syntax: CreateObject('phpgwapi.contacts');
* Example1: $contacts = CreateObject('phpgwapi.contacts');
*
* The contacts class now uses user-time in all param- and return-values.
* The time stored in the DB is in server-time, as in all other eGW apps.
*
* Last Editor: $Author$
* @class contacts_
* @abstract Contact Management System
* @author jengo/Milosch
* @version $Revision$
* @license LGPL
*/
/* $Id$ */
class contacts_
{
var $db = '';
var $std_table='egw_addressbook';
var $ext_table='egw_addressbook_extra';
/**
* @var int $tz_offset_s offset in secconds between user and server-time,
* it need to be add to a server-time to get the user-time or substracted from a user-time to get the server-time
*/
var $tz_offset_s;
var $account_id = 0;
var $total_records = 0;
var $grants = '';
/* The left side are the array elements used throughout phpgw, right side are the db field names. */
var $stock_contact_fields = array(
'fn' => 'fn',
'n_given' => 'n_given',
'n_family' => 'n_family',
'n_middle' => 'n_middle',
'n_prefix' => 'n_prefix',
'n_suffix' => 'n_suffix',
'sound' => 'sound',
'bday' => 'bday',
'note' => 'note',
'tz' => 'tz',
'geo' => 'geo',
'url' => 'url',
'pubkey' => 'pubkey',
'org_name' => 'org_name',
'org_unit' => 'org_unit',
'title' => 'title',
'adr_one_street' => 'adr_one_street',
'adr_one_locality' => 'adr_one_locality',
'adr_one_region' => 'adr_one_region',
'adr_one_postalcode' => 'adr_one_postalcode',
'adr_one_countryname' => 'adr_one_countryname',
'adr_one_type' => 'adr_one_type',
'label' => 'label',
'adr_two_street' => 'adr_two_street',
'adr_two_locality' => 'adr_two_locality',
'adr_two_region' => 'adr_two_region',
'adr_two_postalcode' => 'adr_two_postalcode',
'adr_two_countryname' => 'adr_two_countryname',
'adr_two_type' => 'adr_two_type',
'tel_work' => 'tel_work',
'tel_home' => 'tel_home',
'tel_voice' => 'tel_voice',
'tel_fax' => 'tel_fax',
'tel_msg' => 'tel_msg',
'tel_cell' => 'tel_cell',
'tel_pager' => 'tel_pager',
'tel_bbs' => 'tel_bbs',
'tel_modem' => 'tel_modem',
'tel_car' => 'tel_car',
'tel_isdn' => 'tel_isdn',
'tel_video' => 'tel_video',
'tel_prefer' => 'tel_prefer',
'email' => 'email',
'email_type' => 'email_type',
'email_home' => 'email_home',
'email_home_type' => 'email_home_type'
);
var $non_contact_fields = array(
'id' => 'id',
'lid' => 'lid',
'tid' => 'tid',
'cat_id' => 'cat_id',
'access' => 'access',
'owner' => 'owner'
);
var $adr_types = array();
/* Used to set preferred number field */
var $tel_types = array(
'work' => 'work',
'home' => 'home',
'voice' => 'voice',
'fax' => 'fax',
'msg' => 'msg',
'cell' => 'cell',
'pager' => 'pager',
'bbs' => 'bbs',
'modem' => 'modem',
'car' => 'car',
'isdn' => 'isdn',
'video' => 'video'
);
/* Used to set email_type fields */
var $email_types = array(
'INTERNET' => 'INTERNET',
'CompuServe' => 'CompuServe',
'AOL' => 'AOL',
'Prodigy' => 'Prodigy',
'eWorld' => 'eWorld',
'AppleLink' => 'AppleLink',
'AppleTalk' => 'AppleTalk',
'PowerShare' => 'PowerShare',
'IBMMail' => 'IBMMail',
'ATTMail' => 'ATTMail',
'MCIMail' => 'MCIMail',
'X.400' => 'X.400',
'TLX' => 'TLX'
);
function contacts_($useacl=True)
{
$this->db = clone($GLOBALS['egw']->db);
$this->db->set_app('phpgwapi');
if($useacl)
{
$this->grants = $GLOBALS['egw']->acl->get_grants('addressbook');
}
$this->account_id = $GLOBALS['egw_info']['user']['account_id'];
/* Used to flag an address as being:
domestic AND/OR international(default)
parcel(default)
postal(default)
*/
$this->adr_types = array(
'dom' => lang('Domestic'),
'intl' => lang('International'),
'parcel' => lang('Parcel'),
'postal' => lang('Postal')
);
if (!is_object($GLOBALS['egw']->datetime))
{
$GLOBALS['egw']->datetime =& CreateObject('phpgwapi.datetime');
}
$this->tz_offset_s = $GLOBALS['egw']->datetime->tz_offset;
}
/* send this the id and whatever fields you want to see */
function read_single_entry($id,$fields='')
{
if (!$fields || empty($fields))
{
$fields = $this->stock_contact_fields;
}
list($stock_fields,$stock_fieldnames,$extra_fields) = $this->split_stock_and_extras($fields);
if (count($stock_fieldnames))
{
$t_fields = ',' . implode(',',$stock_fieldnames);
if ($t_fields == ',')
{
unset($t_fields);
}
}
$this->db->select($this->std_table,'id,lid,tid,owner,access,cat_id'.$t_fields,array('id' => $id),__LINE__,__FILE__);
$this->db->next_record();
$return_fields[0]['id'] = $this->db->f('id');
$return_fields[0]['lid'] = $this->db->f('lid');
$return_fields[0]['tid'] = $this->db->f('tid');
$return_fields[0]['owner'] = $this->db->f('owner');
$return_fields[0]['access'] = $this->db->f('access');
$return_fields[0]['cat_id'] = $this->db->f('cat_id');
$return_fields[0]['rights'] = (int)$this->grants[$this->db->f('owner')];
if(@is_array($stock_fieldnames))
{
foreach($stock_fieldnames as $f_name)
{
$return_fields[0][$f_name] = $this->db->f($f_name);
}
}
/* Setup address type fields for ui forms display */
if ($this->db->f('adr_one_type'))
{
$one_type = $this->db->f('adr_one_type');
foreach($this->adr_types as $name => $val)
{
if (strstr($one_type,$name))
{
$return_fields[0]['one_'.$name] = 'on';
}
}
}
if ($this->db->f('adr_two_type'))
{
$two_type = $this->db->f('adr_two_type');
foreach($this->adr_types as $name => $val)
{
if (strstr($two_type,$name))
{
$return_fields[0]['two_'.$name] = 'on';
}
}
}
$this->db->select($this->ext_table,'contact_name,contact_value',array('contact_id'=>$id),__LINE__,__FILE__);
while ($this->db->next_record())
{
if ($extra_fields[$this->db->f('contact_name')])
{
$return_fields[0][$this->db->f('contact_name')] = $this->db->f('contact_value');
}
}
return $return_fields;
}
// better use get_last_insert_id !!!
function read_last_entry($fields='')
{
$this->db->select($this->std_table,'max(id)',False,__LINE__,__FILE__);
$this->db->next_record();
return $this->read_single_entry($this->db->f(0),$fields);
}
/**
* Searches for contacts meating certain criteria and evtl. return only a range of them
*
* @param int $start=0 starting number of the range, if $limit != 0
* @param int $limit=0 max. number of entries to return, 0=all
* @param array $fields=null fields to return or null for all stock fields
* @param string $query='' search pattern or '' for none
* @param string $filter='' filters with syntax like <name>=<value>,<name2>=<value2> OR <name3>=<value3>,<name4>=!'' 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 read($start=0,$limit=0,$fields=null,$query='',$filter='',$sort='',$order='', $lastmod=-1,$cquery='')
{
if (!$limit)
{
$limit = -1; // db::query uses -1 for all
}
elseif (!$start)
{
$start = 0;
}
if(!$filter) { $filter = 'tid=n'; }
if (!$fields || empty($fields)) { $fields = $this->stock_contact_fields; }
$DEBUG = 0;
list($stock_fields,$stock_fieldnames,$extra_fields) = $this->split_stock_and_extras($fields);
if (count($stock_fieldnames))
{
$t_fields = ',' . implode(',',$stock_fieldnames);
if ($t_fields == ',')
{
unset($t_fields);
}
}
/* turn filter's a=b,c=d OR a=b into an array */
if ($filter)
{
$check_stock = $this->stock_contact_fields + $this->non_contact_fields;
if ($DEBUG) { echo 'DEBUG - Inbound filter is: #'.$filter.'#'; }
$filterlist = array();
foreach(explode(',',$filter) as $pair)
{
list($name,$value) = explode('=',$pair,2);
if (!$name || !isset($check_stock[$name])) // only use valid column-names
{
continue;
}
if ($DEBUG) { echo '<br>DEBUG - Filter intermediate strings 2: #'.$name.'# => #'.$value.'#'; }
if (empty($value))
{
if ($DEBUG) { echo '<br>DEBUG - filter field "'.$name.'" is empty (NULL)'; }
$filterlist[] = $name.' is NULL';
}
else
{
if($name == 'cat_id')
{
if (!(int)$value) continue; // nothing to filter
//$filterlist[] = "(" . $name . " LIKE '%," . (int)$value . ",%' OR " . $name."='".(int)$value."')";
if (!is_object($GLOBALS['egw']->categories))
{
$GLOBALS['egw']->categories = CreateObject('phpgwapi.categories');
}
$cats = $GLOBALS['egw']->categories->return_all_children((int)$value);
foreach($cats as $cat)
{
$cat_filter[] = $this->db->concat("','",cat_id,"','")." LIKE '%,$cat,%'";
}
$filterlist[] = '('.implode(' OR ',$cat_filter).')';
}
elseif(@is_int($value))
{
$filterlist[] = $name . '=' . $value;
}
elseif ($value == "!''") // check for not empty
{
$filterlist[] = $name . "!=''";
}
else
{
$filterlist[] = $name . "='" . $this->db->db_addslashes($value) . "'";
}
}
}
$filterlist = implode(' AND ',$filterlist);
if ($DEBUG)
{
echo '<br>DEBUG - Filter output string: #'.$filterlist.'#';
}
if ($filterlist)
{
$filtermethod = '('.$filterlist.') ';
$fwhere = ' WHERE '; $fand = ' AND ';
}
}
else
{
$filtermethod = " AND (tid='n' OR tid is null)";
}
if (!$filtermethod)
{
if($this->account_id)
{
$fwhere .= ' (owner=' . $this->account_id;
$fand .= ' (owner=' . $this->account_id;
}
}
else
{
if($this->account_id)
{
$fwhere .= $filtermethod . ' AND (owner=' . $this->account_id;
$fand .= $filtermethod . ' AND (owner=' . $this->account_id;
}
else
{
$filtermethod = substr($filtermethod,0,-2);
$fwhere .= $filtermethod;
$fand .= $filtermethod;
}
}
if(@is_array($this->grants))
{
$grants = $this->grants;
foreach($grants as $user => $_right)
{
$public_user_list[] = $user;
}
$fwhere .= " OR (access='public' AND owner in(" . implode(',',$public_user_list) . "))) ";
$fand .= " OR (access='public' AND owner in(" . implode(',',$public_user_list) . "))) ";
}
else
{
$fwhere .= ') '; $fand .= ') ';
}
if ($DEBUG && $filtermethod)
{
echo '<br>DEBUG - Filtering with: #' . $filtermethod . '#';
}
if (!$sort) { $sort = 'ASC'; }
if (!empty($order) && preg_match('/^[a-zA-Z_0-9, ]+$/',$order) && (empty($sort) || preg_match('/^(DESC|ASC|desc|asc)$/',$sort)))
{
$ordermethod = "ORDER BY $order $sort ";
}
else
{
$ordermethod = "ORDER BY n_family,n_given,email ASC";
}
if ($DEBUG && $ordermethod)
{
echo "<br>DEBUG - $ordermethod";
}
if($lastmod >= 0)
{
if (!$fwhere) $fwhere = ' WHERE ';
$fwhere .= " AND last_mod > ".(int)($lastmod - $this->tz_offset_s).' ';
if ($DEBUG)
{
echo "<br>DEBUG - last_mod_filter added to fwhere: $fwhere";
}
}
$filtermethod = '';
if($cquery)
{
$sql = 'SELECT * FROM ' . $this->std_table . ' WHERE (';
$sqlcount = 'SELECT COUNT(id) FROM ' . $this->std_table . ' WHERE (';
foreach(array(
'fn' => 'cn',
'n_family' => 'sn',
'n_given' => 'givenname',
'org_name' => 'o'
) as $f => $x)
{
$cquery = strtoupper($this->db->db_addslashes($cquery));
$sql .= " UPPER($f) LIKE '$cquery%' OR ";
$sqlcount .= " UPPER($f) LIKE '$cquery%' OR ";
}
$sql = substr($sql,0,-3) . ') ' . $fand . $filtermethod . $ordermethod;
$sqlcount = substr($sqlcount,0,-3) . ') ' . $fand . $filtermethod;
unset($f); unset($x);
}
elseif($query)
{
if(is_array($query))
{
$sql = "SELECT * FROM $this->std_table WHERE (";
$sqlcount = "SELECT COUNT(id) FROM $this->std_table WHERE (";
foreach($query as $queryKey => $queryValue)
{
if (!preg_match('/^[a-zA-Z0-9_]+$/',$queryKey))
{
continue; // this can be something nasty
}
// special handling of text columns for certain db's;
if (in_array($f,array('note','pubkey','label')))
{
switch($this->db->Type)
{
case 'sapdb': case 'maxdb':
$queryKey = false; // sapdb cant use LIKE on text/LONG columns
break;
case 'mssql':
$queryKey = "CAST($queryKey AS varchar)"; // mssql cant use UPPER on text columns
break;
}
if (!$queryKey) continue;
}
$queryValue = strtoupper($this->db->db_addslashes($queryValue));
if(empty($queryValue)) {
$sql .= " ($queryKey IS NULL OR $queryKey = '') AND ";
$sqlcount .= " ($queryKey IS NULL OR $queryKey = '') AND ";
} else {
$sql .= " UPPER($queryKey) LIKE '$queryValue' AND ";
$sqlcount .= " UPPER($queryKey) LIKE '$queryValue' AND ";
}
}
$sql = substr($sql,0,-5) . ') ' . $fand . $filtermethod . $ordermethod;
$sqlcount = substr($sqlcount,0,-5) . ') ' . $fand . $filtermethod;
unset($queryKey); unset($queryValue);
}
else
{
$query = strtoupper($this->db->db_addslashes($query));
$sql = "SELECT * FROM $this->std_table WHERE (";
$sqlcount = "SELECT COUNT(*) FROM $this->std_table WHERE (";
foreach($this->stock_contact_fields as $f => $x)
{
// special handling of text columns for certain db's;
if (in_array($f,array('note','pubkey','label')))
{
switch($this->db->Type)
{
case 'sapdb': case 'maxdb':
$f = false; // sapdb cant use LIKE on text/LONG columns
break;
case 'mssql':
$f = "CAST($f AS varchar)"; // mssql cant use UPPER on text columns
break;
}
if (!$f) continue;
}
$sql .= " UPPER($f) LIKE '%$query%' OR ";
$sqlcount .= " UPPER($f) LIKE '%$query%' OR ";
}
$sql = substr($sql,0,-3) . ') ' . $fand . $filtermethod . $ordermethod;
$sqlcount = substr($sqlcount,0,-3) . ') ' . $fand . $filtermethod;
unset($f); unset($x);
}
}
else
{
$sql = "SELECT id,lid,tid,owner,access,cat_id,last_mod $t_fields FROM $this->std_table " . $fwhere
. $filtermethod . ' ' . $ordermethod;
$sqlcount = "SELECT COUNT(*) FROM $this->std_table " . $fwhere . $filtermethod;
}
if($DEBUG)
{
echo '<br>COUNT QUERY' . $sqlcount;
echo '<br>FULL QUERY' . $sql;
}
// $db2 = $this->db;
copyobj($this->db,$db2);
/* Perhaps it is more efficient to count records for this query, which is all we need here */
$this->db->query($sqlcount,__LINE__,__FILE__);
$this->db->next_record();
unset($sqlcount);
$this->total_records = $this->db->f(0);
//echo "<p align=right>search() start=$start, limit=$limit, total=$this->total_records</p>\n";
if ($this->total_records < $start) $start = 0;
$this->db->query($sql,__LINE__,__FILE__,$start,$limit);
$i = 0;
while($this->db->next_record())
{
$return_fields[$i]['id'] = $this->db->f('id');
$return_fields[$i]['lid'] = $this->db->f('lid');
$return_fields[$i]['tid'] = $this->db->f('tid');
$return_fields[$i]['owner'] = $this->db->f('owner');
$return_fields[$i]['access'] = $this->db->f('access');
$return_fields[$i]['cat_id'] = $this->db->f('cat_id');
$return_fields[$i]['last_mod'] = $this->db->f('last_mod')+$this->tz_offset_s;
$return_fields[$i]['rights'] = (int)$this->grants[$this->db->f('owner')];
if(@is_array($stock_fieldnames))
{
foreach($stock_fieldnames as $f_name)
{
$return_fields[$i][$f_name] = $this->db->f($f_name);
}
reset($stock_fieldnames);
}
$db2->select($this->ext_table,'contact_name,contact_value',array('contact_id'=>$this->db->f('id')),__LINE__,__FILE__);
while($db2->next_record())
{
if($extra_fields[$db2->f('contact_name')])
{
$return_fields[$i][$db2->f('contact_name')] = $db2->f('contact_value');
}
}
$i++;
}
return $return_fields;
}
function add($owner,$fields,$access=NULL,$cat_id=NULL,$tid=NULL)
{
$owner = (int)$owner;
// access, cat_id and tid can be in $fields now or as extra params
foreach(array('access','cat_id','tid') as $extra)
{
if (!is_null($$extra))
{
$fields[$extra] = $$extra;
}
}
if(empty($fields['tid']))
{
$fields['tid'] = 'n';
}
// setting the telephone numbers to empty if unset, as otherwise the db-default adds '+1 (000) 000-0000'
// I dont want to change the default in the stable release
foreach($this->stock_contact_fields as $name)
{
if (substr($name,0,4) == 'tel_' && !isset($fields[$name])) $fields[$name] = '';
}
list($stock_fields,$stock_fieldnames,$extra_fields) = $this->split_stock_and_extras($fields);
//this is added here so it is never tainted
$this->stock_contact_fields['last_mod'] = 'last_mod';
$stock_fields['last_mod'] = time();
$data = array(
'owner' => $owner,
'access' => $fields['access'],
'cat_id' => $fields['cat_id'],
'tid' => $fields['tid'],
);
if (isset($fields['lid'])) $data['lid'] = $fields['lid'];
$this->db->insert($this->std_table,array_merge($data,$stock_fields),False,__LINE__,__FILE__);
$id = $this->db->get_last_insert_id($this->std_table, 'id');
if($id && count($extra_fields))
{
foreach($extra_fields as $name => $value)
{
$this->db->insert($this->ext_table,array(
'contact_id' => $id,
'contact_owner' => $owner,
'contact_name' => $name,
'contact_value' => $value,
),False,__LINE__,__FILE__);
}
}
return ($id ? $id : False);
}
function field_exists($id,$field_name)
{
$this->db->select($this->ext_table,'COUNT(*)',array(
'contact_id' => $id,
'contact_name' => $field_name,
),__LINE__,__FILE__);
$this->db->next_record();
return $this->db->f(0);
}
function add_single_extra_field($id,$owner,$field_name,$field_value)
{
$this->db->insert($this->ext_table,array(
'contact_id' => $id,
'contact_owner' => $owner,
'contact_name' => $field_name,
'contact_value' => $field_value,
),False,__LINE__,__FILE__);
}
function delete_single_extra_field($id,$field_name)
{
$this->db->delete($this->ext_table,array(
'contact_id' => $id,
'contact_name' => $field_name,
),__LINE__,__FILE__);
}
function update($id,$owner,$fields,$access=NULL,$cat_id=NULL,$tid=NULL)
{
/* First make sure that id number exists */
$this->db->select($this->std_table,'COUNT(*)',array('id'=>$id),__LINE__,__FILE__);
$this->db->next_record();
if (!$this->db->f(0))
{
return False;
}
list($stock_fields,,$extra_fields) = $this->split_stock_and_extras($fields);
// access, cat_id and tid can be in $fields now or as extra params
foreach(array('access','cat_id','tid','owner') as $extra)
{
if (!is_null($$extra))
{
$fields[$extra] = $$extra;
}
if (isset($fields[$extra]))
{
$stock_fields[$extra] = $fields[$extra];
}
}
if (count($stock_fields))
{
$stock_fields['last_mod'] = time();
$this->db->update($this->std_table,$stock_fields,array('id'=>$id),__LINE__,__FILE__);
}
if (is_array($extra_fields))
{
foreach($extra_fields as $x_name => $x_value)
{
if ($this->field_exists($id,$x_name))
{
if (!$x_value)
{
$this->delete_single_extra_field($id,$x_name);
}
else
{
$this->db->update($this->ext_table,array(
'contact_value' => $x_value,
'contact_owner' => $owner,
),array(
'contact_name' => $x_name,
'contact_id' => $id,
),__LINE__,__FILE__);
}
}
elseif($x_value) // dont write emtpy extra-fields
{
$this->add_single_extra_field($id,$owner,$x_name,$x_value);
}
}
}
return True;
}
/* Used by admin to change ownership on account delete */
function change_owner($old_owner,$new_owner)
{
if (!(int)$new_owner || !(int)$old_owner)
{
return False;
}
$this->db->update($this->std_table,array('owner'=>$new_owner),array('owner'=>$old_owner),__LINE__,__FILE__);
$this->db->update($this->ext_table,array('contact_owner'=>$new_owner),array('contact_owner'=>$old_owner),__LINE__,__FILE__);
}
/* This is where the real work of delete() is done, shared class file contains calling function */
function delete_($id)
{
$this->db->delete($this->std_table,array('id'=>$id),__LINE__,__FILE__);
$this->db->delete($this->ext_table,array('contact_id'=>$id),__LINE__,__FILE__);
}
/* This is for the admin script deleteaccount.php */
function delete_all($owner=0)
{
if ($owner)
{
$this->db->delete($this->std_table,array('owner'=>$owner),__LINE__,__FILE__);
$this->db->delete($this->ext_table,array('contact_owner'=>$owner),__LINE__,__FILE__);
}
}
}
?>