2005-11-04 00:47:52 +01:00
|
|
|
<?php
|
2006-06-13 06:30:16 +02:00
|
|
|
/**
|
|
|
|
* Addressbook - General business object
|
|
|
|
*
|
|
|
|
* @link http://www.egroupware.org
|
|
|
|
* @author Cornelius Weiss <egw@von-und-zu-weiss.de>
|
|
|
|
* @author Ralf Becker <RalfBecker-AT-outdoor-training.de>
|
|
|
|
* @package addressbook
|
|
|
|
* @copyright (c) 2005/6 by Cornelius Weiss <egw@von-und-zu-weiss.de> and Ralf Becker <RalfBecker-AT-outdoor-training.de>
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
* @version $Id$
|
|
|
|
*/
|
2005-11-04 00:47:52 +01:00
|
|
|
|
|
|
|
require_once(EGW_INCLUDE_ROOT.'/addressbook/inc/class.socontacts.inc.php');
|
|
|
|
|
|
|
|
/**
|
2006-04-23 16:40:31 +02:00
|
|
|
* General business object of the adressbook
|
|
|
|
*
|
|
|
|
* @package addressbook
|
|
|
|
* @author Cornelius Weiss <egw@von-und-zu-weiss.de>
|
|
|
|
* @author Ralf Becker <RalfBecker-AT-outdoor-training.de>
|
|
|
|
* @copyright (c) 2005/6 by Cornelius Weiss <egw@von-und-zu-weiss.de> and Ralf Becker <RalfBecker-AT-outdoor-training.de>
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
*/
|
2005-11-04 00:47:52 +01:00
|
|
|
class bocontacts extends socontacts
|
|
|
|
{
|
2006-03-07 23:43:08 +01:00
|
|
|
/**
|
|
|
|
* @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 int $now_su actual user (!) time
|
|
|
|
*/
|
|
|
|
var $now_su;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array $timestamps timestamps
|
|
|
|
*/
|
2006-04-23 16:40:31 +02:00
|
|
|
var $timestamps = array('modified','created');
|
2006-03-07 23:43:08 +01:00
|
|
|
|
2006-04-23 16:40:31 +02:00
|
|
|
/**
|
|
|
|
* @var array $fileas_types
|
|
|
|
*/
|
|
|
|
var $fileas_types = array(
|
|
|
|
'org_name: n_family, n_given',
|
|
|
|
'org_name: n_family, n_prefix',
|
|
|
|
'org_name: n_given n_family',
|
|
|
|
'org_name: n_fn',
|
|
|
|
'n_family, n_given: org_name',
|
|
|
|
'n_family, n_prefix: org_name',
|
|
|
|
'n_given n_family: org_name',
|
|
|
|
'n_prefix n_family: org_name',
|
|
|
|
'n_fn: org_name',
|
|
|
|
'org_name',
|
|
|
|
'n_given n_family',
|
|
|
|
'n_prefix n_family',
|
|
|
|
'n_family, n_given',
|
|
|
|
'n_family, n_prefix',
|
|
|
|
'n_fn',
|
|
|
|
);
|
2006-04-26 23:23:38 +02:00
|
|
|
|
2006-04-30 11:34:24 +02:00
|
|
|
/**
|
|
|
|
* @var array $org_fields fields belonging to the (virtual) organisation entry
|
|
|
|
*/
|
|
|
|
var $org_fields = array(
|
|
|
|
'org_name',
|
|
|
|
'org_unit',
|
|
|
|
'adr_one_street',
|
|
|
|
'adr_one_street2',
|
|
|
|
'adr_one_locality',
|
|
|
|
'adr_one_region',
|
|
|
|
'adr_one_postalcode',
|
|
|
|
'adr_one_countryname',
|
|
|
|
'label',
|
|
|
|
'tel_work',
|
|
|
|
'tel_fax',
|
|
|
|
'tel_assistent',
|
|
|
|
'assistent',
|
|
|
|
'email',
|
|
|
|
'url',
|
|
|
|
'tz',
|
|
|
|
);
|
|
|
|
/**
|
|
|
|
* @var double $org_common_factor minimum percentage of the contacts with identical values to construct the "common" (virtual) org-entry
|
|
|
|
*/
|
|
|
|
var $org_common_factor = 0.6;
|
|
|
|
|
2006-04-26 23:23:38 +02:00
|
|
|
var $contact_fields = array();
|
|
|
|
var $business_contact_fields = array();
|
|
|
|
var $home_contact_fields = array();
|
2006-04-23 16:40:31 +02:00
|
|
|
|
2006-06-13 23:53:00 +02:00
|
|
|
/**
|
|
|
|
* Number and message of last error or false if no error, atm. only used for saving
|
|
|
|
*
|
|
|
|
* @var string/boolean
|
|
|
|
*/
|
|
|
|
var $error;
|
|
|
|
|
2005-11-04 00:47:52 +01:00
|
|
|
function bocontacts($contact_app='addressbook')
|
|
|
|
{
|
|
|
|
$this->socontacts($contact_app);
|
2006-04-23 16:40:31 +02:00
|
|
|
|
2006-03-07 23:43:08 +01:00
|
|
|
$this->tz_offset_s = 3600 * $GLOBALS['egw_info']['user']['preferences']['common']['tz_offset'];
|
|
|
|
$this->now_su = time() + $this->tz_offset_s;
|
|
|
|
|
2005-11-04 00:47:52 +01:00
|
|
|
/* foreach(array(
|
|
|
|
'so' => $appname. 'soadb',
|
|
|
|
) as $my => $app_class)
|
|
|
|
{
|
|
|
|
list(,$class) = explode('.',$app_class);
|
|
|
|
|
|
|
|
if (!is_object($GLOBALS['egw']->$class))
|
|
|
|
{
|
|
|
|
$GLOBALS['egw']->$class =& CreateObject($app_class);
|
|
|
|
}
|
|
|
|
$this->$my = &$GLOBALS['egw']->$class;
|
|
|
|
}*/
|
|
|
|
|
2006-04-26 23:23:38 +02:00
|
|
|
$this->contact_fields = array(
|
|
|
|
'id' => lang('Contact ID'),
|
|
|
|
'tid' => lang('Type'),
|
|
|
|
'owner' => lang('Addressbook'),
|
|
|
|
'private' => lang('private'),
|
|
|
|
'cat_id' => lang('Category'),
|
|
|
|
'n_prefix' => lang('prefix'),
|
|
|
|
'n_given' => lang('first name'),
|
|
|
|
'n_middle' => lang('middle name'),
|
|
|
|
'n_family' => lang('last name'),
|
|
|
|
'n_suffix' => lang('suffix'),
|
|
|
|
'n_fn' => lang('full name'),
|
|
|
|
'n_fileas' => lang('own sorting'),
|
|
|
|
'bday' => lang('birthday'),
|
|
|
|
'org_name' => lang('Company'),
|
|
|
|
'org_unit' => lang('Department'),
|
|
|
|
'title' => lang('Title'),
|
|
|
|
'role' => lang('Role'),
|
|
|
|
'assistent' => lang('Assistent'),
|
|
|
|
'room' => lang('Room'),
|
|
|
|
'adr_one_street' => lang('street').' ('.lang('business').')',
|
|
|
|
'adr_one_street2' => lang('address line 2').' ('.lang('business').')',
|
|
|
|
'adr_one_locality' => lang('city').' ('.lang('business').')',
|
|
|
|
'adr_one_region' => lang('state').' ('.lang('business').')',
|
|
|
|
'adr_one_postalcode' => lang('zip code').' ('.lang('business').')',
|
|
|
|
'adr_one_countryname' => lang('country').' ('.lang('business').')',
|
|
|
|
'label' => lang('label'),
|
|
|
|
'adr_two_street' => lang('street').' ('.lang('private').')',
|
|
|
|
'adr_two_street2' => lang('address line 2').' ('.lang('private').')',
|
|
|
|
'adr_two_locality' => lang('city').' ('.lang('private').')',
|
|
|
|
'adr_two_region' => lang('state').' ('.lang('private').')',
|
|
|
|
'adr_two_postalcode' => lang('zip code').' ('.lang('private').')',
|
|
|
|
'adr_two_countryname' => lang('country').' ('.lang('private').')',
|
|
|
|
'tel_work' => lang('work phone'),
|
|
|
|
'tel_cell' => lang('mobile phone'),
|
|
|
|
'tel_fax' => lang('fax').' ('.lang('business').')',
|
|
|
|
'tel_assistent' => lang('assistent phone'),
|
|
|
|
'tel_car' => lang('car phone'),
|
|
|
|
'tel_pager' => lang('pager'),
|
|
|
|
'tel_home' => lang('home phone'),
|
|
|
|
'tel_fax_home' => lang('fax').' ('.lang('private').')',
|
|
|
|
'tel_cell_private' => lang('mobile phone').' ('.lang('private').')',
|
|
|
|
'tel_other' => lang('other phone'),
|
|
|
|
'tel_prefer' => lang('preferred phone'),
|
|
|
|
'email' => lang('email').' ('.lang('business').')',
|
|
|
|
'email_home' => lang('email').' ('.lang('private').')',
|
|
|
|
'url' => lang('url').' ('.lang('business').')',
|
|
|
|
'url_home' => lang('url').' ('.lang('private').')',
|
|
|
|
'freebusy_uri' => lang('Freebusy URI'),
|
|
|
|
'calendar_uri' => lang('Calendar URI'),
|
|
|
|
'note' => lang('note'),
|
|
|
|
'tz' => lang('time zone'),
|
|
|
|
'geo' => lang('geo'),
|
|
|
|
'pubkey' => lang('public key'),
|
|
|
|
'created' => lang('created'),
|
|
|
|
'creator' => lang('created by'),
|
|
|
|
'modified' => lang('last modified'),
|
|
|
|
'modifier' => lang('last modified by'),
|
|
|
|
'jpegphoto' => lang('photo'),
|
|
|
|
);
|
|
|
|
$this->business_contact_fields = array(
|
|
|
|
'org_name' => lang('Company'),
|
|
|
|
'org_unit' => lang('Department'),
|
|
|
|
'title' => lang('Title'),
|
|
|
|
'role' => lang('Role'),
|
|
|
|
'n_prefix' => lang('prefix'),
|
|
|
|
'n_given' => lang('first name'),
|
|
|
|
'n_middle' => lang('middle name'),
|
|
|
|
'n_family' => lang('last name'),
|
|
|
|
'n_suffix' => lang('suffix'),
|
|
|
|
'adr_one_street' => lang('street').' ('.lang('business').')',
|
|
|
|
'adr_one_street2' => lang('address line 2').' ('.lang('business').')',
|
|
|
|
'adr_one_locality' => lang('city').' ('.lang('business').')',
|
|
|
|
'adr_one_region' => lang('state').' ('.lang('business').')',
|
|
|
|
'adr_one_postalcode' => lang('zip code').' ('.lang('business').')',
|
|
|
|
'adr_one_countryname' => lang('country').' ('.lang('business').')',
|
|
|
|
);
|
|
|
|
$this->home_contact_fields = array(
|
|
|
|
'org_name' => lang('Company'),
|
|
|
|
'org_unit' => lang('Department'),
|
|
|
|
'title' => lang('Title'),
|
|
|
|
'role' => lang('Role'),
|
|
|
|
'n_prefix' => lang('prefix'),
|
|
|
|
'n_given' => lang('first name'),
|
|
|
|
'n_middle' => lang('middle name'),
|
|
|
|
'n_family' => lang('last name'),
|
|
|
|
'n_suffix' => lang('suffix'),
|
|
|
|
'adr_two_street' => lang('street').' ('.lang('business').')',
|
|
|
|
'adr_two_street2' => lang('address line 2').' ('.lang('business').')',
|
|
|
|
'adr_two_locality' => lang('city').' ('.lang('business').')',
|
|
|
|
'adr_two_region' => lang('state').' ('.lang('business').')',
|
|
|
|
'adr_two_postalcode' => lang('zip code').' ('.lang('business').')',
|
|
|
|
'adr_two_countryname' => lang('country').' ('.lang('business').')',
|
|
|
|
);
|
|
|
|
//_debug_array($this->contact_fields);
|
2005-11-04 00:47:52 +01:00
|
|
|
}
|
2006-04-23 16:40:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* calculate the file_as string from the contact and the file_as type
|
|
|
|
*
|
|
|
|
* @param array $contact
|
|
|
|
* @param string $type=null file_as type, default null to read it from the contact, unknown/not set type default to the first one
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function fileas($contact,$type=null)
|
|
|
|
{
|
|
|
|
if (is_null($type)) $type = $contact['fileas_type'];
|
|
|
|
if (!$type || !in_array($type,$this->fileas_types)) $type = $this->fileas_types[0];
|
|
|
|
|
|
|
|
if (strstr($type,'n_fn')) $contact['n_fn'] = $this->fullname($contact);
|
|
|
|
|
|
|
|
return str_replace(array_keys($contact),array_values($contact),$type);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* determine the file_as type from the file_as string and the contact
|
|
|
|
*
|
|
|
|
* @param array $contact
|
|
|
|
* @param string $type=null file_as type, default null to read it from the contact, unknown/not set type default to the first one
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function fileas_type($contact,$file_as=null)
|
|
|
|
{
|
|
|
|
if (is_null($file_as)) $file_as = $contact['n_fileas'];
|
|
|
|
|
|
|
|
if ($file_as)
|
|
|
|
{
|
|
|
|
foreach($this->fileas_types as $type)
|
|
|
|
{
|
|
|
|
if ($this->fileas($contact,$type) == $file_as)
|
|
|
|
{
|
|
|
|
return $type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $this->fileas_types[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get selectbox options for the fileas types with translated labels, or real content
|
|
|
|
*
|
|
|
|
* @param array $contact=null real content to use, default none
|
|
|
|
* @return array with options: fileas type => label pairs
|
|
|
|
*/
|
|
|
|
function fileas_options($contact=null)
|
|
|
|
{
|
|
|
|
$labels = array(
|
|
|
|
'n_prefix' => lang('prefix'),
|
|
|
|
'n_given' => lang('first name'),
|
|
|
|
'n_middle' => lang('middle name'),
|
|
|
|
'n_family' => lang('last name'),
|
|
|
|
'n_suffix' => lang('suffix'),
|
|
|
|
'n_fn' => lang('full name'),
|
2006-04-25 14:43:41 +02:00
|
|
|
'org_name' => lang('company'),
|
2006-04-23 16:40:31 +02:00
|
|
|
);
|
|
|
|
foreach($labels as $name => $label)
|
|
|
|
{
|
|
|
|
if ($contact[$name]) $labels[$name] = $contact[$name];
|
|
|
|
}
|
|
|
|
foreach($this->fileas_types as $fileas_type)
|
|
|
|
{
|
|
|
|
$options[$fileas_type] = $this->fileas($labels,$fileas_type);
|
|
|
|
}
|
|
|
|
return $options;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get full name from the name-parts
|
|
|
|
*
|
|
|
|
* @param array $contact
|
|
|
|
* @return string full name
|
|
|
|
*/
|
|
|
|
function fullname($contact)
|
|
|
|
{
|
|
|
|
$parts = array();
|
|
|
|
foreach(array('n_prefix','n_given','n_middle','n_family','n_suffix') as $n)
|
|
|
|
{
|
|
|
|
if ($contact[$n]) $parts[] = $contact[$n];
|
|
|
|
}
|
|
|
|
return implode(' ',$parts);
|
|
|
|
}
|
|
|
|
|
2006-03-07 23:43:08 +01:00
|
|
|
/**
|
|
|
|
* changes the data from the db-format to your work-format
|
|
|
|
*
|
|
|
|
* it gets called everytime when data is read from the db
|
|
|
|
* This function needs to be reimplemented in the derived class
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
*/
|
|
|
|
function db2data($data)
|
|
|
|
{
|
|
|
|
// convert timestamps from server-time in the db to user-time
|
|
|
|
foreach($this->timestamps as $name)
|
|
|
|
{
|
|
|
|
if(isset($data[$name]))
|
|
|
|
{
|
|
|
|
$data[$name] += $this->tz_offset_s;
|
|
|
|
}
|
|
|
|
}
|
2006-04-23 16:40:31 +02:00
|
|
|
$data['photo'] = $this->photo_src($data['id'],$data['jpegphoto']);
|
|
|
|
|
2006-03-07 23:43:08 +01:00
|
|
|
return $data;
|
|
|
|
}
|
2006-04-23 16:40:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* src for photo: returns array with linkparams if jpeg exists or the $default image-name if not
|
|
|
|
* @param int $id contact_id
|
|
|
|
* @param boolean $jpeg=false jpeg exists or not
|
|
|
|
* @param string $default='' image-name to use if !$jpeg, eg. 'template'
|
|
|
|
* @return string/array
|
|
|
|
*/
|
|
|
|
function photo_src($id,$jpeg,$default='')
|
|
|
|
{
|
|
|
|
return $jpeg ? array(
|
|
|
|
'menuaction' => 'addressbook.uicontacts.photo',
|
|
|
|
'contact_id' => $id,
|
|
|
|
) : $default;
|
|
|
|
}
|
2006-03-07 23:43:08 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* changes the data from your work-format to the db-format
|
|
|
|
*
|
|
|
|
* It gets called everytime when data gets writen into db or on keys for db-searches
|
|
|
|
* this needs to be reimplemented in the derived class
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
*/
|
|
|
|
function data2db($data)
|
|
|
|
{
|
|
|
|
// convert timestamps from user-time to server-time in the db
|
|
|
|
foreach($this->timestamps as $name)
|
|
|
|
{
|
|
|
|
if(isset($data[$name]))
|
|
|
|
{
|
|
|
|
$data[$name] -= $this->tz_offset_s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2005-11-04 00:47:52 +01:00
|
|
|
/**
|
|
|
|
* deletes contact in db
|
|
|
|
*
|
2006-04-23 16:40:31 +02:00
|
|
|
* @param mixed &$contact contact array with key id or (array of) id(s)
|
|
|
|
* @return boolean true on success or false on failiure
|
2005-11-04 00:47:52 +01:00
|
|
|
*/
|
2006-04-23 16:40:31 +02:00
|
|
|
function delete($contact)
|
2005-11-04 00:47:52 +01:00
|
|
|
{
|
2006-04-23 16:40:31 +02:00
|
|
|
if (is_array($contact) && isset($contact['id']))
|
2005-11-04 00:47:52 +01:00
|
|
|
{
|
2006-04-23 16:40:31 +02:00
|
|
|
$contact = array($contact);
|
2005-11-04 00:47:52 +01:00
|
|
|
}
|
2006-04-23 16:40:31 +02:00
|
|
|
elseif (!is_array($contact))
|
2005-11-04 00:47:52 +01:00
|
|
|
{
|
2006-04-23 16:40:31 +02:00
|
|
|
$contact = array($contact);
|
2005-11-04 00:47:52 +01:00
|
|
|
}
|
2006-04-23 16:40:31 +02:00
|
|
|
foreach($contact as $c)
|
2005-11-04 00:47:52 +01:00
|
|
|
{
|
2006-04-23 16:40:31 +02:00
|
|
|
$id = is_array($c) ? $c['id'] : $c;
|
2005-12-23 12:54:51 +01:00
|
|
|
|
2006-04-23 16:40:31 +02:00
|
|
|
if ($this->check_perms(EGW_ACL_DELETE,$c) && parent::delete($id))
|
|
|
|
{
|
|
|
|
$GLOBALS['egw']->contenthistory->updateTimeStamp('contacts', $id, 'delete', time());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2005-11-04 00:47:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* saves contact to db
|
|
|
|
*
|
2006-04-25 14:43:41 +02:00
|
|
|
* @param array &$contact contact array from etemplate::exec
|
2006-06-22 01:12:45 +02:00
|
|
|
* @param boolean $ignore_acl=false should the acl be checked or not
|
2006-07-10 00:58:04 +02:00
|
|
|
* @return int/string/boolean id on success, false on failure, the error-message is in $this->error
|
2005-11-04 00:47:52 +01:00
|
|
|
*/
|
2006-06-22 01:12:45 +02:00
|
|
|
function save(&$contact,$ignore_acl=false)
|
2005-11-04 00:47:52 +01:00
|
|
|
{
|
2005-12-23 12:54:51 +01:00
|
|
|
// stores if we add or update a entry
|
2006-04-23 16:40:31 +02:00
|
|
|
if (!($isUpdate = $contact['id']))
|
2005-11-04 00:47:52 +01:00
|
|
|
{
|
2006-04-23 16:40:31 +02:00
|
|
|
if (!isset($contact['owner'])) $contact['owner'] = $this->user; // write to users personal addressbook
|
|
|
|
$contact['creator'] = $this->user;
|
|
|
|
$contact['created'] = $this->now_su;
|
2006-04-26 23:23:38 +02:00
|
|
|
|
|
|
|
if (!$contact['tid']) $contact['tid'] = 'n';
|
2005-11-04 00:47:52 +01:00
|
|
|
}
|
2006-06-22 01:12:45 +02:00
|
|
|
if(!$ignore_acl && !$this->check_perms($isUpdate ? EGW_ACL_EDIT : EGW_ACL_ADD,$contact))
|
2005-11-04 00:47:52 +01:00
|
|
|
{
|
2006-06-13 23:53:00 +02:00
|
|
|
$this->error = 'access denied';
|
2006-04-23 16:40:31 +02:00
|
|
|
return false;
|
2005-11-04 00:47:52 +01:00
|
|
|
}
|
|
|
|
// convert categories
|
2006-04-23 16:40:31 +02:00
|
|
|
$contact['cat_id'] = is_array($contact['cat_id']) ? implode(',',$contact['cat_id']) : $contact['cat_id'];
|
2005-11-04 00:47:52 +01:00
|
|
|
// last modified
|
2006-04-23 16:40:31 +02:00
|
|
|
$contact['modifier'] = $this->user;
|
|
|
|
$contact['modified'] = $this->now_su;
|
|
|
|
// set full name and fileas from the content
|
2006-06-17 20:50:07 +02:00
|
|
|
if (isset($contact['n_family']) && isset($contact['n_given']))
|
|
|
|
{
|
|
|
|
$contact['n_fn'] = $this->fullname($contact);
|
|
|
|
if (isset($contact['org_name'])) $contact['n_fileas'] = $this->fileas($contact);
|
|
|
|
}
|
2006-07-10 00:46:58 +02:00
|
|
|
// savegard the account_id against changes not triggered by the accounts-class
|
|
|
|
if (isset($contact['account_id']) && !$ignore_acl)
|
|
|
|
{
|
|
|
|
$account_id = $contact['account_id'];
|
|
|
|
unset($contact['account_id']);
|
|
|
|
}
|
2006-06-18 07:03:35 +02:00
|
|
|
// we dont update the content-history, if we run inside setup (admin-account-creation)
|
|
|
|
if(!($this->error = parent::save($contact)) && is_object($GLOBALS['egw']->contenthistory))
|
2005-12-23 12:54:51 +01:00
|
|
|
{
|
2006-04-23 16:40:31 +02:00
|
|
|
$GLOBALS['egw']->contenthistory->updateTimeStamp('contacts', $contact['id'],$isUpdate ? 'modify' : 'add', time());
|
2006-06-22 01:12:45 +02:00
|
|
|
|
|
|
|
if ($contact['account_id']) // invalidate the cache of the accounts class
|
|
|
|
{
|
|
|
|
$GLOBALS['egw']->accounts->cache_invalidate($contact['account_id']);
|
|
|
|
}
|
2005-12-23 12:54:51 +01:00
|
|
|
}
|
2006-07-10 00:46:58 +02:00
|
|
|
// restoring the unset account_id
|
|
|
|
if ($account_id) $contact['account_id'] = $acount_id;
|
|
|
|
|
2006-07-10 00:58:04 +02:00
|
|
|
return $this->error ? false : $contact['id'];
|
2005-11-04 00:47:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* reads contacts matched by key and puts all cols in the data array
|
|
|
|
*
|
2006-04-24 22:52:14 +02:00
|
|
|
* @param int/string $contact_id
|
2006-06-24 18:03:00 +02:00
|
|
|
* @return array/boolean array with contact data, null if not found or false on no view perms
|
2005-11-04 00:47:52 +01:00
|
|
|
*/
|
2006-04-24 22:52:14 +02:00
|
|
|
function read($contact_id)
|
2005-11-04 00:47:52 +01:00
|
|
|
{
|
2006-06-24 18:03:00 +02:00
|
|
|
if (!($data = parent::read($contact_id)))
|
2005-11-04 00:47:52 +01:00
|
|
|
{
|
2006-06-24 18:03:00 +02:00
|
|
|
return null; // not found
|
|
|
|
}
|
|
|
|
if (!$this->check_perms(EGW_ACL_READ,$data))
|
|
|
|
{
|
|
|
|
return false; // no view perms
|
2005-11-04 00:47:52 +01:00
|
|
|
}
|
2006-04-23 16:40:31 +02:00
|
|
|
// determine the file-as type
|
|
|
|
$data['fileas_type'] = $this->fileas_type($data);
|
2005-11-04 00:47:52 +01:00
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
2006-03-14 15:26:55 +01:00
|
|
|
|
2005-11-04 00:47:52 +01:00
|
|
|
/**
|
|
|
|
* Checks if the current user has the necessary ACL rights
|
|
|
|
*
|
2006-04-23 16:40:31 +02:00
|
|
|
* If the access of a contact is set to private, one need a private grant for a personal addressbook
|
|
|
|
* or the group membership for a group-addressbook
|
|
|
|
*
|
2005-11-04 00:47:52 +01:00
|
|
|
* @param int $needed necessary ACL right: EGW_ACL_{READ|EDIT|DELETE}
|
|
|
|
* @param mixed $contact contact as array or the contact-id
|
|
|
|
* @return boolean true permission granted or false for permission denied
|
|
|
|
*/
|
2006-04-26 23:23:38 +02:00
|
|
|
function check_perms($needed,$contact)
|
2005-11-04 00:47:52 +01:00
|
|
|
{
|
2006-07-09 22:07:09 +02:00
|
|
|
if ((!is_array($contact) || !isset($contact['owner'])) &&
|
|
|
|
!($contact = parent::read(is_array($contact) ? $contact['id'] : $contact)))
|
2006-04-23 16:40:31 +02:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$owner = $contact['owner'];
|
|
|
|
|
2006-06-14 18:53:14 +02:00
|
|
|
// allow the user to edit his own account
|
2006-04-23 16:40:31 +02:00
|
|
|
if (!$owner && $needed == EGW_ACL_EDIT && $contact['account_id'] == $this->user)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2006-06-14 18:53:14 +02:00
|
|
|
// dont allow to delete own account (as admin handels it too)
|
|
|
|
if (!$owner && $needed == EGW_ACL_DELETE && $contact['account_id'] == $this->user)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2006-04-23 16:40:31 +02:00
|
|
|
return ($this->grants[$owner] & $needed) &&
|
|
|
|
(!$contact['private'] || ($this->grants[$owner] & EGW_ACL_PRIVATE) || in_array($owner,$this->memberships));
|
|
|
|
}
|
2006-04-30 11:34:24 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Read (virtual) org-entry (values "common" for most contacts in the given org)
|
|
|
|
*
|
|
|
|
* @param string $org_id org_name:oooooo|||org_unit:uuuuuuuuu|||adr_one_locality:lllllll (org_unit and adr_one_locality are optional)
|
|
|
|
* @return array/boolean array with common org fields or false if org not found
|
|
|
|
*/
|
|
|
|
function read_org($org_id)
|
|
|
|
{
|
|
|
|
if (!$org_id) return false;
|
|
|
|
|
|
|
|
$org = array();
|
|
|
|
foreach(explode('|||',$org_id) as $part)
|
|
|
|
{
|
|
|
|
list($name,$value) = explode(':',$part);
|
|
|
|
$org[$name] = $value;
|
|
|
|
}
|
|
|
|
$contacts = parent::search('',$this->org_fields,'','','',false,'AND',false,$org);
|
|
|
|
|
|
|
|
if (!$contacts) return false;
|
|
|
|
|
|
|
|
// create a statistic about the commonness of each fields values
|
|
|
|
$fields = array();
|
|
|
|
foreach($contacts as $contact)
|
|
|
|
{
|
|
|
|
foreach($contact as $name => $value)
|
|
|
|
{
|
|
|
|
$fields[$name][$value]++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
foreach($fields as $name => $values)
|
|
|
|
{
|
|
|
|
if (!in_array($name,$this->org_fields)) continue;
|
|
|
|
|
|
|
|
arsort($values,SORT_NUMERIC);
|
|
|
|
|
|
|
|
list($value,$num) = each($values);
|
|
|
|
//echo "<p>$name: '$value' $num/".count($contacts)."=".($num / (double) count($contacts))." >= $this->org_common_factor = ".($num / (double) count($contacts) >= $this->org_common_factor ? 'true' : 'false')."</p>\n";
|
|
|
|
if ($value && $num / (double) count($contacts) >= $this->org_common_factor)
|
|
|
|
{
|
|
|
|
$org[$name] = $value;
|
|
|
|
}
|
|
|
|
}
|
2006-05-04 09:37:15 +02:00
|
|
|
//echo $org_id; _debug_array($org);
|
2006-04-30 11:34:24 +02:00
|
|
|
|
|
|
|
return $org;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return all org-members with same content in one or more of the given fields (only org_fields are counting)
|
|
|
|
*
|
|
|
|
* @param string $org_name
|
|
|
|
* @param array $fields field-name => value pairs
|
|
|
|
* @return array with contacts
|
|
|
|
*/
|
|
|
|
function org_similar($org_name,$fields)
|
|
|
|
{
|
|
|
|
$criteria = array();
|
|
|
|
foreach($this->org_fields as $name)
|
|
|
|
{
|
|
|
|
if (isset($fields[$name]))
|
|
|
|
{
|
|
|
|
$criteria[$name] = $fields[$name];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return parent::search($criteria,false,'n_family,n_given','','',false,'OR',false,array('org_name'=>$org_name));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the changed fields from two versions of a contact (not modified or modifier)
|
|
|
|
*
|
|
|
|
* @param array $from original/old version of the contact
|
|
|
|
* @param array $to changed/new version of the contact
|
|
|
|
* @param boolean $onld_org_fields=true check and return only org_fields, default true
|
|
|
|
* @return array with field-name => value from $from
|
|
|
|
*/
|
|
|
|
function changed_fields($from,$to,$only_org_fields=true)
|
|
|
|
{
|
|
|
|
$changed = array();
|
|
|
|
foreach($only_org_fields ? $this->org_fields : array_keys($this->contact_fields) as $name)
|
|
|
|
{
|
|
|
|
if (!isset($from[$name]) || in_array($name,array('modified','modifier'))) // never count these
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ((string) $from[$name] != (string) $to[$name])
|
|
|
|
{
|
|
|
|
$changed[$name] = $from[$name];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Change given fields in all members of the org with identical content in the field
|
|
|
|
*
|
|
|
|
* @param string $org_name
|
|
|
|
* @param array $from original/old version of the contact
|
|
|
|
* @param array $to changed/new version of the contact
|
|
|
|
* @param array $members=null org-members to change, default null --> function queries them itself
|
|
|
|
* @return array/boolean (changed-members,changed-fields,failed-members) or false if no org_fields changed or no (other) members matching that fields
|
|
|
|
*/
|
|
|
|
function change_org($org_name,$from,$to,$members=null)
|
|
|
|
{
|
|
|
|
if (!($changed = $this->changed_fields($from,$to,true))) return false;
|
|
|
|
|
|
|
|
if (is_null($members) || !is_array($members))
|
|
|
|
{
|
|
|
|
$members = $this->org_similar($org_name,$changed);
|
|
|
|
}
|
|
|
|
if (!$members) return false;
|
|
|
|
|
|
|
|
$changed_members = $changed_fields = $failed_members = 0;
|
|
|
|
foreach($members as $member)
|
|
|
|
{
|
|
|
|
$fields = 0;
|
|
|
|
foreach($changed as $name => $value)
|
|
|
|
{
|
|
|
|
if ((string)$value == (string)$member[$name])
|
|
|
|
{
|
|
|
|
$member[$name] = $to[$name];
|
|
|
|
//echo "<p>$member[n_family], $member[n_given]: $name='{$to[$name]}'</p>\n";
|
|
|
|
++$fields;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($fields)
|
|
|
|
{
|
|
|
|
if (!$this->check_perms(EGW_ACL_EDIT,$member) || !$this->save($member))
|
|
|
|
{
|
|
|
|
++$failed_members;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++$changed_members;
|
|
|
|
$changed_fields += $fields;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return array($changed_members,$changed_fields,$failed_members);
|
|
|
|
}
|
2006-04-23 16:40:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* get title for a contact identified by $contact
|
|
|
|
*
|
|
|
|
* Is called as hook to participate in the linking
|
|
|
|
*
|
|
|
|
* @param int/string/array $contact int/string id or array with contact
|
2006-06-24 18:03:00 +02:00
|
|
|
* @param string/boolean string with the title, null if contact does not exitst, false if no perms to view it
|
2006-04-23 16:40:31 +02:00
|
|
|
*/
|
|
|
|
function link_title($contact)
|
|
|
|
{
|
|
|
|
if (!is_array($contact) && $contact)
|
2005-11-04 00:47:52 +01:00
|
|
|
{
|
2006-04-23 16:40:31 +02:00
|
|
|
$contact = $this->read($contact);
|
2005-11-04 00:47:52 +01:00
|
|
|
}
|
2006-04-23 16:40:31 +02:00
|
|
|
if (!is_array($contact))
|
2005-11-04 00:47:52 +01:00
|
|
|
{
|
2006-06-24 18:03:00 +02:00
|
|
|
return $contact;
|
2005-11-04 00:47:52 +01:00
|
|
|
}
|
2006-06-24 18:03:00 +02:00
|
|
|
return $contact['n_fileas'] ? $contact['n_fileas'] : $this->fileas($contact);
|
2006-04-23 16:40:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* query addressbook for contacts matching $pattern
|
|
|
|
*
|
|
|
|
* Is called as hook to participate in the linking
|
|
|
|
*
|
|
|
|
* @param string $pattern pattern to search
|
|
|
|
* @return array with id - title pairs of the matching entries
|
|
|
|
*/
|
|
|
|
function link_query($pattern)
|
|
|
|
{
|
|
|
|
$result = $criteria = array();
|
|
|
|
if ($pattern)
|
2005-11-04 00:47:52 +01:00
|
|
|
{
|
2006-04-23 16:40:31 +02:00
|
|
|
foreach($this->columns_to_search as $col)
|
|
|
|
{
|
|
|
|
$criteria[$col] = $pattern;
|
|
|
|
}
|
|
|
|
}
|
2006-05-04 09:37:15 +02:00
|
|
|
foreach((array) parent::search($criteria,false,'org_name,n_family,n_given','','%',false,'OR') as $contact)
|
2006-04-23 16:40:31 +02:00
|
|
|
{
|
|
|
|
$result[$contact['id']] = $this->link_title($contact);
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hook called by link-class to include calendar in the appregistry of the linkage
|
|
|
|
*
|
|
|
|
* @param array/string $location location and other parameters (not used)
|
|
|
|
* @return array with method-names
|
|
|
|
*/
|
|
|
|
function search_link($location)
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
'query' => 'addressbook.bocontacts.link_query',
|
|
|
|
'title' => 'addressbook.bocontacts.link_title',
|
|
|
|
'view' => array(
|
|
|
|
'menuaction' => 'addressbook.uicontacts.view'
|
|
|
|
),
|
|
|
|
'view_id' => 'contact_id',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-04-25 14:43:41 +02:00
|
|
|
* Delete contact linked to account, called by delete-account hook, when an account get deleted
|
2006-04-23 16:40:31 +02:00
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
*/
|
|
|
|
function deleteaccount($data)
|
|
|
|
{
|
|
|
|
// delete/move personal addressbook
|
|
|
|
parent::deleteaccount($data);
|
|
|
|
|
|
|
|
// delete contact linked to account
|
|
|
|
if (($contact_id = $GLOBALS['egw']->accounts->id2name($data['account_id'],'person_id')))
|
|
|
|
{
|
|
|
|
$this->delete($contact_id);
|
2005-11-04 00:47:52 +01:00
|
|
|
}
|
|
|
|
}
|
2006-04-25 14:43:41 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Update contact if linked account get updated, called by edit-account hook, when an account get edited
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
*/
|
|
|
|
function editaccount($data)
|
|
|
|
{
|
|
|
|
//echo "bocontacts::editaccount()"; _debug_array($data);
|
|
|
|
|
|
|
|
// check if account is linked to a contact
|
|
|
|
if (($contact_id = $GLOBALS['egw']->accounts->id2name($data['account_id'],'person_id')) &&
|
|
|
|
($contact = $this->read($contact_id)))
|
|
|
|
{
|
|
|
|
$need_update = false;
|
|
|
|
foreach(array(
|
|
|
|
'n_family' => 'lastname',
|
|
|
|
'n_given' => 'firstname',
|
|
|
|
'email' => 'email',
|
|
|
|
) as $cname => $aname)
|
|
|
|
{
|
|
|
|
if ($contact[$cname] != $data[$aname]) $need_update = true;
|
|
|
|
|
|
|
|
$contact[$cname] = $data[$aname];
|
|
|
|
}
|
|
|
|
if ($need_update)
|
|
|
|
{
|
|
|
|
$this->save($contact);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-02-16 12:50:16 +01:00
|
|
|
}
|