1 |
<?php |
2 |
/**************************************************************************\ |
3 |
* phpGroupWare - InfoLog Links * |
4 |
* http://www.phpgroupware.org * |
5 |
* Written by Ralf Becker <RalfBecker@outdoor-training.de> * |
6 |
* -------------------------------------------- * |
7 |
* This program is free software; you can redistribute it and/or modify it * |
8 |
* under the terms of the GNU General Public License as published by the * |
9 |
* Free Software Foundation; either version 2 of the License, or (at your * |
10 |
* option) any later version. * |
11 |
\**************************************************************************/ |
12 |
|
13 |
/* $Id$ */ |
14 |
|
15 |
$GLOBALS['phpgw_info']['flags']['included_classes']['solink'] = True; |
16 |
/*! |
17 |
@class solink |
18 |
@author ralfbecker |
19 |
@copyright GPL - GNU General Public License |
20 |
@abstract generalized linking between entries of phpGroupware apps - DB layer |
21 |
@discussion This class is to access the links in the DB<br> |
22 |
Links have to ends each pointing two an entry, each entry is a double:<br> |
23 |
app app-name or directory-name of an phpgw application, eg. 'infolog'<br> |
24 |
id this is the id, eg. an integer or a tupple like '0:INBOX:1234' |
25 |
@note All vars passed to this class are run either through addslashes or intval |
26 |
to prevent query insertion and to get pgSql 7.3 compatibility. |
27 |
*/ |
28 |
class solink // DB-Layer |
29 |
{ |
30 |
var $public_functions = array |
31 |
( |
32 |
'link' => True, |
33 |
'get_links' => True, |
34 |
'unlink' => True, |
35 |
'chown' => True, |
36 |
'get_link' => True |
37 |
); |
38 |
var $db; |
39 |
var $user; |
40 |
var $db_name = 'phpgw_links'; |
41 |
var $debug; |
42 |
|
43 |
/*! |
44 |
@function solink |
45 |
@syntax solink( ) |
46 |
@author ralfbecker |
47 |
@abstract constructor |
48 |
*/ |
49 |
function solink( ) |
50 |
{ |
51 |
$this->db = $GLOBALS['phpgw']->db; |
52 |
$this->user = $GLOBALS['phpgw_info']['user']['account_id']; |
53 |
} |
54 |
|
55 |
/*! |
56 |
@function link |
57 |
@syntax link( $app1,$id1,$app2,$id2,$remark='',$user=0 ) |
58 |
@author ralfbecker |
59 |
@abstract creats a link between $app1,$id1 and $app2,$id2 |
60 |
@param $remark Remark to be saved with the link (defaults to '') |
61 |
@param $owner Owner of the link (defaults to user) |
62 |
@discussion Does NOT check if link already exists |
63 |
@result False (for db or param-error) or link_id for success |
64 |
*/ |
65 |
function link( $app1,$id1,$app2,$id2,$remark='',$owner=0,$lastmod=0 ) |
66 |
{ |
67 |
if ($this->debug) |
68 |
{ |
69 |
echo "<p>solink.link('$app1',$id1,'$app2',$id2,'$remark',$owner)</p>\n"; |
70 |
} |
71 |
if ($app1 == $app2 && $id1 == $id2 || |
72 |
$id1 == '' || $id2 == '' || $app1 == '' || $app2 == '') |
73 |
{ |
74 |
return False; // dont link to self or other nosense |
75 |
} |
76 |
if ($link = $this->get_link($app1,$id1,$app2,$id2)) |
77 |
{ |
78 |
return $link['link_id']; // link alread exist |
79 |
} |
80 |
if (!$owner) |
81 |
{ |
82 |
$owner = $this->user; |
83 |
} |
84 |
$vars2addslashes = array('app1','id1','app2','id2','remark'); |
85 |
foreach ($vars2addslashes as $var) |
86 |
{ |
87 |
$$var = $this->db->db_addslashes($$var); |
88 |
} |
89 |
if (!$lastmod) |
90 |
{ |
91 |
$lastmod = time(); |
92 |
} |
93 |
$sql = "INSERT INTO $this->db_name (link_app1,link_id1,link_app2,link_id2,link_remark,link_lastmod,link_owner) ". |
94 |
" VALUES ('$app1','$id1','$app2','$id2','$remark',".intval($lastmod).','.intval($owner).')'; |
95 |
|
96 |
if ($this->debug) |
97 |
{ |
98 |
echo "<p>solink.link($app1,$id1,$app2,$id2,'$remark',$owner) sql='$sql'</p>\n"; |
99 |
} |
100 |
$this->db->query($sql,__LINE__,__FILE__); |
101 |
|
102 |
return $this->db->errno ? False : $this->db->get_last_insert_id($this->db_name,'link_id'); |
103 |
} |
104 |
|
105 |
/*! |
106 |
@function get_links |
107 |
@syntax get_links( $app,$id,$only_app='',$only_name='',$order='link_lastmod DESC' ) |
108 |
@author ralfbecker |
109 |
@abstract returns array of links to $app,$id |
110 |
@param $only_app if set return only links from $only_app (eg. only addressbook-entries) or NOT from if $only_app[0]=='!' |
111 |
@param $order defaults to newest links first |
112 |
@result array of links (only_app: ids) or empty array if no matching links found |
113 |
*/ |
114 |
function get_links( $app,$id,$only_app='',$order='link_lastmod DESC' ) |
115 |
{ |
116 |
$links = array(); |
117 |
|
118 |
$vars2addslashes = array('app','id','only_app','order'); |
119 |
foreach ($vars2addslashes as $var) |
120 |
{ |
121 |
$$var = $this->db->db_addslashes($$var); |
122 |
} |
123 |
$sql = "SELECT * FROM $this->db_name". |
124 |
" WHERE (link_app1 = '$app' AND link_id1 = '$id')". |
125 |
" OR (link_app2 = '$app' AND link_id2 = '$id')". |
126 |
($order != '' ? " ORDER BY $order" : ''); |
127 |
|
128 |
if ($this->debug) |
129 |
{ |
130 |
echo "<p>solink.get_links($app,$id,$only_app,$order) sql='$sql'</p>\n"; |
131 |
} |
132 |
$this->db->query($sql,__LINE__,__FILE__); |
133 |
|
134 |
if ($not_only = $only_app[0] == '!') |
135 |
{ |
136 |
$only_app = substr($only_app,1); |
137 |
} |
138 |
while ($this->db->next_record()) |
139 |
{ |
140 |
$row = $this->db->Record; |
141 |
|
142 |
if ($row['link_app1'] == $app AND $row['link_id1'] == $id) |
143 |
{ |
144 |
$link = array( |
145 |
'app' => $row['link_app2'], |
146 |
'id' => $row['link_id2'] |
147 |
); |
148 |
} |
149 |
else |
150 |
{ |
151 |
$link = array( |
152 |
'app' => $row['link_app1'], |
153 |
'id' => $row['link_id1'] |
154 |
); |
155 |
} |
156 |
if ($only_app && $not_only == ($link['app'] == $only_app) || |
157 |
!$GLOBALS['phpgw_info']['user']['apps'][$link['app']]) |
158 |
{ |
159 |
continue; |
160 |
} |
161 |
$link['remark'] = $row['link_remark']; |
162 |
$link['owner'] = $row['link_owner']; |
163 |
$link['lastmod'] = $row['link_lastmod']; |
164 |
$link['link_id'] = $row['link_id']; |
165 |
|
166 |
$links[] = $only_app && !$not_only ? $link['id'] : $link; |
167 |
} |
168 |
return $links; |
169 |
} |
170 |
|
171 |
/*! |
172 |
@function get_link |
173 |
@syntax get_link( $app_link_id,$id='',$app2='',$id2='' ) |
174 |
@author ralfbecker |
175 |
@abstract returns data of a link |
176 |
@param $app_link_id > 0 link_id of link or app-name of link |
177 |
@param $id,$app2,$id2 other param of the link if not link_id given |
178 |
@result array with link-data or False |
179 |
*/ |
180 |
function get_link($app_link_id,$id='',$app2='',$id2='') |
181 |
{ |
182 |
if ($this->debug) |
183 |
{ |
184 |
echo "<p>solink.get_link('$app_link_id',$id,'$app2','$id2')</p>\n"; |
185 |
} |
186 |
$sql = "SELECT * FROM $this->db_name WHERE "; |
187 |
if (intval($app_link_id) > 0) |
188 |
{ |
189 |
$sql .= 'link_id='.intval($app_link_id); |
190 |
} |
191 |
else |
192 |
{ |
193 |
if ($app_link_id == '' || $id == '' || $app2 == '' || $id2 == '') |
194 |
{ |
195 |
return False; |
196 |
} |
197 |
$vars2addslashes = array('app_link_id','id','app2','id2'); |
198 |
foreach ($vars2addslashes as $var) |
199 |
{ |
200 |
$$var = $this->db->db_addslashes($$var); |
201 |
} |
202 |
$sql .= "(link_app1='$app_link_id' AND link_id1='$id' AND link_app2='$app2' AND link_id2='$id2') OR". |
203 |
"(link_app2='$app_link_id' AND link_id2='$id' AND link_app1='$app2' AND link_id1='$id2')"; |
204 |
} |
205 |
$this->db->query($sql,__LINE__,__FILE__); |
206 |
|
207 |
if ($this->db->next_record()) |
208 |
{ |
209 |
if ($this->debug) |
210 |
{ |
211 |
_debug_array($this->db->Record); |
212 |
} |
213 |
return $this->db->Record; |
214 |
} |
215 |
return False; |
216 |
} |
217 |
|
218 |
/*! |
219 |
@function unlink |
220 |
@syntax unlink( $link_id,$app='',$id='',$owner='',$app2='',$id2='' ) |
221 |
@author ralfbecker |
222 |
@abstract Remove link with $link_id or all links matching given params |
223 |
@param $link_id link-id to remove if > 0 |
224 |
@param $app,$id,$owner,$app2,$id2 if $link_id <= 0: removes all links matching the non-empty params |
225 |
@result the number of links deleted |
226 |
*/ |
227 |
function unlink($link_id,$app='',$id='',$owner='',$app2='',$id2='') |
228 |
{ |
229 |
$sql = "DELETE FROM $this->db_name WHERE "; |
230 |
if (intval($link_id) > 0) |
231 |
{ |
232 |
$sql .= 'link_id='.intval($link_id); |
233 |
} |
234 |
elseif ($app == '' AND $owner == '') |
235 |
{ |
236 |
return 0; |
237 |
} |
238 |
else |
239 |
{ |
240 |
$vars2addslashes = array('app','id','app2','id2'); |
241 |
foreach ($vars2addslashes as $var) |
242 |
{ |
243 |
$$var = $this->db->db_addslashes($$var); |
244 |
} |
245 |
if ($app != '' && $app2 == '') |
246 |
{ |
247 |
$sql .= "((link_app1='$app'"; |
248 |
$sql2 = ''; |
249 |
if ($id != '') |
250 |
{ |
251 |
$sql .= " AND link_id1='$id'"; |
252 |
$sql2 .= " AND link_id2='$id'"; |
253 |
} |
254 |
$sql .= ") OR (link_app2='$app'$sql2))"; |
255 |
} |
256 |
elseif ($app != '' && $app2 != '') |
257 |
{ |
258 |
$sql .= "((link_app1='$app' AND link_id1='$id' AND link_app2='$app2' AND link_id2='$id2') OR"; |
259 |
$sql .= " (link_app1='$app2' AND link_id1='$id2' AND link_app2='$app' AND link_id2='$id'))"; |
260 |
} |
261 |
if ($owner != '') |
262 |
{ |
263 |
$sql .= ($app != '' ? ' AND ' : '') . 'link_owner='.intval($owner); |
264 |
} |
265 |
} |
266 |
if ($this->debug) |
267 |
{ |
268 |
echo "<p>solink.unlink($link_id,$app,$id,$owner,$app2,$id2) sql='$sql'</p>\n"; |
269 |
} |
270 |
$this->db->query($sql,__LINE__,__FILE__); |
271 |
|
272 |
return $this->db->affected_rows(); |
273 |
} |
274 |
|
275 |
/*! |
276 |
@function chown |
277 |
@syntax chown( $owner,$new_owner ) |
278 |
@author ralfbecker |
279 |
@abstract Changes ownership of all links from $owner to $new_owner |
280 |
@discussion This is needed when a user/account gets deleted |
281 |
@discussion Does NOT change the modification-time |
282 |
@result the number of links changed |
283 |
*/ |
284 |
function chown($owner,$new_owner) |
285 |
{ |
286 |
if (intval($owner) <= 0 || intval($new_owner) <= 0) |
287 |
{ |
288 |
return 0; |
289 |
} |
290 |
$this->db->query("UPDATE $this->db_name SET owner=".intval($new_owner).' WHERE owner='.intval($owner),__LINE__,__FILE__); |
291 |
|
292 |
return $this->db->affected_rows(); |
293 |
} |
294 |
} |
295 |
|
296 |
|
297 |
|