Skip to content

Commit

Permalink
add like
Browse files Browse the repository at this point in the history
  • Loading branch information
skywalker512 committed Jun 28, 2018
1 parent e6ec619 commit 738d3b1
Show file tree
Hide file tree
Showing 8 changed files with 307 additions and 0 deletions.
37 changes: 37 additions & 0 deletions addons/plugins/Likes/.gitignore
@@ -0,0 +1,37 @@
.buildpath
.project
.settings
.htaccess

# Compiled source
*.com
*.class
*.dll
*.exe
*.o
*.so

# Packages
*.7z
*.dmg
*.gz
*.iso
*.jar
*.rar
*.tar
*.zip

# Logs and databases
*.log
*.sql
*.sqlite

# OS generated files
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db
*~
22 changes: 22 additions & 0 deletions addons/plugins/Likes/README.md
@@ -0,0 +1,22 @@
# Likes Plugin

Allows members to like posts.

## Installation

[Download](https://github.com/esotalk/Likes/archive/master.zip) or clone the Likes plugin repo into your esoTalk plugin directory:

cd ESOTALK_DIR/addons/plugins/
git clone git@github.com:esotalk/Likes.git Likes

Navigate to the the admin/plugins page and activate the Likes plugin.

## Translation

Create `definitions.Likes.php` in your language pack with the following definitions:

$definitions["Unlike"] = "Unlike";
$definitions["Like"] = "Like";
$definitions["Members Who Liked This Post"] = "Members Who Liked This Post";
$definitions["%s likes this."] = "%s likes this.";
$definitions["%s like this."] = "%s like this.";
Binary file added addons/plugins/Likes/icon.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file added addons/plugins/Likes/index.html
Empty file.
176 changes: 176 additions & 0 deletions addons/plugins/Likes/plugin.php
@@ -0,0 +1,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" => "support@esotalk.org",
"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["deleteMemberId"]) 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'" : "").">&nbsp;&middot;&nbsp;</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;
}

}
6 changes: 6 additions & 0 deletions addons/plugins/Likes/resources/likes.css
@@ -0,0 +1,6 @@
.likes {
border-top:1px solid #eee;
font-size:11px;
padding:5px 12px;
margin:1px 0 0;
}
23 changes: 23 additions & 0 deletions addons/plugins/Likes/resources/likes.js
@@ -0,0 +1,23 @@
$(function() {

$(document).on("click", ".likes .showMore", function(e) {
e.preventDefault();
ETSheet.loadSheet("onlineSheet", "conversation/liked.view/"+$(this).parents(".post").data("id"));
});

$(document).on("click", ".likes .like-button", function(e) {
e.preventDefault();
var area = $(this).parents(".likes");
area.find(".like-button").html(area.hasClass("liked") ? T("Like") : T("Unlike"));

$.ETAjax({
url: "conversation/"+(area.hasClass("liked") ? "unlike" : "like")+".json/"+area.parents(".post").data("id"),
success: function(data) {
area.find(".like-members").html(data.names);
area.find(".like-separator").toggle(!!data.names);
area.toggleClass("liked");
}
});
});

});
43 changes: 43 additions & 0 deletions addons/plugins/Likes/views/liked.php
@@ -0,0 +1,43 @@
<?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;

?>
<div class='sheet' id='onlineSheet'>
<div class='sheetContent'>

<h3><?php echo T("Members Who Liked This Post"); ?><?php if (count($data["members"])) echo " (".count($data["members"]).")"; ?></h3>

<?php
// If there are members online, list them.
if (count($data["members"])): ?>

<div class='section' id='onlineList'>

<ul class='list'>
<?php foreach ($data["members"] as $memberId => $member): ?>
<li>
<span class='action'>
<?php echo avatar($member, "thumb"), " ", memberLink($memberId, $member["username"]), " "; ?>
</span>
</li>
<?php endforeach; ?>
</ul>

</div>

<?php
// Otherwise, display a 'no members online' message.
else: ?>

<div class='section'>
<div class='noResults help'>
</div>
</div>

<?php endif; ?>

</div>
</div>

0 comments on commit 738d3b1

Please sign in to comment.