Elgg  Version 2.2
 All Classes Namespaces Files Functions Variables Pages
input.php
Go to the documentation of this file.
1 <?php
27 function get_input($variable, $default = null, $filter_result = true) {
28  return _elgg_services()->input->get($variable, $default, $filter_result);
29 }
30 
41 function set_input($variable, $value) {
42  _elgg_services()->input->set($variable, $value);
43 }
44 
53 function filter_tags($var) {
54  return elgg_trigger_plugin_hook('validate', 'input', null, $var);
55 }
56 
65 function current_page_url() {
66  $url = parse_url(elgg_get_site_url());
67 
68  $page = $url['scheme'] . "://" . $url['host'];
69 
70  if (isset($url['port']) && $url['port']) {
71  $page .= ":" . $url['port'];
72  }
73 
74  $page = trim($page, "/");
75 
76  $page .= _elgg_services()->request->getRequestUri();
77 
78  return $page;
79 }
80 
88 function is_email_address($address) {
89  return filter_var($address, FILTER_VALIDATE_EMAIL) === $address;
90 }
91 
103 function elgg_make_sticky_form($form_name) {
104  _elgg_services()->stickyForms->makeStickyForm($form_name);
105 }
106 
119 function elgg_clear_sticky_form($form_name) {
120  _elgg_services()->stickyForms->clearStickyForm($form_name);
121 }
122 
131 function elgg_is_sticky_form($form_name) {
132  return _elgg_services()->stickyForms->isStickyForm($form_name);
133 }
134 
148 function elgg_get_sticky_value($form_name, $variable = '', $default = null, $filter_result = true) {
149  return _elgg_services()->stickyForms->getStickyValue($form_name, $variable, $default, $filter_result);
150 }
151 
161 function elgg_get_sticky_values($form_name, $filter_result = true) {
162  return _elgg_services()->stickyForms->getStickyValues($form_name, $filter_result);
163 }
164 
174 function elgg_clear_sticky_value($form_name, $variable) {
175  _elgg_services()->stickyForms->clearStickyValue($form_name, $variable);
176 }
177 
196  $dbprefix = elgg_get_config('dbprefix');
197 
198  // only return results to logged in users.
200  exit;
201  }
202 
203  if (!$q = get_input('term', get_input('q'))) {
204  exit;
205  }
206 
207  $input_name = get_input('name', 'members');
208 
209  $q = sanitise_string($q);
210 
211  // replace mysql vars with escaped strings
212  $q = str_replace(array('_', '%'), array('\_', '\%'), $q);
213 
214  $match_on = get_input('match_on', 'all');
215 
216  if (!is_array($match_on)) {
217  $match_on = array($match_on);
218  }
219 
220  // all = users and groups
221  if (in_array('all', $match_on)) {
222  $match_on = array('users', 'groups');
223  }
224 
226  if (get_input('match_owner', false)) {
227  $owner_guid = $user->getGUID();
228  }
229 
230  $limit = sanitise_int(get_input('limit', elgg_get_config('default_limit')));
231 
232  // grab a list of entities and send them in json.
233  $results = array();
234  foreach ($match_on as $match_type) {
235  switch ($match_type) {
236  case 'users':
237  $options = array(
238  'type' => 'user',
239  'limit' => $limit,
240  'joins' => array("JOIN {$dbprefix}users_entity ue ON e.guid = ue.guid"),
241  'wheres' => array(
242  "ue.banned = 'no'",
243  "(ue.name LIKE '$q%' OR ue.name LIKE '% $q%' OR ue.username LIKE '$q%')"
244  )
245  );
246 
247  $entities = elgg_get_entities($options);
248  if (!empty($entities)) {
249  foreach ($entities as $entity) {
250 
251  if (in_array('groups', $match_on)) {
252  $value = $entity->guid;
253  } else {
254  $value = $entity->username;
255  }
256 
257  $output = elgg_view_list_item($entity, array(
258  'use_hover' => false,
259  'use_link' => false,
260  'class' => 'elgg-autocomplete-item',
261  'title' => $entity->name, // Default title would be a link
262  ));
263 
264  $icon = elgg_view_entity_icon($entity, 'tiny', array(
265  'use_hover' => false,
266  ));
267 
268  $result = array(
269  'type' => 'user',
270  'name' => $entity->name,
271  'desc' => $entity->username,
272  'guid' => $entity->guid,
273  'label' => $output,
274  'value' => $value,
275  'icon' => $icon,
276  'url' => $entity->getURL(),
277  'html' => elgg_view('input/userpicker/item', array(
278  'entity' => $entity,
279  'input_name' => $input_name,
280  )),
281  );
282  $results[$entity->name . rand(1, 100)] = $result;
283  }
284  }
285  break;
286 
287  case 'groups':
288  // don't return results if groups aren't enabled.
289  if (!elgg_is_active_plugin('groups')) {
290  continue;
291  }
292 
293  $options = array(
294  'type' => 'group',
295  'limit' => $limit,
296  'owner_guid' => $owner_guid,
297  'joins' => array("JOIN {$dbprefix}groups_entity ge ON e.guid = ge.guid"),
298  'wheres' => array(
299  "(ge.name LIKE '$q%' OR ge.name LIKE '% $q%' OR ge.description LIKE '% $q%')"
300  )
301  );
302 
303  $entities = elgg_get_entities($options);
304  if (!empty($entities)) {
305  foreach ($entities as $entity) {
306  $output = elgg_view_list_item($entity, array(
307  'use_hover' => false,
308  'class' => 'elgg-autocomplete-item',
309  'full_view' => false,
310  'href' => false,
311  'title' => $entity->name, // Default title would be a link
312  ));
313 
314  $icon = elgg_view_entity_icon($entity, 'tiny', array(
315  'use_hover' => false,
316  ));
317 
318  $result = array(
319  'type' => 'group',
320  'name' => $entity->name,
321  'desc' => strip_tags($entity->description),
322  'guid' => $entity->guid,
323  'label' => $output,
324  'value' => $entity->guid,
325  'icon' => $icon,
326  'url' => $entity->getURL(),
327  );
328 
329  $results[$entity->name . rand(1, 100)] = $result;
330  }
331  }
332  break;
333 
334  case 'friends':
335  $options = array(
336  'type' => 'user',
337  'limit' => $limit,
338  'relationship' => 'friend',
339  'relationship_guid' => $user->getGUID(),
340  'joins' => array("JOIN {$dbprefix}users_entity ue ON e.guid = ue.guid"),
341  'wheres' => array(
342  "ue.banned = 'no'",
343  "(ue.name LIKE '$q%' OR ue.name LIKE '% $q%' OR ue.username LIKE '$q%')"
344  )
345  );
346 
348  if (!empty($entities)) {
349  foreach ($entities as $entity) {
350 
351  $output = elgg_view_list_item($entity, array(
352  'use_hover' => false,
353  'use_link' => false,
354  'class' => 'elgg-autocomplete-item',
355  'title' => $entity->name, // Default title would be a link
356  ));
357 
358  $icon = elgg_view_entity_icon($entity, 'tiny', array(
359  'use_hover' => false,
360  ));
361 
362  $result = array(
363  'type' => 'user',
364  'name' => $entity->name,
365  'desc' => $entity->username,
366  'guid' => $entity->guid,
367  'label' => $output,
368  'value' => $entity->username,
369  'icon' => $icon,
370  'url' => $entity->getURL(),
371  'html' => elgg_view('input/userpicker/item', array(
372  'entity' => $entity,
373  'input_name' => $input_name,
374  )),
375  );
376  $results[$entity->name . rand(1, 100)] = $result;
377  }
378  }
379  break;
380 
381  default:
382  header("HTTP/1.0 400 Bad Request", true);
383  echo "livesearch: unknown match_on of $match_type";
384  exit;
385  break;
386  }
387  }
388 
389  ksort($results);
390  header("Content-Type: application/json;charset=utf-8");
391  echo json_encode(array_values($results));
392  exit;
393 }
394 
401 function _elgg_input_init() {
402  // register an endpoint for live search / autocomplete.
403  elgg_register_page_handler('livesearch', 'input_livesearch_page_handler');
404 }
405 
406 return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
407  $events->registerHandler('init', 'system', '_elgg_input_init');
408 };
elgg_get_config($name, $site_guid=0)
Get an Elgg configuration value.
$input_name
Definition: item.php:14
$value
Definition: longtext.php:26
current_page_url()
Returns the current page's complete URL.
Definition: input.php:65
$default
Definition: checkbox.php:35
filter_tags($var)
Filter tags from a given string based on registered hooks.
Definition: input.php:53
$url
Definition: exceptions.php:24
$options
Elgg admin footer.
Definition: footer.php:6
if(!$owner) $icon
Definition: default.php:16
is_email_address($address)
Validates an email address.
Definition: input.php:88
set_input($variable, $value)
Sets an input value that may later be retrieved by get_input.
Definition: input.php:41
$owner_guid
elgg_view_list_item($item, array $vars=array())
View an item in a list.
Definition: views.php:1422
$limit
Definition: userpicker.php:38
elgg_get_sticky_value($form_name, $variable= '', $default=null, $filter_result=true)
Get a specific value from cached form submission data.
Definition: input.php:148
get_input($variable, $default=null, $filter_result=true)
Get some input from variables passed submitted through GET or POST.
Definition: input.php:27
elgg_make_sticky_form($form_name)
Save form submission data (all GET and POST vars) into a session cache.
Definition: input.php:103
sanitise_string($string)
Alias of sanitize_string.
Definition: database.php:166
$dbprefix
Definition: index.php:13
$user
Definition: ban.php:13
const ELGG_ENTITIES_ANY_VALUE
Definition: elgglib.php:2001
elgg_get_entities(array $options=array())
Returns an array of entities with optional filtering.
Definition: entities.php:316
elgg_trigger_plugin_hook($hook, $type, $params=null, $returnvalue=null)
Definition: elgglib.php:801
elgg_view($view, $vars=array(), $ignore1=false, $ignore2=false, $viewtype= '')
Return a parsed view.
Definition: views.php:342
elgg_get_site_url($site_guid=0)
Get the URL for the current (or specified) site.
input_livesearch_page_handler($page)
Page handler for autocomplete endpoint.
Definition: input.php:195
_elgg_input_init()
Initialize the input library.
Definition: input.php:401
_elgg_services(\Elgg\Di\ServiceProvider $services=null)
Get the global service provider.
Definition: autoloader.php:17
elgg_clear_sticky_value($form_name, $variable)
Remove one value of form submission data from the session.
Definition: input.php:174
elgg_is_active_plugin($plugin_id, $site_guid=null)
Returns if a plugin is active for a current site.
Definition: plugins.php:120
$entity
Definition: delete.php:7
elgg_view_entity_icon(\ElggEntity $entity, $size= 'medium', $vars=array())
View the icon of an entity.
Definition: views.php:895
sanitise_int($int, $signed=true)
Alias of sanitize_int.
Definition: database.php:194
elgg_get_logged_in_user_entity()
Return the current logged in user, or null if no user is logged in.
Definition: sessions.php:32
exit
Definition: autoloader.php:34
elgg_get_entities_from_relationship($options)
Return entities matching a given query joining against a relationship.
$output
Definition: item.php:10
elgg_is_sticky_form($form_name)
Does form submission data exist for this form?
Definition: input.php:131
elgg_clear_sticky_form($form_name)
Remove form submission data from the session.
Definition: input.php:119
elgg_get_sticky_values($form_name, $filter_result=true)
Get all submission data cached for a form.
Definition: input.php:161