Elgg  Version 2.3
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', 'relationship', '_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  echo elgg_view_resource("friends/index");
132  break;
133  case 'friendsof':
134  echo elgg_view_resource("friends/of");
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  if (isset($page_elements[0])) {
153  switch ($page_elements[0]) {
154  case 'add':
156 
157  echo elgg_view_resource("friends/collections/add");
158  return true;
159  case 'owner':
160  $user = get_user_by_username($page_elements[1]);
161  if ($user) {
162  elgg_set_page_owner_guid($user->getGUID());
163 
164  echo elgg_view_resource("friends/collections/view");
165  return true;
166  }
167  break;
168  case 'pickercallback':
169  echo elgg_view_resource('friends/collections/pickercallback');
170  return true;
171  }
172  }
173  return false;
174 }
175 
183 
186 
187  elgg_register_menu_item('page', array(
188  'name' => 'friends:view:collections',
189  'text' => elgg_echo('friends:collections'),
190  'href' => "collections/owner/$user->username",
191  'contexts' => array('friends')
192  ));
193  }
194 }
195 
207  if ($object->relationship != 'friend') {
208  return true;
209  }
210 
211  $user_one = get_entity($object->guid_one);
212  /* @var \ElggUser $user_one */
213 
214  $user_two = get_entity($object->guid_two);
215  /* @var ElggUser $user_two */
216 
217  // Notification subject
218  $subject = elgg_echo('friend:newfriend:subject', array(
219  $user_one->name
220  ), $user_two->language);
221 
222  // Notification body
223  $body = elgg_echo("friend:newfriend:body", array(
224  $user_one->name,
225  $user_one->getURL()
226  ), $user_two->language);
227 
228  // Notification params
229  $params = [
230  'action' => 'add_friend',
231  'object' => $user_one,
232  'friend' => $user_two,
233  ];
234 
235  return notify_user($user_two->guid, $object->guid_one, $subject, $body, $params);
236 }
237 
238 return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
239  $events->registerHandler('init', 'system', '_elgg_friends_init');
240 };
$object
These two snippets demonstrates triggering an event and how to register for that event.
Definition: trigger.php:7
elgg_view_icon($name, $vars=array())
View one of the icons.
Definition: views.php:1582
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:1151
$subject
Definition: exceptions.php:25
elgg_echo($message_key, $args=array(), $language="")
Given a message key, returns an appropriately translated full-text string.
Definition: languages.php:21
elgg_view_resource($name, array $vars=[])
Render a resource view.
Definition: views.php:510
$return
Definition: opendd.php:15
elgg_register_plugin_hook_handler($hook, $type, $callback, $priority=500)
Definition: elgglib.php:740
elgg_set_page_owner_guid($guid)
Set the guid of the entity that owns this page.
Definition: pageowner.php:72
_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
$owner
Definition: crop.php:8
elgg_set_context($context)
Sets the page context.
Definition: pageowner.php:192
$user
Definition: ban.php:13
notify_user($to, $from=0, $subject= '', $message= '', array $params=array(), $methods_override=null)
Notify a user via their preferences.
elgg echo
Translates a string.
Definition: languages.js:48
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:56
elgg_register_event_handler($event, $object_type, $callback, $priority=500)
Definition: elgglib.php:550
_elgg_send_friend_notification($event, $type, $object)
Notify user that someone has friended them.
Definition: friends.php:206
_elgg_friends_setup_user_hover_menu($hook, $type, $return, $params)
Adds friending to user hover menu.
Definition: friends.php:79
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:96
_elgg_setup_collections_menu()
Adds collection sidebar menu items.
Definition: friends.php:182
elgg_register_widget_type($handler, $name=null, $description=null, $context=array('all'), $multiple=false)
Register a widget type.
Definition: widgets.php:74
elgg_get_page_owner_guid($guid=0)
Gets the guid of the entity that owns the current page.
Definition: pageowner.php:22
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:204
if(!$display_name) $type
Definition: delete.php:27