forked from Public/pics
Allow all users to create and link people tags.
This commit is contained in:
parent
7f5ce1820d
commit
0a55730696
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -226,6 +226,9 @@ class Tag
|
||||
if (!isset($data['id_parent']))
|
||||
$data['id_parent'] = 0;
|
||||
|
||||
if (!isset($data['description']))
|
||||
$data['description'] = 0;
|
||||
|
||||
if (!isset($data['count']))
|
||||
$data['count'] = 0;
|
||||
|
||||
@ -297,6 +300,23 @@ class Tag
|
||||
['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)
|
||||
{
|
||||
if (!is_string($tag))
|
||||
|
@ -173,6 +173,6 @@ TagAutoSuggest.prototype.fillContainer = function(response) {
|
||||
};
|
||||
|
||||
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);
|
||||
}
|
||||
|
@ -134,7 +134,7 @@ class PhotoPage extends SubTemplate
|
||||
{
|
||||
echo '
|
||||
<h3>Tags</h3>
|
||||
<ul>';
|
||||
<ul id="tag_list">';
|
||||
|
||||
foreach ($this->photo->getTags() as $tag)
|
||||
{
|
||||
@ -153,9 +153,7 @@ class PhotoPage extends SubTemplate
|
||||
echo '
|
||||
<div>
|
||||
<h3>Link tags</h3>
|
||||
<ul id="tag_list">
|
||||
<li id="new_tag_container"><input type="text" id="new_tag" placeholder="Type to link a new tag"></li>
|
||||
</ul>
|
||||
<p style="position: relative"><input type="text" id="new_tag" placeholder="Type to link a new tag"></p>
|
||||
</div>
|
||||
<script type="text/javascript" src="', BASEURL, '/js/ajax.js"></script>
|
||||
<script type="text/javascript" src="', BASEURL, '/js/autosuggest.js"></script>
|
||||
@ -166,26 +164,15 @@ class PhotoPage extends SubTemplate
|
||||
listElement: "tag_list",
|
||||
baseUrl: "', BASEURL, '",
|
||||
appendCallback: function(item) {
|
||||
if (document.getElementById("linked_tag_" + item.id_tag)) {
|
||||
return;
|
||||
}
|
||||
var request = new HttpRequest("post", "', $this->photo->getPageUrl(), '",
|
||||
"id_tag=" + item.id_tag, function(response) {
|
||||
var newNode = document.createElement("li");
|
||||
var newLabel = document.createTextNode(item.label);
|
||||
newNode.appendChild(newLabel);
|
||||
|
||||
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");
|
||||
newNode.appendChild(newCheck);
|
||||
|
||||
var newLabel = document.createTextNode(item.label);
|
||||
newNode.appendChild(newLabel);
|
||||
|
||||
var list = document.getElementById("tag_list");
|
||||
var input = document.getElementById("new_tag_container");
|
||||
list.insertBefore(newNode, input);
|
||||
var list = document.getElementById("tag_list");
|
||||
list.appendChild(newNode);
|
||||
}, this);
|
||||
}
|
||||
});
|
||||
}, 100);
|
||||
|
Loading…
Reference in New Issue
Block a user