From 25587657328ca8b492a497e385a2fc3887925848 Mon Sep 17 00:00:00 2001 From: cash Date: Mon, 21 Feb 2011 01:45:21 +0000 Subject: Fixes #2971 added canComment() method and implemented it for ElggBlog git-svn-id: http://code.elgg.org/elgg/trunk@8381 36083f99-b078-4883-b0ff-0f9b5a30f544 --- engine/classes/ElggEntity.php | 22 ++++++++++++++++++++++ engine/classes/ElggGroup.php | 17 +++++++++++++++++ engine/classes/ElggObject.php | 35 +++++++++++++++++++++++++++++++++++ engine/classes/ElggUser.php | 17 +++++++++++++++++ mod/blog/classes/ElggBlog.php | 23 +++++++++++++++++++++++ mod/thewire/classes/ElggWire.php | 34 ++++++++++++++++++++++++++++++++++ mod/thewire/start.php | 4 ++++ views/default/core/river/controls.php | 18 ++++++++++-------- 8 files changed, 162 insertions(+), 8 deletions(-) create mode 100644 mod/thewire/classes/ElggWire.php diff --git a/engine/classes/ElggEntity.php b/engine/classes/ElggEntity.php index d6c8a1311..0298d6d6e 100644 --- a/engine/classes/ElggEntity.php +++ b/engine/classes/ElggEntity.php @@ -934,6 +934,28 @@ abstract class ElggEntity extends ElggData implements return can_write_to_container($user_guid, $this->guid, $type, $subtype); } + /** + * Can a user comment on an entity + * + * @tip Can be overridden vy registering for the permissions_check:comment, + * plugin hook. + * + * @param int $user_guid User guid (default is logged in user) + * + * @return bool + */ + public function canComment($user_guid = 0) { + if ($user_guid == 0) { + $user_guid = elgg_get_logged_in_user_guid(); + } + $user = get_entity($user_guid); + + // By default, we don't take a position of whether commenting is allowed + // because it is handled by the subclasses of ElggEntity + $params = array('entity' => $this, 'user' => $user); + return elgg_trigger_plugin_hook('permissions_check:comment', $this->type, $params, null); + } + /** * Returns the access_id. * diff --git a/engine/classes/ElggGroup.php b/engine/classes/ElggGroup.php index c709eb3a6..49ba27204 100644 --- a/engine/classes/ElggGroup.php +++ b/engine/classes/ElggGroup.php @@ -380,4 +380,21 @@ class ElggGroup extends ElggEntity 'description', )); } + + /** + * Can a user comment on this group? + * + * @see ElggEntity::canComment() + * + * @param int $user_guid User guid (default is logged in user) + * @return bool + * @since 1.8.0 + */ + public function canComment($user_guid = 0) { + $result = parent::canComment($user_guid); + if ($result !== null) { + return $result; + } + return false; + } } diff --git a/engine/classes/ElggObject.php b/engine/classes/ElggObject.php index c47cd217f..f71801144 100644 --- a/engine/classes/ElggObject.php +++ b/engine/classes/ElggObject.php @@ -184,4 +184,39 @@ class ElggObject extends ElggEntity { 'description', )); } + + /** + * Can a user comment on this object? + * + * @see ElggEntity::canComment() + * + * @param int $user_guid User guid (default is logged in user) + * @return bool + * @since 1.8.0 + */ + public function canComment($user_guid = 0) { + $result = parent::canComment($user_guid); + if ($result !== null) { + return $result; + } + + if ($user_guid == 0) { + $user_guid = elgg_get_logged_in_user_guid(); + } + + // must be logged in to comment + if (!$user_guid) { + return false; + } + + // must be member of group + if (elgg_instanceof($this->getContainerEntity(), 'group')) { + if (!$this->getContainerEntity()->isMember(get_user($user_guid))) { + return false; + } + } + + // no checks on read access since a user cannot see entities outside his access + return true; + } } diff --git a/engine/classes/ElggUser.php b/engine/classes/ElggUser.php index 82f03c6ed..cceaacf58 100644 --- a/engine/classes/ElggUser.php +++ b/engine/classes/ElggUser.php @@ -559,4 +559,21 @@ class ElggUser extends ElggEntity return parent::__get($name); } + + /** + * Can a user comment on this user? + * + * @see ElggEntity::canComment() + * + * @param int $user_guid User guid (default is logged in user) + * @return bool + * @since 1.8.0 + */ + public function canComment($user_guid = 0) { + $result = parent::canComment($user_guid); + if ($result !== null) { + return $result; + } + return false; + } } diff --git a/mod/blog/classes/ElggBlog.php b/mod/blog/classes/ElggBlog.php index 7a64a1777..0f0f27cf8 100644 --- a/mod/blog/classes/ElggBlog.php +++ b/mod/blog/classes/ElggBlog.php @@ -39,4 +39,27 @@ class ElggBlog extends ElggObject { // // return FALSE; // } + + /** + * Can a user comment on this blog? + * + * @see ElggObject::canComment() + * + * @param int $user_guid User guid (default is logged in user) + * @return bool + * @since 1.8.0 + */ + public function canComment($user_guid = 0) { + $result = parent::canComment($user_guid); + if ($result == false) { + return $result; + } + + if ($this->comments_on == 'Off') { + return false; + } + + return true; + } + } \ No newline at end of file diff --git a/mod/thewire/classes/ElggWire.php b/mod/thewire/classes/ElggWire.php new file mode 100644 index 000000000..d864cc259 --- /dev/null +++ b/mod/thewire/classes/ElggWire.php @@ -0,0 +1,34 @@ +attributes['subtype'] = 'thewire'; + } + + /** + * Can a user comment on this wire post? + * + * @see ElggObject::canComment() + * + * @param int $user_guid User guid (default is logged in user) + * @return bool + * @since 1.8.0 + */ + public function canComment($user_guid = 0) { + $result = parent::canComment($user_guid); + if ($result == false) { + return $result; + } + + return false; + } + +} diff --git a/mod/thewire/start.php b/mod/thewire/start.php index 12e4b0884..0f51d052e 100644 --- a/mod/thewire/start.php +++ b/mod/thewire/start.php @@ -17,6 +17,10 @@ register_elgg_event_handler('init', 'system', 'thewire_init'); function thewire_init() { global $CONFIG; + if (!update_subtype('object', 'thewire', 'ElggWire')) { + add_subtype('object', 'thewire', 'ElggWire'); + } + // add a site navigation item $item = new ElggMenuItem('thewire', elgg_echo('thewire'), 'pg/thewire/all'); elgg_register_menu_item('site', $item); diff --git a/views/default/core/river/controls.php b/views/default/core/river/controls.php index 6f2be623c..615da7f96 100644 --- a/views/default/core/river/controls.php +++ b/views/default/core/river/controls.php @@ -10,14 +10,16 @@ $object = $vars['item']->getObjectEntity(); if (elgg_is_logged_in()) { // comments and non-objects cannot be commented on or liked - if ($object->getType() == 'object' && $vars['item']->annotation_id == 0) { - $params = array( - 'href' => '#', - 'text' => elgg_echo('generic_comments:text'), - 'class' => 'elgg-toggle', - 'id' => "elgg-toggler-{$object->getGUID()}", - ); - echo elgg_view('output/url', $params); + if ($vars['item']->annotation_id == 0) { + if ($object->canComment()) { + $params = array( + 'href' => '#', + 'text' => elgg_echo('generic_comments:text'), + 'class' => 'elgg-toggle', + 'id' => "elgg-toggler-{$object->getGUID()}", + ); + echo elgg_view('output/url', $params); + } // like this if (!elgg_annotation_exists($object->getGUID(), 'likes')) { -- cgit v1.2.3