forked from Public/pics
Allow all users to create and link people tags.
This commit is contained in:
@@ -14,9 +14,6 @@ class ManageTags extends HTMLController
|
||||
if (!Registry::get('user')->isAdmin())
|
||||
throw new NotAllowedException();
|
||||
|
||||
if (isset($_REQUEST['create']) && isset($_POST['tag']))
|
||||
$this->handleTagCreation();
|
||||
|
||||
$options = [
|
||||
'columns' => [
|
||||
'id_post' => [
|
||||
@@ -92,36 +89,4 @@ class ManageTags extends HTMLController
|
||||
parent::__construct('Tag management - Page ' . $table->getCurrentPage() .' - ' . SITE_TITLE);
|
||||
$this->page->adopt(new TabularData($table));
|
||||
}
|
||||
|
||||
private function handleTagCreation()
|
||||
{
|
||||
header('Content-Type: text/json; charset=utf-8');
|
||||
|
||||
// It better not already exist!
|
||||
if (Tag::exactMatch($_POST['tag']))
|
||||
{
|
||||
echo '{"error":"Tag already exists!"}';
|
||||
exit;
|
||||
}
|
||||
|
||||
$label = htmlentities(trim($_POST['tag']));
|
||||
$slug = strtr(strtolower($label), [' ' => '-']);
|
||||
$tag = Tag::createNew([
|
||||
'tag' => $label,
|
||||
'slug' => $slug,
|
||||
]);
|
||||
|
||||
// Did we succeed?
|
||||
if (!$tag)
|
||||
{
|
||||
echo '{"error":"Could not create tag."}';
|
||||
exit;
|
||||
}
|
||||
|
||||
echo json_encode([
|
||||
'label' => $tag->tag,
|
||||
'id_tag' => $tag->id_tag,
|
||||
]);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,9 @@ class ViewPhoto extends HTMLController
|
||||
if (empty($photo))
|
||||
throw new NotFoundException();
|
||||
|
||||
if (!empty($_POST))
|
||||
$this->handleTagging($photo->getImage());
|
||||
|
||||
parent::__construct($photo->getTitle() . ' - ' . SITE_TITLE);
|
||||
$page = new PhotoPage($photo->getImage());
|
||||
|
||||
@@ -47,4 +50,21 @@ class ViewPhoto extends HTMLController
|
||||
if (Registry::get('user')->isAdmin())
|
||||
$this->admin_bar->appendItem(BASEURL . '/editasset/?id=' . $photo->getId(), 'Edit this photo');
|
||||
}
|
||||
|
||||
private function handleTagging(Image $photo)
|
||||
{
|
||||
header('Content-Type: text/json; charset=utf-8');
|
||||
|
||||
// Are we tagging a photo?
|
||||
if (!isset($_POST['id_tag']))
|
||||
{
|
||||
echo json_encode(['error' => true, 'msg' => 'Invalid tag request.']);
|
||||
exit;
|
||||
}
|
||||
|
||||
// We are!
|
||||
$photo->linkTags([(int) $_POST['id_tag']]);
|
||||
echo json_encode(['success' => true]);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user