diff --git a/web/ajax/event.php b/web/ajax/event.php index 4b11e9641b..5ba3daa3f2 100644 --- a/web/ajax/event.php +++ b/web/ajax/event.php @@ -250,7 +250,9 @@ if ( canEdit('Events') ) { if ($tagId === '') ajaxError('addtag requires a numeric tid and id'); $eventId = $Event->Id(); - $sql = 'INSERT INTO Events_Tags (TagId, EventId, AssignedBy) VALUES (?, ?, ?)'; + if (!dbFetchOne('SELECT Id FROM Tags WHERE Id = ?', 'Id', array($tagId))) ajaxError('No such tag'); + + $sql = 'INSERT IGNORE INTO Events_Tags (TagId, EventId, AssignedBy) VALUES (?, ?, ?)'; $values = array($tagId, $eventId, $user->Id()); $response = dbFetchAll($sql, NULL, $values); diff --git a/web/ajax/tags.php b/web/ajax/tags.php index a1aa55841d..303d2d8ee0 100644 --- a/web/ajax/tags.php +++ b/web/ajax/tags.php @@ -42,13 +42,19 @@ switch ( $_REQUEST['action'] ) { if (!canEdit('Events')) { ajaxError('Insufficient permissions to create tags'); } - $sql = 'INSERT INTO Tags (Name, CreatedBy) VALUES (?, ?)'; - $values = array($_REQUEST['tname'], $user->Id()); - $result = dbFetchAll($sql, NULL, $values); + $tagName = is_scalar($_REQUEST['tname'] ?? null) ? trim($_REQUEST['tname']) : ''; + if ($tagName === '') ajaxError('createtag requires a tag name'); - $sql = 'SELECT * FROM Tags WHERE Id = ?'; - $values = array(dbInsertId()); - $dbFetchResult = dbFetchAll($sql, NULL, $values); + $dbFetchResult = dbFetchAll('SELECT * FROM Tags WHERE Name = ?', NULL, array($tagName)); + if (!count($dbFetchResult)) { + $sql = 'INSERT INTO Tags (Name, CreatedBy) VALUES (?, ?)'; + $values = array($tagName, $user->Id()); + if (!dbQuery($sql, $values)) ajaxError('Failed to create tag'); + + $sql = 'SELECT * FROM Tags WHERE Id = ?'; + $values = array(dbInsertId()); + $dbFetchResult = dbFetchAll($sql, NULL, $values); + } ajaxResponse(array('response'=>$dbFetchResult)); break; diff --git a/web/includes/FilterTerm.php b/web/includes/FilterTerm.php index 3cac942394..f6db919a59 100644 --- a/web/includes/FilterTerm.php +++ b/web/includes/FilterTerm.php @@ -473,7 +473,12 @@ class FilterTerm { $operator = $this->sql_operator(); $values = $this->sql_values(); - if ((count($values) > 1) and !(($operator == ' IN ') or ($operator == ' NOT IN ') or ($operator == ' =[] ') or ($operator == ' ![] '))) { + if (($this->attr === 'Tags') and count($values) and !array_intersect($values, ["'0'", "'-1'"]) + and in_array($operator, [' = ', ' != ', ' IN ', ' NOT IN ', ' =[] ', ' ![] '])) { + // Comparing the joined tag row by row gets "is not" wrong for events with several tags or none. + $negate = in_array($operator, [' != ', ' NOT IN ', ' ![] ']); + $sql .= ($negate ? 'NOT ' : '').'EXISTS (SELECT NULL FROM Events_Tags AS ET WHERE ET.EventId = E.Id AND ET.TagId IN ('.join(',', $values).'))'; + } elseif ((count($values) > 1) and !(($operator == ' IN ') or ($operator == ' NOT IN ') or ($operator == ' =[] ') or ($operator == ' ![] '))) { $subterms = []; foreach ($values as $value) { $subterm = $this->sql_attr(); diff --git a/web/skins/classic/views/js/event.js b/web/skins/classic/views/js/event.js index 85a9d2dab6..14e8928c83 100644 --- a/web/skins/classic/views/js/event.js +++ b/web/skins/classic/views/js/event.js @@ -2112,7 +2112,7 @@ function removeTag(tag) { function createTag(tagName, buttonPressed = null) { if (!canEdit.Events) return; - $j.getJSON(thisUrl + '?request=tags&action=createtag&tname=' + tagName) + $j.getJSON(thisUrl + '?request=tags&action=createtag&tname=' + encodeURIComponent(tagName)) .done(function(data) { if (data.result != 'Ok') { console.error('createtag refused', data.message);