Elgg  Version 1.9
friends.php
Go to the documentation of this file.
1 <?php
11 elgg_register_event_handler('init', 'system', '_elgg_friends_init');
12 
18 function _elgg_friends_init() {
19  elgg_register_action('friends/add');
20  elgg_register_action('friends/remove');
21 
22  elgg_register_action('friends/collections/add');
23  elgg_register_action('friends/collections/delete');
24  elgg_register_action('friends/collections/edit');
25 
26  elgg_register_page_handler('friends', '_elgg_friends_page_handler');
27  elgg_register_page_handler('friendsof', '_elgg_friends_page_handler');
28  elgg_register_page_handler('collections', '_elgg_collections_page_handler');
29 
30  elgg_register_widget_type('friends', elgg_echo('friends'), elgg_echo('friends:widget:description'));
31 
32  elgg_register_event_handler('pagesetup', 'system', '_elgg_friends_page_setup');
33  elgg_register_event_handler('pagesetup', 'system', '_elgg_setup_collections_menu');
34  elgg_register_plugin_hook_handler('register', 'menu:user_hover', '_elgg_friends_setup_user_hover_menu');
35  elgg_register_event_handler('create', 'friend', '_elgg_send_friend_notification');
36 }
37 
45 
46  if ($owner) {
47  $params = array(
48  'name' => 'friends',
49  'text' => elgg_echo('friends'),
50  'href' => 'friends/' . $owner->username,
51  'contexts' => array('friends')
52  );
54 
55  $params = array(
56  'name' => 'friends:of',
57  'text' => elgg_echo('friends:of'),
58  'href' => 'friendsof/' . $owner->username,
59  'contexts' => array('friends')
60  );
62  }
63 
64  // topbar
65  if ($viewer) {
66  elgg_register_menu_item('topbar', array(
67  'name' => 'friends',
68  'href' => "friends/{$viewer->username}",
69  'text' => elgg_view_icon('users'),
70  'title' => elgg_echo('friends'),
71  'priority' => 300,
72  ));
73  }
74 }
75 
82  $user = $params['entity'];
83  /* @var ElggUser $user */
84 
85  if (elgg_is_logged_in()) {
86  if (elgg_get_logged_in_user_guid() != $user->guid) {
87  $isFriend = $user->isFriend();
88 
89  // Always emit both to make it super easy to toggle with ajax
91  'name' => 'remove_friend',
92  'href' => elgg_add_action_tokens_to_url("action/friends/remove?friend={$user->guid}"),
93  'text' => elgg_echo('friend:remove'),
94  'section' => 'action',
95  'item_class' => $isFriend ? '' : 'hidden',
96  ));
97 
99  'name' => 'add_friend',
100  'href' => elgg_add_action_tokens_to_url("action/friends/add?friend={$user->guid}"),
101  'text' => elgg_echo('friend:add'),
102  'section' => 'action',
103  'item_class' => $isFriend ? 'hidden' : '',
104  ));
105  }
106  }
107 
108  return $return;
109 }
110 
120 function _elgg_friends_page_handler($segments, $handler) {
121  elgg_set_context('friends');
122 
123  if (isset($segments[0]) && $user = get_user_by_username($segments[0])) {
124  elgg_set_page_owner_guid($user->getGUID());
125  }
126 
127  if (!elgg_get_page_owner_guid()) {
128  return false;
129  }
130 
131  switch ($handler) {
132  case 'friends':
133  require_once(dirname(dirname(dirname(__FILE__))) . "/pages/friends/index.php");
134  break;
135  case 'friendsof':
136  require_once(dirname(dirname(dirname(__FILE__))) . "/pages/friends/of.php");
137  break;
138  default:
139  return false;
140  }
141  return true;
142 }
143 
152 function _elgg_collections_page_handler($page_elements) {
153  elgg_set_context('friends');
154  $base = elgg_get_config('path');
155  if (isset($page_elements[0])) {
156  switch ($page_elements[0]) {
157  case 'add':
159 
160  require_once "{$base}pages/friends/collections/add.php";
161  return true;
162  break;
163  case 'owner':
164  $user = get_user_by_username($page_elements[1]);
165  if ($user) {
166  elgg_set_page_owner_guid($user->getGUID());
167 
168  require_once "{$base}pages/friends/collections/view.php";
169  return true;
170  }
171  break;
172  }
173  }
174  return false;
175 }
176 
184 
187 
188  elgg_register_menu_item('page', array(
189  'name' => 'friends:view:collections',
190  'text' => elgg_echo('friends:collections'),
191  'href' => "collections/owner/$user->username",
192  'contexts' => array('friends')
193  ));
194  }
195 }
196 
208  $user_one = get_entity($object->guid_one);
209  /* @var ElggUser $user_one */
210 
211  $user_two = get_entity($object->guid_two);
212  /* @var ElggUser $user_two */
213 
214  // Notification subject
215  $subject = elgg_echo('friend:newfriend:subject', array(
216  $user_one->name
217  ), $user_two->language);
218 
219  // Notification body
220  $body = elgg_echo("friend:newfriend:body", array(
221  $user_one->name,
222  $user_one->getURL()
223  ), $user_two->language);
224 
225  return notify_user($user_two->guid, $object->guid_one, $subject, $body);
226 }
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:56
elgg_view_icon($name, $class= '')
View one of the elgg sprite icons.
Definition: views.php:1370
elgg_add_action_tokens_to_url($url, $html_encode=false)
Adds action tokens to URL.
Definition: elgglib.php:1277
$subject
Definition: exceptions.php:25
$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)
Register a callback as a plugin hook handler.
Definition: elgglib.php:853
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:42
get_user_by_username($username)
Get user by username.
Definition: users.php:246
$params
Definition: login.php:72
_elgg_collections_page_handler($page_elements)
Page handler for friends collections.
Definition: friends.php:152
static factory($options)
Create an ElggMenuItem from an associative array.
_elgg_friends_init()
Init friends library.
Definition: friends.php:18
_elgg_friends_page_handler($segments, $handler)
Page handler for friends-related pages.
Definition: friends.php:120
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:73
elgg_register_event_handler($event, $object_type, $callback, $priority=500)
Register a callback as an Elgg event handler.
Definition: elgglib.php:669
_elgg_send_friend_notification($event, $type, $object)
Notify user that someone has friended them.
Definition: friends.php:207
_elgg_friends_setup_user_hover_menu($hook, $type, $return, $params)
Adds friending to user hover menu.
Definition: friends.php:81
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:183
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:604