-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplugin.php
176 lines (138 loc) · 5.17 KB
/
plugin.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
172
173
174
175
176
<?php
// Copyright 2011 Toby Zerner, Simon Zerner
// This file is part of esoTalk. Please see the included license file for usage information.
if (!defined("IN_ESOTALK")) exit;
ET::$pluginInfo["Likes"] = array(
"name" => "Likes",
"description" => "Allows members to like posts.",
"version" => ESOTALK_VERSION,
"author" => "Toby Zerner",
"authorEmail" => "[email protected]",
"authorURL" => "http://esotalk.org",
"license" => "GPLv2",
"dependencies" => array(
"esoTalk" => "1.0.0g4"
)
);
class ETPlugin_Likes extends ETPlugin {
// Like a post.
public function action_conversationController_like($sender, $postId = false)
{
$sender->responseType = RESPONSE_TYPE_JSON;
if (!$sender->validateToken() or !ET::$session->userId or ET::$session->isSuspended()) return;
// Get the conversation.
if (!($conversation = ET::conversationModel()->getByPostId($postId))) return false;
// Get the post.
$post = ET::postModel()->getById($postId);
ET::SQL()->insert("like")
->set("postId", $post["postId"])
->set("memberId", ET::$session->userId)
->setOnDuplicateKey("memberId", ET::$session->userId)
->exec();
$post["likes"][ET::$session->userId] = array("avatarFormat" => ET::$session->user["avatarFormat"], "username" => ET::$session->user["username"]);
$sender->json("names", $this->getNames($post["likes"]));
$sender->render();
}
// Unlike a post.
public function action_conversationController_unlike($sender, $postId = false)
{
$sender->responseType = RESPONSE_TYPE_JSON;
if (!$sender->validateToken() or !ET::$session->userId) return;
// Get the conversation.
if (!($conversation = ET::conversationModel()->getByPostId($postId))) return false;
// Get the post.
$post = ET::postModel()->getById($postId);
ET::SQL()->delete()
->from("like")
->where("postId=:postId")->bind(":postId", $post["postId"])
->where("memberId=:memberId")->bind(":memberId", ET::$session->userId)
->exec();
unset($post["likes"][ET::$session->userId]);
$sender->json("names", $this->getNames($post["likes"]));
$sender->render();
}
// Show a list of members who liked a post.
public function action_conversationController_liked($sender, $postId = false)
{
if (!($postId = (int)$postId)) return;
$post = ET::postModel()->getById($postId);
if (!$post) return;
$sender->data("members", $post["likes"]);
$sender->render($this->view("liked"));
}
// Get the likes from the database and attach them to the posts.
public function handler_postModel_getPostsAfter($sender, &$posts)
{
$postsById = array();
foreach ($posts as &$post) {
$postsById[$post["postId"]] = &$post;
$post["likes"] = array();
}
if (!count($postsById)) return;
$result = ET::SQL()
->select("postId, m.memberId, m.email, username, avatarFormat")
->from("like l")
->from("member m", "m.memberId=l.memberId", "left")
->where("postId IN (:ids)")
->bind(":ids", array_keys($postsById))
->exec();
while ($row = $result->nextRow()) {
$postsById[$row["postId"]]["likes"][$row["memberId"]] = array("memberId" => $row["memberId"], "username" => $row["username"], "email" => $row["email"], "avatarFormat" => $row["avatarFormat"]);
}
}
public function handler_conversationController_renderBefore($sender)
{
$sender->addJSLanguage("Like", "Unlike");
$sender->addJSFile($this->resource("likes.js"));
$sender->addCSSFile($this->resource("likes.css"));
}
public function handler_conversationController_formatPostForTemplate($sender, &$formatted, $post, $conversation)
{
if ($post["deleteTime"]) return;
$liked = array_key_exists(ET::$session->userId, $post["likes"]);
$members = $this->getNames($post["likes"]);
if ( ! ET::$session->userId and ! $members) return;
$likeText = isset(ET::$session->userId) ? T($liked ? "Unlike" : "Like") : "";
$likes = "<p class='likes".($liked ? " liked" : "")."'>
<a href='#' class='like-button'>$likeText</a>
<span class='like-separator'".( (! ET::$session->userId or ! $members) ? " style='display:none'" : "")."> · </span>
<span class='like-members'>$members</span>
</p>";
$formatted["footer"][] = $likes;
}
public function getNames($likes)
{
$names = array();
foreach ($likes as $id => $member) $names[] = memberLink($id, $member["username"]);
// If there's more than one name, construct the list so that it has the word "and" in it.
if (count($names) > 1) {
// If there're more than 3 names, chop off everything after the first 3 and replace them with a
// "x others" link.
if (count($names) > 3) {
$otherNames = array_splice($names, 3);
$lastName = "<a href='#' class='showMore name'>".sprintf(T("%s others"), count($otherNames))."</a>";
} else {
$lastName = array_pop($names);
}
$members = sprintf(T("%s like this."), sprintf(T("%s and %s"), implode(", ", $names), $lastName));
}
// If there's only one name, we don't need to do anything gramatically fancy.
elseif (count($names)) {
$members = sprintf(T("%s likes this."), $names[0]);
}
else {
$members = "";
}
return $members;
}
public function setup($oldVersion = "")
{
$structure = ET::$database->structure();
$structure->table("like")
->column("postId", "int unsigned", false)
->column("memberId", "int unsigned", false)
->key(array("postId", "memberId"), "primary")
->exec(false);
return true;
}
}