From 0030fe2d63684b6612316b5162d34bfaa5dbfee1 Mon Sep 17 00:00:00 2001 From: cash Date: Mon, 21 Feb 2011 02:35:43 +0000 Subject: fixes #2885 #2569 likes controlled by canAnnotate() with plugin hook git-svn-id: http://code.elgg.org/elgg/trunk@8382 36083f99-b078-4883-b0ff-0f9b5a30f544 --- actions/likes/add.php | 4 +-- engine/classes/ElggEntity.php | 37 +++++++++++++++++++++++++-- languages/en.php | 1 - views/default/core/likes/display.php | 2 +- views/default/core/river/controls.php | 47 +++++++++++++++++++---------------- 5 files changed, 63 insertions(+), 28 deletions(-) diff --git a/actions/likes/add.php b/actions/likes/add.php index a59ccae2b..12eed2427 100644 --- a/actions/likes/add.php +++ b/actions/likes/add.php @@ -21,8 +21,8 @@ if (!$entity) { } // cannot like your own stuff -if (elgg_get_logged_in_user_guid() == $entity->getOwnerGUID()) { - register_error(elgg_echo("likes:no_self_like")); +if (!$entity->canAnnotate(0, 'likes')) { + // plugins should register the error message to explain why liking isn't allowed forward(REFERER); } diff --git a/engine/classes/ElggEntity.php b/engine/classes/ElggEntity.php index 0298d6d6e..69401a7e5 100644 --- a/engine/classes/ElggEntity.php +++ b/engine/classes/ElggEntity.php @@ -935,9 +935,9 @@ abstract class ElggEntity extends ElggData implements } /** - * Can a user comment on an entity + * Can a user comment on an entity? * - * @tip Can be overridden vy registering for the permissions_check:comment, + * @tip Can be overridden by registering for the permissions_check:comment, * plugin hook. * * @param int $user_guid User guid (default is logged in user) @@ -956,6 +956,39 @@ abstract class ElggEntity extends ElggData implements return elgg_trigger_plugin_hook('permissions_check:comment', $this->type, $params, null); } + /** + * Can a user annotate an entity? + * + * @tip Can be overridden by registering for the permissions_check:annotate, + * plugin hook. + * + * @tip If you want logged out users to annotate an object, do not call + * canAnnotate(). It's easier than using the plugin hook. + * + * @param int $user_guid User guid (default is logged in user) + * @param string $annotation_name The name of the annotation (default is unspecified) + * + * @return bool + */ + public function canAnnotate($user_guid = 0, $annotation_name = '') { + if ($user_guid == 0) { + $user_guid = elgg_get_logged_in_user_guid(); + } + $user = get_entity($user_guid); + + $return = true; + if (!$user) { + $return = false; + } + + $params = array( + 'entity' => $this, + 'user' => $user, + 'annotation_name' => $annotation_name, + ); + return elgg_trigger_plugin_hook('permissions_check:annotate', $this->type, $params, $return); + } + /** * Returns the access_id. * diff --git a/languages/en.php b/languages/en.php index 59eedb9e1..60eea1a65 100644 --- a/languages/en.php +++ b/languages/en.php @@ -973,7 +973,6 @@ If you requested this click on the link below, otherwise ignore this email. 'likes:deleted' => 'Your like has been removed', 'likes:see' => 'See who else liked this', 'likes:remove' => 'Unlike this', - 'likes:no_self_like' => 'You cannot like your own content', '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', diff --git a/views/default/core/likes/display.php b/views/default/core/likes/display.php index 28ffafcb4..570c4d1ff 100644 --- a/views/default/core/likes/display.php +++ b/views/default/core/likes/display.php @@ -14,7 +14,7 @@ if (!isset($vars['entity'])) { $guid = $vars['entity']->getGUID(); // check to see if the user has already liked this -if (elgg_is_logged_in()) { +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( diff --git a/views/default/core/river/controls.php b/views/default/core/river/controls.php index 615da7f96..21ce8414c 100644 --- a/views/default/core/river/controls.php +++ b/views/default/core/river/controls.php @@ -11,6 +11,7 @@ $object = $vars['item']->getObjectEntity(); if (elgg_is_logged_in()) { // comments and non-objects cannot be commented on or liked if ($vars['item']->annotation_id == 0) { + // comments if ($object->canComment()) { $params = array( 'href' => '#', @@ -22,28 +23,30 @@ if (elgg_is_logged_in()) { } // like this - if (!elgg_annotation_exists($object->getGUID(), 'likes')) { - $url = "action/likes/add?guid={$object->getGUID()}"; - $params = array( - 'href' => $url, - 'text' => elgg_echo('likes:likethis'), - 'is_action' => true, - ); - echo 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_echo('likes:remove'), - 'is_action' => true, - ); - echo elgg_view('output/url', $params); + if ($object->canAnnotate(0, 'likes')) { + if (!elgg_annotation_exists($object->getGUID(), 'likes')) { + $url = "action/likes/add?guid={$object->getGUID()}"; + $params = array( + 'href' => $url, + 'text' => elgg_echo('likes:likethis'), + 'is_action' => true, + ); + echo 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_echo('likes:remove'), + 'is_action' => true, + ); + echo elgg_view('output/url', $params); + } } } -- cgit v1.2.3