mirror of
https://github.com/standardebooks/web.git
synced 2025-07-22 07:14:59 -04:00
Rename Vote object to PollVote
This commit is contained in:
parent
8168a125d0
commit
4efc5dcdaf
12 changed files with 41 additions and 30 deletions
116
lib/PollVote.php
Normal file
116
lib/PollVote.php
Normal file
|
@ -0,0 +1,116 @@
|
|||
<?
|
||||
use Safe\DateTime;
|
||||
|
||||
/**
|
||||
* @property User $User
|
||||
* @property PollItem $PollItem
|
||||
* @property string $Url
|
||||
*/
|
||||
class PollVote extends PropertiesBase{
|
||||
public $UserId;
|
||||
protected $_User = null;
|
||||
public $Created;
|
||||
public $PollItemId;
|
||||
protected $_PollItem = null;
|
||||
protected $_Url = null;
|
||||
|
||||
|
||||
// *******
|
||||
// GETTERS
|
||||
// *******
|
||||
|
||||
protected function GetUrl(): string{
|
||||
if($this->_Url === null){
|
||||
$this->_Url = $this->PollItem->Poll->Url . '/votes/' . $this->UserId;
|
||||
}
|
||||
|
||||
return $this->_Url;
|
||||
}
|
||||
|
||||
|
||||
// *******
|
||||
// METHODS
|
||||
// *******
|
||||
|
||||
protected function Validate(): void{
|
||||
$error = new Exceptions\ValidationException();
|
||||
|
||||
if($this->UserId === null){
|
||||
$error->Add(new Exceptions\InvalidPatronException());
|
||||
}
|
||||
|
||||
if($this->PollItemId === null){
|
||||
$error->Add(new Exceptions\PollItemRequiredException());
|
||||
}
|
||||
else{
|
||||
if($this->PollItem === null){
|
||||
$error->Add(new Exceptions\InvalidPollException());
|
||||
}
|
||||
}
|
||||
|
||||
if(!$this->PollItem->Poll->IsActive()){
|
||||
$error->Add(new Exceptions\PollClosedException());
|
||||
}
|
||||
|
||||
if(!$error->HasExceptions){
|
||||
// Basic sanity checks done, now check if we've already voted
|
||||
// in this poll
|
||||
|
||||
if($this->User === null){
|
||||
$error->Add(new Exceptions\InvalidPatronException());
|
||||
}
|
||||
else{
|
||||
// Do we already have a vote for this poll, from this user?
|
||||
if(Db::QueryInt('
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if($error->HasExceptions){
|
||||
throw $error;
|
||||
}
|
||||
}
|
||||
|
||||
public function Create(?string $email = null): void{
|
||||
if($email !== null){
|
||||
try{
|
||||
$patron = Patron::GetByEmail($email);
|
||||
$this->UserId = $patron->UserId;
|
||||
$this->User = $patron->User;
|
||||
}
|
||||
catch(Exceptions\InvalidPatronException $ex){
|
||||
// Can't validate patron email - do nothing for now,
|
||||
// this will be caught later when we validate the vote during creation.
|
||||
// Save the email in the User object in case we want it later,
|
||||
// for example prefilling the 'create' form after an error is returned.
|
||||
$this->User = new User();
|
||||
$this->User->Email = $email;
|
||||
}
|
||||
}
|
||||
|
||||
$this->Validate();
|
||||
$this->Created = new DateTime();
|
||||
Db::Query('INSERT into PollVotes (UserId, PollItemId, Created) values (?, ?, ?)', [$this->UserId, $this->PollItemId, $this->Created]);
|
||||
}
|
||||
|
||||
public static function Get(?string $pollUrlName, ?int $userId): PollVote{
|
||||
if($pollUrlName === null || $userId === null){
|
||||
throw new Exceptions\InvalidPollVoteException();
|
||||
}
|
||||
|
||||
$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 using (PollItemId) where pv.UserId = ?', [$pollUrlName, $userId], 'PollVote');
|
||||
|
||||
if(sizeof($result) == 0){
|
||||
throw new Exceptions\InvalidPollVoteException();
|
||||
}
|
||||
|
||||
return $result[0];
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue