/** * Page handler for parcing autocomplete results * * @param type $page */ function elgg_tokeninput_page_handler($page) { elgg_load_library('elgg.tokeninput'); $user = elgg_get_logged_in_user_entity(); $callback = urldecode(get_input('callback')); if ($callback) { $hmac = get_input('hmac'); $ts = get_input('ts'); if (hash_hmac('sha256', $ts . $callback, elgg_tokeninput_get_secret()) !== $hmac) { header('HTTP/1.1 403 Forbidden'); exit; } } else { $callback = 'elgg_tokeninput_search_all'; } $q = urldecode(get_input('term', get_input('q', ''))); $strict = (bool) get_input('strict', true); if (!is_callable($callback)) { header('HTTP/1.1 400 Bad Request'); exit; } $results = array(); $options = get_input('options', array()); $entities = call_user_func($callback, $q, $options); if (is_array($entities) && count($entities)) { foreach ($entities as $entity) { if (elgg_instanceof($entity)) { $results[] = elgg_tokeninput_export_entity($entity); } else { if ($entity instanceof ElggMetadata) { $results[] = elgg_tokeninput_export_metadata($entity); } else { $results[] = (array) $entity; } } } } if (!count($results) && $strict === false) { $suggest = array('label' => $q, 'value' => $q, 'html_result' => '<span>' . elgg_echo('tokeninput:suggest', array($q)) . '</span>'); $results[] = $suggest; } header("Content-Type: application/json"); echo json_encode($results); exit; }
/** * Get exportable entity values * * @param ElggEntity $entity * @return array */ function elgg_tokeninput_export_entity($entity) { if (!elgg_instanceof($entity)) { if ($entity_from_guid = get_entity($entity)) { $entity = $entity_from_guid; } else { return elgg_tokeninput_export_metadata($entity); } } $type = $entity->getType(); $subtype = $entity->getSubtype(); $icon = elgg_view_entity_icon($entity, 'small', array('use_hover' => false)); if (elgg_instanceof($entity, 'user')) { $title = "{$entity->name} ({$entity->username})"; } else { if (elgg_instanceof($entity, 'group')) { $title = $entity->name; } else { $title = $entity->getDisplayName(); if (!$title) { $title = elgg_echo('untitled'); } $metadata[] = elgg_echo('byline', array($entity->getOwnerEntity()->name)); } } if ($entity->description) { $metadata[] = elgg_get_excerpt(elgg_strip_tags($entity->description), 100); } if ($entity->location) { $metadata[] = $entity->location; } $export = array('label' => $title, 'value' => $entity->guid, 'metadata' => $metadata ? implode('<br />', $metadata) : '', 'icon' => $icon, 'type' => $type, 'subtype' => $subtype, 'html_result' => elgg_view_exists("tokeninput/{$type}/{$subtype}") ? elgg_view("tokeninput/{$type}/{$subtype}", array('entity' => $entity, 'for' => 'result')) : null, 'html_token' => elgg_view_exists("tokeninput/{$type}/{$subtype}") ? elgg_view("tokeninput/{$type}/{$subtype}", array('entity' => $entity, 'for' => 'token')) : null); $export = elgg_trigger_plugin_hook('tokeninput:entity:export', $type, array('entity' => $entity), $export); array_walk_recursive($export, function (&$value) { $value = is_string($value) ? html_entity_decode($value, ENT_QUOTES, 'UTF-8') : $value; }); return $export; }