aboutsummaryrefslogtreecommitdiff
path: root/mod/likes
diff options
context:
space:
mode:
authorcash <cash@36083f99-b078-4883-b0ff-0f9b5a30f544>2011-03-09 03:07:40 +0000
committercash <cash@36083f99-b078-4883-b0ff-0f9b5a30f544>2011-03-09 03:07:40 +0000
commitbdb5d2b95792782ebc379e3348cad1812c1980b4 (patch)
treec923d740327b3aeb62a24246f3725ced8677a5f7 /mod/likes
parent9ea30ea494cea4c950894a0ac1131eefda10868b (diff)
downloadelgg-bdb5d2b95792782ebc379e3348cad1812c1980b4.tar.gz
elgg-bdb5d2b95792782ebc379e3348cad1812c1980b4.tar.bz2
Fixes #3058 pulls likes out as a plugin
git-svn-id: http://code.elgg.org/elgg/trunk@8639 36083f99-b078-4883-b0ff-0f9b5a30f544
Diffstat (limited to 'mod/likes')
-rw-r--r--mod/likes/actions/likes/add.php64
-rw-r--r--mod/likes/actions/likes/delete.php29
-rw-r--r--mod/likes/languages/en.php25
-rw-r--r--mod/likes/manifest.xml17
-rw-r--r--mod/likes/start.php104
-rw-r--r--mod/likes/views/default/annotation/likes.php48
-rw-r--r--mod/likes/views/default/likes/css.php13
-rw-r--r--mod/likes/views/default/likes/display.php67
-rw-r--r--mod/likes/views/default/river/annotation/likes/create.php26
9 files changed, 393 insertions, 0 deletions
diff --git a/mod/likes/actions/likes/add.php b/mod/likes/actions/likes/add.php
new file mode 100644
index 000000000..f56493864
--- /dev/null
+++ b/mod/likes/actions/likes/add.php
@@ -0,0 +1,64 @@
+<?php
+/**
+ * Elgg add like action
+ *
+ */
+
+$entity_guid = (int) get_input('guid');
+
+//check to see if the user has already liked the item
+if (elgg_annotation_exists($entity_guid, 'likes')) {
+ system_message(elgg_echo("likes:alreadyliked"));
+ forward(REFERER);
+}
+// Let's see if we can get an entity with the specified GUID
+$entity = get_entity($entity_guid);
+if (!$entity) {
+ register_error(elgg_echo("likes:notfound"));
+ forward(REFERER);
+}
+
+// cannot like your own stuff
+if (!$entity->canAnnotate(0, 'likes')) {
+ // plugins should register the error message to explain why liking isn't allowed
+ forward(REFERER);
+}
+
+$user = elgg_get_logged_in_user_entity();
+$annotation = create_annotation($entity->guid,
+ 'likes',
+ "likes",
+ "",
+ $user->guid,
+ $entity->access_id);
+
+// tell user annotation didn't work if that is the case
+if (!$annotation) {
+ register_error(elgg_echo("likes:failure"));
+ forward(REFERER);
+}
+
+// notify if poster wasn't owner
+if ($entity->owner_guid != $user->guid) {
+
+ notify_user($entity->owner_guid,
+ $user->guid,
+ elgg_echo('likes:email:subject'),
+ elgg_echo('likes:email:body', array(
+ $user->name,
+ $entity->title,
+ //$comment_text,
+ $entity->getURL(),
+ $user->name,
+ $user->getURL()
+ ))
+ );
+}
+
+system_message(elgg_echo("likes:likes"));
+
+//add to river
+add_to_river('annotation/annotatelike', 'likes', $user->guid, $entity->guid, "", 0, $annotation);
+
+// Forward back to the page where the user 'liked' the object
+forward(REFERER);
diff --git a/mod/likes/actions/likes/delete.php b/mod/likes/actions/likes/delete.php
new file mode 100644
index 000000000..867d5dfef
--- /dev/null
+++ b/mod/likes/actions/likes/delete.php
@@ -0,0 +1,29 @@
+<?php
+/**
+ * Elgg delete like action
+ *
+ */
+
+// Ensure we're logged in
+if (!elgg_is_logged_in()) {
+ forward();
+}
+
+// Make sure we can get the comment in question
+$annotation_id = (int) get_input('annotation_id');
+if ($likes = elgg_get_annotation_from_id($annotation_id)) {
+
+ $entity = get_entity($likes->entity_guid);
+
+ if ($likes->canEdit()) {
+ $likes->delete();
+ system_message(elgg_echo("likes:deleted"));
+ forward(REFERER);
+ }
+
+} else {
+ $url = "";
+}
+
+register_error(elgg_echo("likes:notdeleted"));
+forward(REFERER); \ No newline at end of file
diff --git a/mod/likes/languages/en.php b/mod/likes/languages/en.php
new file mode 100644
index 000000000..52e2d4160
--- /dev/null
+++ b/mod/likes/languages/en.php
@@ -0,0 +1,25 @@
+<?php
+/**
+ * Likes English language file
+ */
+
+$english = array(
+ 'likes:this' => 'liked this',
+ 'likes:deleted' => 'Your like has been removed',
+ 'likes:see' => 'See who liked this',
+ 'likes:remove' => 'Unlike this',
+ 'likes:notdeleted' => 'There was a problem removing your like',
+ 'likes:likes' => 'You now like this item',
+ 'likes:failure' => 'There was a problem liking this item',
+ 'likes:alreadyliked' => 'You have already liked this item',
+ 'likes:notfound' => 'The item you are trying to like cannot be found',
+ 'likes:likethis' => 'Like this',
+ 'likes:userlikedthis' => 'user liked this',
+ 'likes:userslikedthis' => 'users like this',
+ 'likes:river:annotate' => 'likes',
+ 'likes:email:body' => '%s liked %s',
+ 'likes:email:subject' => 'A user liked one of your objects',
+ 'river:likes' => 'likes %s %s',
+);
+
+add_translation('en', $english);
diff --git a/mod/likes/manifest.xml b/mod/likes/manifest.xml
new file mode 100644
index 000000000..abd3f7c76
--- /dev/null
+++ b/mod/likes/manifest.xml
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<plugin_manifest xmlns="http://www.elgg.org/plugin_manifest/1.8">
+ <name>Likes</name>
+ <author>Core developers</author>
+ <version>1.8</version>
+ <category>bundled</category>
+ <description>Enables users to like content on the site.</description>
+ <website>http://www.elgg.org/</website>
+ <copyright>See COPYRIGHT.txt</copyright>
+ <license>GNU Public License version 2</license>
+ <requires>
+ <type>elgg_version</type>
+ <version>2010030101</version>
+ </requires>
+ <activate_on_install>true</activate_on_install>
+ <admin_interface>advanced</admin_interface>
+</plugin_manifest> \ No newline at end of file
diff --git a/mod/likes/start.php b/mod/likes/start.php
new file mode 100644
index 000000000..97823087a
--- /dev/null
+++ b/mod/likes/start.php
@@ -0,0 +1,104 @@
+<?php
+/**
+ * Likes plugin
+ *
+ */
+
+elgg_register_event_handler('init', 'system', 'likes_init');
+
+function likes_init() {
+
+ elgg_extend_view('css/elgg', 'likes/css');
+
+ // registered with priority < 500 so other plugins can remove likes
+ elgg_register_plugin_hook_handler('register', 'menu:river', 'likes_river_menu_setup', 400);
+ elgg_register_plugin_hook_handler('register', 'menu:entity', 'likes_entity_menu_setup', 400);
+
+ $actions_base = elgg_get_plugins_path() . 'likes/actions/likes';
+ elgg_register_action('likes/add', "$actions_base/add.php");
+ elgg_register_action('likes/delete', "$actions_base/delete.php");
+}
+
+/**
+ * Add likes to entity menu at end of the menu
+ */
+function likes_entity_menu_setup($hook, $type, $return, $params) {
+ if (elgg_in_context('widgets')) {
+ return $return;
+ }
+
+ $entity = $params['entity'];
+
+ // likes
+ $options = array(
+ 'name' => 'likes',
+ 'text' => elgg_view('likes/display', array('entity' => $entity)),
+ 'href' => false,
+ 'priority' => 1000,
+ );
+ $return[] = ElggMenuItem::factory($options);
+
+ return $return;
+}
+
+/**
+ * Add a like button to river actions
+ */
+function likes_river_menu_setup($hook, $type, $return, $params) {
+ if (elgg_is_logged_in()) {
+ $item = $params['item'];
+ $object = $item->getObjectEntity();
+ if (!elgg_in_context('widgets') && $item->annotation_id == 0) {
+ if ($object->canAnnotate(0, 'likes')) {
+ if (!elgg_annotation_exists($object->getGUID(), 'likes')) {
+ // user has not liked this yet
+ $url = "action/likes/add?guid={$object->getGUID()}";
+ $options = array(
+ 'name' => 'like',
+ 'href' => $url,
+ 'text' => elgg_echo('likes:likethis'),
+ 'is_action' => true,
+ 'priority' => 100,
+ );
+ } else {
+ // user has liked this
+ $likes = elgg_get_annotations(array(
+ 'guid' => $guid,
+ 'annotation_name' => 'likes',
+ 'owner_guid' => elgg_get_logged_in_user_guid()
+ ));
+ $url = elgg_get_site_url() . "action/likes/delete?annotation_id={$likes[0]->id}";
+ $options = array(
+ 'name' => 'like',
+ 'href' => $url,
+ 'text' => elgg_echo('likes:remove'),
+ 'is_action' => true,
+ 'priority' => 100,
+ );
+ }
+ $return[] = ElggMenuItem::factory($options);
+ }
+ }
+ }
+
+ return $return;
+}
+
+/**
+ * Count how many people have liked an entity.
+ *
+ * @param ElggEntity $entity
+ *
+ * @return int Number of likes
+ */
+function likes_count($entity) {
+ $type = $entity->getType();
+ $params = array('entity' => $entity);
+ $number = elgg_trigger_plugin_hook('likes:count', $type, $params, false);
+
+ if ($number) {
+ return $number;
+ } else {
+ return $entity->countAnnotations('likes');
+ }
+}
diff --git a/mod/likes/views/default/annotation/likes.php b/mod/likes/views/default/annotation/likes.php
new file mode 100644
index 000000000..e1ae20818
--- /dev/null
+++ b/mod/likes/views/default/annotation/likes.php
@@ -0,0 +1,48 @@
+<?php
+/**
+ * Elgg show the users who liked the object
+ *
+ * @uses $vars['annotation']
+ */
+
+if (!isset($vars['annotation'])) {
+ return true;
+}
+
+$like = $vars['annotation'];
+
+$user = $like->getOwnerEntity();
+if (!$user) {
+ return true;
+}
+
+$user_icon = elgg_view_entity_icon($user, 'tiny');
+$user_link = elgg_view('output/url', array(
+ 'href' => $user->getURL(),
+ 'text' => $user->name,
+));
+
+$likes_string = elgg_echo('likes:this');
+
+$friendlytime = elgg_view_friendly_time($like->time_created);
+
+if ($like->canEdit()) {
+ $delete_button = elgg_view("output/confirmlink",array(
+ 'href' => "action/likes/delete?annotation_id={$like->id}",
+ 'text' => "<span class=\"elgg-icon elgg-icon-delete right\"></span>",
+ 'confirm' => elgg_echo('deleteconfirm'),
+ 'text_encode' => false,
+ ));
+}
+
+$body = <<<HTML
+<p class="mbn">
+ $delete_button
+ $user_link $likes_string
+ <span class="elgg-subtext">
+ $friendlytime
+ </span>
+</p>
+HTML;
+
+echo elgg_view_image_block($user_icon, $body);
diff --git a/mod/likes/views/default/likes/css.php b/mod/likes/views/default/likes/css.php
new file mode 100644
index 000000000..cb0334b7c
--- /dev/null
+++ b/mod/likes/views/default/likes/css.php
@@ -0,0 +1,13 @@
+<?php
+/**
+ * Likes CSS
+ */
+?>
+
+/* ***************************************
+ Likes
+*************************************** */
+.elgg-likes-list {
+ width: 345px;
+ position: absolute;
+}
diff --git a/mod/likes/views/default/likes/display.php b/mod/likes/views/default/likes/display.php
new file mode 100644
index 000000000..00f6c5a34
--- /dev/null
+++ b/mod/likes/views/default/likes/display.php
@@ -0,0 +1,67 @@
+<?php
+/**
+ * Elgg likes display
+ *
+ * @package Elgg
+ *
+ * @uses $vars['entity']
+ */
+
+if (!isset($vars['entity'])) {
+ return true;
+}
+
+$guid = $vars['entity']->getGUID();
+
+// check to see if the user has already liked this
+if (elgg_is_logged_in() && $vars['entity']->canAnnotate(0, 'likes')) {
+ if (!elgg_annotation_exists($guid, 'likes')) {
+ $url = elgg_get_site_url() . "action/likes/add?guid={$guid}";
+ $params = array(
+ 'href' => $url,
+ 'text' => elgg_view_icon('thumbs-up'),
+ 'title' => elgg_echo('likes:likethis'),
+ 'is_action' => true,
+ );
+ $likes_button = elgg_view('output/url', $params);
+ } else {
+ $options = array(
+ 'guid' => $guid,
+ 'annotation_name' => 'likes',
+ 'owner_guid' => elgg_get_logged_in_user_guid()
+ );
+ $likes = elgg_get_annotations($options);
+ $url = elgg_get_site_url() . "action/likes/delete?annotation_id={$likes[0]->id}";
+ $params = array(
+ 'href' => $url,
+ 'text' => elgg_view_icon('thumbs-up-alt'),
+ 'title' => elgg_echo('likes:remove'),
+ 'is_action' => true,
+ );
+ $likes_button = elgg_view('output/url', $params);
+ }
+}
+
+$list = '';
+$num_of_likes = likes_count($vars['entity']);
+if ($num_of_likes) {
+ // display the number of likes
+ if ($num_of_likes == 1) {
+ $likes_string = elgg_echo('likes:userlikedthis');
+ } else {
+ $likes_string = elgg_echo('likes:userslikedthis');
+ }
+ $params = array(
+ 'text' => "$num_of_likes $likes_string",
+ 'title' => elgg_echo('likes:see'),
+ 'class' => 'elgg-popup-rb-at-lt',
+ 'rel' => 'popup',
+ 'href' => "#$guid-likes"
+ );
+ $list = elgg_view('output/url', $params);
+ $list .= "<div class='elgg-module elgg-module-popup elgg-likes-list hidden clearfix' id='$guid-likes'>";
+ $list .= elgg_list_annotations(array('guid' => $guid, 'annotation_name' => 'likes', 'limit' => 99));
+ $list .= "</div>";
+}
+
+echo elgg_view_image_block($likes_button, $list); \ No newline at end of file
diff --git a/mod/likes/views/default/river/annotation/likes/create.php b/mod/likes/views/default/river/annotation/likes/create.php
new file mode 100644
index 000000000..463fe5049
--- /dev/null
+++ b/mod/likes/views/default/river/annotation/likes/create.php
@@ -0,0 +1,26 @@
+<?php
+/**
+ * Likes river view
+ *
+ * @todo not finished
+ */
+
+$object = $vars['item']->getObjectEntity();
+$comment = $vars['item']->getAnnotation();
+
+$url = $object->getURL();
+$title = $object->title;
+if (!$title) {
+ $title = elgg_echo('untitled');
+}
+$params = array(
+ 'href' => $object->getURL(),
+ 'text' => $title,
+);
+$object_link = elgg_view('output/url', $params);
+
+$type = $object->getType();
+$subtype = $object->getSubtype();
+
+$type_string = elgg_echo("river:commented:$type:$subtype");
+echo elgg_echo('river:likes', array($type_string, $object_link));