diff --git a/database/migrations/0.base.sql b/database/migrations/0.base.sql index b191b0a..dbaa398 100644 --- a/database/migrations/0.base.sql +++ b/database/migrations/0.base.sql @@ -136,6 +136,7 @@ CREATE TABLE `votes` ( `id` int(11) NOT NULL AUTO_INCREMENT, `delta` int(11) NOT NULL, `quote` int(11) NOT NULL, + `user` int(11) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; /*!40101 SET character_set_client = @saved_cs_client */; diff --git a/src/includes/templates/quote.tpl b/src/includes/templates/quote.tpl index 89516d5..68f944a 100644 --- a/src/includes/templates/quote.tpl +++ b/src/includes/templates/quote.tpl @@ -1,9 +1,9 @@
{if $isVotingEnabled}
- + {$quote->voteCount} - +
{/if} diff --git a/src/resources/javascript/main.js b/src/resources/javascript/main.js index 8d5d7cc..9db96f3 100644 --- a/src/resources/javascript/main.js +++ b/src/resources/javascript/main.js @@ -1,5 +1,12 @@ -function vote(id, dir) -{ +function voteUp (id) { + return vote(id, 'up') +} + +function voteDown (id) { + return vote(id, 'down') +} + +function vote (id, dir) { window.fetch('vote.php', { method: 'POST', headers: { @@ -13,17 +20,18 @@ function vote(id, dir) .then(response => response.json()) .then(onVoteReply) .catch(onError) + + return false // prevent default } -function onError(res) -{ +function onError (res) { console.log('err', res) document.querySelectorAll('p.error').forEach(function (el) { el.remove() }) - if (typeof res.message != "undefined") { + if (typeof res.message !== 'undefined') { const p = document.createElement('p') p.classList.add('error') p.textContent = 'Error: ' + res.message @@ -35,8 +43,7 @@ function onError(res) } } -function onVoteReply(json) -{ +function onVoteReply (json) { if (json.type === 'error') { if (json.cause === 'needsLogin') { window.location = 'login.php' diff --git a/src/resources/stylesheets/app.css b/src/resources/stylesheets/app.css index 0a46ded..2d6c02c 100644 --- a/src/resources/stylesheets/app.css +++ b/src/resources/stylesheets/app.css @@ -34,6 +34,10 @@ section *:last-child { margin-bottom: 0; } +.quoteContainer { + flex-grow: 1; +} + .quoteContainer ul { padding: 0; margin: 0; @@ -121,6 +125,7 @@ div.container { section.quote { margin-bottom: 2.5em; + display: flex; } .quoteContainer { @@ -136,16 +141,19 @@ div.voteContainer { min-width: 2em; } -div.voteContainer a { +div.voteContainer button { display: block; color: lightgray; cursor: pointer; + border: 0; font-size: 1.5em; text-decoration: none; + background-color: transparent; } -div.voteContainer a:hover { - color: gray; +div.voteContainer button:hover { + color: black; + background-color: transparent; } div.quoteHeader { diff --git a/src/vote.php b/src/vote.php index 91d609b..161ed1d 100644 --- a/src/vote.php +++ b/src/vote.php @@ -5,7 +5,7 @@ use libAllure\Session; use libAllure\DatabaseFactory; -if (!$cfg->getBool('VOTING_ENABLED')) { +if ($cfg->getBool('VOTING_ENABLED')) { outputJson( array( "type" => "error", @@ -18,8 +18,11 @@ $cause = ""; try { - $dir = libAllure\Shortcuts::san()->filterString('dir'); - $id = libAllure\Shortcuts::san()->filterUint('id'); + $jsonData = file_get_contents('php://input'); + $data = json_decode($jsonData, true); + + $dir = $data['direction']; + $id = $data['id']; switch ($dir) { case 'up': @@ -29,7 +32,7 @@ $delta = -1; break; default: - throw new Exception('What direction is that?!'); + throw new Exception('What direction is that?! '); } if (!Session::isLoggedIn()) { @@ -43,16 +46,13 @@ $stmt->execute(); if ($stmt->numRows() > 0) { - $currentVote = $stmt->fetchRow(); - $currentVote['delta'] = intval($currentVote['delta']); - - $delta = $currentVote['delta'] + $delta; + $currentRow = $stmt->fetchRow(); - if ($delta > 1) { - $delta = 1; - } elseif ($delta < -1) { - $delta = -1; - } + $sql = 'DELETE FROM votes WHERE quote = :quote AND user = :user'; + $stmt = DatabaseFactory::getInstance()->prepare($sql); + $stmt->bindValue('quote', $id); + $stmt->bindValue('user', Session::getUser()->getId()); + $stmt->execute(); } $sql = 'INSERT INTO votes (quote, user, delta) VALUES (:quote, :user, :delta1) ON DUPLICATE KEY UPDATE delta = :delta2';