/
e_module.php
167 lines (144 loc) · 4.31 KB
/
e_module.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
<?php
/**
* @file
* This file is loaded every time the core of e107 is included. ie. Wherever
* you see require_once("class2.php") in a script. It allows a developer to
* modify or define constants, parameters etc. which should be loaded prior to
* the header or anything that is sent to the browser as output. It may also be
* included in Ajax calls.
*/
e107::lan('nodejs_comment', false, true);
// Register events.
$event = e107::getEvent();
$event->register('postcomment', 'nodejs_comment_event_postcomment_callback');
$event->register('login', 'nodejs_comment_event_login_callback');
// TODO: send notifications after comment has been approved.
/**
* Event callback after triggering "postcomment".
*
* @param array $comment
* Comment item.
*
* $comment contains:
* - comment_pid
* - comment_item_id
* - comment_subject
* - comment_author_id
* - comment_author_name
* - comment_author_email
* - comment_datestamp
* - comment_comment
* - comment_blocked
* - comment_ip
* - comment_type
* - comment_lock
* - comment_share
* - comment_nick
* - comment_time
* - comment_id
*
* getCommentData() returns with array, which contains:
* - comment_datestamp
* - comment_author_id
* - comment_author
* - comment_comment
* - comment_subject
* - comment_type
* - comment_title
* - comment_url
*/
function nodejs_comment_event_postcomment_callback($comment)
{
e107_require_once(e_PLUGIN . 'nodejs/nodejs.main.php');
$tpl = e107::getTemplate('nodejs_comment');
$sc = e107::getScBatch('nodejs_comment', true);
$tp = e107::getParser();
$cm = e107::getComment();
$cid = (int) vartrue($comment['comment_id'], 0);
$pid = (int) vartrue($comment['comment_pid'], 0);
$uid = (int) vartrue($comment['comment_author_id'], 0);
$commentData = $cm->getCommentData(1, 0, 'comment_id=' . $cid);
if(!isset($commentData[0]))
{
return;
}
$authorData = e107::user($uid);
// Send notification to everyone for updating latest comments menu.
$sc->setVars($commentData[0]);
$markup = $tp->parseTemplate($tpl['MENU']['LATEST']['ITEM'], true, $sc);
$message = (object) array(
'broadcast' => true,
'channel' => 'nodejs_notify',
'callback' => 'nodejsCommentMenu',
'type' => 'latestComments',
'markup' => $markup,
);
nodejs_enqueue_message($message);
// Send notification to everyone for notifying about new comment.
$sc->setVars(array(
'account' => $authorData,
'comment' => $commentData[0],
));
$markup = $tp->parseTemplate($tpl['NOTIFICATION']['POST_ALL'], true, $sc);
$message = (object) array(
'broadcast' => true,
'channel' => 'nodejs_notify',
'callback' => 'nodejsComments',
'type' => 'newCommentAny',
'markup' => $markup,
'exclude' => $authorData['user_id'],
);
nodejs_enqueue_message($message);
// Reply on comment.
if($pid > 0)
{
$commentParentData = $cm->getCommentData(1, 0, 'comment_id=' . $pid);
array_pop($commentParentData);
$authorParentData = e107::user();
// Send notification to author of parent comment for notifying about new reply.
$sc->setVars(array(
'account' => $authorData,
'comment' => $commentData[0],
));
$markup = $tp->parseTemplate($tpl['NOTIFICATION']['POST_OWN'], true, $sc);
$message = (object) array(
'channel' => 'nodejs_user_' . $authorParentData['user_id'],
'callback' => 'nodejsComments',
'type' => 'newCommentOwn',
'markup' => $markup,
'exclude' => $authorData['user_id'],
);
nodejs_enqueue_message($message);
}
}
/**
* Callback function to check EUF defaults.
*/
function nodejs_comment_event_login_callback($data)
{
$db = e107::getDb();
$uid = (int) $data['user_id'];
// Start session.
$session_started = session_id() === '' ? false : true;
if($session_started)
{
session_start();
}
if($uid > 0 && !isset($_SESSION['nodejs_comment_eufs_updated']))
{
$_SESSION['nodejs_comment_eufs_updated'] = 1;
$visits = $db->retrieve('user', 'user_visits', 'user_id = ' . $uid);
// First time visit.
if((int) $visits === 0)
{
$eufs = array(
'user_plugin_nodejs_comment_alert_comment_any' => 1,
'user_plugin_nodejs_comment_sound_comment_any' => 1,
'user_plugin_nodejs_comment_alert_comment_own' => 1,
'user_plugin_nodejs_comment_sound_comment_own' => 1,
'WHERE' => 'user_extended_id = ' . $uid,
);
$db->update('user_extended', $eufs);
}
}
}