From 738d3b10093d1f9829006165ef1ddec6c194c0a9 Mon Sep 17 00:00:00 2001 From: skywalker512 Date: Thu, 28 Jun 2018 08:57:01 +0800 Subject: [PATCH] add like --- addons/plugins/Likes/.gitignore | 37 +++++ addons/plugins/Likes/README.md | 22 +++ addons/plugins/Likes/icon.png | Bin 0 -> 754 bytes addons/plugins/Likes/index.html | 0 addons/plugins/Likes/plugin.php | 176 +++++++++++++++++++++++ addons/plugins/Likes/resources/likes.css | 6 + addons/plugins/Likes/resources/likes.js | 23 +++ addons/plugins/Likes/views/liked.php | 43 ++++++ 8 files changed, 307 insertions(+) create mode 100644 addons/plugins/Likes/.gitignore create mode 100644 addons/plugins/Likes/README.md create mode 100644 addons/plugins/Likes/icon.png create mode 100644 addons/plugins/Likes/index.html create mode 100644 addons/plugins/Likes/plugin.php create mode 100644 addons/plugins/Likes/resources/likes.css create mode 100644 addons/plugins/Likes/resources/likes.js create mode 100644 addons/plugins/Likes/views/liked.php diff --git a/addons/plugins/Likes/.gitignore b/addons/plugins/Likes/.gitignore new file mode 100644 index 0000000..86aad64 --- /dev/null +++ b/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 +*~ diff --git a/addons/plugins/Likes/README.md b/addons/plugins/Likes/README.md new file mode 100644 index 0000000..870b88a --- /dev/null +++ b/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."; diff --git a/addons/plugins/Likes/icon.png b/addons/plugins/Likes/icon.png new file mode 100644 index 0000000000000000000000000000000000000000..a12a1d0597fd991f5fc99c03e9c87a67c63d60d0 GIT binary patch literal 754 zcmVwFfEb^%^xPRRnQ_RhzOiYrCby)B8b|Bo6;a?)21LPXrbt$MQuvM!dzGkgprX8 zO)19BkY+min)mLzo%?)Fqx5s&d+*$P&-a~k?vDVQ?Di1}YNA5%RD;7HMHz_HVFT!= z*B6fW+90>>6Wsx)wA8Hmj1|aXbW?|}-s@P(&Ml2TxUR4w0d8_@t>}_P{0ImMVcLcD z*7AnK^^6L{AB+gL4U*%D>*`vSGW^{xNt&LhIB+b<2h-`{iJuh4xj#P#o9 z+zZ4|u|N!QLexlg-MOwd#-t#l6iRnDCeGe$N^H*Zvg?erF3`w#8cQb8>SX2i(sMeq!sx&k%@QnEE3%V8$lvcSscH-H!hgmB)k>(G9q=j8Hyc6qet+M}gB zjJg7O2YeI*91hIl0y8tmac>m&`TFdq4-12Rchg?>LM0-$pzr{jmt90=NalBj`&>L; zwI`Ii*m-c9E@b?Q@G78q7s<4MWxq~;3r1q4HV^htr~v;w!N~I`nfGr;GDz#6!GoN^ zJw49_au(|u9JsHS#dnn(k!Vm+1FF6yqWjceMbmCB7pG0 k|II?I@GF7>&MyH509`IKUUtV$RsaA107*qoM6N<$f?pnDVgLXD literal 0 HcmV?d00001 diff --git a/addons/plugins/Likes/index.html b/addons/plugins/Likes/index.html new file mode 100644 index 0000000..e69de29 diff --git a/addons/plugins/Likes/plugin.php b/addons/plugins/Likes/plugin.php new file mode 100644 index 0000000..f0c8b42 --- /dev/null +++ b/addons/plugins/Likes/plugin.php @@ -0,0 +1,176 @@ + "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 = "

+ + + +

"; + + $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 = "".sprintf(T("%s others"), count($otherNames)).""; + } 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; +} + +} diff --git a/addons/plugins/Likes/resources/likes.css b/addons/plugins/Likes/resources/likes.css new file mode 100644 index 0000000..3d01093 --- /dev/null +++ b/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; +} diff --git a/addons/plugins/Likes/resources/likes.js b/addons/plugins/Likes/resources/likes.js new file mode 100644 index 0000000..83c8a98 --- /dev/null +++ b/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"); + } + }); + }); + +}); diff --git a/addons/plugins/Likes/views/liked.php b/addons/plugins/Likes/views/liked.php new file mode 100644 index 0000000..00ac1b4 --- /dev/null +++ b/addons/plugins/Likes/views/liked.php @@ -0,0 +1,43 @@ + +
+
+ +

+ + + +
+ +
    + $member): ?> +
  • + + + +
  • + +
+ +
+ + + +
+
+
+
+ + + +
+
\ No newline at end of file