Elgg  Version 1.11
friends.php
Go to the documentation of this file.
1 <?php
16 function _elgg_friends_init() {
17  elgg_register_action('friends/add');
18  elgg_register_action('friends/remove');
19 
20  elgg_register_action('friends/collections/add');
21  elgg_register_action('friends/collections/delete');
22  elgg_register_action('friends/collections/edit');
23 
24  elgg_register_page_handler('friends', '_elgg_friends_page_handler');
25  elgg_register_page_handler('friendsof', '_elgg_friends_page_handler');
26  elgg_register_page_handler('collections', '_elgg_collections_page_handler');
27 
28  elgg_register_widget_type('friends', elgg_echo('friends'), elgg_echo('friends:widget:description'));
29 
30  elgg_register_event_handler('pagesetup', 'system', '_elgg_friends_page_setup');
31  elgg_register_event_handler('pagesetup', 'system', '_elgg_setup_collections_menu');
32  elgg_register_plugin_hook_handler('register', 'menu:user_hover', '_elgg_friends_setup_user_hover_menu');
33  elgg_register_event_handler('create', 'friend', '_elgg_send_friend_notification');
34 }
35 
43 
44  if ($owner) {
45  $params = array(
46  'name' => 'friends',
47  'text' => elgg_echo('friends'),
48  'href' => 'friends/' . $owner->username,
49  'contexts' => array('friends')
50  );
52 
53  $params = array(
54  'name' => 'friends:of',
55  'text' => elgg_echo('friends:of'),
56  'href' => 'friendsof/' . $owner->username,
57  'contexts' => array('friends')
58  );
60  }
61 
62  // topbar
63  if ($viewer) {
64  elgg_register_menu_item('topbar', array(
65  'name' => 'friends',
66  'href' => "friends/{$viewer->username}",
67  'text' => elgg_view_icon('users'),
68  'title' => elgg_echo('friends'),
69  'priority' => 300,
70  ));
71  }
72 }
73 
80  $user = $params['entity'];
81  /* @var \ElggUser $user */
82 
83  if (elgg_is_logged_in()) {
84  if (elgg_get_logged_in_user_guid() != $user->guid) {
85  $isFriend = $user->isFriend();
86 
87  // Always emit both to make it super easy to toggle with ajax
89  'name' => 'remove_friend',
90  'href' => elgg_add_action_tokens_to_url("action/friends/remove?friend={$user->guid}"),
91  'text' => elgg_echo('friend:remove'),
92  'section' => 'action',
93  'item_class' => $isFriend ? '' : 'hidden',
94  ));
95 
97  'name' => 'add_friend',
98  'href' => elgg_add_action_tokens_to_url("action/friends/add?friend={$user->guid}"),
99  'text' => elgg_echo('friend:add'),
100  'section' => 'action',
101  'item_class' => $isFriend ? 'hidden' : '',
102  ));
103  }
104  }
105 
106  return $return;
107 }
108 
118 function _elgg_friends_page_handler($segments, $handler) {
119  elgg_set_context('friends');
120 
121  if (isset($segments[0]) && $user = get_user_by_username($segments[0])) {
122  elgg_set_page_owner_guid($user->getGUID());
123  }
124 
125  if (!elgg_get_page_owner_guid()) {
126  return false;
127  }
128 
129  switch ($handler) {
130  case 'friends':
131  require_once(dirname(dirname(dirname(__FILE__))) . "/pages/friends/index.php");
132  break;
133  case 'friendsof':
134  require_once(dirname(dirname(dirname(__FILE__))) . "/pages/friends/of.php");
135  break;
136  default:
137  return false;
138  }
139  return true;
140 }
141 
150 function _elgg_collections_page_handler($page_elements) {
151  elgg_set_context('friends');
152  $base = elgg_get_config('path');
153  if (isset($page_elements[0])) {
154  switch ($page_elements[0]) {
155  case 'add':
157 
158  require_once "{$base}pages/friends/collections/add.php";
159  return true;
160  break;
161  case 'owner':
162  $user = get_user_by_username($page_elements[1]);
163  if ($user) {
164  elgg_set_page_owner_guid($user->getGUID());
165 
166  require_once "{$base}pages/friends/collections/view.php";
167  return true;
168  }
169  break;
170  }
171  }
172  return false;
173 }
174 
182 
185 
186  elgg_register_menu_item('page', array(
187  'name' => 'friends:view:collections',
188  'text' => elgg_echo('friends:collections'),
189  'href' => "collections/owner/$user->username",
190  'contexts' => array('friends')
191  ));
192  }
193 }
194 
206  $user_one = get_entity($object->guid_one);
207  /* @var \ElggUser $user_one */
208 
209  $user_two = get_entity($object->guid_two);
210  /* @var ElggUser $user_two */
211 
212  // Notification subject
213  $subject = elgg_echo('friend:newfriend:subject', array(
214  $user_one->name
215  ), $user_two->language);
216 
217  // Notification body
218  $body = elgg_echo("friend:newfriend:body", array(
219  $user_one->name,
220  $user_one->getURL()
221  ), $user_two->language);
222 
223  return notify_user($user_two->guid, $object->guid_one, $subject, $body);
224 }
225 
226 return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
227  $events->registerHandler('init', 'system', '_elgg_friends_init');
228 };
elgg_get_config($name, $site_guid=0)
Get an Elgg configuration value.
elgg_is_logged_in()
Returns whether or not the user is currently logged in.
Definition: sessions.php:51
elgg_add_action_tokens_to_url($url, $html_encode=false)
Adds action tokens to URL.
Definition: elgglib.php:1059
$subject
Definition: exceptions.php:25
elgg_view_icon($name, $vars=array())
View one of the elgg sprite icons.
Definition: views.php:1403
$object
Definition: upgrade.php:12
elgg_register_widget_type($handler, $name, $description, $context=array('all'), $multiple=false)
Register a widget type.
Definition: widgets.php:73
$return
Definition: opendd.php:15
elgg_register_plugin_hook_handler($hook, $type, $callback, $priority=500)
Definition: elgglib.php:703
elgg_set_page_owner_guid($guid)
Set the guid of the entity that owns this page.
Definition: pageowner.php:73
_elgg_friends_page_setup()
Register some menu items for friends UI private.
Definition: friends.php:40
get_user_by_username($username)
Get user by username.
Definition: users.php:98
$params
Definition: login.php:72
_elgg_collections_page_handler($page_elements)
Page handler for friends collections.
Definition: friends.php:150
static factory($options)
Create an ElggMenuItem from an associative array.
_elgg_friends_init()
Init friends library.
Definition: friends.php:16
_elgg_friends_page_handler($segments, $handler)
Page handler for friends-related pages.
Definition: friends.php:118
elgg_echo($message_key, $args=array(), $language="")
Given a message key, returns an appropriately translated full-text string.
Definition: languages.php:21
$owner
Definition: crop.php:8
elgg_set_context($context)
Sets the page context.
Definition: pageowner.php:201
$user
Definition: ban.php:13
elgg_register_page_handler($identifier, $function)
Registers a page handler for a particular identifier.
Definition: pagehandler.php:34
elgg_get_page_owner_entity()
Gets the owner entity for the current page.
Definition: pageowner.php:53
$type
Definition: add.php:8
elgg_register_menu_item($menu_name, $menu_item)
Register an item for an Elgg menu.
Definition: navigation.php:92
elgg_register_event_handler($event, $object_type, $callback, $priority=500)
Definition: elgglib.php:519
_elgg_send_friend_notification($event, $type, $object)
Notify user that someone has friended them.
Definition: friends.php:205
_elgg_friends_setup_user_hover_menu($hook, $type, $return, $params)
Adds friending to user hover menu.
Definition: friends.php:79
notify_user($to, $from, $subject, $message, array $params=array(), $methods_override="")
Notify a user via their preferences.
elgg_get_logged_in_user_entity()
Return the current logged in user, or null if no user is logged in.
Definition: sessions.php:32
$handler
Definition: add.php:10
elgg_register_action($action, $filename="", $access= 'logged_in')
Registers an action.
Definition: actions.php:85
_elgg_setup_collections_menu()
Adds collection sidebar menu items.
Definition: friends.php:181
elgg_get_page_owner_guid($guid=0)
Gets the guid of the entity that owns the current page.
Definition: pageowner.php:18
elgg_get_logged_in_user_guid()
Return the current logged in user by guid.
Definition: sessions.php:42
get_entity($guid)
Loads and returns an entity object from a guid.
Definition: entities.php:382