Allow all users to create and link people tags.

This commit is contained in:
2016-11-13 14:42:53 +01:00
parent 7f5ce1820d
commit 0a55730696
6 changed files with 94 additions and 63 deletions

View File

@@ -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,
];
}
}