| [ Index ] |
|
Code source de eGroupWare 1.2.106-2 |
1 <?php 2 /**************************************************************************\ 3 * eGroupWare - eGroupWare Interapplication Links * 4 * http://www.egroupware.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: class.solink.inc.php 20295 2006-02-15 12:31:25Z $ */ 14 15 /** 16 * generalized linking between entries of eGroupware apps - SO layer 17 * 18 * Links have two ends each pointing to an entry, each entry is a double: 19 * - app app-name or directory-name of an egw application, eg. 'infolog' 20 * - id this is the id, eg. an integer or a tupple like '0:INBOX:1234' 21 * 22 * All vars passed to this class get correct escaped to prevent query insertion. 23 * 24 * @package API 25 * @subpackage link 26 * @author RalfBecker-At-outdoor-training.de 27 * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License 28 */ 29 class solink 30 { 31 var $db; 32 var $user; 33 var $link_table = 'egw_links'; 34 var $debug; 35 36 /** 37 * constructor 38 */ 39 function solink( ) 40 { 41 $this->db = clone($GLOBALS['egw']->db); 42 $this->db->set_app('phpgwapi'); 43 $this->user = $GLOBALS['egw_info']['user']['account_id']; 44 } 45 46 /** 47 * creats a link between $app1,$id1 and $app2,$id2 48 * 49 * @param string $app1 appname of 1. endpoint of the link 50 * @param string $id1 id in $app1 51 * @param string $app2 appname of 2. endpoint of the link 52 * @param string $id2 id in $app2 53 * @param string $remark='' Remark to be saved with the link (defaults to '') 54 * @param int $owner=0 Owner of the link (defaults to user) 55 * @return boolean/int False (for db or param-error) or link_id for success 56 */ 57 function link( $app1,$id1,$app2,$id2,$remark='',$owner=0,$lastmod=0 ) 58 { 59 if ($this->debug) 60 { 61 echo "<p>solink.link('$app1',$id1,'$app2',$id2,'$remark',$owner)</p>\n"; 62 } 63 if ($app1 == $app2 && $id1 == $id2 || 64 $id1 == '' || $id2 == '' || $app1 == '' || $app2 == '') 65 { 66 return False; // dont link to self or other nosense 67 } 68 if ($link = $this->get_link($app1,$id1,$app2,$id2)) 69 { 70 if ($link['link_remark'] != $remark) 71 { 72 $this->update_remark($link['link_id'],$remark); 73 } 74 return $link['link_id']; // link alread exist 75 } 76 if (!$owner) 77 { 78 $owner = $this->user; 79 } 80 return $this->db->insert($this->link_table,array( 81 'link_app1' => $app1, 82 'link_id1' => $id1, 83 'link_app2' => $app2, 84 'link_id2' => $id2, 85 'link_remark' => $remark, 86 'link_lastmod' => $lastmod ? $lastmod : time(), 87 'link_owner' => $owner, 88 ),False,__LINE__,__FILE__) ? $this->db->get_last_insert_id($this->link_table,'link_id') : false; 89 } 90 91 /** 92 * update the remark of a link 93 * 94 * @param int $link_id link to update 95 * @param string $remark new text for the remark 96 * @return boolean true on success, else false 97 */ 98 function update_remark($link_id,$remark) 99 { 100 return $this->db->update($this->link_table,array( 101 'link_remark' => $remark, 102 'link_lastmod' => time(), 103 ),array( 104 'link_id' => $link_id, 105 ),__LINE__,__FILE__); 106 } 107 108 /** 109 * returns array of links to $app,$id 110 * 111 * @param string $app appname 112 * @param string $id id in $app 113 * @param string $only_app if set return only links from $only_app (eg. only addressbook-entries) or NOT from if $only_app[0]=='!' 114 * @param string $order defaults to newest links first 115 * @return array of links (only_app: ids) or empty array if no matching links found 116 */ 117 function get_links( $app,$id,$only_app='',$order='link_lastmod DESC' ) 118 { 119 if ($this->debug) 120 { 121 echo "<p>solink.get_links($app,$id,$only_app,$order)</p>\n"; 122 } 123 $links = array(); 124 125 $this->db->select($this->link_table,'*',$this->db->expression($this->link_table,'(',array( 126 'link_app1' => $app, 127 'link_id1' => $id, 128 ),') OR (',array( 129 'link_app2' => $app, 130 'link_id2' => $id, 131 ),')' 132 ),__LINE__,__FILE__,False,$order ? " ORDER BY $order" : ''); 133 134 $this->db->query($sql,__LINE__,__FILE__); 135 136 if ($not_only = $only_app[0] == '!') 137 { 138 $only_app = substr($only_app,1); 139 } 140 while ($this->db->next_record()) 141 { 142 $row = $this->db->Record; 143 144 if ($row['link_app1'] == $app AND $row['link_id1'] == $id) 145 { 146 $link = array( 147 'app' => $row['link_app2'], 148 'id' => $row['link_id2'] 149 ); 150 } 151 else 152 { 153 $link = array( 154 'app' => $row['link_app1'], 155 'id' => $row['link_id1'] 156 ); 157 } 158 if ($only_app && $not_only == ($link['app'] == $only_app) || 159 !$GLOBALS['egw_info']['user']['apps'][$link['app']]) 160 { 161 continue; 162 } 163 $link['remark'] = $row['link_remark']; 164 $link['owner'] = $row['link_owner']; 165 $link['lastmod'] = $row['link_lastmod']; 166 $link['link_id'] = $row['link_id']; 167 168 $links[$row['link_id']] = $only_app && !$not_only ? $link['id'] : $link; 169 } 170 return $links; 171 } 172 173 /** 174 * returns data of a link 175 * 176 * @param ing/string $app_link_id > 0 link_id of link or app-name of link 177 * @param string $id='' id in $app, if no integer link_id given in $app_link_id 178 * @param string $app2='' appname of 2. endpoint of the link, if no integer link_id given in $app_link_id 179 * @param string $id2='' id in $app2, if no integer link_id given in $app_link_id 180 * @return array with link-data or False 181 */ 182 function get_link($app_link_id,$id='',$app2='',$id2='') 183 { 184 if ($this->debug) 185 { 186 echo "<p>solink.get_link('$app_link_id',$id,'$app2','$id2')</p>\n"; 187 } 188 if ((int) $app_link_id > 0) 189 { 190 $where = array('link_id' => $app_link_id); 191 } 192 else 193 { 194 if ($app_link_id == '' || $id == '' || $app2 == '' || $id2 == '') 195 { 196 return False; 197 } 198 $vars2addslashes = array('app_link_id','id','app2','id2'); 199 foreach ($vars2addslashes as $var) 200 { 201 $$var = $this->db->db_addslashes($$var); 202 } 203 $where = $this->db->expression($this->link_table,'(',array( 204 'link_app1' => $app_link_id, 205 'link_id1' => $id, 206 'link_app2' => $app2, 207 'link_id2' => $id2, 208 ),') OR (',array( 209 'link_app2' => $app_link_id, 210 'link_id2' => $id, 211 'link_app1' => $app2, 212 'link_id1' => $id2, 213 ),')'); 214 } 215 $this->db->select($this->link_table,'*',$where,__LINE__,__FILE__); 216 217 if ($this->db->next_record()) 218 { 219 if ($this->debug) 220 { 221 _debug_array($this->db->Record); 222 } 223 return $this->db->Record; 224 } 225 return False; 226 } 227 228 /** 229 * Remove link with $link_id or all links matching given params 230 * 231 * @param $link_id link-id to remove if > 0 232 * @param string $app='' app-name of links to remove 233 * @param string $id='' id in $app or '' remove all links from $app 234 * @param int $owner=0 account_id to delete all links of a given owner, or 0 235 * @param string $app2='' appname of 2. endpoint of the link 236 * @param string $id2='' id in $app2 237 * @return array with deleted links 238 */ 239 function &unlink($link_id,$app='',$id='',$owner=0,$app2='',$id2='') 240 { 241 if ($this->debug) 242 { 243 echo "<p>solink.unlink($link_id,$app,$id,$owner,$app2,$id2)</p>\n"; 244 } 245 if ((int)$link_id > 0) 246 { 247 $where = array('link_id' => $link_id); 248 } 249 elseif ($app == '' AND $owner == '') 250 { 251 return 0; 252 } 253 else 254 { 255 if ($app != '' && $app2 == '') 256 { 257 $check1 = array('link_app1' => $app); 258 $check2 = array('link_app2' => $app); 259 if ($id != '') 260 { 261 $check1['link_id1'] = $id; 262 $check2['link_id2'] = $id; 263 } 264 $where = $this->db->expression($this->link_table,'((',$check1,') OR (',$check2,'))'); 265 } 266 elseif ($app != '' && $app2 != '') 267 { 268 $where = $this->db->expression($this->link_table,'(',array( 269 'link_app1' => $app, 270 'link_id1' => $id, 271 'link_app2' => $app2, 272 'link_id2' => $id2, 273 ),') OR (',array( 274 'link_app1' => $app2, 275 'link_id1' => $id2, 276 'link_app2' => $app, 277 'link_id2' => $id, 278 ),')'); 279 } 280 if ($owner) 281 { 282 if ($app) $where = array($where); 283 $where['link_owner'] = $owner; 284 } 285 } 286 $this->db->select($this->link_table,'*',$where,__LINE__,__FILE__); 287 $deleted = array(); 288 while (($row = $this->db->row(true))) 289 { 290 $deleted[] = $row; 291 } 292 $this->db->delete($this->link_table,$where,__LINE__,__FILE__); 293 294 return $deleted; 295 } 296 297 /** 298 * Changes ownership of all links from $owner to $new_owner 299 * 300 * This is needed when a user/account gets deleted 301 * Does NOT change the modification-time 302 * 303 * @param int $owner acount_id of owner to change 304 * @param int $new_owner account_id of new owner 305 * @return int number of links changed 306 */ 307 function chown($owner,$new_owner) 308 { 309 if ((int)$owner <= 0 || (int) $new_owner <= 0) 310 { 311 return 0; 312 } 313 $this->db->update($this->link_table,array('owner'=>$new_owner),array('owner'=>$owner),__LINE__,__FILE__); 314 315 return $this->db->affected_rows(); 316 } 317 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Sun Feb 25 17:20:01 2007 | par Balluche grâce à PHPXref 0.7 |