2016-09-01 23:13:23 +02:00
|
|
|
<?php
|
|
|
|
/*****************************************************************************
|
|
|
|
* ProvideAutoSuggest.php
|
|
|
|
* Contains the autosuggest provider.
|
|
|
|
*
|
|
|
|
* Kabuki CMS (C) 2013-2015, Aaron van Geffen
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
class ProvideAutoSuggest extends JSONController
|
|
|
|
{
|
|
|
|
public function __construct()
|
|
|
|
{
|
2016-09-02 11:16:01 +02:00
|
|
|
// Ensure we're logged in at this point.
|
|
|
|
if (!Registry::get('user')->isLoggedIn())
|
2016-09-01 23:13:23 +02:00
|
|
|
throw new NotAllowedException();
|
|
|
|
|
2016-11-13 14:42:53 +01:00
|
|
|
if (!isset($_REQUEST['type']))
|
2016-09-01 23:13:23 +02:00
|
|
|
throw new UnexpectedValueException('Unsupported autosuggest request.');
|
|
|
|
|
2016-11-13 14:42:53 +01:00
|
|
|
if ($_REQUEST['type'] === 'tags' && isset($_REQUEST['data']))
|
|
|
|
return $this->handleTagSearch();
|
|
|
|
|
|
|
|
if ($_REQUEST['type'] === 'createtag' && isset($_REQUEST['tag']))
|
|
|
|
return $this->handleTagCreation();
|
|
|
|
}
|
|
|
|
|
|
|
|
private function handleTagSearch()
|
|
|
|
{
|
2016-09-01 23:13:23 +02:00
|
|
|
{
|
2016-11-13 14:42:53 +01:00
|
|
|
$data = array_unique(explode(' ', urldecode($_REQUEST['data'])));
|
2016-09-01 23:13:23 +02:00
|
|
|
$data = array_filter($data, function($item) {
|
|
|
|
return strlen($item) >= 3;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->payload = ['items' => []];
|
|
|
|
|
|
|
|
// Nothing to look for?
|
|
|
|
if (count($data) === 0)
|
|
|
|
return;
|
|
|
|
|
2016-11-13 14:42:53 +01:00
|
|
|
$results = Tag::matchPeople($data);
|
2016-09-01 23:13:23 +02:00
|
|
|
foreach ($results as $id_tag => $tag)
|
|
|
|
$this->payload['items'][] = [
|
|
|
|
'label' => $tag,
|
|
|
|
'id_tag' => $id_tag,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
2016-11-13 14:42:53 +01:00
|
|
|
|
|
|
|
private function handleTagCreation()
|
|
|
|
{
|
|
|
|
// It better not already exist!
|
|
|
|
if (Tag::exactMatch($_REQUEST['tag']))
|
|
|
|
{
|
|
|
|
$this->payload = ['error' => true, 'msg' => "Tag already exists!"];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$label = htmlentities(trim($_REQUEST['tag']));
|
|
|
|
$slug = strtr($label, [' ' => '-']);
|
|
|
|
$tag = Tag::createNew([
|
|
|
|
'tag' => $label,
|
|
|
|
'kind' => 'Person',
|
|
|
|
'slug' => $slug,
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Did we succeed?
|
|
|
|
if (!$tag)
|
|
|
|
{
|
|
|
|
$this->payload = ['error' => true, 'msg' => "Could not create tag."];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->payload = [
|
|
|
|
'success' => true,
|
|
|
|
'label' => $tag->tag,
|
|
|
|
'id_tag' => $tag->id_tag,
|
|
|
|
];
|
|
|
|
}
|
2016-09-01 23:13:23 +02:00
|
|
|
}
|