Allow all users to create and link people tags.

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

View File

@ -14,9 +14,6 @@ class ManageTags extends HTMLController
if (!Registry::get('user')->isAdmin()) if (!Registry::get('user')->isAdmin())
throw new NotAllowedException(); throw new NotAllowedException();
if (isset($_REQUEST['create']) && isset($_POST['tag']))
$this->handleTagCreation();
$options = [ $options = [
'columns' => [ 'columns' => [
'id_post' => [ 'id_post' => [
@ -92,36 +89,4 @@ class ManageTags extends HTMLController
parent::__construct('Tag management - Page ' . $table->getCurrentPage() .' - ' . SITE_TITLE); parent::__construct('Tag management - Page ' . $table->getCurrentPage() .' - ' . SITE_TITLE);
$this->page->adopt(new TabularData($table)); $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;
}
} }

View File

@ -14,12 +14,20 @@ class ProvideAutoSuggest extends JSONController
if (!Registry::get('user')->isLoggedIn()) if (!Registry::get('user')->isLoggedIn())
throw new NotAllowedException(); throw new NotAllowedException();
if (!isset($_GET['type'])) if (!isset($_REQUEST['type']))
throw new UnexpectedValueException('Unsupported autosuggest request.'); 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) { $data = array_filter($data, function($item) {
return strlen($item) >= 3; return strlen($item) >= 3;
}); });
@ -30,7 +38,7 @@ class ProvideAutoSuggest extends JSONController
if (count($data) === 0) if (count($data) === 0)
return; return;
$results = Tag::match($data); $results = Tag::matchPeople($data);
foreach ($results as $id_tag => $tag) foreach ($results as $id_tag => $tag)
$this->payload['items'][] = [ $this->payload['items'][] = [
'label' => $tag, '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,
];
}
} }

View File

@ -18,6 +18,9 @@ class ViewPhoto extends HTMLController
if (empty($photo)) if (empty($photo))
throw new NotFoundException(); throw new NotFoundException();
if (!empty($_POST))
$this->handleTagging($photo->getImage());
parent::__construct($photo->getTitle() . ' - ' . SITE_TITLE); parent::__construct($photo->getTitle() . ' - ' . SITE_TITLE);
$page = new PhotoPage($photo->getImage()); $page = new PhotoPage($photo->getImage());
@ -47,4 +50,21 @@ class ViewPhoto extends HTMLController
if (Registry::get('user')->isAdmin()) if (Registry::get('user')->isAdmin())
$this->admin_bar->appendItem(BASEURL . '/editasset/?id=' . $photo->getId(), 'Edit this photo'); $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;
}
} }

View File

@ -226,6 +226,9 @@ class Tag
if (!isset($data['id_parent'])) if (!isset($data['id_parent']))
$data['id_parent'] = 0; $data['id_parent'] = 0;
if (!isset($data['description']))
$data['description'] = 0;
if (!isset($data['count'])) if (!isset($data['count']))
$data['count'] = 0; $data['count'] = 0;
@ -297,6 +300,23 @@ class Tag
['tokens' => '%' . strtolower(implode('%', $tokens)) . '%']); ['tokens' => '%' . strtolower(implode('%', $tokens)) . '%']);
} }
public static function matchPeople($tokens)
{
if (!is_array($tokens))
$tokens = explode(' ', $tokens);
return Registry::get('db')->queryPair('
SELECT id_tag, tag
FROM tags
WHERE LOWER(tag) LIKE {string:tokens} AND
kind = {string:person}
ORDER BY tag ASC',
[
'tokens' => '%' . strtolower(implode('%', $tokens)) . '%',
'person' => 'Person',
]);
}
public static function exactMatch($tag) public static function exactMatch($tag)
{ {
if (!is_string($tag)) if (!is_string($tag))

View File

@ -173,6 +173,6 @@ TagAutoSuggest.prototype.fillContainer = function(response) {
}; };
TagAutoSuggest.prototype.createNewTag = function(callback) { TagAutoSuggest.prototype.createNewTag = function(callback) {
var request_uri = this.baseurl + '/managetags/?create'; var request_uri = this.baseurl + '/suggest/?type=createtag';
var request = new HttpRequest('post', request_uri, 'tag=' + encodeURIComponent(this.input.value), callback, this); var request = new HttpRequest('post', request_uri, 'tag=' + encodeURIComponent(this.input.value), callback, this);
} }

View File

@ -134,7 +134,7 @@ class PhotoPage extends SubTemplate
{ {
echo ' echo '
<h3>Tags</h3> <h3>Tags</h3>
<ul>'; <ul id="tag_list">';
foreach ($this->photo->getTags() as $tag) foreach ($this->photo->getTags() as $tag)
{ {
@ -153,9 +153,7 @@ class PhotoPage extends SubTemplate
echo ' echo '
<div> <div>
<h3>Link tags</h3> <h3>Link tags</h3>
<ul id="tag_list"> <p style="position: relative"><input type="text" id="new_tag" placeholder="Type to link a new tag"></p>
<li id="new_tag_container"><input type="text" id="new_tag" placeholder="Type to link a new tag"></li>
</ul>
</div> </div>
<script type="text/javascript" src="', BASEURL, '/js/ajax.js"></script> <script type="text/javascript" src="', BASEURL, '/js/ajax.js"></script>
<script type="text/javascript" src="', BASEURL, '/js/autosuggest.js"></script> <script type="text/javascript" src="', BASEURL, '/js/autosuggest.js"></script>
@ -166,26 +164,15 @@ class PhotoPage extends SubTemplate
listElement: "tag_list", listElement: "tag_list",
baseUrl: "', BASEURL, '", baseUrl: "', BASEURL, '",
appendCallback: function(item) { appendCallback: function(item) {
if (document.getElementById("linked_tag_" + item.id_tag)) { var request = new HttpRequest("post", "', $this->photo->getPageUrl(), '",
return; "id_tag=" + item.id_tag, function(response) {
}
var newCheck = document.createElement("input");
newCheck.type = "checkbox";
newCheck.name = "tag[" + item.id_tag + "]";
newCheck.id = "linked_tag_" + item.id_tag;
newCheck.title = "Uncheck to delete";
newCheck.checked = "checked";
var newNode = document.createElement("li"); var newNode = document.createElement("li");
newNode.appendChild(newCheck);
var newLabel = document.createTextNode(item.label); var newLabel = document.createTextNode(item.label);
newNode.appendChild(newLabel); newNode.appendChild(newLabel);
var list = document.getElementById("tag_list"); var list = document.getElementById("tag_list");
var input = document.getElementById("new_tag_container"); list.appendChild(newNode);
list.insertBefore(newNode, input); }, this);
} }
}); });
}, 100); }, 100);