egroupware_official/phpgwapi/inc/class.contacts_sql.inc.php

743 lines
22 KiB
PHP
Raw Normal View History

2001-07-05 09:37:36 +02:00
<?php
/**************************************************************************\
2004-05-05 14:06:13 +02:00
* 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> *
2004-05-05 14:06:13 +02:00
* and Miles Lott <milosch@groupwhere.org> *
* View and manipulate contact records using SQL *
* Copyright (C) 2001 Joseph Engo *
* ------------------------------------------------------------------------ *
2004-05-05 14:06:13 +02:00
* 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');
*
* Last Editor: $Author$
* @class contacts_
* @abstract Contact Management System
* @author jengo/Milosch
* @version $Revision$
* @license LGPL
2001-07-05 09:37:36 +02:00
*/
/* $Id$ */
2001-07-05 09:37:36 +02:00
class contacts_
{
var $db = '';
2001-07-05 09:37:36 +02:00
var $std_table='phpgw_addressbook';
var $ext_table='phpgw_addressbook_extra';
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'
);
2001-09-06 01:19:06 +02:00
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'
);
2001-07-05 09:37:36 +02:00
function contacts_($useacl=True)
{
// $this->db = $GLOBALS['phpgw']->db;
copyobj($GLOBALS['phpgw']->db,$this->db);
$this->db->set_app('phpgwapi');
2001-07-05 09:37:36 +02:00
if($useacl)
{
2001-08-23 08:55:12 +02:00
$this->grants = $GLOBALS['phpgw']->acl->get_grants('addressbook');
2001-07-05 09:37:36 +02:00
}
2001-08-23 08:55:12 +02:00
$this->account_id = $GLOBALS['phpgw_info']['user']['account_id'];
2001-09-06 01:19:06 +02:00
/* 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')
);
2001-07-05 09:37:36 +02:00
}
/* send this the id and whatever fields you want to see */
function read_single_entry($id,$fields='')
2001-07-05 09:37:36 +02:00
{
if (!$fields || empty($fields))
{
$fields = $this->stock_contact_fields;
}
list($stock_fields,$stock_fieldnames,$extra_fields) = $this->split_stock_and_extras($fields);
2001-07-05 09:37:36 +02:00
if (count($stock_fieldnames))
2001-07-05 09:37:36 +02:00
{
$t_fields = ',' . implode(',',$stock_fieldnames);
if ($t_fields == ',')
2001-07-05 09:37:36 +02:00
{
unset($t_fields);
}
}
$this->db->select($this->std_table,'id,lid,tid,owner,access,cat_id'.$t_fields,array('id' => $id),__LINE__,__FILE__);
2001-07-05 09:37:36 +02:00
$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')];
2001-07-05 09:37:36 +02:00
2003-12-10 12:36:54 +01:00
if(@is_array($stock_fieldnames))
2001-07-05 09:37:36 +02:00
{
foreach($stock_fieldnames as $f_name)
2001-07-05 09:37:36 +02:00
{
$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'))
2001-07-05 09:37:36 +02:00
{
$one_type = $this->db->f('adr_one_type');
foreach($this->adr_types as $name => $val)
2001-07-05 09:37:36 +02:00
{
if (strstr($one_type,$name)) $return_fields[0]['one_'.$name] = 'on';
2001-07-05 09:37:36 +02:00
}
}
if ($this->db->f('adr_two_type'))
2001-07-05 09:37:36 +02:00
{
$two_type = $this->db->f('adr_two_type');
foreach($this->adr_types as $name => $val)
2001-07-05 09:37:36 +02:00
{
if (strstr($two_type,$name)) $return_fields[0]['two_'.$name] = 'on';
2001-07-05 09:37:36 +02:00
}
}
$this->db->select($this->ext_table,'contact_name,contact_value',array('contact_id'=>$id),__LINE__,__FILE__);
while ($this->db->next_record())
2001-07-05 09:37:36 +02:00
{
if ($extra_fields[$this->db->f('contact_name')])
2001-07-05 09:37:36 +02:00
{
$return_fields[0][$this->db->f('contact_name')] = $this->db->f('contact_value');
2001-07-05 09:37:36 +02:00
}
}
return $return_fields;
}
// better use get_last_insert_id !!!
2001-07-05 09:37:36 +02:00
function read_last_entry($fields='')
{
$this->db->select($this->std_table,'max(id)',False,__LINE__,__FILE__);
2001-07-05 09:37:36 +02:00
$this->db->next_record();
return $this->read_single_entry($this->db->f(0),$fields);
2001-07-05 09:37:36 +02:00
}
/* send this the range, query, sort, order and whatever fields you want to see */
function read($start=0,$limit=0,$fields='',$query='',$filter='',$sort='',$order='', $lastmod=-1,$cquery='')
2001-07-05 09:37:36 +02:00
{
2001-08-23 08:55:12 +02:00
if(!$start) { $start = 0; }
if(!$limit) { $limit = 0; }
if(!$filter) { $filter = 'tid=n'; }
2001-07-05 09:37:36 +02:00
if (!$fields || empty($fields)) { $fields = $this->stock_contact_fields; }
2001-07-05 09:37:36 +02:00
$DEBUG = 0;
list($stock_fields,$stock_fieldnames,$extra_fields) = $this->split_stock_and_extras($fields);
if (count($stock_fieldnames))
2001-07-05 09:37:36 +02:00
{
$t_fields = ',' . implode(',',$stock_fieldnames);
if ($t_fields == ',')
2001-07-05 09:37:36 +02:00
{
unset($t_fields);
}
}
/* turn filter's a=b,c=d OR a=b into an array */
if ($filter)
2001-07-05 09:37:36 +02:00
{
$check_stock = $this->stock_contact_fields + $this->non_contact_fields;
if ($DEBUG) { echo 'DEBUG - Inbound filter is: #'.$filter.'#'; }
2004-04-04 22:50:29 +02:00
$filterlist = array();
foreach(explode(',',$filter) as $pair)
2001-07-05 09:37:36 +02:00
{
2004-04-04 22:50:29 +02:00
list($name,$value) = explode('=',$pair,2);
if (!$name || !isset($check_stock[$name])) // only use valid column-names
2001-07-05 09:37:36 +02:00
{
2004-04-04 22:50:29 +02:00
continue;
2001-07-05 09:37:36 +02:00
}
if ($DEBUG) { echo '<br>DEBUG - Filter intermediate strings 2: #'.$name.'# => #'.$value.'#'; }
2004-04-04 22:50:29 +02:00
if (empty($value))
2001-07-05 09:37:36 +02:00
{
if ($DEBUG) { echo '<br>DEBUG - filter field "'.$name.'" is empty (NULL)'; }
2004-04-04 22:50:29 +02:00
$filterlist[] = $name.' is NULL';
2001-07-05 09:37:36 +02:00
}
2004-04-04 22:50:29 +02:00
else
2001-07-05 09:37:36 +02:00
{
2004-04-04 22:50:29 +02:00
if($name == 'cat_id')
2001-07-05 09:37:36 +02:00
{
if (!(int)$value) continue; // nothing to filter
//$filterlist[] = "(" . $name . " LIKE '%," . (int)$value . ",%' OR " . $name."='".(int)$value."')";
if (!is_object($GLOBALS['phpgw']->categories))
{
$GLOBALS['phpgw']->categories = CreateObject('phpgwapi.categories');
}
$cats = $GLOBALS['phpgw']->categories->return_all_children((int)$value);
$cat_filter = "(cat_id IN ('".implode("','",$cats)."')";
foreach($cats as $cat)
{
$cat_filter .= " OR cat_id LIKE '%,$cat,%'";
}
$cat_filter .= ')';
$filterlist[] = $cat_filter;
2004-04-04 22:50:29 +02:00
}
elseif(@is_int($value))
{
$filterlist[] = $name . '=' . $value;
}
elseif ($value == "!''") // check for not empty
{
$filterlist[] = $name . "!=''";
}
else
{
$filterlist[] = $name . "='" . $this->db->db_addslashes($value) . "'";
2001-07-05 09:37:36 +02:00
}
}
}
2004-04-04 22:50:29 +02:00
$filterlist = implode(' AND ',$filterlist);
if ($DEBUG)
2001-07-05 09:37:36 +02:00
{
echo '<br>DEBUG - Filter output string: #'.$filterlist.'#';
2001-07-05 09:37:36 +02:00
}
if ($filterlist)
2001-07-05 09:37:36 +02:00
{
$filtermethod = '('.$filterlist.') ';
$fwhere = ' WHERE '; $fand = ' AND ';
}
}
else
{
$filtermethod = " AND (tid='n' OR tid is null)";
}
if (!$filtermethod)
2001-07-05 09:37:36 +02:00
{
if($this->account_id)
2001-07-05 09:37:36 +02:00
{
$fwhere .= ' (owner=' . $this->account_id;
$fand .= ' (owner=' . $this->account_id;
2001-07-05 09:37:36 +02:00
}
}
else
{
if($this->account_id)
2001-07-05 09:37:36 +02:00
{
$fwhere .= $filtermethod . ' AND (owner=' . $this->account_id;
$fand .= $filtermethod . ' AND (owner=' . $this->account_id;
2001-07-05 09:37:36 +02:00
}
else
{
$filtermethod = substr($filtermethod,0,-2);
$fwhere .= $filtermethod;
$fand .= $filtermethod;
}
}
2003-12-10 12:36:54 +01:00
if(@is_array($this->grants))
2001-07-05 09:37:36 +02:00
{
$grants = $this->grants;
foreach($grants as $user => $_right)
2001-07-05 09:37:36 +02:00
{
$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)
2001-07-05 09:37:36 +02:00
{
echo '<br>DEBUG - Filtering with: #' . $filtermethod . '#';
2001-07-05 09:37:36 +02:00
}
if (!$sort) { $sort = 'ASC'; }
2001-07-05 09:37:36 +02:00
2004-04-04 22:50:29 +02:00
if (!empty($order) && preg_match('/^[a-zA-Z_0-9, ]+$/',$order) && (empty($sort) || preg_match('/^(DESC|ASC|desc|asc)$/',$sort)))
2001-07-05 09:37:36 +02:00
{
2001-08-23 08:55:12 +02:00
$ordermethod = "ORDER BY $order $sort ";
2001-07-05 09:37:36 +02:00
}
else
{
2004-04-04 22:50:29 +02:00
$ordermethod = "ORDER BY n_family,n_given,email ASC";
2001-07-05 09:37:36 +02:00
}
if ($DEBUG && $ordermethod)
2001-07-05 09:37:36 +02:00
{
echo "<br>DEBUG - $ordermethod";
}
if($lastmod >= 0 && $fwhere)
{
2004-04-04 22:50:29 +02:00
$fwhere .= " AND last_mod > ".(int)$lastmod.' ';
}
elseif($lastmod >= 0)
{
2004-04-04 22:50:29 +02:00
$fwhere = " WHERE last_mod > ".(int)$lastmod.' ';
}
if ($DEBUG && $last_mod_filter && $fwhere)
{
echo "<br>DEBUG - last_mod_filter added to fwhere: $fwhere";
}
$filtermethod = '';
if($cquery)
{
2004-04-04 22:50:29 +02:00
$sql = 'SELECT * FROM ' . $this->std_table . ' WHERE (';
$sqlcount = 'SELECT COUNT(id) FROM ' . $this->std_table . ' WHERE (';
foreach(array(
'fn' => 'cn',
'n_family' => 'sn',
'org_name' => 'o'
2004-04-04 22:50:29 +02:00
) as $f => $x)
{
2004-04-04 22:50:29 +02:00
$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)
2001-07-05 09:37:36 +02:00
{
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)
{
2004-04-04 22:50:29 +02:00
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;
}
2004-04-04 22:50:29 +02:00
$queryValue = strtoupper($this->db->db_addslashes($queryValue));
$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
{
2004-04-04 22:50:29 +02:00
$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;
}
2004-04-04 22:50:29 +02:00
$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);
}
2001-07-05 09:37:36 +02:00
}
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;
2001-07-05 09:37:36 +02:00
}
2003-10-19 10:12:16 +02:00
if($DEBUG)
{
echo '<br>COUNT QUERY' . $sqlcount;
echo '<br>FULL QUERY' . $sql;
}
2001-07-05 09:37:36 +02:00
// $db2 = $this->db;
copyobj($this->db,$db2);
2001-09-19 17:49:57 +02:00
/* Perhaps it is more efficient to count records for this query, which is all we need here */
$this->db->query($sqlcount,__LINE__,__FILE__);
2003-10-19 10:12:16 +02:00
$this->db->next_record();
unset($sqlcount);
2003-10-19 10:12:16 +02:00
$this->total_records = $this->db->f(0);
2001-07-05 09:37:36 +02:00
if($start && $limit)
2001-07-05 09:37:36 +02:00
{
if($this->total_records <= $limit)
{
$this->db->query($sql,__LINE__,__FILE__);
}
else
{
$this->db->limit_query($sql,$start,__LINE__,__FILE__,$limit);
}
2001-07-05 09:37:36 +02:00
}
elseif(!$limit)
2001-07-05 09:37:36 +02:00
{
$this->db->query($sql,__LINE__,__FILE__);
}
else
{
$this->db->limit_query($sql,$start,__LINE__,__FILE__);
}
$i = 0;
while($this->db->next_record())
2001-07-05 09:37:36 +02:00
{
$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');
$return_fields[$i]['rights'] = (int)$this->grants[$this->db->f('owner')];
2001-07-05 09:37:36 +02:00
if(@is_array($stock_fieldnames))
2001-07-05 09:37:36 +02:00
{
foreach($stock_fieldnames as $f_name)
2001-07-05 09:37:36 +02:00
{
$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())
2001-07-05 09:37:36 +02:00
{
if($extra_fields[$db2->f('contact_name')])
2001-07-05 09:37:36 +02:00
{
2001-09-19 17:49:57 +02:00
$return_fields[$i][$db2->f('contact_name')] = $db2->f('contact_value');
2001-07-05 09:37:36 +02:00
}
}
$i++;
}
return $return_fields;
}
function add($owner,$fields,$access=NULL,$cat_id=NULL,$tid=NULL)
2001-07-05 09:37:36 +02:00
{
2003-12-10 12:36:54 +01:00
$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 following fields to empty if unset, as the addressbook does not seem to set them anymore and I dont want to change the db-default atm.
foreach(array('tel_voice','tel_bbs','tel_modem') as $name)
2001-07-05 09:37:36 +02:00
{
if (!isset($fields[$name])) $fields[$name] = '';
2001-07-05 09:37:36 +02:00
}
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'] = $GLOBALS['phpgw']->datetime->gmtnow;
$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__);
2001-07-05 09:37:36 +02:00
2003-12-17 12:22:37 +01:00
$id = $this->db->get_last_insert_id($this->std_table, 'id');
2001-07-05 09:37:36 +02:00
if($id && count($extra_fields))
2001-07-05 09:37:36 +02:00
{
foreach($extra_fields as $name => $value)
2001-07-05 09:37:36 +02:00
{
$this->db->insert($this->ext_table,array(
'contact_id' => $id,
'contact_owner' => $owner,
'contact_name' => $name,
'contact_value' => $value,
),False,__LINE__,__FILE__);
2001-07-05 09:37:36 +02:00
}
}
return ($id ? $id : False);
2001-07-05 09:37:36 +02:00
}
function field_exists($id,$field_name)
{
$this->db->select($this->ext_table,'COUNT(*)',array(
'contact_id' => $id,
'contact_name' => $field_name,
),__LINE__,__FILE__);
2001-07-05 09:37:36 +02:00
$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__);
2001-07-05 09:37:36 +02:00
}
function delete_single_extra_field($id,$field_name)
{
$this->db->delete($this->ext_table,array(
'contact_id' => $id,
'contact_name' => $field_name,
),__LINE__,__FILE__);
2001-07-05 09:37:36 +02:00
}
2003-10-24 22:42:00 +02:00
function update($id,$owner,$fields,$access=NULL,$cat_id=NULL,$tid=NULL)
2001-07-05 09:37:36 +02:00
{
/* First make sure that id number exists */
$this->db->select($this->std_table,'COUNT(*)',array('id'=>$id),__LINE__,__FILE__);
2001-07-05 09:37:36 +02:00
$this->db->next_record();
if (!$this->db->f(0))
2001-07-05 09:37:36 +02:00
{
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
2003-10-24 22:42:00 +02:00
foreach(array('access','cat_id','tid') as $extra)
{
if (!is_null($$extra))
{
$fields[$extra] = $$extra;
}
if (isset($fields[$extra]))
{
$stock_fields[$extra] = $fields[$extra];
}
}
if (count($stock_fields))
2001-07-05 09:37:36 +02:00
{
$stock_fields['last_mod'] = $GLOBALS['phpgw']->datetime->gmtnow;
$this->db->update($this->std_table,$stock_fields,array('id'=>$id),__LINE__,__FILE__);
2001-07-05 09:37:36 +02:00
}
2004-03-06 21:34:56 +01:00
if (is_array($extra_fields))
2001-07-05 09:37:36 +02:00
{
2004-03-06 21:34:56 +01:00
foreach($extra_fields as $x_name => $x_value)
2001-07-05 09:37:36 +02:00
{
2004-03-06 21:34:56 +01:00
if ($this->field_exists($id,$x_name))
2001-07-05 09:37:36 +02:00
{
2004-03-06 21:34:56 +01:00
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__);
2004-03-06 21:34:56 +01:00
}
2001-07-05 09:37:36 +02:00
}
2004-03-06 21:34:56 +01:00
elseif($x_value) // dont write emtpy extra-fields
2001-07-05 09:37:36 +02:00
{
2004-03-06 21:34:56 +01:00
$this->add_single_extra_field($id,$owner,$x_name,$x_value);
2001-07-05 09:37:36 +02:00
}
}
}
2004-03-07 10:53:26 +01:00
return True;
2001-07-05 09:37:36 +02:00
}
/* Used by admin to change ownership on account delete */
2004-04-04 22:50:29 +02:00
function change_owner($old_owner,$new_owner)
2001-07-05 09:37:36 +02:00
{
if (!(int)$new_owner || !(int)$old_owner)
2001-07-05 09:37:36 +02:00
{
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('owner'=>$old_owner),__LINE__,__FILE__);
2001-07-05 09:37:36 +02:00
}
/* 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__);
2001-07-05 09:37:36 +02:00
}
/* This is for the admin script deleteaccount.php */
function delete_all($owner=0)
{
if ($owner)
2001-07-05 09:37:36 +02:00
{
$this->db->delete($this->std_table,array('owner'=>$owner),__LINE__,__FILE__);
$this->db->delete($this->ext_table,array('contact_owner'=>$owner),__LINE__,__FILE__);
2001-07-05 09:37:36 +02:00
}
}
}
?>