diff --git a/controllers/ManageTags.php b/controllers/ManageTags.php index c62f83e..0e00cb1 100644 --- a/controllers/ManageTags.php +++ b/controllers/ManageTags.php @@ -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; - } } diff --git a/controllers/ProvideAutoSuggest.php b/controllers/ProvideAutoSuggest.php index 4424d00..884e279 100644 --- a/controllers/ProvideAutoSuggest.php +++ b/controllers/ProvideAutoSuggest.php @@ -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, + ]; + } } diff --git a/controllers/ViewPhoto.php b/controllers/ViewPhoto.php index 5a1262e..61518e8 100644 --- a/controllers/ViewPhoto.php +++ b/controllers/ViewPhoto.php @@ -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; + } } diff --git a/models/Tag.php b/models/Tag.php index f4ef0b2..ba444b5 100644 --- a/models/Tag.php +++ b/models/Tag.php @@ -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)) diff --git a/public/js/autosuggest.js b/public/js/autosuggest.js index 6233d9c..12a6ac0 100644 --- a/public/js/autosuggest.js +++ b/public/js/autosuggest.js @@ -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); } diff --git a/templates/PhotoPage.php b/templates/PhotoPage.php index 2aa6f04..7820d54 100644 --- a/templates/PhotoPage.php +++ b/templates/PhotoPage.php @@ -134,7 +134,7 @@ class PhotoPage extends SubTemplate { echo '

Tags

-