aboutsummaryrefslogtreecommitdiff
path: root/mod/likes
diff options
context:
space:
mode:
Diffstat (limited to 'mod/likes')
-rw-r--r--mod/likes/actions/likes/add.php50
-rw-r--r--mod/likes/actions/likes/delete.php31
-rw-r--r--mod/likes/languages/en.php45
-rw-r--r--mod/likes/manifest.xml17
-rw-r--r--mod/likes/start.php171
-rw-r--r--mod/likes/views/default/annotation/likes.php49
-rw-r--r--mod/likes/views/default/likes/button.php39
-rw-r--r--mod/likes/views/default/likes/count.php36
-rw-r--r--mod/likes/views/default/likes/css.php17
-rw-r--r--mod/likes/views/default/likes/js.php26
10 files changed, 481 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..a6a8d6c45
--- /dev/null
+++ b/mod/likes/actions/likes/add.php
@@ -0,0 +1,50 @@
+<?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);
+}
+
+// limit likes through a plugin hook (to prevent liking your own content for example)
+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) {
+
+ likes_notify_user($entity->getOwnerEntity(), $user, $entity);
+}
+
+system_message(elgg_echo("likes:likes"));
+
+// 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..322d512e8
--- /dev/null
+++ b/mod/likes/actions/likes/delete.php
@@ -0,0 +1,31 @@
+<?php
+/**
+ * Elgg delete like action
+ *
+ */
+
+// Support deleting by id in case we're deleting another user's likes
+$id = (int) get_input('id');
+
+$like = NULL;
+if ($id) {
+ $like = elgg_get_annotation_from_id($id);
+}
+
+if (!$like) {
+ $likes = elgg_get_annotations(array(
+ 'guid' => (int) get_input('guid'),
+ 'annotation_owner_guid' => elgg_get_logged_in_user_guid(),
+ 'annotation_name' => 'likes',
+ ));
+ $like = $likes[0];
+}
+
+if ($like && $like->canEdit()) {
+ $like->delete();
+ system_message(elgg_echo("likes:deleted"));
+ forward(REFERER);
+}
+
+register_error(elgg_echo("likes:notdeleted"));
+forward(REFERER);
diff --git a/mod/likes/languages/en.php b/mod/likes/languages/en.php
new file mode 100644
index 000000000..bdbd2a963
--- /dev/null
+++ b/mod/likes/languages/en.php
@@ -0,0 +1,45 @@
+<?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' => '%s like',
+ 'likes:userslikedthis' => '%s likes',
+ 'likes:river:annotate' => 'likes',
+ 'likes:delete:confirm' => 'Are you sure you want to delete this like?',
+
+ 'river:likes' => 'likes %s %s',
+
+ // notifications. yikes.
+ 'likes:notifications:subject' => '%s likes your post "%s"',
+ 'likes:notifications:body' =>
+'Hi %1$s,
+
+%2$s likes your post "%3$s" on %4$s
+
+See your original post here:
+
+%5$s
+
+or view %2$s\'s profile here:
+
+%6$s
+
+Thanks,
+%4$s
+',
+
+);
+
+add_translation('en', $english);
diff --git a/mod/likes/manifest.xml b/mod/likes/manifest.xml
new file mode 100644
index 000000000..62835fcf5
--- /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>
+ <category>social</category>
+ <description>Enables users to like content on the site.</description>
+ <website>http://www.elgg.org/</website>
+ <copyright>See COPYRIGHT.txt</copyright>
+ <license>GNU General Public License version 2</license>
+ <requires>
+ <type>elgg_release</type>
+ <version>1.8</version>
+ </requires>
+ <activate_on_install>true</activate_on_install>
+</plugin_manifest>
diff --git a/mod/likes/start.php b/mod/likes/start.php
new file mode 100644
index 000000000..0f8e12159
--- /dev/null
+++ b/mod/likes/start.php
@@ -0,0 +1,171 @@
+<?php
+/**
+ * Likes plugin
+ *
+ */
+
+elgg_register_event_handler('init', 'system', 'likes_init');
+
+function likes_init() {
+
+ elgg_extend_view('css/elgg', 'likes/css');
+ elgg_extend_view('js/elgg', 'likes/js');
+
+ // 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 button
+ $options = array(
+ 'name' => 'likes',
+ 'text' => elgg_view('likes/button', array('entity' => $entity)),
+ 'href' => false,
+ 'priority' => 1000,
+ );
+ $return[] = ElggMenuItem::factory($options);
+
+ // likes count
+ $count = elgg_view('likes/count', array('entity' => $entity));
+ if ($count) {
+ $options = array(
+ 'name' => 'likes_count',
+ 'text' => $count,
+ 'href' => false,
+ 'priority' => 1001,
+ );
+ $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'];
+
+ // only like group creation #3958
+ if ($item->type == "group" && $item->view != "river/group/create") {
+ return $return;
+ }
+
+ // don't like users #4116
+ if ($item->type == "user") {
+ return $return;
+ }
+
+ $object = $item->getObjectEntity();
+ if (!elgg_in_context('widgets') && $item->annotation_id == 0) {
+ if ($object->canAnnotate(0, 'likes')) {
+ // like button
+ $options = array(
+ 'name' => 'likes',
+ 'href' => false,
+ 'text' => elgg_view('likes/button', array('entity' => $object)),
+ 'is_action' => true,
+ 'priority' => 100,
+ );
+ $return[] = ElggMenuItem::factory($options);
+
+ // likes count
+ $count = elgg_view('likes/count', array('entity' => $object));
+ if ($count) {
+ $options = array(
+ 'name' => 'likes_count',
+ 'text' => $count,
+ 'href' => false,
+ 'priority' => 101,
+ );
+ $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');
+ }
+}
+
+/**
+ * Notify $user that $liker liked his $entity.
+ *
+ * @param type $user
+ * @param type $liker
+ * @param type $entity
+ */
+function likes_notify_user(ElggUser $user, ElggUser $liker, ElggEntity $entity) {
+
+ if (!$user instanceof ElggUser) {
+ return false;
+ }
+
+ if (!$liker instanceof ElggUser) {
+ return false;
+ }
+
+ if (!$entity instanceof ElggEntity) {
+ return false;
+ }
+
+ $title_str = $entity->title;
+ if (!$title_str) {
+ $title_str = elgg_get_excerpt($entity->description);
+ }
+
+ $site = get_config('site');
+
+ $subject = elgg_echo('likes:notifications:subject', array(
+ $liker->name,
+ $title_str
+ ));
+
+ $body = elgg_echo('likes:notifications:body', array(
+ $user->name,
+ $liker->name,
+ $title_str,
+ $site->name,
+ $entity->getURL(),
+ $liker->getURL()
+ ));
+
+ notify_user($user->guid,
+ $liker->guid,
+ $subject,
+ $body
+ );
+} \ No newline at end of file
diff --git a/mod/likes/views/default/annotation/likes.php b/mod/likes/views/default/annotation/likes.php
new file mode 100644
index 000000000..2b5acc520
--- /dev/null
+++ b/mod/likes/views/default/annotation/likes.php
@@ -0,0 +1,49 @@
+<?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,
+ 'is_trusted' => true,
+));
+
+$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?id={$like->id}",
+ 'text' => "<span class=\"elgg-icon elgg-icon-delete float-alt\"></span>",
+ 'confirm' => elgg_echo('likes:delete:confirm'),
+ 'encode_text' => 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/button.php b/mod/likes/views/default/likes/button.php
new file mode 100644
index 000000000..bc7c8fd8a
--- /dev/null
+++ b/mod/likes/views/default/likes/button.php
@@ -0,0 +1,39 @@
+<?php
+/**
+ * Elgg likes button
+ *
+ * @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,
+ 'is_trusted' => true,
+ );
+ $likes_button = elgg_view('output/url', $params);
+ } else {
+ $url = elgg_get_site_url() . "action/likes/delete?guid={$guid}";
+ $params = array(
+ 'href' => $url,
+ 'text' => elgg_view_icon('thumbs-up-alt'),
+ 'title' => elgg_echo('likes:remove'),
+ 'is_action' => true,
+ 'is_trusted' => true,
+ );
+ $likes_button = elgg_view('output/url', $params);
+ }
+}
+
+echo $likes_button;
diff --git a/mod/likes/views/default/likes/count.php b/mod/likes/views/default/likes/count.php
new file mode 100644
index 000000000..071a069bd
--- /dev/null
+++ b/mod/likes/views/default/likes/count.php
@@ -0,0 +1,36 @@
+<?php
+/**
+ * Count of who has liked something
+ *
+ * @uses $vars['entity']
+ */
+
+
+$list = '';
+$num_of_likes = likes_count($vars['entity']);
+$guid = $vars['entity']->getGUID();
+
+if ($num_of_likes) {
+ // display the number of likes
+ if ($num_of_likes == 1) {
+ $likes_string = elgg_echo('likes:userlikedthis', array($num_of_likes));
+ } else {
+ $likes_string = elgg_echo('likes:userslikedthis', array($num_of_likes));
+ }
+ $params = array(
+ 'text' => $likes_string,
+ 'title' => elgg_echo('likes:see'),
+ 'rel' => 'popup',
+ 'href' => "#likes-$guid"
+ );
+ $list = elgg_view('output/url', $params);
+ $list .= "<div class='elgg-module elgg-module-popup elgg-likes hidden clearfix' id='likes-$guid'>";
+ $list .= elgg_list_annotations(array(
+ 'guid' => $guid,
+ 'annotation_name' => 'likes',
+ 'limit' => 99,
+ 'list_class' => 'elgg-list-likes'
+ ));
+ $list .= "</div>";
+ echo $list;
+}
diff --git a/mod/likes/views/default/likes/css.php b/mod/likes/views/default/likes/css.php
new file mode 100644
index 000000000..1ec07b366
--- /dev/null
+++ b/mod/likes/views/default/likes/css.php
@@ -0,0 +1,17 @@
+<?php
+/**
+ * Likes CSS
+ */
+?>
+
+/* ***************************************
+ Likes
+*************************************** */
+.elgg-likes {
+ width: 345px;
+ position: absolute;
+}
+
+.elgg-menu .elgg-menu-item-likes-count {
+ margin-left: 3px;
+}
diff --git a/mod/likes/views/default/likes/js.php b/mod/likes/views/default/likes/js.php
new file mode 100644
index 000000000..f701ebedf
--- /dev/null
+++ b/mod/likes/views/default/likes/js.php
@@ -0,0 +1,26 @@
+<?php
+/**
+ * Likes JavaScript extension for elgg.js
+ */
+?>
+
+/**
+ * Repositions the likes popup
+ *
+ * @param {String} hook 'getOptions'
+ * @param {String} type 'ui.popup'
+ * @param {Object} params An array of info about the target and source.
+ * @param {Object} options Options to pass to
+ *
+ * @return {Object}
+ */
+elgg.ui.likesPopupHandler = function(hook, type, params, options) {
+ if (params.target.hasClass('elgg-likes')) {
+ options.my = 'right bottom';
+ options.at = 'left top';
+ return options;
+ }
+ return null;
+};
+
+elgg.register_hook_handler('getOptions', 'ui.popup', elgg.ui.likesPopupHandler); \ No newline at end of file