From 4efc5dcdaf1921886eeee3c8c4888fa8b352b1ed Mon Sep 17 00:00:00 2001 From: Alex Cabal Date: Wed, 6 Jul 2022 11:36:42 -0500 Subject: [PATCH] Rename Vote object to PollVote --- config/sql/se/{Votes.sql => PollVotes.sql} | 2 +- ...ption.php => InvalidPollVoteException.php} | 2 +- ...eption.php => PollVoteExistsException.php} | 2 +- lib/Poll.php | 2 +- lib/PollItem.php | 2 +- lib/{Vote.php => PollVote.php} | 27 +++++++++---------- www/css/core.css | 14 ++++++++++ www/patrons-circle/polls/get.php | 2 +- www/patrons-circle/polls/votes/get.php | 8 +++--- www/patrons-circle/polls/votes/index.php | 2 +- www/patrons-circle/polls/votes/new.php | 4 +-- www/patrons-circle/polls/votes/post.php | 4 +-- 12 files changed, 41 insertions(+), 30 deletions(-) rename config/sql/se/{Votes.sql => PollVotes.sql} (88%) rename lib/Exceptions/{InvalidVoteException.php => InvalidPollVoteException.php} (62%) rename lib/Exceptions/{VoteExistsException.php => PollVoteExistsException.php} (63%) rename lib/{Vote.php => PollVote.php} (74%) diff --git a/config/sql/se/Votes.sql b/config/sql/se/PollVotes.sql similarity index 88% rename from config/sql/se/Votes.sql rename to config/sql/se/PollVotes.sql index 622e5e8d..5171e6de 100644 --- a/config/sql/se/Votes.sql +++ b/config/sql/se/PollVotes.sql @@ -1,4 +1,4 @@ -CREATE TABLE `Votes` ( +CREATE TABLE `PollVotes` ( `UserId` int(11) unsigned NOT NULL, `PollItemId` int(11) unsigned NOT NULL, `Created` datetime NOT NULL, diff --git a/lib/Exceptions/InvalidVoteException.php b/lib/Exceptions/InvalidPollVoteException.php similarity index 62% rename from lib/Exceptions/InvalidVoteException.php rename to lib/Exceptions/InvalidPollVoteException.php index a872fbe9..ac11efce 100644 --- a/lib/Exceptions/InvalidVoteException.php +++ b/lib/Exceptions/InvalidPollVoteException.php @@ -1,6 +1,6 @@ _VoteCount === null){ - $this->_VoteCount = Db::QueryInt('select count(*) from Votes v inner join PollItems pi on v.PollItemId = pi.PollItemId where pi.PollId = ?', [$this->PollId]); + $this->_VoteCount = Db::QueryInt('SELECT count(*) from PollVotes pv inner join PollItems pi using (PollItemId) where pi.PollId = ?', [$this->PollId]); } return $this->_VoteCount; diff --git a/lib/PollItem.php b/lib/PollItem.php index baab7d8a..2760b25c 100644 --- a/lib/PollItem.php +++ b/lib/PollItem.php @@ -19,7 +19,7 @@ class PollItem extends PropertiesBase{ protected function GetVoteCount(): int{ if($this->_VoteCount === null){ - $this->_VoteCount = Db::QueryInt('select count(*) from Votes v inner join PollItems pi on v.PollItemId = pi.PollItemId where pi.PollItemId = ?', [$this->PollItemId]); + $this->_VoteCount = Db::QueryInt('SELECT count(*) from PollVotes pv inner join PollItems pi using (PollItemId) where pi.PollItemId = ?', [$this->PollItemId]); } return $this->_VoteCount; diff --git a/lib/Vote.php b/lib/PollVote.php similarity index 74% rename from lib/Vote.php rename to lib/PollVote.php index 96e24011..866e380b 100644 --- a/lib/Vote.php +++ b/lib/PollVote.php @@ -6,8 +6,7 @@ use Safe\DateTime; * @property PollItem $PollItem * @property string $Url */ -class Vote extends PropertiesBase{ - public $VoteId; +class PollVote extends PropertiesBase{ public $UserId; protected $_User = null; public $Created; @@ -63,10 +62,10 @@ class Vote extends PropertiesBase{ else{ // Do we already have a vote for this poll, from this user? if(Db::QueryInt(' - SELECT count(*) from Votes v inner join - (select PollItemId from PollItems pi inner join Polls p on pi.PollId = p.PollId) x - on v.PollItemId = x.PollItemId where v.UserId = ?', [$this->UserId]) > 0){ - $error->Add(new Exceptions\VoteExistsException()); + SELECT count(*) from PollVotes pv inner join + (select PollItemId from PollItems pi inner join Polls p using (PollId)) x + using (PollItemId) where pv.UserId = ?', [$this->UserId]) > 0){ + $error->Add(new Exceptions\PollVoteExistsException()); } } } @@ -95,23 +94,21 @@ class Vote extends PropertiesBase{ $this->Validate(); $this->Created = new DateTime(); - Db::Query('INSERT into Votes (UserId, PollItemId, Created) values (?, ?, ?)', [$this->UserId, $this->PollItemId, $this->Created]); - - $this->VoteId = Db::GetLastInsertedId(); + Db::Query('INSERT into PollVotes (UserId, PollItemId, Created) values (?, ?, ?)', [$this->UserId, $this->PollItemId, $this->Created]); } - public static function Get(?string $pollUrlName, ?int $userId): Vote{ + public static function Get(?string $pollUrlName, ?int $userId): PollVote{ if($pollUrlName === null || $userId === null){ - throw new Exceptions\InvalidVoteException(); + throw new Exceptions\InvalidPollVoteException(); } - $result = Db::Query('SELECT v.* from Votes v inner join - (select pi.PollItemId from PollItems pi inner join Polls p on pi.PollId = p.PollID + $result = Db::Query('SELECT pv.* from PollVotes pv inner join + (select pi.PollItemId from PollItems pi inner join Polls p using (PollId) where p.UrlName = ? - ) x on v.PollItemId = x.PollItemId where v.UserId = ?', [$pollUrlName, $userId], 'Vote'); + ) x using (PollItemId) where pv.UserId = ?', [$pollUrlName, $userId], 'PollVote'); if(sizeof($result) == 0){ - throw new Exceptions\InvalidVoteException(); + throw new Exceptions\InvalidPollVoteException(); } return $result[0]; diff --git a/www/css/core.css b/www/css/core.css index 97b7e4bf..4e3a16d7 100644 --- a/www/css/core.css +++ b/www/css/core.css @@ -238,6 +238,16 @@ main > article{ align-items: center; } +main > section.narrow > *{ + margin-left: auto; + margin-right: auto; +} + +main > section.narrow > h1{ + max-width: none; +} + +main > section.narrow > *, main > article > *{ width: 100%; max-width: 40rem; @@ -625,6 +635,10 @@ input[type="checkbox"]:focus{ outline: none; } +h1 + .message{ + margin-top: 0; +} + .message{ border: 2px solid rgba(0, 0, 0, .25); border-radius: .25rem; diff --git a/www/patrons-circle/polls/get.php b/www/patrons-circle/polls/get.php index ab0a3cfe..5d09bfbd 100644 --- a/www/patrons-circle/polls/get.php +++ b/www/patrons-circle/polls/get.php @@ -14,7 +14,7 @@ catch(Exceptions\SeException $ex){ ?> $poll->Name, 'highlight' => '', 'description' => $poll->Description]) ?>
-
+

Name) ?>

Description ?>

IsActive()){ ?> diff --git a/www/patrons-circle/polls/votes/get.php b/www/patrons-circle/polls/votes/get.php index e9aaeb53..de487a34 100644 --- a/www/patrons-circle/polls/votes/get.php +++ b/www/patrons-circle/polls/votes/get.php @@ -5,12 +5,12 @@ use function Safe\session_unset; session_start(); -$vote = new Vote(); +$vote = new PollVote(); try{ - $vote = Vote::Get(HttpInput::Str(GET, 'pollurlname'), HttpInput::Int(GET, 'userid')); + $vote = PollVote::Get(HttpInput::Str(GET, 'pollurlname'), HttpInput::Int(GET, 'userid')); - if(isset($_SESSION['vote-created']) && $_SESSION['vote-created'] == $vote->VoteId){ + if(isset($_SESSION['vote-created']) && $_SESSION['vote-created'] == $vote->UserId){ http_response_code(201); session_unset(); } @@ -21,7 +21,7 @@ catch(Exceptions\SeException $ex){ ?> 'Thank you for voting!', 'highlight' => '', 'description' => 'Thank you for voting in a Standard Ebooks poll!']) ?>
-
+

Thank you for voting!

Your vote in the PollItem->Poll->Name) ?> poll has been recorded.

view results

diff --git a/www/patrons-circle/polls/votes/index.php b/www/patrons-circle/polls/votes/index.php index d2d30ce8..e9e02c90 100644 --- a/www/patrons-circle/polls/votes/index.php +++ b/www/patrons-circle/polls/votes/index.php @@ -12,7 +12,7 @@ catch(Exceptions\SeException $ex){ ?> 'Results for the ' . $poll->Name . ' poll', 'highlight' => '', 'description' => 'The voting results for the ' . $poll->Name . ' poll.']) ?>
-
+

Results for the Name) ?> Poll

Total votes: VoteCount) ?>

IsActive()){ ?> diff --git a/www/patrons-circle/polls/votes/new.php b/www/patrons-circle/polls/votes/new.php index d08b5478..2af2c088 100644 --- a/www/patrons-circle/polls/votes/new.php +++ b/www/patrons-circle/polls/votes/new.php @@ -5,7 +5,7 @@ use function Safe\session_unset; session_start(); -$vote = $_SESSION['vote'] ?? new Vote(); +$vote = $_SESSION['vote'] ?? new PollVote(); $exception = $_SESSION['exception'] ?? null; $poll = new Poll(); @@ -24,7 +24,7 @@ if($exception){ ?> $poll->Name . ' - Vote Now', 'highlight' => '', 'description' => 'Vote in the ' . $poll->Name . ' poll']) ?>
-
+

Vote in the Name) ?> Poll

$exception]) ?>
diff --git a/www/patrons-circle/polls/votes/post.php b/www/patrons-circle/polls/votes/post.php index 6bf18629..8be7d858 100644 --- a/www/patrons-circle/polls/votes/post.php +++ b/www/patrons-circle/polls/votes/post.php @@ -13,7 +13,7 @@ session_start(); $requestType = HttpInput::RequestType(); -$vote = new Vote(); +$vote = new PollVote(); try{ $error = new Exceptions\ValidationException(); @@ -25,7 +25,7 @@ try{ session_unset(); if($requestType == WEB){ - $_SESSION['vote-created'] = $vote->VoteId; + $_SESSION['vote-created'] = $vote->UserId; http_response_code(303); header('Location: ' . $vote->Url); }