2002-09-01 20:59:01 +02:00
|
|
|
<?php
|
|
|
|
/**************************************************************************\
|
2004-08-09 13:43:57 +02:00
|
|
|
* eGroupWare - InfoLog Links *
|
|
|
|
* http://www.egroupware.org *
|
2002-09-01 20:59:01 +02:00
|
|
|
* Written by Ralf Becker <RalfBecker@outdoor-training.de> *
|
|
|
|
* -------------------------------------------- *
|
|
|
|
* This program is free software; you can redistribute it and/or modify it *
|
|
|
|
* under the terms of the GNU General Public License as published by the *
|
|
|
|
* Free Software Foundation; either version 2 of the License, or (at your *
|
|
|
|
* option) any later version. *
|
|
|
|
\**************************************************************************/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
2005-04-06 13:05:57 +02:00
|
|
|
/**
|
2005-04-08 20:50:51 +02:00
|
|
|
* generalized linking between entries of eGroupware apps - BO layer
|
2005-04-06 13:05:57 +02:00
|
|
|
*
|
2005-04-08 20:50:51 +02:00
|
|
|
* This class is the SO-layer of the links
|
|
|
|
*
|
|
|
|
* Links have two ends each pointing to an entry, each entry is a double:
|
|
|
|
* - app app-name or directory-name of an egw application, eg. 'infolog'
|
|
|
|
* - id this is the id, eg. an integer or a tupple like '0:INBOX:1234'
|
|
|
|
*
|
|
|
|
* All vars passed to this class get correct escaped to prevent query insertion.
|
|
|
|
*
|
|
|
|
* @package infolog
|
|
|
|
* @subpackage link
|
|
|
|
* @author RalfBecker-At-outdoor-training.de
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
2005-04-06 13:05:57 +02:00
|
|
|
*/
|
2005-04-08 20:50:51 +02:00
|
|
|
class solink
|
2002-09-01 20:59:01 +02:00
|
|
|
{
|
2002-09-04 01:09:21 +02:00
|
|
|
var $db;
|
2002-09-01 20:59:01 +02:00
|
|
|
var $user;
|
2004-09-19 18:15:53 +02:00
|
|
|
var $link_table = 'phpgw_links';
|
2003-02-22 13:04:12 +01:00
|
|
|
var $debug;
|
2002-09-01 20:59:01 +02:00
|
|
|
|
2005-04-06 13:05:57 +02:00
|
|
|
/**
|
|
|
|
* constructor
|
|
|
|
*/
|
2002-09-01 20:59:01 +02:00
|
|
|
function solink( )
|
|
|
|
{
|
2005-04-06 13:05:57 +02:00
|
|
|
$this->db = clone($GLOBALS['egw']->db);
|
2004-09-19 18:15:53 +02:00
|
|
|
$this->db->set_app('infolog');
|
2005-04-06 13:05:57 +02:00
|
|
|
$this->user = $GLOBALS['egw_info']['user']['account_id'];
|
2002-09-01 20:59:01 +02:00
|
|
|
}
|
|
|
|
|
2005-04-06 13:05:57 +02:00
|
|
|
/**
|
|
|
|
* creats a link between $app1,$id1 and $app2,$id2
|
|
|
|
*
|
2005-04-08 20:50:51 +02:00
|
|
|
* @param string $app1 appname of 1. endpoint of the link
|
|
|
|
* @param string $id1 id in $app1
|
|
|
|
* @param string $app2 appname of 2. endpoint of the link
|
|
|
|
* @param string $id2 id in $app2
|
|
|
|
* @param string $remark='' Remark to be saved with the link (defaults to '')
|
|
|
|
* @param int $owner=0 Owner of the link (defaults to user)
|
|
|
|
* @return boolean/int False (for db or param-error) or link_id for success
|
2005-04-06 13:05:57 +02:00
|
|
|
*/
|
2002-10-09 02:37:00 +02:00
|
|
|
function link( $app1,$id1,$app2,$id2,$remark='',$owner=0,$lastmod=0 )
|
2002-09-01 20:59:01 +02:00
|
|
|
{
|
2002-10-20 17:06:52 +02:00
|
|
|
if ($this->debug)
|
2002-09-04 01:09:21 +02:00
|
|
|
{
|
|
|
|
echo "<p>solink.link('$app1',$id1,'$app2',$id2,'$remark',$owner)</p>\n";
|
|
|
|
}
|
2002-09-01 20:59:01 +02:00
|
|
|
if ($app1 == $app2 && $id1 == $id2 ||
|
|
|
|
$id1 == '' || $id2 == '' || $app1 == '' || $app2 == '')
|
|
|
|
{
|
2002-10-18 00:02:44 +02:00
|
|
|
return False; // dont link to self or other nosense
|
|
|
|
}
|
2003-06-14 15:51:53 +02:00
|
|
|
if ($link = $this->get_link($app1,$id1,$app2,$id2))
|
2002-10-18 00:02:44 +02:00
|
|
|
{
|
|
|
|
return $link['link_id']; // link alread exist
|
2002-09-01 20:59:01 +02:00
|
|
|
}
|
|
|
|
if (!$owner)
|
|
|
|
{
|
|
|
|
$owner = $this->user;
|
|
|
|
}
|
2004-09-19 18:15:53 +02:00
|
|
|
return $this->db->insert($this->link_table,array(
|
|
|
|
'link_app1' => $app1,
|
|
|
|
'link_id1' => $id1,
|
|
|
|
'link_app2' => $app2,
|
|
|
|
'link_id2' => $id2,
|
|
|
|
'link_remark' => $remark,
|
|
|
|
'link_lastmod' => $lastmod ? $lastmod : time(),
|
|
|
|
'link_owner' => $owner,
|
|
|
|
),False,__LINE__,__FILE__) ? $this->db->get_last_insert_id($this->link_table,'link_id') : false;
|
2002-09-01 20:59:01 +02:00
|
|
|
}
|
|
|
|
|
2005-04-06 13:05:57 +02:00
|
|
|
/**
|
|
|
|
* returns array of links to $app,$id
|
|
|
|
*
|
2005-04-08 20:50:51 +02:00
|
|
|
* @param string $app appname
|
|
|
|
* @param string $id id in $app
|
|
|
|
* @param string $only_app if set return only links from $only_app (eg. only addressbook-entries) or NOT from if $only_app[0]=='!'
|
|
|
|
* @param string $order defaults to newest links first
|
2005-04-06 13:05:57 +02:00
|
|
|
* @return array of links (only_app: ids) or empty array if no matching links found
|
|
|
|
*/
|
2002-09-01 20:59:01 +02:00
|
|
|
function get_links( $app,$id,$only_app='',$order='link_lastmod DESC' )
|
|
|
|
{
|
|
|
|
if ($this->debug)
|
|
|
|
{
|
2004-09-19 18:15:53 +02:00
|
|
|
echo "<p>solink.get_links($app,$id,$only_app,$order)</p>\n";
|
2002-09-01 20:59:01 +02:00
|
|
|
}
|
2004-09-19 18:15:53 +02:00
|
|
|
$links = array();
|
|
|
|
|
|
|
|
$this->db->select($this->link_table,'*',$this->db->expression($this->link_table,'(',array(
|
|
|
|
'link_app1' => $app,
|
|
|
|
'link_id1' => $id,
|
|
|
|
),') OR (',array(
|
|
|
|
'link_app2' => $app,
|
|
|
|
'link_id2' => $id,
|
|
|
|
),')'
|
|
|
|
),__LINE__,__FILE__,False,$order ? " ORDER BY $order" : '');
|
|
|
|
|
2002-10-18 00:02:44 +02:00
|
|
|
$this->db->query($sql,__LINE__,__FILE__);
|
2002-09-01 20:59:01 +02:00
|
|
|
|
2002-09-04 01:09:21 +02:00
|
|
|
if ($not_only = $only_app[0] == '!')
|
|
|
|
{
|
|
|
|
$only_app = substr($only_app,1);
|
|
|
|
}
|
2002-09-01 20:59:01 +02:00
|
|
|
while ($this->db->next_record())
|
|
|
|
{
|
|
|
|
$row = $this->db->Record;
|
|
|
|
|
2002-10-16 02:22:04 +02:00
|
|
|
if ($row['link_app1'] == $app AND $row['link_id1'] == $id)
|
2002-09-01 20:59:01 +02:00
|
|
|
{
|
|
|
|
$link = array(
|
|
|
|
'app' => $row['link_app2'],
|
2003-06-14 15:51:53 +02:00
|
|
|
'id' => $row['link_id2']
|
2002-09-01 20:59:01 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$link = array(
|
|
|
|
'app' => $row['link_app1'],
|
2003-06-14 15:51:53 +02:00
|
|
|
'id' => $row['link_id1']
|
2002-09-01 20:59:01 +02:00
|
|
|
);
|
|
|
|
}
|
2002-10-09 02:37:00 +02:00
|
|
|
if ($only_app && $not_only == ($link['app'] == $only_app) ||
|
2005-04-06 13:05:57 +02:00
|
|
|
!$GLOBALS['egw_info']['user']['apps'][$link['app']])
|
2002-09-01 20:59:01 +02:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2003-06-14 15:51:53 +02:00
|
|
|
$link['remark'] = $row['link_remark'];
|
2002-09-01 20:59:01 +02:00
|
|
|
$link['owner'] = $row['link_owner'];
|
|
|
|
$link['lastmod'] = $row['link_lastmod'];
|
|
|
|
$link['link_id'] = $row['link_id'];
|
|
|
|
|
2005-04-08 20:50:51 +02:00
|
|
|
$links[$row['link_id']] = $only_app && !$not_only ? $link['id'] : $link;
|
2002-09-01 20:59:01 +02:00
|
|
|
}
|
|
|
|
return $links;
|
|
|
|
}
|
2002-10-18 00:02:44 +02:00
|
|
|
|
2005-04-06 13:05:57 +02:00
|
|
|
/**
|
|
|
|
* returns data of a link
|
|
|
|
*
|
2005-04-08 20:50:51 +02:00
|
|
|
* @param ing/string $app_link_id > 0 link_id of link or app-name of link
|
|
|
|
* @param string $id='' id in $app, if no integer link_id given in $app_link_id
|
|
|
|
* @param string $app2='' appname of 2. endpoint of the link, if no integer link_id given in $app_link_id
|
|
|
|
* @param string $id2='' id in $app2, if no integer link_id given in $app_link_id
|
2005-04-06 13:05:57 +02:00
|
|
|
* @return array with link-data or False
|
|
|
|
*/
|
2002-10-18 00:02:44 +02:00
|
|
|
function get_link($app_link_id,$id='',$app2='',$id2='')
|
|
|
|
{
|
2002-10-18 22:20:35 +02:00
|
|
|
if ($this->debug)
|
|
|
|
{
|
|
|
|
echo "<p>solink.get_link('$app_link_id',$id,'$app2','$id2')</p>\n";
|
|
|
|
}
|
2004-09-19 18:15:53 +02:00
|
|
|
if ((int) $app_link_id > 0)
|
2002-10-18 00:02:44 +02:00
|
|
|
{
|
2004-09-19 18:15:53 +02:00
|
|
|
$where = array('link_id' => $app_link_id);
|
2002-10-18 00:02:44 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ($app_link_id == '' || $id == '' || $app2 == '' || $id2 == '')
|
|
|
|
{
|
|
|
|
return False;
|
|
|
|
}
|
2003-06-14 15:51:53 +02:00
|
|
|
$vars2addslashes = array('app_link_id','id','app2','id2');
|
|
|
|
foreach ($vars2addslashes as $var)
|
|
|
|
{
|
|
|
|
$$var = $this->db->db_addslashes($$var);
|
|
|
|
}
|
2004-09-19 18:15:53 +02:00
|
|
|
$where = $this->db->expression($this->link_table,'(',array(
|
|
|
|
'link_app1' => $app_link_id,
|
|
|
|
'link_id1' => $id,
|
|
|
|
'link_app2' => $app2,
|
|
|
|
'link_id2' => $id2,
|
|
|
|
),') OR (',array(
|
|
|
|
'link_app2' => $app_link_id,
|
|
|
|
'link_id2' => $id,
|
|
|
|
'link_app1' => $app2,
|
|
|
|
'link_id1' => $id2,
|
|
|
|
),')');
|
2002-10-18 00:02:44 +02:00
|
|
|
}
|
2004-09-19 18:15:53 +02:00
|
|
|
$this->db->select($this->link_table,'*',$where,__LINE__,__FILE__);
|
2002-10-18 00:02:44 +02:00
|
|
|
|
|
|
|
if ($this->db->next_record())
|
|
|
|
{
|
2002-10-18 22:20:35 +02:00
|
|
|
if ($this->debug)
|
|
|
|
{
|
|
|
|
_debug_array($this->db->Record);
|
|
|
|
}
|
2002-10-18 00:02:44 +02:00
|
|
|
return $this->db->Record;
|
|
|
|
}
|
|
|
|
return False;
|
|
|
|
}
|
2002-09-01 20:59:01 +02:00
|
|
|
|
2005-04-06 13:05:57 +02:00
|
|
|
/**
|
|
|
|
* Remove link with $link_id or all links matching given params
|
|
|
|
*
|
|
|
|
* @param $link_id link-id to remove if > 0
|
2005-04-08 20:50:51 +02:00
|
|
|
* @param string $app='' app-name of links to remove
|
|
|
|
* @param string $id='' id in $app or '' remove all links from $app
|
|
|
|
* @param int $owner=0 account_id to delete all links of a given owner, or 0
|
|
|
|
* @param string $app2='' appname of 2. endpoint of the link
|
|
|
|
* @param string $id2='' id in $app2
|
|
|
|
* @return array with deleted links
|
2005-04-06 13:05:57 +02:00
|
|
|
*/
|
2005-04-08 20:50:51 +02:00
|
|
|
function &unlink($link_id,$app='',$id='',$owner=0,$app2='',$id2='')
|
2002-09-01 20:59:01 +02:00
|
|
|
{
|
2004-09-19 18:15:53 +02:00
|
|
|
if ($this->debug)
|
|
|
|
{
|
|
|
|
echo "<p>solink.unlink($link_id,$app,$id,$owner,$app2,$id2)</p>\n";
|
|
|
|
}
|
|
|
|
if ((int)$link_id > 0)
|
2002-09-01 20:59:01 +02:00
|
|
|
{
|
2004-09-19 18:15:53 +02:00
|
|
|
$where = array('link_id' => $link_id);
|
2002-09-01 20:59:01 +02:00
|
|
|
}
|
|
|
|
elseif ($app == '' AND $owner == '')
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2002-10-09 02:37:00 +02:00
|
|
|
if ($app != '' && $app2 == '')
|
2002-09-01 20:59:01 +02:00
|
|
|
{
|
2004-09-19 18:15:53 +02:00
|
|
|
$check1 = array('link_app1' => $app);
|
|
|
|
$check2 = array('link_app2' => $app);
|
2002-09-01 20:59:01 +02:00
|
|
|
if ($id != '')
|
|
|
|
{
|
2004-09-19 18:15:53 +02:00
|
|
|
$check1['link_id1'] = $id;
|
|
|
|
$check2['link_id2'] = $id;
|
2002-09-01 20:59:01 +02:00
|
|
|
}
|
2004-09-19 18:15:53 +02:00
|
|
|
$where = $this->db->expression($this->link_table,'((',$check1,') OR (',$check2,'))');
|
2002-09-01 20:59:01 +02:00
|
|
|
}
|
2002-10-09 02:37:00 +02:00
|
|
|
elseif ($app != '' && $app2 != '')
|
|
|
|
{
|
2004-09-19 18:15:53 +02:00
|
|
|
$where = $this->db->expression($this->link_table,'((',array(
|
|
|
|
'link_app1' => $app,
|
|
|
|
'link_id1' => $id,
|
|
|
|
'link_app2' => $app2,
|
|
|
|
'link_id2' => $id2,
|
|
|
|
),') OR (',array(
|
|
|
|
'link_app1' => $app2,
|
|
|
|
'link_id1' => $id2,
|
|
|
|
'link_app2' => $app,
|
|
|
|
'link_id2' => $id,
|
|
|
|
),')');
|
2002-10-09 02:37:00 +02:00
|
|
|
}
|
2005-04-08 20:50:51 +02:00
|
|
|
if ($owner)
|
2002-09-01 20:59:01 +02:00
|
|
|
{
|
2004-09-19 18:15:53 +02:00
|
|
|
if ($app) $where = array($where);
|
|
|
|
$where['link_owner'] = $owner;
|
2002-09-01 20:59:01 +02:00
|
|
|
}
|
|
|
|
}
|
2005-04-08 20:50:51 +02:00
|
|
|
$this->db->select($this->link_table,'*',$where,__LINE__,__FILE__);
|
|
|
|
$deleted = array();
|
|
|
|
while (($row = $this->db->row(true)))
|
|
|
|
{
|
|
|
|
$deleted[] = $row;
|
|
|
|
}
|
2004-09-19 18:15:53 +02:00
|
|
|
$this->db->delete($this->link_table,$where,__LINE__,__FILE__);
|
2002-09-01 20:59:01 +02:00
|
|
|
|
2005-04-08 20:50:51 +02:00
|
|
|
return $deleted;
|
2002-09-01 20:59:01 +02:00
|
|
|
}
|
|
|
|
|
2005-04-06 13:05:57 +02:00
|
|
|
/**
|
|
|
|
* Changes ownership of all links from $owner to $new_owner
|
|
|
|
*
|
|
|
|
* This is needed when a user/account gets deleted
|
|
|
|
* Does NOT change the modification-time
|
2005-04-08 20:50:51 +02:00
|
|
|
*
|
|
|
|
* @param int $owner acount_id of owner to change
|
|
|
|
* @param int $new_owner account_id of new owner
|
|
|
|
* @return int number of links changed
|
2005-04-06 13:05:57 +02:00
|
|
|
*/
|
2002-09-01 20:59:01 +02:00
|
|
|
function chown($owner,$new_owner)
|
|
|
|
{
|
2005-04-08 20:50:51 +02:00
|
|
|
if ((int)$owner <= 0 || (int) $new_owner <= 0)
|
2002-09-01 20:59:01 +02:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2004-09-19 18:15:53 +02:00
|
|
|
$this->db->update($this->link_table,array('owner'=>$new_owner),array('owner'=>$owner),__LINE__,__FILE__);
|
2002-09-01 20:59:01 +02:00
|
|
|
|
|
|
|
return $this->db->affected_rows();
|
|
|
|
}
|
|
|
|
}
|