table_home_friend.php
4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
/**
* [Discuz!] (C)2001-2099 Comsenz Inc.
* This is NOT a freeware, use is subject to license terms
*
* $Id: table_home_friend.php 31044 2012-07-12 01:50:32Z chenmengshu $
*/
if(!defined('IN_DISCUZ')) {
exit('Access Denied');
}
class table_home_friend extends discuz_table
{
public function __construct() {
$this->_table = 'home_friend';
$this->_pk = 'uid';
parent::__construct();
}
public function fetch_all_by_uid_username($uid, $username, $start = 0, $limit = 0) {
return DB::fetch_all('SELECT * FROM %t WHERE uid=%d AND fusername IN (%n) %i', array($this->_table, $uid, $username, DB::limit($start, $limit)));
}
public function fetch_all_by_uid_fuid($uid, $fuid) {
if(!$uid || !$fuid) {
return null;
}
return DB::fetch_all('SELECT * FROM %t WHERE uid=%d AND fuid IN (%n)', array($this->_table, $uid, $fuid));
}
public function fetch_all_by_uid_gid($uid, $gid, $start = 0, $limit = 100, $order = true) {
$parameter = array($this->_table, $uid, $gid);
if($order) {
$limitsql = ' ORDER BY num DESC, dateline DESC';
}
if($limit) {
$parameter[] = DB::limit($start, $limit);
$limitsql .= ' %i';
}
return DB::fetch_all('SELECT * FROM %t WHERE uid=%d AND gid=%d'.$limitsql, $parameter);
}
public function fetch_all_by_uid_common($uid, $fuid) {
return DB::fetch_all('SELECT * FROM %t WHERE uid=%d OR uid=%d', array($this->_table, $uid, $fuid));
}
public function fetch_all_by_uid($uid, $start, $limit, $order = false) {
return $this->fetch_all_search($uid, '', '', false, $start, $limit, $order);
}
public function fetch_all_search($uid, $gid, $searchkey, $count = false, $start = 0, $limit = 0, $order = false) {
$parameter = array($this->_table);
$wherearr = array();
$parameter[] = $uid;
$wherearr[] = is_array($uid) ? 'uid IN(%n)' : 'uid=%d';
if(is_numeric($gid) && $gid > -1) {
$parameter[] = $gid;
$wherearr[] = 'gid=%d';
}
if($searchkey) {
$field = "fusername LIKE '{text}%'";
$keyword = $searchkey;
if(preg_match("(AND|\+|&|\s)", $keyword) && !preg_match("(OR|\|)", $keyword)) {
$andor = ' AND ';
$keywordsrch = '1';
$keyword = preg_replace("/( AND |&| )/is", "+", $keyword);
} else {
$andor = ' OR ';
$keywordsrch = '0';
$keyword = preg_replace("/( OR |\|)/is", "+", $keyword);
}
$keyword = str_replace('*', '%', addcslashes(daddslashes($keyword), '%_'));
foreach(explode('+', $keyword) as $text) {
$text = trim($text);
if($text) {
$keywordsrch .= $andor;
$keywordsrch .= str_replace('{text}', $text, $field);
}
}
$parameter[] = " ($keywordsrch)";
$wherearr[] = '%i';
}
if(!$count) {
if($order) {
$limitsql = ' ORDER BY num DESC, dateline DESC';
}
if($limit) {
$parameter[] = DB::limit($start, $limit);
$limitsql .= ' %i';
}
}
$wheresql = !empty($wherearr) && is_array($wherearr) ? ' WHERE '.implode(' AND ', $wherearr) : '';
if($count) {
return DB::result_first('SELECT COUNT(*) FROM %t '.$wheresql, $parameter);
} else {
return DB::fetch_all('SELECT * FROM %t '.$wheresql.$limitsql, $parameter);
}
}
public function count_by_uid($uid) {
return DB::result_first('SELECT COUNT(*) FROM %t WHERE uid=%d', array($this->_table, $uid));
}
public function delete_by_uid_fuid($uids) {
if(!$uids) {
return null;
}
return DB::delete($this->_table, DB::field('uid', $uids).' OR '.DB::field('fuid', $uids));
}
public function delete_by_uid_fuid_dual($uids, $touid) {
if(!$uids || !$touid) {
return null;
}
return DB::delete($this->_table, '('.DB::field('uid', $uids).' AND '.DB::field('fuid', $touid).') OR ('.DB::field('fuid', $uids).' AND '.DB::field('uid', $touid).')');
}
public function update_num_by_uid_fuid($incnum, $uid, $fuid) {
return DB::query('UPDATE %t SET num=num+\'%d\' WHERE uid=%d AND fuid=%d', array($this->_table, $incnum, $uid, $fuid));
}
public function update_by_uid_fuid($uid, $fuid, $data) {
if(!$uid || !$fuid || empty($data) || !is_array($data)) {
return null;
}
return DB::update($this->_table, $data, DB::field('uid', $uid).' AND '.DB::field('fuid', $fuid));
}
}
?>