egroupware/phpgwapi/inc/class.contacts_sql.inc.php

767 lines
23 KiB
PHP
Raw Normal View History

2001-07-05 09:37:36 +02:00
<?php
/**************************************************************************\
* phpGroupWare API - Contacts manager for SQL *
* This file written by Joseph Engo <jengo@phpgroupware.org> *
* and Miles Lott <milosch@phpgroupware.org> *
* View and manipulate contact records using SQL *
* Copyright (C) 2001 Joseph Engo *
* ------------------------------------------------------------------------ *
2001-07-05 09:37:36 +02:00
* This library is part of the phpGroupWare API *
* http://www.phpgroupware.org/api *
2001-07-05 09:37:36 +02:00
* ------------------------------------------------------------------------ *
* 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$ */
/*!
@class contacts_
@abstract Contact Management System
@discussion Author: jengo/Milosch <br>
This class provides a contact database scheme. <br>
It attempts to be based on the vcard 2.1 standard, with mods as needed to make for more reasonable sql storage. <br>
Note that changes here must also work in the LDAP version. <br>
Syntax: CreateObject('phpgwapi.contacts'); <br>
Example1: $contacts = CreateObject('phpgwapi.contacts');
*/
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);
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->query("SELECT id,lid,tid,owner,access,cat_id $t_fields FROM $this->std_table WHERE id=" . (int)$id);
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
{
eval("if (strstr(\$one_type,\$name)) { \$return_fields[0][\"one_\$name\"] = \"on\"; }");
}
}
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
{
eval("if (strstr(\$two_type,\$name)) { \$return_fields[0][\"two_\$name\"] = \"on\"; }");
}
}
$this->db->query("SELECT contact_name,contact_value FROM $this->ext_table WHERE contact_id=" . (int)$this->db->f('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;
}
function read_last_entry($fields='')
{
if (!$fields || empty($fields)) { $fields = $this->stock_contact_fields; }
2001-07-05 09:37:36 +02:00
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);
}
}
$this->db->query('SELECT max(id) FROM '.$this->std_table,__LINE__,__FILE__);
$this->db->next_record();
$id = $this->db->f(0);
$this->db->query("SELECT id,lid,tid,owner,access,cat_id $t_fields FROM $this->std_table WHERE id=" . (int)$id,__LINE__,__FILE__);
2001-07-05 09:37:36 +02:00
$this->db->next_record();
$return_fields[0]['id'] = $this->db->f('id');
2001-07-05 09:37:36 +02:00
$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
{
eval("if (strstr(\$one_type,\$name)) { \$return_fields[0][\"one_\$name\"] = \"on\"; }");
}
}
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
{
eval("if (strstr(\$two_type,\$name)) { \$return_fields[0][\"two_\$name\"] = \"on\"; }");
}
}
$this->db->query("SELECT contact_name,contact_value FROM $this->ext_table WHERE contact_id=" . (int)$this->db->f('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;
}
/* 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.'#'; }
2001-07-05 09:37:36 +02:00
$filterarray = split(',',$filter);
if ($filterarray[1])
2001-07-05 09:37:36 +02:00
{
$i=0;
for ($i=0;$i<count($filterarray);$i++)
2001-07-05 09:37:36 +02:00
{
list($name,$value) = split('=',$filterarray[$i]);
if ($name)
2001-07-05 09:37:36 +02:00
{
if ($DEBUG) { echo '<br>DEBUG - Filter intermediate strings 1: #'.$name.'# => #'.$value.'#'; }
2001-07-05 09:37:36 +02:00
$filterfields[$name] = $value;
}
}
}
else
{
list($name,$value) = split('=',$filter);
if ($DEBUG)
2001-07-05 09:37:36 +02:00
{
echo '<br>DEBUG - Filter intermediate strings 1: #'.$name.'# => #'.$value.'#';
}
$filterfields = array($name => $value);
}
/* now check each element of the array and convert into SQL for queries below */
$i = 0;
foreach($filterfields as $name => $value)
2001-07-05 09:37:36 +02:00
{
if ($DEBUG) { echo '<br>DEBUG - Filter intermediate strings 2: #'.$name.'# => #'.$value.'#'; }
2001-07-05 09:37:36 +02:00
$isstd=0;
if ($name && empty($value))
2001-07-05 09:37:36 +02:00
{
if ($DEBUG) { echo '<br>DEBUG - filter field "'.$name.'" is empty (NULL)'; }
foreach($check_stock as $fname => $fvalue)
2001-07-05 09:37:36 +02:00
{
if($fvalue == $name)
2001-07-05 09:37:36 +02:00
{
$filterlist .= $name.' is NULL,';
if ($DEBUG) { echo '<br>DEBUG - filter field "'.$name.'" is a stock field'; }
2001-07-05 09:37:36 +02:00
break;
}
}
}
elseif($name && $value)
{
foreach($check_stock as $fname => $fvalue)
2001-07-05 09:37:36 +02:00
{
if($fvalue == $name)
2001-07-05 09:37:36 +02:00
{
if($name == 'cat_id')
2001-07-05 09:37:36 +02:00
{
$filterlist .= "(" . $name . " LIKE '%," . $value . ",%' OR " . $name."='".$value."');";
2001-07-05 09:37:36 +02:00
}
2003-12-10 12:36:54 +01:00
elseif(@is_int($value))
2001-07-05 09:37:36 +02:00
{
$filterlist .= $name . '=' . $value . ';';
2001-07-05 09:37:36 +02:00
}
elseif ($value == "!''") // check for not empty
{
$filterlist .= $name . "!='';";
}
2001-07-05 09:37:36 +02:00
else
{
$filterlist .= $name . "='" . $value . "';";
2001-07-05 09:37:36 +02:00
}
break;
}
}
}
$i++;
}
$filterlist = substr($filterlist,0,-1);
2003-12-10 12:36:54 +01:00
$filterlist = str_replace(';',' 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;
}
reset($public_user_list);
$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
if ($order)
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
{
2001-08-23 08:55:12 +02:00
$ordermethod = "ORDER BY n_family,n_given,email $sort";
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)
{
$fwhere .= " AND last_mod > $lastmod ";
}
elseif($lastmod >= 0)
{
$fwhere = " WHERE last_mod > $lastmod ";
}
if ($DEBUG && $last_mod_filter && $fwhere)
{
echo "<br>DEBUG - last_mod_filter added to fwhere: $fwhere";
}
$filtermethod = '';
if($cquery)
{
$cfields = array(
'fn' => 'cn',
'n_family' => 'sn',
'org_name' => 'o'
);
$sql = 'SELECT * FROM ' . $this->std_table . ' WHERE (';
$sqlcount = 'SELECT COUNT(id) FROM ' . $this->std_table . ' WHERE (';
while(list($f,$x) = each($cfields))
{
$sql .= " UPPER($f) LIKE UPPER('$cquery%') OR ";
$sqlcount .= " UPPER($f) LIKE UPPER('$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
{
2003-12-10 12:36:54 +01:00
$query = str_replace("'",'',$query);
$query = str_replace('"','',$query);
$sql = "SELECT * FROM $this->std_table WHERE (";
$sqlcount = "SELECT COUNT(id) FROM $this->std_table WHERE (";
foreach($this->stock_contact_fields as $f => $x)
{
2002-08-22 14:22:58 +02:00
$sql .= " UPPER($f) LIKE UPPER('%$query%') OR ";
$sqlcount .= " UPPER($f) LIKE UPPER('%$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 $t_fields FROM $this->std_table " . $fwhere
. $filtermethod . ' ' . $ordermethod;
$sqlcount = "SELECT COUNT(id) FROM $this->std_table " . $fwhere
2003-10-19 20:29:44 +02:00
. $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->query("SELECT contact_name,contact_value FROM $this->ext_table WHERE contact_id="
. (int)$this->db->f('id') . $filterextra,__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;
2003-12-17 12:22:37 +01:00
$lid = array();
// 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';
}
2003-12-17 12:22:37 +01:00
if(isset($fields['lid']))
2001-07-05 09:37:36 +02:00
{
//fix by pim
//$lid = array('lid,' => $fields['lid']."','");
2003-12-17 12:22:37 +01:00
$lid = array('lid,', $fields['lid'] . "','");
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;
2003-12-17 12:22:37 +01:00
$sql = 'INSERT INTO ' . $this->std_table . " (owner,access,cat_id,tid," . $lid[0]
. implode(',',$this->stock_contact_fields)
. ') VALUES (' . $owner . ",'" . $fields['access'] . "','" . $fields['cat_id']
. "','" . $fields['tid'] . "','" . $lid[1]
. implode("','",$this->loop_addslashes($stock_fields)) . "')";
$this->db->query($sql,__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(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->query("INSERT INTO $this->ext_table VALUES (" . (int)$id . ",'" . $owner . "','"
2001-09-17 04:26:46 +02:00
. $this->db->db_addslashes($name) . "','" . $this->db->db_addslashes($value) . "')",__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->query("SELECT COUNT(*) FROM $this->ext_table WHERE contact_id=" . (int)$id . " AND contact_name='"
2001-09-17 04:26:46 +02:00
. $this->db->db_addslashes($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->query("INSERT INTO $this->ext_table VALUES (" . (int)$id . ",'$owner','" . $this->db->db_addslashes($field_name)
2001-09-17 04:26:46 +02:00
. "','" . $this->db->db_addslashes($field_value) . "')",__LINE__,__FILE__);
2001-07-05 09:37:36 +02:00
}
function delete_single_extra_field($id,$field_name)
{
$this->db->query("DELETE FROM $this->ext_table WHERE contact_id=" . (int)$id . " AND contact_name='"
2001-09-17 04:26:46 +02:00
. $this->db->db_addslashes($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
{
2003-12-10 12:36:54 +01:00
$owner = (int)$owner;
$id = (int)$id;
2001-07-05 09:37:36 +02:00
/* First make sure that id number exists */
$this->db->query("SELECT COUNT(*) FROM $this->std_table WHERE 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
{
foreach($stock_fields as $name => $value)
2001-07-05 09:37:36 +02:00
{
$ta[] = $name . "='" . $this->db->db_addslashes($value) . "'";
2001-07-05 09:37:36 +02:00
}
2003-10-24 22:42:00 +02:00
$ta[] = 'last_mod=' . $GLOBALS['phpgw']->datetime->gmtnow;
$fields_s = implode(',',$ta);
if ($field_s == ',')
2001-07-05 09:37:36 +02:00
{
unset($field_s);
}
$this->db->query($sql="UPDATE $this->std_table SET $fields_s WHERE "
. "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->query("UPDATE $this->ext_table SET contact_value='" . $this->db->db_addslashes($x_value)
. "',contact_owner=$owner WHERE contact_name='" . $this->db->db_addslashes($x_name)
. "' AND contact_id=" . (int)$id,__LINE__,__FILE__);
}
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
}
}
}
}
/* Used by admin to change ownership on account delete */
function change_owner($old_owner='',$new_owner='')
{
if (!($new_owner && $old_owner))
2001-07-05 09:37:36 +02:00
{
return False;
}
$this->db->query("UPDATE $this->std_table SET owner='$new_owner' WHERE owner=$old_owner",__LINE__,__FILE__);
$this->db->query("UPDATE $this->ext_table SET contact_owner='$new_owner' WHERE contact_owner=$old_owner",__LINE__,__FILE__);
return;
}
/* This is where the real work of delete() is done, shared class file contains calling function */
function delete_($id)
{
$this->db->query("DELETE FROM $this->std_table WHERE id=" . (int)$id,__LINE__,__FILE__);
$this->db->query("DELETE FROM $this->ext_table WHERE contact_id=" . (int)$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->query("DELETE FROM $this->std_table WHERE owner=$owner",__LINE__,__FILE__);
$this->db->query("DELETE FROM $this->ext_table WHERE contact_owner=$owner",__LINE__,__FILE__);
}
return;
}
}
?>