diff options
author | dave <dave@36083f99-b078-4883-b0ff-0f9b5a30f544> | 2010-03-21 16:22:19 +0000 |
---|---|---|
committer | dave <dave@36083f99-b078-4883-b0ff-0f9b5a30f544> | 2010-03-21 16:22:19 +0000 |
commit | 64be91148a881f65224c583b92c407205db24634 (patch) | |
tree | 3b4eba097b3fa6e990091c7b882642fe139f86c6 | |
parent | ad745e050da855d326d1b6bc3df8032c05574392 (diff) | |
download | elgg-64be91148a881f65224c583b92c407205db24634.tar.gz elgg-64be91148a881f65224c583b92c407205db24634.tar.bz2 |
generic like this functionality added
git-svn-id: http://code.elgg.org/elgg/trunk@5456 36083f99-b078-4883-b0ff-0f9b5a30f544
-rw-r--r-- | actions/likes/add.php | 64 | ||||
-rw-r--r-- | actions/likes/delete.php | 32 | ||||
-rw-r--r-- | engine/lib/annotations.php | 23 | ||||
-rw-r--r-- | engine/lib/elgglib.php | 35 | ||||
-rw-r--r-- | languages/en.php | 15 | ||||
-rw-r--r-- | views/default/annotation/annotatelike.php | 11 | ||||
-rw-r--r-- | views/default/annotation/likes.php | 39 | ||||
-rw-r--r-- | views/default/likes/forms/edit.php | 30 |
8 files changed, 248 insertions, 1 deletions
diff --git a/actions/likes/add.php b/actions/likes/add.php new file mode 100644 index 000000000..36672d3ec --- /dev/null +++ b/actions/likes/add.php @@ -0,0 +1,64 @@ +<?php
+/**
+ * Elgg add like action
+ *
+ * @package Elgg
+ * @author Curverider <curverider.co.uk>
+ * @link http://elgg.org/
+ */
+
+// Make sure we're logged in; forward to the front page if not
+gatekeeper();
+
+// Get input
+$entity_guid = (int) get_input('guid');
+//check to see if the user has already liked the item
+if(elgg_already_created_annotation($entity_guid, 'likes')){
+ system_message(elgg_echo("likes:alreadyliked"));
+ forward($_SERVER['HTTP_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($_SERVER['HTTP_REFERER']);
+}
+
+$user = get_loggedin_user();
+$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($_SERVER['HTTP_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'),
+ sprintf(
+ elgg_echo('likes:email:body'),
+ $entity->title,
+ $user->name,
+ $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($_SERVER['HTTP_REFERER']);
diff --git a/actions/likes/delete.php b/actions/likes/delete.php new file mode 100644 index 000000000..cbb89f67a --- /dev/null +++ b/actions/likes/delete.php @@ -0,0 +1,32 @@ +<?php
+/**
+ * Elgg delete like action
+ *
+ * @package Elgg
+ * @author Curverider <curverider.co.uk>
+ * @link http://elgg.org/
+ */
+
+// Ensure we're logged in
+if (!isloggedin()) {
+ forward();
+}
+
+// Make sure we can get the comment in question
+$annotation_id = (int) get_input('annotation_id');
+if ($likes = get_annotation($annotation_id)) {
+
+ $entity = get_entity($likes->entity_guid);
+
+ if ($likes->canEdit()) {
+ $likes->delete();
+ system_message(elgg_echo("likes:deleted"));
+ forward($entity->getURL());
+ }
+
+} else {
+ $url = "";
+}
+
+register_error(elgg_echo("likes:notdeleted"));
+forward($entity->getURL());
\ No newline at end of file diff --git a/engine/lib/annotations.php b/engine/lib/annotations.php index b18475d57..b7dc1b9d3 100644 --- a/engine/lib/annotations.php +++ b/engine/lib/annotations.php @@ -1250,6 +1250,29 @@ function get_annotation_url($id) { return false; } +/** + * Check to see if a user has already created an annotation on an object + * + * @param ElggEntity $entity + * @return true | false + */ +function elgg_already_created_annotation($entity_guid, $annotation_type) { + global $CONFIG; + $entity_guid = (int)$entity_guid; + $annotation_type = sanitise_string($annotation_type); + $owner_guid = get_loggedin_userid(); + $sql = "select a.id" . + " FROM {$CONFIG->dbprefix}annotations a, {$CONFIG->dbprefix}metastrings m " . + " WHERE a.owner_guid={$owner_guid} AND a.entity_guid={$entity_guid} " . + " AND a.name_id=m.id AND m.string='{$annotation_type}'"; + //get the annotation type id + $check_annotation = get_data_row($sql); + //check to see if the user has already liked + if($check_annotation) + return true; + else + return false; +} /** * Register an annotation url handler. diff --git a/engine/lib/elgglib.php b/engine/lib/elgglib.php index 7691ca5bb..fb61b7d10 100644 --- a/engine/lib/elgglib.php +++ b/engine/lib/elgglib.php @@ -1002,7 +1002,38 @@ function get_submenu() { return $submenu_total; } - +/** + * Automatically views likes and a like input relating to the given entity + * + * @param ElggEntity $entity The entity to like + * @return string|false The HTML (etc) for the likes, or false on failure + */ +function elgg_view_likes($entity){ + if (!($entity instanceof ElggEntity)) { + return false; + } + if ($likes = trigger_plugin_hook('likes',$entity->getType(),array('entity' => $entity),false)) { + return $likes; + } else { + //display the form + $likes = elgg_view('likes/forms/edit',array('entity' => $entity)); + return $likes; + } +} +/** + * Count the number of likes attached to an entity + * + * @param ElggEntity $entity + * @return int Number of likes + */ +function elgg_count_likes($entity) { + if ($likeno = trigger_plugin_hook('likes:count', $entity->getType(), + array('entity' => $entity), false)) { + return $likeno; + } else { + return count_annotations($entity->getGUID(), "", "", "likes"); + } +} /** * Automatically views comments and a comment form relating to the given entity @@ -2901,6 +2932,8 @@ function elgg_boot() { // Actions register_action('comments/add'); register_action('comments/delete'); + register_action('likes/add'); + register_action('likes/delete'); elgg_view_register_simplecache('css'); elgg_view_register_simplecache('js/friendsPickerv1'); diff --git a/languages/en.php b/languages/en.php index f51940e6e..0fc4e3ba6 100644 --- a/languages/en.php +++ b/languages/en.php @@ -823,6 +823,21 @@ If you requested this click on the link below, otherwise ignore this email. * XML-RPC */ 'xmlrpc:noinputdata' => "Input data missing", + +/** + * Likes + **/ + 'likes:this' => 'liked this', + 'likes:deleted' => 'Your like has been removed', + '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:userslikethis' => 'users liked this', + 'likes:river:annotate' => 'likes', /** * Comments diff --git a/views/default/annotation/annotatelike.php b/views/default/annotation/annotatelike.php new file mode 100644 index 000000000..7c7b1887a --- /dev/null +++ b/views/default/annotation/annotatelike.php @@ -0,0 +1,11 @@ +<?php
+
+$performed_by = get_entity($vars['item']->subject_guid);
+$object = get_entity($vars['item']->object_guid);
+$url = $object->getURL();
+$title = $object->title;
+
+$string = "<a href=\"{$performed_by->getURL()}\">{$performed_by->name}</a> ";
+$string .= elgg_echo("likes:river:annotate") . " <a href=\"{$object->getURL()}\">" . $title . "</a> " . friendly_time($object->time_created);
+
+echo $string;
\ No newline at end of file diff --git a/views/default/annotation/likes.php b/views/default/annotation/likes.php new file mode 100644 index 000000000..d095c82eb --- /dev/null +++ b/views/default/annotation/likes.php @@ -0,0 +1,39 @@ +<?php
+/**
+ * Elgg show the user who liked the object
+ */
+
+$owner = get_user($vars['annotation']->owner_guid);
+
+?>
+<div class="elgg_likes clearfloat">
+ <div class="elgg_likes_icon">
+ <?php
+ echo elgg_view("profile/icon", array(
+ 'entity' => $owner,
+ 'size' => 'tiny'
+ ));
+ ?>
+ </div>
+
+ <div class="elgg_likes_details">
+ <?php
+ // if the user looking at the like listing can edit, show the delete link
+ if ($vars['annotation']->canEdit()) {
+ ?>
+ <span class="delete_button">
+ <?php echo elgg_view("output/confirmlink",array(
+ 'href' => $vars['url'] . "action/likes/delete?annotation_id=" . $vars['annotation']->id,
+ 'text' => elgg_echo('remove'),
+ 'confirm' => elgg_echo('deleteconfirm')
+ ));
+ ?>
+ </span>
+ <?php
+ } //end of can edit if statement
+ ?>
+ <p class="elgg_likes_owner">
+ <a href="<?php echo $owner->getURL(); ?>"><?php echo $owner->name; ?></a> <?php echo elgg_echo('likes:this'); ?>
+ </p>
+ </div>
+</div>
\ No newline at end of file diff --git a/views/default/likes/forms/edit.php b/views/default/likes/forms/edit.php new file mode 100644 index 000000000..4a9bbb7d7 --- /dev/null +++ b/views/default/likes/forms/edit.php @@ -0,0 +1,30 @@ +<?php +/** + * Elgg likes add form + * + * @package Elgg + * @author Curverider Ltd <info@elgg.com> + * @link http://elgg.com/ + * + * @uses $vars['entity'] + */ + +if (isset($vars['entity']) && isloggedin()) { + $guid = $vars['entity']->getGuid(); + $url = elgg_add_action_tokens_to_url($vars['url'] . "action/likes/add?guid={$guid}"); + //check to see if the user has already liked + if(!elgg_already_created_annotation($guid, 'likes')) + echo "<a href=\"{$url}\">" . elgg_echo('likes:likethis') . "</a>"; + //display the number of likes + $numoflikes = elgg_count_likes($vars['entity']); + if($numoflikes != 0){ + if($numoflikes == 1) + echo "<br /><a onclick=\" $('#showLikes').show('slow');\">" . elgg_count_likes($vars['entity']) . " " . elgg_echo('likes:userlikedthis') . "</a>"; + else + echo "<br /><a onclick=\" $('#showLikes').show('slow');\">" . elgg_count_likes($vars['entity']) . " " . elgg_echo('likes:userslikethis') . "</a>"; + } + //show the users who liked the object + echo "<div id=\"showLikes\" style=\"display:none;\">"; + echo list_annotations($vars['entity']->getGUID(),'likes'); + echo "</div>"; +}
\ No newline at end of file |