|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Model for friend management |
|
5
|
|
|
*/ |
|
6
|
|
|
class Friend_model extends CI_Model { |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Get Friends |
|
10
|
|
|
* |
|
11
|
|
|
* Gets list of friends and their associated groups |
|
12
|
|
|
* @return mixed |
|
13
|
|
|
*/ |
|
14
|
1 |
|
public function get_friends() |
|
15
|
|
|
{ |
|
16
|
1 |
|
$user_id = $this->session->userdata('uid'); |
|
17
|
|
|
|
|
18
|
|
|
//Get the list of friends |
|
19
|
1 |
|
$friends = $this->db |
|
20
|
1 |
|
->select('user_friend_id,user_friend_link.user_id as uid,user.username,user.email') |
|
21
|
1 |
|
->from('todo_user_friend_link') |
|
22
|
1 |
|
->join('user', 'user.id=user_friend_link.user_friend_id OR todo_user.id=todo_user_friend_link.user_id', 'inner') |
|
23
|
|
|
|
|
24
|
1 |
|
->group_start() |
|
25
|
1 |
|
->where_in('todo_user_friend_link.user_id', $user_id) |
|
26
|
1 |
|
->or_where_in('todo_user_friend_link.user_friend_id', $user_id) |
|
27
|
1 |
|
->group_end() |
|
28
|
|
|
|
|
29
|
1 |
|
->where('confirmed', FRIEND_CONFIRMED) |
|
30
|
1 |
|
->where('user.id !=', $user_id) |
|
31
|
1 |
|
->order_by('username', 'asc') |
|
32
|
1 |
|
->get(); |
|
33
|
|
|
|
|
34
|
1 |
|
if($friends->num_rows() > 0) //Retrieve friends |
|
35
|
1 |
|
{ |
|
36
|
1 |
|
$res_array = array(); |
|
37
|
1 |
|
$friend_list = array(); |
|
38
|
|
|
|
|
39
|
1 |
|
foreach ($friends->result_array() as $friend) |
|
40
|
|
|
{ |
|
41
|
1 |
|
$friend_id = ($friend['uid'] !== $user_id) |
|
42
|
1 |
|
? $friend['uid'] |
|
43
|
1 |
|
: $friend['user_friend_id']; |
|
44
|
|
|
|
|
45
|
1 |
|
$res_array[$friend_id] = $friend; |
|
46
|
|
|
|
|
47
|
1 |
|
$friend_list[] = $friend_id; |
|
48
|
|
|
|
|
49
|
1 |
|
} |
|
50
|
|
|
|
|
51
|
|
|
//Get each user's groups |
|
52
|
1 |
|
$groups = $this->db->select('user_id, name as group_name') |
|
53
|
1 |
|
->distinct() |
|
54
|
1 |
|
->from('group') |
|
55
|
1 |
|
->join('group_users_link', 'group_users_link.group_id=group.id', 'inner') |
|
56
|
1 |
|
->where_in('todo_group_users_link.user_id', $friend_list) |
|
57
|
1 |
|
->order_by('user_id, group_name', 'asc') |
|
58
|
1 |
|
->get(); |
|
59
|
|
|
|
|
60
|
1 |
|
if($groups->num_rows() > 0) |
|
61
|
1 |
|
{ |
|
62
|
1 |
|
foreach($groups->result_array() as $group) |
|
63
|
|
|
{ |
|
64
|
1 |
|
$res_array[$group['user_id']]['groups'][] = $group['group_name']; |
|
65
|
1 |
|
} |
|
66
|
1 |
|
} |
|
67
|
|
|
|
|
68
|
1 |
|
return $res_array; |
|
69
|
|
|
|
|
70
|
|
|
} |
|
71
|
|
|
else |
|
72
|
|
|
{ |
|
73
|
1 |
|
return FALSE; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
// -------------------------------------------------------------------------- |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Find Friends |
|
81
|
|
|
* |
|
82
|
|
|
* Gets list of possible friends from search query |
|
83
|
|
|
* @return mixed |
|
84
|
|
|
*/ |
|
85
|
5 |
|
public function find_friends() |
|
86
|
|
|
{ |
|
87
|
5 |
|
$query = $this->input->get('q', TRUE); |
|
88
|
5 |
|
$user_id = (int) $this->session->userdata('uid'); |
|
89
|
|
|
|
|
90
|
|
|
// Don't allow empty searches to reach the database |
|
91
|
5 |
|
if (empty($query)) return []; |
|
92
|
|
|
|
|
93
|
|
|
//Loosely match usernames and emails to query |
|
94
|
4 |
|
$res = $this->db->select('id, username, email') |
|
95
|
4 |
|
->from('user') |
|
96
|
4 |
|
->like('username', $query, 'after') |
|
97
|
4 |
|
->or_like('email', $query, 'after') |
|
98
|
4 |
|
->order_by('username', 'asc') |
|
99
|
4 |
|
->get(); |
|
100
|
|
|
|
|
101
|
4 |
|
if($res->num_rows() > 0) |
|
102
|
4 |
|
{ |
|
103
|
3 |
|
$return = array(); |
|
104
|
|
|
|
|
105
|
3 |
|
foreach($res->result_array() as $friend) |
|
106
|
|
|
{ |
|
107
|
|
|
//This person is already a friend |
|
108
|
3 |
|
if($this->_check_friend($friend['id']) == TRUE) |
|
109
|
3 |
|
continue; |
|
110
|
|
|
|
|
111
|
|
|
//If the person is you :/ |
|
112
|
2 |
|
if($user_id == $friend['id']) |
|
113
|
2 |
|
continue; |
|
114
|
|
|
|
|
115
|
1 |
|
$return[] = $friend; |
|
116
|
3 |
|
} |
|
117
|
|
|
|
|
118
|
3 |
|
return $return; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
1 |
|
return (isset($return)) ? $return : []; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
// -------------------------------------------------------------------------- |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Get Request |
|
128
|
|
|
* |
|
129
|
|
|
* Gets friend requests to the current user |
|
130
|
|
|
* @return mixed |
|
131
|
|
|
*/ |
|
132
|
2 |
|
public function get_requests() |
|
133
|
|
|
{ |
|
134
|
2 |
|
$user_id = $this->session->userdata('uid'); |
|
135
|
|
|
|
|
136
|
|
|
//Get the list of requests |
|
137
|
2 |
|
$requests = $this->db->select('user_id, username, email') |
|
138
|
2 |
|
->from('user_friend_link') |
|
139
|
2 |
|
->where('user_friend_id', $user_id) |
|
140
|
2 |
|
->where('confirmed', -1) |
|
141
|
2 |
|
->join('user', 'user.id=user_friend_link.user_id') |
|
142
|
2 |
|
->get(); |
|
143
|
|
|
|
|
144
|
2 |
|
if($requests->num_rows() > 0) |
|
145
|
2 |
|
{ |
|
146
|
1 |
|
return $requests->result_array(); |
|
147
|
|
|
} |
|
148
|
|
|
else |
|
149
|
|
|
{ |
|
150
|
1 |
|
return false; |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
// -------------------------------------------------------------------------- |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Send Request |
|
158
|
|
|
* |
|
159
|
|
|
* Sends a friend request, or confirms a mutual friend request |
|
160
|
|
|
* |
|
161
|
|
|
* @param int $friend_id |
|
162
|
|
|
* @return int |
|
163
|
|
|
*/ |
|
164
|
3 |
|
public function send_request($friend_id) |
|
165
|
|
|
{ |
|
166
|
3 |
|
$user_id = (int) $this->session->userdata('uid'); |
|
167
|
|
|
|
|
168
|
|
|
//Check for request from the user |
|
169
|
3 |
|
$friend_check = $this->db->select('user_id') |
|
170
|
3 |
|
->from('user_friend_link') |
|
171
|
3 |
|
->where('user_id', $friend_id) |
|
172
|
3 |
|
->where('user_friend_id', $user_id) |
|
173
|
3 |
|
->get(); |
|
174
|
|
|
|
|
175
|
3 |
|
if($friend_check->num_rows() > 0) |
|
176
|
3 |
|
{ |
|
177
|
|
|
// Accept the friend request |
|
178
|
|
|
// Allows the user to add a friend they ignored |
|
179
|
|
|
// in their requests |
|
180
|
1 |
|
$this->db->set('confirmed', FRIEND_CONFIRMED) |
|
181
|
1 |
|
->where('user_id', $friend_id) |
|
182
|
1 |
|
->where('user_friend_id', $user_id) |
|
183
|
1 |
|
->where('confirmed', FRIEND_NOT_CONFIRMED) |
|
184
|
1 |
|
->update('user_friend_link'); |
|
185
|
1 |
|
} |
|
186
|
|
View Code Duplication |
else |
|
187
|
|
|
{ |
|
188
|
|
|
//Check if the request already exists |
|
189
|
2 |
|
$request_check = $this->db->from('user_friend_link') |
|
190
|
2 |
|
->where('user_friend_id', $friend_id) |
|
191
|
2 |
|
->where('user_id', $user_id) |
|
192
|
2 |
|
->get(); |
|
193
|
|
|
|
|
194
|
2 |
|
if($request_check->num_rows() > 0) return -1; |
|
195
|
|
|
|
|
196
|
|
|
//Add a friend request only if it doesn't already exist |
|
197
|
2 |
|
$this->db->set('user_id', $user_id) |
|
198
|
2 |
|
->set('user_friend_id', $friend_id) |
|
199
|
2 |
|
->insert('user_friend_link'); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
3 |
|
return $this->db->affected_rows(); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
// -------------------------------------------------------------------------- |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* Accept Request |
|
209
|
|
|
* |
|
210
|
|
|
* Accept a friend request |
|
211
|
|
|
* @param int $request_id |
|
212
|
|
|
* @return int |
|
213
|
|
|
*/ |
|
214
|
1 |
View Code Duplication |
public function accept_request($request_id) |
|
215
|
|
|
{ |
|
216
|
1 |
|
$this->db->set('confirmed', FRIEND_CONFIRMED) |
|
217
|
1 |
|
->where('user_id', (int)$request_id) |
|
218
|
1 |
|
->where('confirmed', FRIEND_NOT_CONFIRMED) |
|
219
|
1 |
|
->update('user_friend_link'); |
|
220
|
|
|
|
|
221
|
1 |
|
return $this->db->affected_rows(); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
// -------------------------------------------------------------------------- |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* Reject Request |
|
228
|
|
|
* |
|
229
|
|
|
* Reject a friend request |
|
230
|
|
|
* @param int $request_id |
|
231
|
|
|
* @return int |
|
232
|
|
|
*/ |
|
233
|
1 |
View Code Duplication |
public function reject_request($request_id) |
|
234
|
|
|
{ |
|
235
|
1 |
|
$this->db->set('confirmed', FRIEND_REJECTED) |
|
236
|
1 |
|
->where('user_id', (int)$request_id) |
|
237
|
1 |
|
->where('confirmed', FRIEND_NOT_CONFIRMED) |
|
238
|
1 |
|
->update('user_friend_link'); |
|
239
|
|
|
|
|
240
|
1 |
|
return $this->db->affected_rows(); |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
// -------------------------------------------------------------------------- |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* Check Friend |
|
247
|
|
|
* |
|
248
|
|
|
* Check if a user is already a friend |
|
249
|
|
|
* @param int $friend_id |
|
250
|
|
|
* @return bool |
|
251
|
|
|
*/ |
|
252
|
3 |
|
public function _check_friend($friend_id) |
|
253
|
|
|
{ |
|
254
|
3 |
|
$user_id = $this->session->userdata('uid'); |
|
255
|
|
|
|
|
256
|
3 |
|
if($user_id == $friend_id) |
|
257
|
3 |
|
return FALSE; |
|
258
|
|
|
|
|
259
|
3 |
|
$friend = $this->db->select('user_id, user_friend_id') |
|
260
|
3 |
|
->from('user_friend_link') |
|
261
|
3 |
|
->where('user_id', $user_id) |
|
262
|
3 |
|
->where('user_friend_id', $friend_id) |
|
263
|
3 |
|
->where('confirmed', FRIEND_CONFIRMED) |
|
264
|
3 |
|
->or_where('user_id', $friend_id) |
|
265
|
3 |
|
->where('user_friend_id', $user_id) |
|
266
|
3 |
|
->where('confirmed', FRIEND_CONFIRMED) |
|
267
|
3 |
|
->get(); |
|
268
|
|
|
|
|
269
|
3 |
|
return (bool)($friend->num_rows() > 0); |
|
270
|
|
|
|
|
271
|
|
|
} |
|
272
|
|
|
} |
|
273
|
|
|
// End of models/friend_model.php |