2001-02-02 08:00:58 +01:00
|
|
|
<?php
|
2001-02-10 13:56:49 +01:00
|
|
|
/**************************************************************************\
|
|
|
|
* phpGroupWare API - Accounts manager for SQL *
|
|
|
|
* This file written by Joseph Engo <jengo@phpgroupware.org> *
|
|
|
|
* View and manipulate contact records using SQL *
|
|
|
|
* Copyright (C) 2001 Joseph Engo *
|
|
|
|
* -------------------------------------------------------------------------*
|
|
|
|
* This library is part of the phpGroupWare API *
|
|
|
|
* http://www.phpgroupware.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 *
|
|
|
|
\**************************************************************************/
|
2001-02-02 08:00:58 +01:00
|
|
|
|
2001-02-10 13:56:49 +01:00
|
|
|
/* $Id$ */
|
2001-02-02 08:00:58 +01:00
|
|
|
|
2001-05-01 11:32:52 +02:00
|
|
|
/*!
|
2001-03-16 04:03:42 +01:00
|
|
|
@class acl
|
|
|
|
@abstract Contact List 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>
|
|
|
|
Syntax: CreateObject('phpgwapi.contacts'); <br>
|
|
|
|
Example1: $contacts = CreateObject('phpgwapi.contacts');
|
2001-02-10 13:56:49 +01:00
|
|
|
*/
|
|
|
|
class contacts_
|
|
|
|
{
|
|
|
|
var $db;
|
2001-05-07 07:04:13 +02:00
|
|
|
var $std_table='phpgw_addressbook';
|
|
|
|
var $ext_table='phpgw_addressbook_extra';
|
2001-02-21 23:41:37 +01:00
|
|
|
|
2001-02-10 13:56:49 +01:00
|
|
|
var $account_id;
|
2001-03-26 01:21:01 +02:00
|
|
|
var $stock_contact_fields; // This is an array of almost the fields in the phpgw_addressbook table, except id,owner,lid,tid,access,cat_id
|
2001-03-28 02:57:47 +02:00
|
|
|
var $non_contact_fields; // Here are the rest
|
|
|
|
var $email_types; // VCard email type array
|
|
|
|
var $total_records; // This will contain numrows for data retrieved
|
|
|
|
var $grants; // This holds all of the users that have granted access to there entrys
|
2001-02-02 08:00:58 +01:00
|
|
|
|
2001-04-29 15:41:36 +02:00
|
|
|
function contacts_($useacl=True)
|
2001-02-11 05:16:18 +01:00
|
|
|
{
|
|
|
|
global $phpgw, $phpgw_info;
|
|
|
|
|
2001-03-24 10:58:09 +01:00
|
|
|
$this->db = $phpgw->db;
|
2001-04-29 15:41:36 +02:00
|
|
|
if($useacl)
|
|
|
|
{
|
|
|
|
$this->grants = $phpgw->acl->get_grants('addressbook');
|
|
|
|
}
|
2001-03-24 10:58:09 +01:00
|
|
|
$this->account_id = $phpgw_info['user']['account_id'];
|
2001-02-11 05:16:18 +01:00
|
|
|
|
2001-03-19 04:46:41 +01:00
|
|
|
// The left side are the array elements used throughout phpgw, right side are the db field names.
|
2001-05-01 11:32:52 +02:00
|
|
|
$this->stock_contact_fields = array(
|
2001-05-07 07:04:13 +02:00
|
|
|
'fn' => 'fn', // 'prefix given middle family suffix'
|
|
|
|
'n_given' => 'n_given', // firstname
|
|
|
|
'n_family' => 'n_family', // lastname
|
|
|
|
'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', // company
|
|
|
|
'org_unit' => 'org_unit', // division
|
|
|
|
'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', // address is domestic/intl/postal/parcel/work/home
|
|
|
|
'label' => 'label', // address 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', // address is domestic/intl/postal/parcel/work/home
|
|
|
|
|
|
|
|
'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', // home, work, voice, etc
|
|
|
|
'email' => 'email',
|
|
|
|
'email_type' => 'email_type', //'INTERNET','CompuServe',etc...
|
|
|
|
'email_home' => 'email_home',
|
|
|
|
'email_home_type' => 'email_home_type' //'INTERNET','CompuServe',etc...
|
2001-02-11 05:16:18 +01:00
|
|
|
);
|
|
|
|
|
2001-03-28 02:57:47 +02:00
|
|
|
$this->non_contact_fields = array(
|
2001-05-07 07:04:13 +02:00
|
|
|
'id' => 'id',
|
|
|
|
'lid' => 'lid',
|
|
|
|
'tid' => 'tid',
|
|
|
|
'cat_id' => 'cat_id',
|
|
|
|
'access' => 'access',
|
|
|
|
'owner' => 'owner'
|
2001-03-28 02:57:47 +02:00
|
|
|
);
|
|
|
|
|
2001-03-16 04:03:42 +01:00
|
|
|
/* Used to flag an address as being:
|
|
|
|
domestic OR international(default)
|
|
|
|
parcel(default)
|
|
|
|
postal(default)
|
|
|
|
*/
|
|
|
|
$this->adr_types = array(
|
2001-05-07 07:04:13 +02:00
|
|
|
'dom' => lang('Domestic'),
|
|
|
|
'intl' => lang('International'),
|
|
|
|
'parcel' => lang('Parcel'),
|
|
|
|
'postal' => lang('Postal')
|
2001-03-16 04:03:42 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
// Used to set preferred number field
|
|
|
|
$this->tel_types = array(
|
2001-05-07 07:04:13 +02:00
|
|
|
'work' => 'work',
|
|
|
|
'home' => 'home',
|
|
|
|
'voice' => 'voice',
|
|
|
|
'fax' => 'fax',
|
|
|
|
'msg' => 'msg',
|
|
|
|
'cell' => 'cell',
|
|
|
|
'pager' => 'pager',
|
|
|
|
'bbs' => 'bbs',
|
|
|
|
'modem' => 'modem',
|
|
|
|
'car' => 'car',
|
|
|
|
'isdn' => 'isdn',
|
|
|
|
'video' => 'video'
|
2001-03-16 04:03:42 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
// Used to set email_type fields
|
2001-05-01 11:32:52 +02:00
|
|
|
$this->email_types = array(
|
2001-05-07 07:04:13 +02:00
|
|
|
'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-02-11 05:16:18 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// send this the id and whatever fields you want to see
|
2001-02-19 04:34:59 +01:00
|
|
|
function read_single_entry($id,$fields="")
|
2001-02-11 05:16:18 +01:00
|
|
|
{
|
2001-02-19 04:34:59 +01:00
|
|
|
if (!$fields || empty($fields)) { $fields = $this->stock_contact_fields; }
|
2001-02-11 05:16:18 +01:00
|
|
|
list($stock_fields,$stock_fieldnames,$extra_fields) =
|
|
|
|
$this->split_stock_and_extras($fields);
|
|
|
|
|
2001-05-07 07:04:13 +02:00
|
|
|
if (count($stock_fieldnames))
|
|
|
|
{
|
2001-02-11 05:16:18 +01:00
|
|
|
$t_fields = "," . implode(",",$stock_fieldnames);
|
2001-05-07 07:04:13 +02:00
|
|
|
if ($t_fields == ",")
|
|
|
|
{
|
2001-02-11 05:16:18 +01:00
|
|
|
unset($t_fields);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->db2 = $this->db;
|
2001-02-02 08:00:58 +01:00
|
|
|
|
2001-03-25 07:41:06 +02:00
|
|
|
$this->db->query("select id,lid,tid,owner,access,cat_id $t_fields from $this->std_table WHERE id='$id'");
|
2001-02-11 05:16:18 +01:00
|
|
|
$this->db->next_record();
|
2001-05-01 11:32:52 +02:00
|
|
|
|
2001-05-07 07:04:13 +02:00
|
|
|
$return_fields[0]['id'] = $this->db->f('id'); // unique id
|
|
|
|
$return_fields[0]['lid'] = $this->db->f('lid'); // lid for group/account records
|
|
|
|
$return_fields[0]['tid'] = $this->db->f('tid'); // type id (g/u) for groups/accounts
|
|
|
|
$return_fields[0]['owner'] = $this->db->f('owner'); // id of owner/parent for the record
|
|
|
|
$return_fields[0]['access'] = $this->db->f('access'); // public/private
|
|
|
|
$return_fields[0]['cat_id'] = $this->db->f('cat_id');
|
2001-03-16 04:03:42 +01:00
|
|
|
|
2001-05-07 07:04:13 +02:00
|
|
|
if (gettype($stock_fieldnames) == 'array')
|
|
|
|
{
|
|
|
|
while (list($f_name) = each($stock_fieldnames))
|
|
|
|
{
|
2001-03-18 19:49:19 +01:00
|
|
|
$return_fields[0][$f_name] = $this->db->f($f_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-03-16 04:03:42 +01:00
|
|
|
// Setup address type fields
|
2001-05-07 07:04:13 +02:00
|
|
|
if ($this->db->f('adr_one_type'))
|
|
|
|
{
|
|
|
|
$one_type = $this->db->f('adr_one_type');
|
2001-03-16 04:03:42 +01:00
|
|
|
reset($this->adr_types);
|
2001-05-07 07:04:13 +02:00
|
|
|
while (list($name,$val) = each($this->adr_types))
|
|
|
|
{
|
2001-03-16 04:03:42 +01:00
|
|
|
eval("if (strstr(\$one_type,\$name)) { \$return_fields[0][\"one_\$name\"] = \"on\"; }");
|
|
|
|
}
|
|
|
|
}
|
2001-05-07 07:04:13 +02:00
|
|
|
if ($this->db->f('adr_two_type'))
|
|
|
|
{
|
|
|
|
$two_type = $this->db->f('adr_two_type');
|
2001-03-16 04:03:42 +01:00
|
|
|
reset($this->adr_types);
|
2001-05-07 07:04:13 +02:00
|
|
|
while (list($name,$val) = each($this->adr_types))
|
|
|
|
{
|
2001-03-16 04:03:42 +01:00
|
|
|
eval("if (strstr(\$two_type,\$name)) { \$return_fields[0][\"two_\$name\"] = \"on\"; }");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-03-19 18:56:43 +01:00
|
|
|
$this->db2->query("SELECT contact_name,contact_value FROM $this->ext_table where contact_id='" . $this->db->f("id") . "'",__LINE__,__FILE__);
|
2001-05-07 07:04:13 +02:00
|
|
|
while ($this->db2->next_record())
|
|
|
|
{
|
2001-02-11 05:16:18 +01:00
|
|
|
// If its not in the list to be returned, don't return it.
|
|
|
|
// This is still quicker then 5(+) separate queries
|
2001-05-07 07:04:13 +02:00
|
|
|
if ($extra_fields[$this->db2->f('contact_name')])
|
|
|
|
{
|
|
|
|
$return_fields[0][$this->db2->f('contact_name')] = $this->db2->f('contact_value');
|
2001-02-11 05:16:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $return_fields;
|
|
|
|
}
|
|
|
|
|
2001-05-07 07:04:13 +02:00
|
|
|
function read_last_entry($fields='')
|
2001-02-19 04:34:59 +01:00
|
|
|
{
|
|
|
|
if (!$fields || empty($fields)) { $fields = $this->stock_contact_fields; }
|
|
|
|
list($stock_fields,$stock_fieldnames,$extra_fields) =
|
|
|
|
$this->split_stock_and_extras($fields);
|
|
|
|
|
2001-05-07 07:04:13 +02:00
|
|
|
if (count($stock_fieldnames))
|
|
|
|
{
|
2001-02-19 04:34:59 +01:00
|
|
|
$t_fields = "," . implode(",",$stock_fieldnames);
|
2001-05-07 07:04:13 +02:00
|
|
|
if ($t_fields == ",")
|
|
|
|
{
|
2001-02-19 04:34:59 +01:00
|
|
|
unset($t_fields);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->db2 = $this->db;
|
2001-03-19 18:56:43 +01:00
|
|
|
|
|
|
|
$this->db->query('SELECT max(id) FROM '.$this->std_table,__LINE__,__FILE__);
|
2001-02-19 04:34:59 +01:00
|
|
|
$this->db->next_record();
|
2001-03-18 19:49:19 +01:00
|
|
|
|
2001-03-19 18:56:43 +01:00
|
|
|
$id = $this->db->f(0);
|
|
|
|
|
2001-03-25 07:41:06 +02:00
|
|
|
$this->db->query("SELECT id,lid,tid,owner,access,cat_id $t_fields from $this->std_table WHERE id='$id'",__LINE__,__FILE__);
|
2001-02-19 04:34:59 +01:00
|
|
|
$this->db->next_record();
|
2001-03-18 19:49:19 +01:00
|
|
|
|
2001-05-07 07:04:13 +02:00
|
|
|
$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'); // public/private
|
|
|
|
$return_fields[0]['cat_id'] = $this->db->f('cat_id');
|
2001-03-18 19:49:19 +01:00
|
|
|
|
2001-05-07 07:04:13 +02:00
|
|
|
if (gettype($stock_fieldnames) == 'array')
|
|
|
|
{
|
|
|
|
while (list($f_name) = each($stock_fieldnames))
|
|
|
|
{
|
2001-02-19 04:34:59 +01:00
|
|
|
$return_fields[0][$f_name] = $this->db->f($f_name);
|
|
|
|
}
|
|
|
|
}
|
2001-03-18 19:49:19 +01:00
|
|
|
|
|
|
|
// Setup address type fields
|
2001-05-07 07:04:13 +02:00
|
|
|
if ($this->db->f('adr_one_type'))
|
|
|
|
{
|
|
|
|
$one_type = $this->db->f('adr_one_type');
|
2001-03-18 19:49:19 +01:00
|
|
|
reset($this->adr_types);
|
2001-05-07 07:04:13 +02:00
|
|
|
while (list($name,$val) = each($this->adr_types))
|
|
|
|
{
|
2001-03-18 19:49:19 +01:00
|
|
|
eval("if (strstr(\$one_type,\$name)) { \$return_fields[0][\"one_\$name\"] = \"on\"; }");
|
|
|
|
}
|
|
|
|
}
|
2001-05-07 07:04:13 +02:00
|
|
|
if ($this->db->f('adr_two_type'))
|
|
|
|
{
|
|
|
|
$two_type = $this->db->f('adr_two_type');
|
2001-03-18 19:49:19 +01:00
|
|
|
reset($this->adr_types);
|
2001-05-07 07:04:13 +02:00
|
|
|
while (list($name,$val) = each($this->adr_types))
|
|
|
|
{
|
2001-03-18 19:49:19 +01:00
|
|
|
eval("if (strstr(\$two_type,\$name)) { \$return_fields[0][\"two_\$name\"] = \"on\"; }");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-02-19 04:34:59 +01:00
|
|
|
$this->db2->query("select contact_name,contact_value from $this->ext_table where contact_id='" . $this->db->f("id") . "'",__LINE__,__FILE__);
|
2001-05-07 07:04:13 +02:00
|
|
|
while ($this->db2->next_record())
|
|
|
|
{
|
2001-02-19 04:34:59 +01:00
|
|
|
// If its not in the list to be returned, don't return it.
|
|
|
|
// This is still quicker then 5(+) separate queries
|
2001-05-07 07:04:13 +02:00
|
|
|
if ($extra_fields[$this->db2->f('contact_name')])
|
|
|
|
{
|
|
|
|
$return_fields[0][$this->db2->f('contact_name')] = $this->db2->f('contact_value');
|
2001-02-19 04:34:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $return_fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
// send this the range, query, sort, order and whatever fields you want to see
|
2001-03-28 14:39:42 +02:00
|
|
|
function read($start=0,$offset=0,$fields="",$query="",$filter="",$sort="",$order="")
|
2001-02-11 05:16:18 +01:00
|
|
|
{
|
|
|
|
global $phpgw,$phpgw_info;
|
2001-02-21 23:41:37 +01:00
|
|
|
|
2001-02-19 04:34:59 +01:00
|
|
|
if (!$fields || empty($fields)) { $fields = $this->stock_contact_fields; }
|
2001-05-26 13:20:03 +02:00
|
|
|
$DEBUG = 1;
|
2001-02-11 05:16:18 +01:00
|
|
|
|
|
|
|
list($stock_fields,$stock_fieldnames,$extra_fields) = $this->split_stock_and_extras($fields);
|
2001-05-07 07:04:13 +02:00
|
|
|
if (count($stock_fieldnames))
|
|
|
|
{
|
2001-02-11 05:16:18 +01:00
|
|
|
$t_fields = "," . implode(",",$stock_fieldnames);
|
2001-05-07 07:04:13 +02:00
|
|
|
if ($t_fields == ",")
|
|
|
|
{
|
2001-02-11 05:16:18 +01:00
|
|
|
unset($t_fields);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-02-21 23:41:37 +01:00
|
|
|
// turn filter's a=b,c=d OR a=b into an array
|
2001-02-11 05:16:18 +01:00
|
|
|
if ($filter) {
|
2001-03-26 04:00:47 +02:00
|
|
|
$extra_stock = array(
|
|
|
|
'id' => 'id',
|
|
|
|
'tid' => 'tid',
|
|
|
|
'lid' => 'lid',
|
|
|
|
'owner' => 'owner',
|
|
|
|
'access' => 'access',
|
|
|
|
'cat_id' => 'cat_id'
|
|
|
|
);
|
|
|
|
$check_stock = $this->stock_contact_fields + $extra_stock;
|
|
|
|
|
2001-02-11 05:16:18 +01:00
|
|
|
if ($DEBUG) { echo "DEBUG - Inbound filter is: #".$filter."#"; }
|
|
|
|
$filterarray = split(',',$filter);
|
2001-05-07 07:04:13 +02:00
|
|
|
if ($filterarray[1])
|
|
|
|
{
|
2001-02-11 05:16:18 +01:00
|
|
|
$i=0;
|
2001-05-07 07:04:13 +02:00
|
|
|
for ($i=0;$i<count($filterarray);$i++)
|
|
|
|
{
|
2001-02-21 23:41:37 +01:00
|
|
|
list($name,$value) = split("=",$filterarray[$i]);
|
2001-05-07 07:04:13 +02:00
|
|
|
if ($name)
|
|
|
|
{
|
2001-02-21 23:41:37 +01:00
|
|
|
if ($DEBUG) { echo "<br>DEBUG - Filter intermediate strings 1: #".$name."# => #".$value."#"; }
|
|
|
|
$filterfields[$name] = $value;
|
|
|
|
}
|
2001-02-11 05:16:18 +01:00
|
|
|
}
|
2001-05-07 07:04:13 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-02-11 05:16:18 +01:00
|
|
|
list($name,$value) = split('=',$filter);
|
2001-05-07 07:04:13 +02:00
|
|
|
if ($DEBUG)
|
|
|
|
{
|
2001-02-11 05:16:18 +01:00
|
|
|
echo "<br>DEBUG - Filter intermediate strings 1: #".$name."# => #".$value."#";
|
|
|
|
}
|
|
|
|
$filterfields = array($name => $value);
|
|
|
|
}
|
|
|
|
|
2001-02-21 23:41:37 +01:00
|
|
|
// now check each element of the array and convert into SQL for queries
|
|
|
|
// below
|
2001-02-11 05:16:18 +01:00
|
|
|
$i=0;
|
2001-03-08 15:37:42 +01:00
|
|
|
reset($filterfields);
|
2001-05-07 07:04:13 +02:00
|
|
|
while (list($name,$value) = each($filterfields))
|
|
|
|
{
|
2001-02-11 05:16:18 +01:00
|
|
|
if ($DEBUG) { echo "<br>DEBUG - Filter intermediate strings 2: #".$name."# => #".$value."#"; }
|
2001-02-21 23:41:37 +01:00
|
|
|
$isstd=0;
|
2001-05-07 07:04:13 +02:00
|
|
|
if ($name && empty($value))
|
|
|
|
{
|
2001-03-08 15:37:42 +01:00
|
|
|
if ($DEBUG) { echo "<br>DEBUG - filter field '".$name."' is empty (NULL)"; }
|
2001-05-07 07:04:13 +02:00
|
|
|
while (list($fname,$fvalue)=each($check_stock))
|
|
|
|
{
|
|
|
|
if ($fvalue==$name)
|
|
|
|
{
|
2001-02-21 23:41:37 +01:00
|
|
|
$filterlist .= $name.' is NULL,';
|
2001-03-08 15:37:42 +01:00
|
|
|
if ($DEBUG) { echo "<br>DEBUG - filter field '".$name."' is a stock field"; }
|
2001-02-21 23:41:37 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2001-05-07 07:04:13 +02:00
|
|
|
}
|
|
|
|
elseif($name && $value)
|
|
|
|
{
|
2001-03-26 04:00:47 +02:00
|
|
|
reset($check_stock);
|
2001-05-07 07:04:13 +02:00
|
|
|
while (list($fname,$fvalue)=each($check_stock))
|
|
|
|
{
|
|
|
|
if ($fvalue==$name)
|
|
|
|
{
|
2001-05-26 13:20:03 +02:00
|
|
|
if ($name == 'cat_id')
|
2001-05-07 07:04:13 +02:00
|
|
|
{
|
2001-05-26 13:20:03 +02:00
|
|
|
// This is the alternative to CONCAT, since it is mysql-only
|
|
|
|
$filterlist .= "(" . $name . " LIKE '%," . $value . ",%' OR " . $name."=".$value.");";
|
|
|
|
}
|
|
|
|
elseif (gettype($value) == "integer")
|
|
|
|
{
|
|
|
|
$filterlist .= $name."=".$value.";";
|
2001-05-07 07:04:13 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-05-26 13:20:03 +02:00
|
|
|
$filterlist .= $name."='".$value."';";
|
2001-03-25 07:41:06 +02:00
|
|
|
}
|
2001-02-21 23:41:37 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-02-11 05:16:18 +01:00
|
|
|
$i++;
|
|
|
|
}
|
2001-02-21 23:41:37 +01:00
|
|
|
$filterlist = substr($filterlist,0,-1);
|
2001-05-26 13:20:03 +02:00
|
|
|
$filterlist = ereg_replace(";"," AND ",$filterlist);
|
2001-05-23 22:49:52 +02:00
|
|
|
|
|
|
|
// echo "<p>contacts->read(): filterlist=\"$filterlist\" -->"; // allow multiple (','-separated) cat's per address
|
2001-05-26 13:20:03 +02:00
|
|
|
//$filterlist = ereg_replace('cat_id=[\']*([0-9]+)[\']*',"CONCAT(',',cat_id,',') LIKE '%,\\1,%'",$filterlist);
|
2001-05-23 22:49:52 +02:00
|
|
|
// echo "\"$filterlist\"</p>\n";
|
2001-05-26 13:20:03 +02:00
|
|
|
// Oops, CONCAT is mysql-only, this is now handled explicitly above for cat_id
|
2001-02-11 05:16:18 +01:00
|
|
|
|
2001-05-07 07:04:13 +02:00
|
|
|
if ($DEBUG)
|
|
|
|
{
|
2001-03-17 22:29:44 +01:00
|
|
|
echo "<br>DEBUG - Filter output string: #".$filterlist."#";
|
2001-02-11 05:16:18 +01:00
|
|
|
}
|
|
|
|
|
2001-05-07 07:04:13 +02:00
|
|
|
if ($filterlist)
|
|
|
|
{
|
2001-03-17 22:29:44 +01:00
|
|
|
$filtermethod = '('.$filterlist.') ';
|
2001-04-21 01:13:56 +02:00
|
|
|
$fwhere = ' WHERE '; $fand = ' AND ';
|
2001-02-11 05:16:18 +01:00
|
|
|
}
|
2001-05-07 07:04:13 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-04-26 03:40:59 +02:00
|
|
|
$filtermethod = " AND (tid='n' OR tid is null)";
|
2001-02-11 05:16:18 +01:00
|
|
|
}
|
2001-03-24 10:58:09 +01:00
|
|
|
|
2001-05-07 07:04:13 +02:00
|
|
|
if (!$filtermethod)
|
|
|
|
{
|
2001-04-29 15:41:36 +02:00
|
|
|
if($phpgw_info['user']['account_id'])
|
|
|
|
{
|
|
|
|
$fwhere .= " (owner=" . $phpgw_info['user']['account_id'];
|
|
|
|
$fand .= " (owner=" . $phpgw_info['user']['account_id'];
|
|
|
|
}
|
2001-05-07 07:04:13 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-04-29 15:41:36 +02:00
|
|
|
if($phpgw_info['user']['account_id'])
|
|
|
|
{
|
|
|
|
$fwhere .= $filtermethod . " AND (owner=" . $phpgw_info['user']['account_id'];
|
|
|
|
$fand .= $filtermethod . " AND (owner=" . $phpgw_info['user']['account_id'];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$filtermethod = substr($filtermethod,0,-2);
|
|
|
|
$fwhere .= $filtermethod;
|
|
|
|
$fand .= $filtermethod;
|
|
|
|
}
|
2001-03-26 15:08:59 +02:00
|
|
|
}
|
|
|
|
|
2001-03-24 10:58:09 +01:00
|
|
|
if (is_array($this->grants))
|
|
|
|
{
|
|
|
|
$grants = $this->grants;
|
|
|
|
while (list($user) = each($grants))
|
|
|
|
{
|
|
|
|
$public_user_list[] = $user;
|
|
|
|
}
|
|
|
|
reset($public_user_list);
|
2001-03-25 07:41:06 +02:00
|
|
|
$fwhere .= " OR (access='public' AND owner in(" . implode(',',$public_user_list) . "))) ";
|
2001-04-21 01:13:56 +02:00
|
|
|
$fand .= " OR (access='public' AND owner in(" . implode(',',$public_user_list) . "))) ";
|
2001-03-24 10:58:09 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-04-21 01:13:56 +02:00
|
|
|
$fwhere .= ') '; $fand .= ') ';
|
2001-03-24 10:58:09 +01:00
|
|
|
}
|
|
|
|
|
2001-05-07 07:04:13 +02:00
|
|
|
if ($DEBUG && $filtermethod)
|
|
|
|
{
|
2001-02-21 23:41:37 +01:00
|
|
|
echo "<br>DEBUG - Filtering with: #" . $filtermethod . "#";
|
2001-02-11 05:16:18 +01:00
|
|
|
}
|
|
|
|
|
2001-05-01 11:32:52 +02:00
|
|
|
if (!$sort) { $sort = "ASC"; }
|
2001-02-11 05:16:18 +01:00
|
|
|
|
2001-05-07 07:04:13 +02:00
|
|
|
if ($order)
|
|
|
|
{
|
2001-03-17 22:29:44 +01:00
|
|
|
$ordermethod = "order by $order $sort ";
|
2001-05-07 07:04:13 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-03-17 22:29:44 +01:00
|
|
|
$ordermethod = "order by n_family,n_given,email $sort";
|
2001-02-21 23:41:37 +01:00
|
|
|
}
|
|
|
|
|
2001-05-07 07:04:13 +02:00
|
|
|
if ($DEBUG && $ordermethod)
|
|
|
|
{
|
2001-02-21 23:41:37 +01:00
|
|
|
echo "<br>DEBUG - $ordermethod";
|
2001-02-11 05:16:18 +01:00
|
|
|
}
|
2001-03-25 07:41:06 +02:00
|
|
|
|
|
|
|
$filtermethod = "";
|
|
|
|
|
2001-03-18 07:49:31 +01:00
|
|
|
// This logic allows you to limit rows, or not.
|
|
|
|
// The export feature, for example, does not limit rows.
|
|
|
|
// This way, it can retrieve all rows at once.
|
|
|
|
if ($start && $offset) {
|
|
|
|
$limit = $this->db->limit($start,$offset);
|
|
|
|
} elseif ($start && !$offset) {
|
|
|
|
$limit = "";
|
|
|
|
} elseif(!$start && !$offset) {
|
|
|
|
$limit = "";
|
|
|
|
} else { #(!$start && $offset) {
|
|
|
|
$start = 0;
|
|
|
|
$limit = $this->db->limit($start,$offset);
|
|
|
|
}
|
|
|
|
|
2001-02-11 05:16:18 +01:00
|
|
|
$this->db3 = $this->db2 = $this->db; // Create new result objects before our queries
|
|
|
|
|
2001-05-07 07:04:13 +02:00
|
|
|
if ($query)
|
|
|
|
{
|
2001-04-06 23:14:38 +02:00
|
|
|
$this->db3->query("SELECT * FROM $this->std_table WHERE (bday LIKE '%$query%' OR n_family LIKE '"
|
2001-03-17 22:29:44 +01:00
|
|
|
. "%$query%' OR n_given LIKE '%$query%' OR email LIKE '%$query%' OR "
|
|
|
|
. "adr_one_street LIKE '%$query%' OR adr_one_locality LIKE '%$query%' OR adr_one_region LIKE '%$query%' OR "
|
|
|
|
. "adr_one_postalcode LIKE '%$query%' OR adr_one_countryname LIKE '%$query%' OR "
|
|
|
|
. "adr_two_street LIKE '%$query%' OR adr_two_locality LIKE '%$query%' OR adr_two_region LIKE '%$query%' OR "
|
|
|
|
. "adr_two_postalcode LIKE '%$query%' OR adr_two_countryname LIKE '%$query%' OR "
|
|
|
|
. "org_name LIKE '%$query%' OR org_unit LIKE '%$query%') " . $fand . $filtermethod . $ordermethod,__LINE__,__FILE__);
|
|
|
|
$this->total_records = $this->db3->num_rows();
|
|
|
|
|
2001-04-08 09:04:10 +02:00
|
|
|
$this->db->query("SELECT * FROM $this->std_table WHERE (bday LIKE '%$query%' OR n_family LIKE '"
|
2001-03-17 22:29:44 +01:00
|
|
|
. "%$query%' OR n_given LIKE '%$query%' OR email LIKE '%$query%' OR "
|
|
|
|
. "adr_one_street LIKE '%$query%' OR adr_one_locality LIKE '%$query%' OR adr_one_region LIKE '%$query%' OR "
|
|
|
|
. "adr_one_postalcode LIKE '%$query%' OR adr_one_countryname LIKE '%$query%' OR "
|
|
|
|
. "adr_two_street LIKE '%$query%' OR adr_two_locality LIKE '%$query%' OR adr_two_region LIKE '%$query%' OR "
|
|
|
|
. "adr_two_postalcode LIKE '%$query%' OR adr_two_countryname LIKE '%$query%' OR "
|
|
|
|
. "org_name LIKE '%$query%' OR org_unit LIKE '%$query%') " . $fand . $filtermethod . $ordermethod . " "
|
2001-03-18 07:49:31 +01:00
|
|
|
. $limit,__LINE__,__FILE__);
|
2001-05-07 07:04:13 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-03-25 09:44:01 +02:00
|
|
|
$this->db3->query("SELECT id,lid,tid,owner,access,cat_id $t_fields FROM $this->std_table " . $fwhere
|
2001-03-17 22:29:44 +01:00
|
|
|
. $filtermethod,__LINE__,__FILE__);
|
|
|
|
$this->total_records = $this->db3->num_rows();
|
|
|
|
|
2001-03-25 09:44:01 +02:00
|
|
|
$this->db->query("SELECT id,lid,tid,owner,access,cat_id $t_fields FROM $this->std_table " . $fwhere
|
2001-03-18 07:49:31 +01:00
|
|
|
. $filtermethod . " " . $ordermethod . " " . $limit,__LINE__,__FILE__);
|
2001-02-22 00:04:09 +01:00
|
|
|
}
|
2001-02-28 16:24:30 +01:00
|
|
|
|
2001-05-26 13:20:03 +02:00
|
|
|
if ($DEBUG) { echo "<br>SELECT id,lid,tid,owner,access,cat_id $t_fields FROM $this->std_table " . $fwhere . $filtermethod; }
|
|
|
|
|
2001-02-11 05:16:18 +01:00
|
|
|
$i=0;
|
2001-05-07 07:04:13 +02:00
|
|
|
while ($this->db->next_record())
|
|
|
|
{
|
2001-02-11 05:16:18 +01:00
|
|
|
// unique id, lid for group/account records,
|
2001-03-17 22:29:44 +01:00
|
|
|
// type id (g/u) for groups/accounts, and
|
2001-02-11 05:16:18 +01:00
|
|
|
// id of owner/parent for the record
|
2001-05-07 07:04:13 +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'); // public/private
|
|
|
|
$return_fields[$i]['cat_id'] = $this->db->f('cat_id');
|
|
|
|
|
|
|
|
if (gettype($stock_fieldnames) == 'array')
|
|
|
|
{
|
|
|
|
while (list($f_name) = each($stock_fieldnames))
|
|
|
|
{
|
2001-02-11 05:16:18 +01:00
|
|
|
$return_fields[$i][$f_name] = $this->db->f($f_name);
|
|
|
|
}
|
2001-03-17 22:29:44 +01:00
|
|
|
reset($stock_fieldnames);
|
|
|
|
}
|
|
|
|
$this->db2->query("SELECT contact_name,contact_value FROM $this->ext_table WHERE contact_id='"
|
|
|
|
. $this->db->f("id") . "'" .$filterextra,__LINE__,__FILE__);
|
2001-05-07 07:04:13 +02:00
|
|
|
while ($this->db2->next_record())
|
|
|
|
{
|
2001-03-17 22:29:44 +01:00
|
|
|
// If its not in the list to be returned, don't return it.
|
|
|
|
// This is still quicker then 5(+) separate queries
|
2001-05-07 07:04:13 +02:00
|
|
|
if ($extra_fields[$this->db2->f('contact_name')])
|
|
|
|
{
|
|
|
|
$return_fields[$i][$this->db2->f('contact_name')] = $this->db2->f('contact_value');
|
2001-03-17 22:29:44 +01:00
|
|
|
}
|
2001-02-11 05:16:18 +01:00
|
|
|
}
|
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
return $return_fields;
|
|
|
|
}
|
|
|
|
|
2001-03-27 01:41:34 +02:00
|
|
|
function add($owner,$fields,$access='',$cat_id='',$tid='n')
|
2001-02-11 05:16:18 +01:00
|
|
|
{
|
|
|
|
list($stock_fields,$stock_fieldnames,$extra_fields) = $this->split_stock_and_extras($fields);
|
|
|
|
|
|
|
|
//$this->db->lock(array("contacts"));
|
2001-05-07 07:04:13 +02:00
|
|
|
if ($fields['lid'])
|
|
|
|
{
|
2001-03-27 01:41:34 +02:00
|
|
|
$lid[0] = 'lid,';
|
|
|
|
$lid[1] = $fields['lid']."','";
|
|
|
|
}
|
|
|
|
$this->db->query("insert into $this->std_table (owner,access,cat_id,tid,".$lid[0]
|
2001-02-11 05:16:18 +01:00
|
|
|
. implode(",",$this->stock_contact_fields)
|
2001-03-27 01:41:34 +02:00
|
|
|
. ") values ('$owner','$access','$cat_id','$tid','".$lid[1]
|
2001-02-11 05:16:18 +01:00
|
|
|
. implode("','",$this->loop_addslashes($stock_fields)) . "')",__LINE__,__FILE__);
|
|
|
|
|
|
|
|
$this->db->query("select max(id) from $this->std_table ",__LINE__,__FILE__);
|
|
|
|
$this->db->next_record();
|
|
|
|
$id = $this->db->f(0);
|
|
|
|
//$this->db->unlock();
|
2001-05-07 07:04:13 +02:00
|
|
|
if (count($extra_fields))
|
|
|
|
{
|
|
|
|
while (list($name,$value) = each($extra_fields))
|
|
|
|
{
|
2001-02-11 05:16:18 +01:00
|
|
|
$this->db->query("insert into $this->ext_table values ('$id','" . $this->account_id . "','"
|
|
|
|
. addslashes($name) . "','" . addslashes($value) . "')",__LINE__,__FILE__);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function field_exists($id,$field_name)
|
|
|
|
{
|
|
|
|
$this->db->query("select count(*) from $this->ext_table where contact_id='$id' and contact_name='"
|
|
|
|
. addslashes($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->query("insert into $this->ext_table values ($id,'$owner','" . addslashes($field_name)
|
|
|
|
. "','" . addslashes($field_value) . "')",__LINE__,__FILE__);
|
|
|
|
}
|
|
|
|
|
|
|
|
function delete_single_extra_field($id,$field_name)
|
|
|
|
{
|
|
|
|
$this->db->query("delete from $this->ext_table where contact_id='$id' and contact_name='"
|
|
|
|
. addslashes($field_name) . "'",__LINE__,__FILE__);
|
|
|
|
}
|
|
|
|
|
2001-03-30 05:37:21 +02:00
|
|
|
function update($id,$owner,$fields,$access='',$cat_id='',$tid='n')
|
2001-02-11 05:16:18 +01:00
|
|
|
{
|
|
|
|
// First make sure that id number exists
|
|
|
|
$this->db->query("select count(*) from $this->std_table where id='$id'",__LINE__,__FILE__);
|
|
|
|
$this->db->next_record();
|
2001-05-07 07:04:13 +02:00
|
|
|
if (!$this->db->f(0))
|
|
|
|
{
|
2001-02-11 05:16:18 +01:00
|
|
|
return False;
|
|
|
|
}
|
|
|
|
|
|
|
|
list($stock_fields,$stock_fieldnames,$extra_fields) = $this->split_stock_and_extras($fields);
|
2001-05-07 07:04:13 +02:00
|
|
|
if (count($stock_fields))
|
|
|
|
{
|
|
|
|
while (list($stock_fieldname) = each($stock_fieldnames))
|
|
|
|
{
|
2001-02-11 05:16:18 +01:00
|
|
|
$ta[] = $stock_fieldname . "='" . addslashes($stock_fields[$stock_fieldname]) . "'";
|
|
|
|
}
|
|
|
|
$fields_s = "," . implode(",",$ta);
|
2001-05-07 07:04:13 +02:00
|
|
|
if ($field_s == ",")
|
|
|
|
{
|
2001-02-11 05:16:18 +01:00
|
|
|
unset($field_s);
|
|
|
|
}
|
2001-03-27 16:44:50 +02:00
|
|
|
$this->db->query("update $this->std_table set access='$access',cat_id='$cat_id', tid='$tid' $fields_s where "
|
2001-02-11 05:16:18 +01:00
|
|
|
. "id='$id'",__LINE__,__FILE__);
|
|
|
|
}
|
|
|
|
|
2001-05-07 07:04:13 +02:00
|
|
|
while (list($x_name,$x_value) = each($extra_fields))
|
|
|
|
{
|
|
|
|
if ($this->field_exists($id,$x_name))
|
|
|
|
{
|
|
|
|
if (!$x_value)
|
|
|
|
{
|
2001-02-11 05:16:18 +01:00
|
|
|
$this->delete_single_extra_field($id,$x_name);
|
2001-05-07 07:04:13 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-02-11 05:16:18 +01:00
|
|
|
$this->db->query("update $this->ext_table set contact_value='" . addslashes($x_value)
|
|
|
|
. "',contact_owner='$owner' where contact_name='" . addslashes($x_name)
|
|
|
|
. "' and contact_id='$id'",__LINE__,__FILE__);
|
|
|
|
}
|
2001-05-07 07:04:13 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-02-11 05:16:18 +01:00
|
|
|
$this->add_single_extra_field($id,$owner,$x_name,$x_value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-03-30 06:18:22 +02:00
|
|
|
// Used by admin to change ownership on account delete
|
|
|
|
function change_owner($old_owner='',$new_owner='')
|
|
|
|
{
|
|
|
|
if (!($new_owner && $old_owner))
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2001-02-28 16:24:30 +01:00
|
|
|
// This is where the real work of delete() is done, shared class file contains calling function
|
2001-02-11 05:16:18 +01:00
|
|
|
function delete_($id)
|
|
|
|
{
|
2001-03-29 10:09:50 +02:00
|
|
|
$this->db->query("delete from $this->std_table where id='$id'",__LINE__,__FILE__);
|
|
|
|
$this->db->query("delete from $this->ext_table where contact_id='$id'",__LINE__,__FILE__);
|
2001-02-11 05:16:18 +01:00
|
|
|
}
|
|
|
|
|
2001-03-28 14:39:42 +02:00
|
|
|
// This is for the admin script deleteaccount.php
|
|
|
|
function delete_all($owner=0)
|
2001-03-26 01:25:23 +02:00
|
|
|
{
|
2001-03-28 14:39:42 +02:00
|
|
|
if ($owner)
|
|
|
|
{
|
|
|
|
$this->db->query("DELETE FROM $this->std_table WHERE owner=$owner",__LINE__,__FILE__);
|
2001-03-30 05:57:38 +02:00
|
|
|
$this->db->query("DELETE FROM $this->ext_table WHERE contact_owner=$owner",__LINE__,__FILE__);
|
2001-03-28 14:39:42 +02:00
|
|
|
}
|
2001-03-26 01:25:23 +02:00
|
|
|
return;
|
|
|
|
}
|
2001-02-11 05:16:18 +01:00
|
|
|
}
|
2001-02-02 08:00:58 +01:00
|
|
|
?>
|