date-poll-api/src/Entity/Poll.php

491 lines
13 KiB
PHP
Raw Normal View History

2019-10-25 14:59:20 +02:00
<?php
namespace App\Entity;
2019-11-12 11:26:19 +01:00
use DateTimeInterface;
2019-10-25 14:59:20 +02:00
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
2019-11-05 17:31:07 +01:00
use JMS\Serializer\Annotation as Serializer;
2019-10-25 14:59:20 +02:00
/**
* @ORM\Entity(repositoryClass="App\Repository\PollRepository")
*/
class Poll {
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
2019-11-06 11:23:43 +01:00
* @Serializer\Expose()
2019-11-06 12:27:46 +01:00
* @Serializer\Type("integer")
2019-10-25 14:59:20 +02:00
*/
2019-11-06 11:23:43 +01:00
public $id;
2019-10-25 14:59:20 +02:00
/**
* @ORM\Column(type="string", length=255)
2019-11-06 11:23:43 +01:00
* @Serializer\Expose()
2019-11-06 12:27:46 +01:00
* @Serializer\Type("string")
2019-10-25 14:59:20 +02:00
*/
public $title;
2019-11-05 17:22:30 +01:00
/**
* @ORM\Column(type="string", length=255, nullable=true)
2019-11-06 11:23:43 +01:00
* @Serializer\Expose()
2019-11-06 12:27:46 +01:00
* @Serializer\Type("string")
2019-11-05 17:22:30 +01:00
*/
2019-11-06 14:54:04 +01:00
public $customUrl;
2019-11-05 17:22:30 +01:00
/**
* @ORM\Column(type="string", length=1000)
2019-11-06 11:23:43 +01:00
* @Serializer\Expose()
2019-11-06 12:27:46 +01:00
* @Serializer\Type("string")
2019-11-05 17:22:30 +01:00
*/
2019-11-06 14:54:04 +01:00
public $description;
2019-10-25 14:59:20 +02:00
/**
2019-11-27 16:39:52 +01:00
* @ORM\Column(type="datetime" , options={"default"="CURRENT_TIMESTAMP"})
2019-11-06 11:23:43 +01:00
* @Serializer\Expose()
2019-10-25 14:59:20 +02:00
*/
2019-11-06 14:54:04 +01:00
public $creationDate;
2019-10-25 14:59:20 +02:00
/**
* @ORM\Column(type="datetime")
*/
2019-11-06 14:54:04 +01:00
public $expiracyDate;
2019-10-25 14:59:20 +02:00
/**
2019-11-06 12:27:46 +01:00
* @ORM\ManyToOne(targetEntity="App\Entity\Owner", inversedBy="polls",cascade={"persist"})
2019-10-25 14:59:20 +02:00
* @ORM\JoinColumn(nullable=false)
2019-11-06 12:27:46 +01:00
* @Serializer\Type("App\Entity\Owner")
2019-10-25 14:59:20 +02:00
*/
2019-11-06 14:54:04 +01:00
public $owner;
2019-11-05 17:22:30 +01:00
/**
* kind of poll, text or date
* @ORM\Column(type="string", length=255)
2019-11-06 12:27:46 +01:00
* @Serializer\Type("string")
* @Serializer\Expose()
2019-11-05 17:22:30 +01:00
*/
2019-11-27 16:39:52 +01:00
public $kind = 'text';
2019-11-27 10:57:06 +01:00
/**
* array of possible answer to each choice, by default: "yes" or nothing only.
* could be also "yes", "maybe", "no". extensible to anything
* @ORM\Column(type="array")
* @Serializer\Type("string")
* @Serializer\Expose()
*/
public $allowedAnswers;
2019-11-05 17:22:30 +01:00
/**
* kind of way the people can modify the poll
* everybody - can modify votes
* self - one can only modify its own vote
* nobody - no one can modify the votes (excepted admin), pray to have it right at first
* @ORM\Column(type="string", length=255)
* @Serializer\Type("string")
2019-11-05 17:22:30 +01:00
*/
2019-11-27 16:14:49 +01:00
public $modificationPolicy = 'nobody';
2019-11-05 17:22:30 +01:00
/**
* send a mail on a new comment
* @ORM\Column(type="boolean", nullable=true)
* @Serializer\Type("boolean")
2019-11-05 17:22:30 +01:00
*/
2019-11-06 14:54:04 +01:00
public $mailOnComment;
2019-11-05 17:22:30 +01:00
/**
* send a mail on a new vote
* @ORM\Column(type="boolean", nullable=true)
* @Serializer\Type("boolean")
2019-11-05 17:22:30 +01:00
*/
2019-11-06 14:54:04 +01:00
public $mailOnVote;
2019-11-05 17:22:30 +01:00
/**
* hide publicly results
* @ORM\Column(type="boolean", nullable=true)
* @Serializer\Type("boolean")
2019-11-05 17:22:30 +01:00
*/
2019-11-06 14:54:04 +01:00
public $hideResults;
2019-11-05 17:22:30 +01:00
/**
* show publicly results even if there is a password to access the vote
* @ORM\Column(type="boolean", nullable=true)
* @Serializer\Type("boolean")
2019-11-05 17:22:30 +01:00
*/
2019-11-06 14:54:04 +01:00
public $showResultEvenIfPasswords;
2019-11-05 17:22:30 +01:00
/**
2019-11-12 11:26:19 +01:00
* @ORM\OneToMany(targetEntity="App\Entity\Vote", mappedBy="poll", orphanRemoval=true,cascade={"persist", "remove"})
2019-11-06 12:27:46 +01:00
* @Serializer\Type("App\Entity\Vote")
2019-11-05 17:22:30 +01:00
*/
2019-11-06 14:54:04 +01:00
public $votes;
/**
2019-11-12 11:26:19 +01:00
* @ORM\OneToMany(targetEntity="App\Entity\StackOfVotes", mappedBy="poll", orphanRemoval=true,cascade={"persist", "remove"})
* @Serializer\Type("App\Entity\StackOfVotes")
*/
2019-11-28 11:51:25 +01:00
public $stacksOfVotes;
2019-11-12 11:26:19 +01:00
/**
* @ORM\OneToMany(targetEntity="App\Entity\Choice", mappedBy="poll", orphanRemoval=true,cascade={"persist", "remove"})
* @Serializer\Type("App\Entity\Choice")
*/
2019-11-06 14:54:04 +01:00
public $choices;
/**
2019-11-20 11:24:54 +01:00
* @ORM\OneToMany(targetEntity="App\Entity\Comment", mappedBy="poll", orphanRemoval=true,cascade={"persist", "remove"})
2019-11-06 14:54:04 +01:00
* @Serializer\Type("App\Entity\Comment")
*/
public $comments;
2019-11-12 11:26:19 +01:00
/**
* vote restricted by a password in md5 format
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $password;
/**
* used to allow administration
* @ORM\Column(type="string", length=255)
* @Serializer\Type("string")
*/
private $adminKey;
2019-11-11 10:44:19 +01:00
2019-11-27 16:14:49 +01:00
/**
* number of days from now for default expiracy date
* @var int
*/
public $defaultExpiracyDaysFromNow = 60;
2019-11-27 16:14:49 +01:00
2019-11-05 17:22:30 +01:00
public function __construct() {
2019-11-28 11:51:25 +01:00
$this->adminKey = uniqid();
$this->votes = new ArrayCollection();
$this->choices = new ArrayCollection();
$this->comments = new ArrayCollection();
$this->stacksOfVotes = new ArrayCollection();
$this->setCreationDate( new \DateTime() );
$this->setExpiracyDate( $this->addDaysToDate(
new \DateTime(),
$this->defaultExpiracyDaysFromNow
) );
$this->setAllowedAnswers( [ 'yes' ] );
}
public function findChoiceById( int $id ) {
error_reporting( E_ALL ^ E_NOTICE );
$choices = $this->getChoices();
var_dump( count( $choices ) );
foreach ( $choices as $choice ) {
if ( $choice && $choice->getId() == $id ) {
return $choice;
}
return null;
}
// if ( $choices && $choices->containsKey( $id ) ) {
// $foundChoice = $choices->get( $id );
// } else {
// $foundChoice = new Choice();
// $foundChoice->setPoll( $this );
// }
//
// return $foundChoice;
}
2019-11-27 21:46:35 +01:00
2019-11-27 16:14:49 +01:00
public function addDaysToDate( \DateTime $date, int $days ) {
2019-11-28 11:51:25 +01:00
$st = strtotime( $date->getTimestamp() . ' + ' . $days . ' days' );
return new \DateTime( $st );
}
2019-11-27 16:14:49 +01:00
2019-10-25 14:59:20 +02:00
public function getId(): ?int {
2019-11-28 11:51:25 +01:00
return $this->id;
}
2019-10-25 14:59:20 +02:00
public function getTitle(): ?string {
2019-11-28 11:51:25 +01:00
return $this->title;
}
2019-10-25 14:59:20 +02:00
public function setTitle( string $title ): self {
2019-11-28 11:51:25 +01:00
$this->title = $title;
return $this;
}
2019-11-12 11:26:19 +01:00
public function getCreationDate(): ?DateTimeInterface {
2019-11-28 11:51:25 +01:00
return $this->creationDate;
}
2019-11-12 11:26:19 +01:00
public function setCreationDate( DateTimeInterface $creationDate ): self {
2019-11-28 11:51:25 +01:00
$this->creationDate = $creationDate;
return $this;
}
2019-11-12 11:26:19 +01:00
public function setExpiracyDate( DateTimeInterface $expiracyDate ): self {
2019-11-28 11:51:25 +01:00
$this->expiracyDate = $expiracyDate;
return $this;
}
2019-10-25 14:59:20 +02:00
public function getOwner(): ?Owner {
2019-11-28 11:51:25 +01:00
return $this->owner;
}
2019-10-25 14:59:20 +02:00
public function setOwner( ?Owner $owner ): self {
2019-11-28 11:51:25 +01:00
$this->owner = $owner;
return $this;
}
2019-11-05 17:22:30 +01:00
/**
* @return Collection|Vote[]
*/
public function getVotes(): Collection {
2019-11-28 11:51:25 +01:00
return $this->votes;
}
2019-11-05 17:22:30 +01:00
public function getAdminKey(): ?string {
2019-11-28 11:51:25 +01:00
return $this->adminKey;
}
2019-11-05 17:22:30 +01:00
public function setAdminKey( string $adminKey ): self {
2019-11-28 11:51:25 +01:00
$this->adminKey = $adminKey;
return $this;
}
2019-11-05 17:22:30 +01:00
public function getDescription(): ?string {
2019-11-28 11:51:25 +01:00
return $this->description;
}
2019-11-05 17:22:30 +01:00
public function setDescription( string $description ): self {
2019-11-28 11:51:25 +01:00
$this->description = $description;
return $this;
}
2019-11-05 17:22:30 +01:00
public function getKind(): ?string {
2019-11-28 11:51:25 +01:00
return $this->kind;
}
2019-11-05 17:22:30 +01:00
public function setKind( string $kind ): self {
2019-11-28 11:51:25 +01:00
$this->kind = $kind;
return $this;
}
2019-11-05 17:22:30 +01:00
public function getCustomUrl(): ?string {
2019-11-28 11:51:25 +01:00
return $this->customUrl;
}
2019-11-05 17:22:30 +01:00
public function setCustomUrl( string $customUrl ): self {
2019-11-28 11:51:25 +01:00
$this->customUrl = $customUrl;
return $this;
}
2019-11-05 17:22:30 +01:00
public function getPassword(): ?string {
2019-11-28 11:51:25 +01:00
return $this->password;
}
2019-11-05 17:22:30 +01:00
public function setPassword( string $password ): self {
2019-11-28 11:51:25 +01:00
$this->password = $password;
return $this;
}
2019-11-05 17:22:30 +01:00
public function getModificationPolicy(): ?string {
2019-11-28 11:51:25 +01:00
return $this->modificationPolicy;
}
2019-11-05 17:22:30 +01:00
public function setModificationPolicy( string $modificationPolicy ): self {
2019-11-28 11:51:25 +01:00
$this->modificationPolicy = $modificationPolicy;
return $this;
}
2019-11-05 17:22:30 +01:00
public function getMailOnComment(): ?bool {
2019-11-28 11:51:25 +01:00
return $this->mailOnComment;
}
2019-11-05 17:22:30 +01:00
public function setMailOnComment( bool $mailOnComment ): self {
2019-11-28 11:51:25 +01:00
$this->mailOnComment = $mailOnComment;
return $this;
}
2019-11-05 17:22:30 +01:00
public function getMailOnVote(): ?bool {
2019-11-28 11:51:25 +01:00
return $this->mailOnVote;
}
2019-11-05 17:22:30 +01:00
public function setMailOnVote( bool $mailOnVote ): self {
2019-11-28 11:51:25 +01:00
$this->mailOnVote = $mailOnVote;
return $this;
}
2019-11-05 17:22:30 +01:00
public function getHideResults(): ?bool {
2019-11-28 11:51:25 +01:00
return $this->hideResults;
}
2019-11-05 17:22:30 +01:00
public function setHideResults( bool $hideResults ): self {
2019-11-28 11:51:25 +01:00
$this->hideResults = $hideResults;
return $this;
}
2019-11-05 17:22:30 +01:00
public function getShowResultEvenIfPasswords(): ?bool {
2019-11-28 11:51:25 +01:00
return $this->showResultEvenIfPasswords;
}
2019-11-05 17:22:30 +01:00
public function setShowResultEvenIfPasswords( bool $showResultEvenIfPasswords ): self {
2019-11-28 11:51:25 +01:00
$this->showResultEvenIfPasswords = $showResultEvenIfPasswords;
return $this;
}
2019-11-06 14:54:04 +01:00
/**
* @return Collection|Comment[]
*/
public function getComments(): Collection {
2019-11-28 11:51:25 +01:00
return $this->comments;
}
2019-11-06 14:54:04 +01:00
public function addComment( Comment $comment ): self {
2019-11-28 11:51:25 +01:00
if ( ! $this->comments->contains( $comment ) ) {
$this->comments[] = $comment;
$comment->setPoll( $this );
}
return $this;
}
2019-11-06 14:54:04 +01:00
public function removeComment( Comment $comment ): self {
2019-11-28 11:51:25 +01:00
if ( $this->comments->contains( $comment ) ) {
$this->comments->removeElement( $comment );
// set the owning side to null (unless already changed)
if ( $comment->getPoll() === $this ) {
$comment->setPoll( null );
}
}
return $this;
}
public function getStacksOfVotes() {
return $this->stacksOfVotes;
}
2019-11-12 11:26:19 +01:00
public function setStacksOfVotes( ?StackOfVotes $stacksOfVotes ): self {
2019-11-28 11:51:25 +01:00
$this->stacksOfVotes = $stacksOfVotes;
return $this;
}
2019-11-20 11:24:54 +01:00
2019-11-12 17:26:56 +01:00
public function addStackOfVote( StackOfVotes $stackOfVote ): self {
2019-11-28 11:51:25 +01:00
if ( ! $this->stacksOfVotes->contains( $stackOfVote ) ) {
$this->stacksOfVotes[] = $stackOfVote;
$stackOfVote->setPoll( $this );
}
return $this;
}
2019-11-12 17:26:56 +01:00
public function removeStackOfVote( StackOfVotes $stackOfVote ): self {
2019-11-28 11:51:25 +01:00
if ( $this->stacksOfVotes->contains( $stackOfVote ) ) {
$this->stacksOfVotes->removeElement( $stackOfVote );
// set the owning side to null (unless already changed)
if ( $stackOfVote->getPoll() === $this ) {
$stackOfVote->setPoll( null );
}
}
return $this;
}
2019-11-12 17:26:56 +01:00
public function getExpiracyDate(): ?\DateTimeInterface {
2019-11-28 11:51:25 +01:00
return $this->expiracyDate;
}
2019-11-12 17:26:56 +01:00
public function addVote( Vote $vote ): self {
2019-11-28 11:51:25 +01:00
if ( ! $this->votes->contains( $vote ) ) {
$this->votes[] = $vote;
$vote->setPoll( $this );
}
return $this;
}
2019-11-12 17:26:56 +01:00
public function removeVote( Vote $vote ): self {
2019-11-28 11:51:25 +01:00
if ( $this->votes->contains( $vote ) ) {
$this->votes->removeElement( $vote );
// set the owning side to null (unless already changed)
if ( $vote->getPoll() === $this ) {
$vote->setPoll( null );
}
}
return $this;
}
2019-11-27 10:57:06 +01:00
/**
* @return Collection|Choice[]
*/
public function getChoices(): Collection {
2019-11-28 11:51:25 +01:00
return $this->choices;
}
2019-11-27 10:57:06 +01:00
public function addTextChoiceArray( Array $choiceTextArray ): self {
2019-11-28 11:51:25 +01:00
foreach ( $choiceTextArray as $text ) {
$newChoice = new Choice();
$newChoice->setName( $text );
$this->addChoice( $newChoice );
}
return $this;
}
2019-11-27 10:57:06 +01:00
public function addChoice( Choice $choice ): self {
2019-11-28 11:51:25 +01:00
if ( ! $this->choices->contains( $choice ) ) {
$this->choices[] = $choice;
$choice->setPoll( $this );
}
return $this;
}
2019-11-27 10:57:06 +01:00
public function removeChoice( Choice $choice ): self {
2019-11-28 11:51:25 +01:00
if ( $this->choices->contains( $choice ) ) {
$this->choices->removeElement( $choice );
// set the owning side to null (unless already changed)
if ( $choice->getPoll() === $this ) {
$choice->setPoll( null );
}
}
return $this;
}
2019-11-27 10:57:06 +01:00
public function getAllowedAnswers(): ?array {
2019-11-28 11:51:25 +01:00
return $this->allowedAnswers;
}
2019-11-27 10:57:06 +01:00
public function setAllowedAnswers( array $allowedAnswers ): self {
2019-11-28 11:51:25 +01:00
$this->allowedAnswers = $allowedAnswers;
return $this;
}
public function addStacksOfVote(StackOfVotes $stacksOfVote): self
{
if (!$this->stacksOfVotes->contains($stacksOfVote)) {
$this->stacksOfVotes[] = $stacksOfVote;
$stacksOfVote->setPoll($this);
}
return $this;
}
public function removeStacksOfVote(StackOfVotes $stacksOfVote): self
{
if ($this->stacksOfVotes->contains($stacksOfVote)) {
$this->stacksOfVotes->removeElement($stacksOfVote);
// set the owning side to null (unless already changed)
if ($stacksOfVote->getPoll() === $this) {
$stacksOfVote->setPoll(null);
}
}
return $this;
}
2019-10-25 14:59:20 +02:00
}