diff options
author | cash <cash@36083f99-b078-4883-b0ff-0f9b5a30f544> | 2011-03-09 03:07:40 +0000 |
---|---|---|
committer | cash <cash@36083f99-b078-4883-b0ff-0f9b5a30f544> | 2011-03-09 03:07:40 +0000 |
commit | bdb5d2b95792782ebc379e3348cad1812c1980b4 (patch) | |
tree | c923d740327b3aeb62a24246f3725ced8677a5f7 /mod/likes/start.php | |
parent | 9ea30ea494cea4c950894a0ac1131eefda10868b (diff) | |
download | elgg-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/start.php')
-rw-r--r-- | mod/likes/start.php | 104 |
1 files changed, 104 insertions, 0 deletions
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'); + } +} |