This repository has been archived by the owner on Jun 13, 2019. It is now read-only.
/
friends.php
171 lines (149 loc) · 3.89 KB
/
friends.php
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
namespace florinp\messenger\controller;
use phpbb\template\template;
use phpbb\request\request;
use phpbb\user;
use phpbb\user_loader;
use florinp\messenger\models\friends_model;
use phpbb\notification\manager;
class friends
{
protected $template;
protected $user;
protected $user_loader;
protected $model;
protected $request;
protected $notification_manager;
protected $u_action;
public function __construct(
template $template,
request $request,
user $user,
user_loader $user_loader,
friends_model $model,
manager $notification_manager
)
{
$this->template = $template;
$this->request = $request;
$this->user = $user;
$this->user_loader = $user_loader;
$this->model = $model;
$this->notification_manager = $notification_manager;
}
public function friends_list()
{
$friends = $this->model->getFriends();
$i = 0;
foreach ($friends as $friend) {
$i = $i + 1;
$this->user_loader->load_users(array(
$friend['user_id']
));
$this->template->assign_block_vars('friends', array(
'user_id' => $friend['user_id'],
'username' => $this->user_loader->get_username($friend['user_id'], 'full'),
'user_colour' => $friend['user_colour'],
'user_status' => ($friend['user_status'] == 1) ? 'online' : 'offline',
'row_count' => $i
));
}
$this->template->assign_vars(array(
'U_ACTION' => $this->u_action,
));
}
public function requests()
{
$requests = $this->model->get_friends_requests();
$i = 0;
foreach ($requests as $request) {
$i = $i + 1;
$this->user_loader->load_users(array(
$request['user_id'],
$request['sender_id']
));
$this->template->assign_block_vars('requests', array(
'request_id' => $request['request_id'],
'sender_username' => $this->user_loader->get_username($request['sender_id'], 'full'),
'status' => ($request['status'] == 1) ? ($this->user->lang['APPROVED']) : ($this->user->lang['WAITING_FOR_APPROVAL']),
'time' => date('d M Y H:i:s', $request['time']),
'row_count' => $i
));
}
$this->template->assign_vars(array(
'U_ACTION' => $this->u_action,
));
}
public function delete_request($requests_id)
{
if (is_array($requests_id)) {
foreach ($requests_id as $id) {
$this->model->delete_friend_request($id);
}
} else {
$this->model->delete_friend_request($requests_id);
}
}
public function approve_request($requests_id)
{
if (is_array($requests_id)) {
foreach ($requests_id as $id) {
$request_data = $this->model->get_friend_request($id);
if ($request_data) {
$this->model->add_friend(array(
'user_id' => $request_data['user_id'],
'friend_id' => $request_data['sender_id']
));
}
}
} else {
$this->model->approve_friend_request($requests_id);
$request_data = $this->model->get_friend_request($requests_id);
$this->model->add_friend(array(
'user_id' => $request_data['user_id'],
'friend_id' => $request_data['sender_id']
));
}
}
public function send_request($user_id)
{
$user_id = (int)$user_id;
$sender_id = $this->user->data['user_id'];
$insert = array(
'user_id' => $user_id,
'sender_id' => $sender_id
);
if ($request_id = $this->model->insert_friends_request($insert)) {
$notification_data = array(
'request_id' => $request_id,
'sender_id' => $sender_id,
'sender_username' => $this->user->data ['username'],
'user_id' => $user_id
);
$this->notification_manager->add_notifications(array(
'florinp.messenger.notification.type.friend_request'
), $notification_data);
return true;
}
return false;
}
public function remove_friend($user_id)
{
if (is_array($user_id)) {
foreach ($user_id as $id) {
$this->model->remove_friend($id);
}
return true;
} else {
if ($user_id > 1) {
$this->model->remove_friend($user_id);
return true;
}
}
return false;
}
public function set_page_url($u_action)
{
$this->u_action = $u_action;
}
}