1<?php
2
3/*
4	[UCenter] (C)2001-2099 Comsenz Inc.
5	This is NOT a freeware, use is subject to license terms
6
7	$Id: friend.php 1059 2011-03-01 07:25:09Z monkey $
8*/
9
10!defined('IN_UC') && exit('Access Denied');
11
12class friendmodel {
13
14	var $db;
15	var $base;
16
17	function __construct(&$base) {
18		$this->friendmodel($base);
19	}
20
21	function friendmodel(&$base) {
22		$this->base = $base;
23		$this->db = $base->db;
24	}
25
26	function add($uid, $friendid, $comment='') {
27		$direction = $this->db->result_first("SELECT direction FROM ".UC_DBTABLEPRE."friends WHERE uid='$friendid' AND friendid='$uid' LIMIT 1");
28		if($direction == 1) {
29			$this->db->query("INSERT INTO ".UC_DBTABLEPRE."friends SET uid='$uid', friendid='$friendid', comment='$comment', direction='3'", 'SILENT');
30			$this->db->query("UPDATE ".UC_DBTABLEPRE."friends SET direction='3' WHERE uid='$friendid' AND friendid='$uid'");
31			return 1;
32		} elseif($direction == 2) {
33			return 1;
34		} elseif($direction == 3) {
35			return -1;
36		} else {
37			$this->db->query("INSERT INTO ".UC_DBTABLEPRE."friends SET uid='$uid', friendid='$friendid', comment='$comment', direction='1'", 'SILENT');
38			return $this->db->insert_id();
39		}
40	}
41
42	function delete($uid, $friendids) {
43		$friendids = $this->base->implode($friendids);
44		$this->db->query("DELETE FROM ".UC_DBTABLEPRE."friends WHERE uid='$uid' AND friendid IN ($friendids)");
45		$affectedrows = $this->db->affected_rows();
46		if($affectedrows > 0) {
47			$this->db->query("UPDATE ".UC_DBTABLEPRE."friends SET direction=1 WHERE uid IN ($friendids) AND friendid='$uid' AND direction='3'");
48		}
49		return $affectedrows;
50	}
51
52	function get_totalnum_by_uid($uid, $direction = 0) {
53		$sqladd = '';
54		if($direction == 0) {
55			$sqladd = "uid='$uid'";
56		} elseif($direction == 1) {
57			$sqladd = "uid='$uid' AND direction='1'";
58		} elseif($direction == 2) {
59			$sqladd = "friendid='$uid' AND direction='1'";
60		} elseif($direction == 3) {
61			$sqladd = "uid='$uid' AND direction='3'";
62		}
63		$totalnum = $this->db->result_first("SELECT COUNT(*) FROM ".UC_DBTABLEPRE."friends WHERE $sqladd");
64		return $totalnum;
65	}
66
67	function get_list($uid, $page, $pagesize, $totalnum, $direction = 0) {
68		$start = $this->base->page_get_start($page, $pagesize, $totalnum);
69		$sqladd = '';
70		if($direction == 0) {
71			$sqladd = "f.uid='$uid'";
72		} elseif($direction == 1) {
73			$sqladd = "f.uid='$uid' AND f.direction='1'";
74		} elseif($direction == 2) {
75			$sqladd = "f.friendid='$uid' AND f.direction='1'";
76		} elseif($direction == 3) {
77			$sqladd = "f.uid='$uid' AND f.direction='3'";
78		}
79		if($sqladd) {
80			$data = $this->db->fetch_all("SELECT f.*, m.username FROM ".UC_DBTABLEPRE."friends f LEFT JOIN ".UC_DBTABLEPRE."members m ON f.friendid=m.uid WHERE $sqladd LIMIT $start, $pagesize");
81			return $data;
82		} else {
83			return array();
84		}
85	}
86
87	function is_friend($uid, $friendids, $direction = 0) {
88		$friendid_str = implode("', '", $friendids);
89		$sqladd = '';
90		if($direction == 0) {
91			$sqladd = "uid='$uid'";
92		} elseif($direction == 1) {
93			$sqladd = "uid='$uid' AND friendid IN ('$friendid_str') AND direction='1'";
94		} elseif($direction == 2) {
95			$sqladd = "friendid='$uid' AND uid IN ('$friendid_str') AND direction='1'";
96		} elseif($direction == 3) {
97			$sqladd = "uid='$uid' AND friendid IN ('$friendid_str') AND direction='3'";
98		}
99		if($this->db->result_first("SELECT COUNT(*) FROM ".UC_DBTABLEPRE."friends WHERE $sqladd") == count($friendids)) {
100			return true;
101		} else {
102			return false;
103		}
104	}
105}
106
107?>