Allow all users to create and link people tags.
This commit is contained in:
@@ -14,12 +14,20 @@ class ProvideAutoSuggest extends JSONController
|
||||
if (!Registry::get('user')->isLoggedIn())
|
||||
throw new NotAllowedException();
|
||||
|
||||
if (!isset($_GET['type']))
|
||||
if (!isset($_REQUEST['type']))
|
||||
throw new UnexpectedValueException('Unsupported autosuggest request.');
|
||||
|
||||
if ($_GET['type'] === 'tags' && isset($_GET['data']))
|
||||
if ($_REQUEST['type'] === 'tags' && isset($_REQUEST['data']))
|
||||
return $this->handleTagSearch();
|
||||
|
||||
if ($_REQUEST['type'] === 'createtag' && isset($_REQUEST['tag']))
|
||||
return $this->handleTagCreation();
|
||||
}
|
||||
|
||||
private function handleTagSearch()
|
||||
{
|
||||
{
|
||||
$data = array_unique(explode(' ', urldecode($_GET['data'])));
|
||||
$data = array_unique(explode(' ', urldecode($_REQUEST['data'])));
|
||||
$data = array_filter($data, function($item) {
|
||||
return strlen($item) >= 3;
|
||||
});
|
||||
@@ -30,7 +38,7 @@ class ProvideAutoSuggest extends JSONController
|
||||
if (count($data) === 0)
|
||||
return;
|
||||
|
||||
$results = Tag::match($data);
|
||||
$results = Tag::matchPeople($data);
|
||||
foreach ($results as $id_tag => $tag)
|
||||
$this->payload['items'][] = [
|
||||
'label' => $tag,
|
||||
@@ -38,4 +46,35 @@ class ProvideAutoSuggest extends JSONController
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user