1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13:
14: class Folks_Friends_sql extends Folks_Friends {
15:
16: 17: 18: 19: 20:
21: private $_db;
22:
23: 24: 25: 26: 27: 28:
29: private $_write_db;
30:
31: 32: 33: 34: 35:
36: protected function __construct($params = array())
37: {
38: parent::__construct($params);
39:
40: $this->_params = $params;
41: $this->_connect();
42: }
43:
44: 45: 46: 47: 48:
49: protected function _getBlacklist()
50: {
51: $query = 'SELECT friend_uid FROM ' . $this->_params['blacklist']
52: . ' WHERE user_uid = ? '
53: . ' ORDER BY friend_uid ASC';
54:
55: return $this->_db->getCol($query, 0, array($this->_user));
56: }
57:
58: 59: 60: 61: 62:
63: protected function _addBlacklisted($user)
64: {
65: $query = 'INSERT INTO ' . $this->_params['blacklist']
66: . ' (user_uid, friend_uid) VALUES (?, ?)';
67:
68: return $this->_write_db->query($query, array($this->_user, $user));
69: }
70:
71: 72: 73: 74: 75:
76: protected function _removeBlacklisted($user)
77: {
78: $query = 'DELETE FROM ' . $this->_params['blacklist']
79: . ' WHERE user_uid = ? AND friend_uid = ?';
80:
81: return $this->_write_db->query($query, array($this->_user, $user));
82: }
83:
84: 85: 86: 87: 88:
89: protected function _addFriend($friend)
90: {
91: $approve = $this->needsApproval($friend) ? 1 : 0;
92:
93: $query = 'INSERT INTO ' . $this->_params['friends']
94: . ' (user_uid, friend_uid, friend_ask) VALUES (?, ?, ?)';
95:
96: return $this->_write_db->query($query, array($this->_user, $friend, $approve));
97: }
98:
99: 100: 101: 102: 103:
104: protected function _approveFriend($friend)
105: {
106: $query = 'UPDATE ' . $this->_params['friends']
107: . ' SET friend_ask = ? WHERE user_uid = ? AND friend_uid = ?';
108:
109: $result = $this->_write_db->query($query, array(0, $friend, $this->_user));
110: if ($result instanceof PEAR_Error) {
111: return $result;
112: }
113:
114:
115: $query = 'REPLACE INTO ' . $this->_params['friends']
116: . ' (user_uid, friend_uid, friend_ask) VALUES (?, ?, ?)';
117:
118: return $this->_write_db->query($query, array($this->_user, $friend, 0));
119: }
120:
121: 122: 123: 124: 125:
126: protected function _removeFriend($friend)
127: {
128: $query = 'DELETE FROM ' . $this->_params['friends']
129: . ' WHERE user_uid = ? AND friend_uid = ?';
130:
131: return $this->_write_db->query($query, array($this->_user, $friend));
132: }
133:
134: 135: 136: 137: 138:
139: protected function _getFriends()
140: {
141: $query = 'SELECT friend_uid FROM ' . $this->_params['friends']
142: . ' WHERE user_uid = ? and friend_ask = ?'
143: . ' ORDER BY friend_uid ASC';
144:
145: return $this->_db->getCol($query, 0, array($this->_user, 0));
146: }
147:
148: 149: 150:
151: public function waitingApprovalFrom()
152: {
153: $query = 'SELECT friend_uid FROM ' . $this->_params['friends']
154: . ' WHERE user_uid = ? AND friend_ask = ?'
155: . ' ORDER BY friend_uid ASC';
156:
157: return $this->_db->getCol($query, 0, array($this->_user, 1));
158: }
159:
160: 161: 162:
163: public function waitingApprovalFor()
164: {
165: $query = 'SELECT user_uid FROM ' . $this->_params['friends']
166: . ' WHERE friend_uid = ? AND friend_ask = ?'
167: . ' ORDER BY user_uid ASC';
168:
169: return $this->_db->getCol($query, 0, array($this->_user, 1));
170: }
171:
172: 173: 174: 175: 176:
177: public function friendOf()
178: {
179: $query = 'SELECT user_uid FROM ' . $this->_params['friends']
180: . ' WHERE friend_uid = ? AND friend_ask = ?'
181: . ' ORDER BY friend_uid ASC';
182:
183: return $this->_db->getCol($query, 0, array($this->_user, 0));
184: }
185:
186: 187: 188:
189: protected function _getGroups()
190: {
191: return array(_("Friends"));
192: }
193:
194: 195: 196: 197: 198: 199:
200: protected function _connect()
201: {
202: $this->_params = array_merge(array(
203: 'blacklist' => 'folks_blacklist',
204: 'friends' => 'folks_friends'
205: ), $this->_params);
206:
207: $this->_db = $GLOBALS['injector']->getInstance('Horde_Core_Factory_DbPear')->create('read', 'folks', 'storage');
208: $this->_write_db = $GLOBALS['injector']->getInstance('Horde_Core_Factory_DbPear')->create('rw', 'folks', 'storage');
209:
210: return true;
211: }
212: }
213: