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) {
|
2016-11-13 22:57:19 +01:00
|
|
|
return strlen($item) >= 2;
|
2016-09-01 23:13:23 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
$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'][] = [
|
2019-03-09 13:22:32 +01:00
|
|
|
'label' => $tag['tag'],
|
2016-09-01 23:13:23 +02:00
|
|
|
'id_tag' => $id_tag,
|
2019-03-09 13:22:32 +01:00
|
|
|
'url' => BASEURL . '/' . $tag['slug'] . '/',
|
2016-09-01 23:13:23 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
2016-11-13 14:42:53 +01:00
|
|
|
|
|
|
|
private function handleTagCreation()
|
|
|
|
{
|
|
|
|
// It better not already exist!
|
|
|
|
if (Tag::exactMatch($_REQUEST['tag']))
|
|
|
|
{
|
2019-09-29 14:47:56 +02:00
|
|
|
$this->payload = ['error' => true, 'msg' => 'Tag already exists!'];
|
2016-11-13 14:42:53 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$label = htmlentities(trim($_REQUEST['tag']));
|
|
|
|
$slug = strtr($label, [' ' => '-']);
|
|
|
|
$tag = Tag::createNew([
|
|
|
|
'tag' => $label,
|
|
|
|
'kind' => 'Person',
|
|
|
|
'slug' => $slug,
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Did we succeed?
|
|
|
|
if (!$tag)
|
|
|
|
{
|
2019-09-29 14:47:56 +02:00
|
|
|
$this->payload = ['error' => true, 'msg' => 'Could not create tag.'];
|
2016-11-13 14:42:53 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->payload = [
|
|
|
|
'success' => true,
|
|
|
|
'label' => $tag->tag,
|
|
|
|
'id_tag' => $tag->id_tag,
|
|
|
|
];
|
|
|
|
}
|
2016-09-01 23:13:23 +02:00
|
|
|
}
|