1) apps can register a callback to receive notifications of new or deleted links to their application or updates in the content of the linked entries (requires to linked app to call the new bolink::notify_update($app,$id,$data=null) method)

2) documentation update in solink
This commit is contained in:
Ralf Becker 2005-04-08 18:50:51 +00:00
parent 167d7e7288
commit 47326b007e
2 changed files with 130 additions and 56 deletions

View File

@ -85,6 +85,9 @@
),
*/
);
var $public_functions = array( // functions callable via menuaction
'get_file' => True
);
var $vfs;
var $vfs_basedir='/infolog'; // might changes to links if class gets imported in the api
var $vfs_appname='file'; // pseudo-appname for own file-attachments in vfs, this is NOT the vfs-app
@ -97,13 +100,7 @@
function bolink( )
{
$this->solink( ); // call constructor of derived class
$this->public_functions += array( // extend the public_functions of solink
'query' => True,
'title' => True,
'view' => True,
'get_file' => True
);
//$this->vfs =& CreateObject('infolog.vfs');
$this->vfs =& CreateObject('phpgwapi.vfs');
$config =& CreateObject('phpgwapi.config');
@ -130,7 +127,7 @@
}
}
}
/**
* creats a link between $app1,$id1 and $app2,$id2 - $id1 does NOT need to exist yet
*
@ -204,6 +201,10 @@
{
$link_id = solink::link($app1,$id1,$link['app'],$link['id'],
$link['remark'],$link['owner'],$link['lastmod']);
// notify both sides
$this->notify('link',$link['app'],$link['id'],$app1,$id1,$link_id);
$this->notify('link',$app1,$id1,$link['app'],$link['id'],$link_id);
}
}
return $link_id;
@ -216,7 +217,12 @@
{
return $this->attach_file($app1,$id1,$id2,$remark);
}
return solink::link($app1,$id1,$app2,$id2,$remark,$owner);
$link_id = solink::link($app1,$id1,$app2,$id2,$remark,$owner);
$this->notify('link',$app2,$id2,$app1,$id1,$link_id);
$this->notify('link',$app1,$id1,$app2,$id2,$link_id);
return $link_id;
}
/**
@ -358,7 +364,12 @@
{
$this->delete_attached($app,$id); // deleting all attachments
}
return solink::unlink($link_id,$app,$id,$owner,$app2,$id2);
$deleted =& solink::unlink($link_id,$app,$id,$owner,$app2,$id2);
// only notify on real links, not the one cached for writing or fileattachments
$this->notify_unlink($deleted);
return count($deleted);
}
if (isset($id[$link_id]))
{
@ -1026,4 +1037,63 @@
}
return $content;
}
/**
* notify other apps about changed content in $app,$id
*
* @param string $app name of app in which the updated happend
* @param string $id id in $app of the updated entry
* @param array $data=null updated data of changed entry, as the read-method of the BO-layer would supply it
*/
function notify_update($app,$id,$data=null)
{
foreach($this->get_links($app,$id,'!'.$this->vfs_appname) as $link_id => $link)
{
$this->notify('update',$link['app'],$link['id'],$app,$id,$link_id,$data);
}
}
/**
* notify an application about a new or deleted links to own entries or updates in the content of the linked entry
*
* Please note: not all apps supply update notifications
*
* @internal
* @param string $type 'link' for new links, 'unlink' for unlinked entries, 'update' of content in linked entries
* @param string $notify_app app to notify
* @param string $notify_id id in $notify_app
* @param string $target_app name of app whos entry changed, linked or deleted
* @param string $target_id id in $target_app
* @param array $data=null data of entry in app2 (optional)
*/
function notify($type,$notify_app,$notify_id,$target_app,$target_id,$link_id,$data=null)
{
if ($link_id && isset($this->app_register[$notify_app]) && isset($this->app_register[$notify_app]['notify']))
{
ExecMethod($this->app_register[$notify_app]['notify'],array(
'type' => $type,
'id' => $notify_id,
'target_app' => $target_app,
'target_id' => $target_id,
'link_id' => $link_id,
'data' => $data,
));
}
}
/**
* notifies about unlinked links
*
* @internal
* @param array &$links unlinked links from the database
*/
function notify_unlink(&$links)
{
foreach($links as $link)
{
// we notify both sides of the link, as the unlink command NOT clearly knows which side initiated the unlink
$this->notify('unlink',$link['link_app1'],$link['link_id1'],$link['link_app2'],$link['link_id2'],$link['link_id']);
$this->notify('unlink',$link['link_app2'],$link['link_id2'],$link['link_app1'],$link['link_id1'],$link['link_id']);
}
}
}

View File

@ -12,38 +12,31 @@
/* $Id$ */
$GLOBALS['egw_info']['flags']['included_classes']['solink'] = True;
/**
* @author ralfbecker
* @copyright GPL - GNU General Public License
* generalized linking between entries of eGroupware apps - DB layer
* generalized linking between entries of eGroupware apps - BO layer
*
* This class is to access the links in the DB<br>
* * Links have to ends each pointing two an entry, each entry is a double:<br>
* * app app-name or directory-name of an egw application, eg. 'infolog'<br>
* * id this is the id, eg. an integer or a tupple like '0:INBOX:1234'
* All vars passed to this class are run either through addslashes or intval
* * to prevent query insertion and to get pgSql 7.3 compatibility.
* 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
*/
class solink // DB-Layer
class solink
{
var $public_functions = array
(
'link' => True,
'get_links' => True,
'unlink' => True,
'chown' => True,
'get_link' => True
);
var $db;
var $user;
var $link_table = 'phpgw_links';
var $debug;
/**
* @author ralfbecker
* constructor
*
*/
function solink( )
{
@ -53,13 +46,15 @@
}
/**
* @author ralfbecker
* creats a link between $app1,$id1 and $app2,$id2
*
* @param $remark Remark to be saved with the link (defaults to '')
* @param $owner Owner of the link (defaults to user)
* Does NOT check if link already exists
* @return False (for db or param-error) or link_id for success
* @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
*/
function link( $app1,$id1,$app2,$id2,$remark='',$owner=0,$lastmod=0 )
{
@ -92,11 +87,12 @@
}
/**
* @author ralfbecker
* returns array of links to $app,$id
*
* @param $only_app if set return only links from $only_app (eg. only addressbook-entries) or NOT from if $only_app[0]=='!'
* @param $order defaults to newest links first
* @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
* @return array of links (only_app: ids) or empty array if no matching links found
*/
function get_links( $app,$id,$only_app='',$order='link_lastmod DESC' )
@ -150,17 +146,18 @@
$link['lastmod'] = $row['link_lastmod'];
$link['link_id'] = $row['link_id'];
$links[] = $only_app && !$not_only ? $link['id'] : $link;
$links[$row['link_id']] = $only_app && !$not_only ? $link['id'] : $link;
}
return $links;
}
/**
* @author ralfbecker
* returns data of a link
*
* @param $app_link_id > 0 link_id of link or app-name of link
* @param $id,$app2,$id2 other param of the link if not link_id given
* @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
* @return array with link-data or False
*/
function get_link($app_link_id,$id='',$app2='',$id2='')
@ -210,20 +207,22 @@
}
/**
* @author ralfbecker
* Remove link with $link_id or all links matching given params
*
* @param $link_id link-id to remove if > 0
* @param $app,$id,$owner,$app2,$id2 if $link_id <= 0: removes all links matching the non-empty params
* @return the number of links deleted
* @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
*/
function unlink($link_id,$app='',$id='',$owner='',$app2='',$id2='')
function &unlink($link_id,$app='',$id='',$owner=0,$app2='',$id2='')
{
if ($this->debug)
{
echo "<p>solink.unlink($link_id,$app,$id,$owner,$app2,$id2)</p>\n";
}
$sql = "DELETE FROM $this->link_table WHERE ";
if ((int)$link_id > 0)
{
$where = array('link_id' => $link_id);
@ -259,28 +258,36 @@
'link_id2' => $id,
),')');
}
if ($owner != '')
if ($owner)
{
if ($app) $where = array($where);
$where['link_owner'] = $owner;
}
}
$this->db->select($this->link_table,'*',$where,__LINE__,__FILE__);
$deleted = array();
while (($row = $this->db->row(true)))
{
$deleted[] = $row;
}
$this->db->delete($this->link_table,$where,__LINE__,__FILE__);
return $this->db->affected_rows();
return $deleted;
}
/**
* @author ralfbecker
* 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
* @return the number of links changed
*
* @param int $owner acount_id of owner to change
* @param int $new_owner account_id of new owner
* @return int number of links changed
*/
function chown($owner,$new_owner)
{
if ((int)$owner <= 0 || (int) $new_owner)
if ((int)$owner <= 0 || (int) $new_owner <= 0)
{
return 0;
}
@ -289,6 +296,3 @@
return $this->db->affected_rows();
}
}