<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\Criteria;
/**
* @ORM\Entity(repositoryClass="App\Repository\PollRepository")
*/
class Poll
{
const STATUS_SCHEDULED = 2;
const STATUS_ACTIVE = 1;
const STATUS_INACTIVE = 0;
const STATUS_EXPIRED = -2;
const STATUS_TRASH = -3;
const SITE_RCS = 1;
const SITE_AAR = 2;
const SITE_MCS = 3;
const SITE_CCS = 4;
const TYPE_HOME = 1;
const TYPE_POPUP = 2;
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="bigint")
*/
private $id;
/**
* @ORM\Column(type="integer")
*/
private $version; //Removed @ORM\Version as poll impressions are stored in the Entity. Should update this.
/**
* @ORM\Column(type="string", length=255)
*/
private $title;
/**
* @ORM\Column(type="text")
*/
private $description;
/**
* @ORM\Column(type="datetime")
*/
private $published_at;
/**
* @ORM\Column(type="datetime")
*/
private $modified_at;
/**
* @ORM\Column(type="datetime")
*/
private $created_at;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $response_close_date;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $expires_at;
/**
* @ORM\Column(type="integer")
*/
private $status;
/**
* @ORM\OneToMany(targetEntity="App\Entity\PollOption", mappedBy="poll", orphanRemoval=true, cascade={"persist", "remove"}, fetch="EAGER")
*/
private $pollOptions;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Customer", mappedBy="polls", cascade={"persist"})
*/
private $customers;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Media", cascade={"persist"})
* @ORM\JoinColumn(nullable=true)
*/
private $media;
/**
* @ORM\OneToOne(targetEntity="App\Entity\MediaGroupItem", inversedBy="poll", cascade={"persist"})
*/
private $ad;
/**
* @ORM\OneToOne(targetEntity="App\Entity\MediaGroupItem", mappedBy="popOutPoll", cascade={"persist"})
*/
private $popOutAd;
/**
* @ORM\Column(type="string", length=255)
*/
private $info_url;
/**
* @ORM\Column(type="integer")
*/
private $total_impressions;
/**
* @ORM\Column(type="integer")
*/
private $requires_login;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Site", inversedBy="polls")
*/
private $site;
/**
* @ORM\Column(type="integer")
*/
private $type;
public function __construct ()
{
$this->title = "";
$this->description = "";
$this->published_at = new \DateTime();
$this->modified_at = new \DateTime();
$this->created_at = new \DateTime ();
$this->status = SELF::STATUS_INACTIVE;
$this->pollOptions = new ArrayCollection();
$this->customers = new ArrayCollection();
$this->info_url = "";
$this->total_impressions = 0;
$this->requires_login = 0;
$this->version = 0;
$this->site = new ArrayCollection();
$this->type = 1;
}
public function getId()
{
return $this->id;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(string $title = null): self
{
$this->title = $title ? $title : "";
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription (string $description = null): ?self
{
$this->description = $description ? $description : "";
return $this;
}
public function getPublishedAt(): ?\DateTimeInterface
{
return $this->published_at;
}
public function setPublishedAt(\DateTimeInterface $published_at): self
{
$this->published_at = $published_at;
return $this;
}
public function getModifiedAt(): ?\DateTimeInterface
{
return $this->modified_at;
}
public function setModifiedAt(\DateTimeInterface $modified_at): self
{
$this->modified_at = $modified_at;
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->created_at;
}
public function setCreatedAt(\DateTimeInterface $created_at): self
{
$this->created_at = $created_at;
return $this;
}
public function getExpiresAt(): ?\DateTimeInterface
{
return $this->expires_at;
}
public function setExpiresAt(?\DateTimeInterface $expires_at): self
{
$this->expires_at = $expires_at;
return $this;
}
public function getResponseCloseDate(): ?\DateTimeInterface
{
return $this->response_close_date;
}
public function setResponseCloseDate(?\DateTimeInterface $response_close_date): self
{
$this->response_close_date = $response_close_date;
return $this;
}
public function getStatus(): ?int
{
return $this->status;
}
public function setStatus(int $status): self
{
$this->status = $status;
return $this;
}
/**
* @return Collection|PollOption[]
*/
public function getPollOptions(): Collection
{
return $this->pollOptions;
}
public function getActivePollOptions(): Collection
{
$criteria = Criteria::create()
->where(Criteria::expr()->eq("status", 1))
;
return $this->pollOptions->matching($criteria);
}
public function setPollOptions (Collection $pollOptions): self {
foreach ($pollOptions as $pollOption) {
$this->addPollOption($pollOption);
}
return $this;
}
public function addPollOption(PollOption $pollOption): self
{
if (!$this->pollOptions->contains($pollOption)) {
$this->pollOptions[] = $pollOption;
$pollOption->setPoll($this);
}
return $this;
}
public function removePollOption(PollOption $pollOption): self
{
if ($this->pollOptions->contains($pollOption)) {
$this->pollOptions->removeElement($pollOption);
// set the owning side to null (unless already changed)
if ($pollOption->getPoll() === $this) {
$pollOption->setPoll(null);
}
}
return $this;
}
public function resetPollOptions(): self
{
foreach($this->pollOptions as $pollOption) {
$this->removePollOption($pollOption);
}
return $this;
}
public function getPollOptionsArray() {
$return = [];
foreach($this->getPollOptions() as $pollOption) {
$return[$pollOption->getId()] = $pollOption->getText();
}
return $return;
}
public function getActivePollOptionsArray() {
$return = [];
foreach($this->getActivePollOptions() as $pollOption) {
$return[$pollOption->getId()] = $pollOption->getText();
}
return $return;
}
public function getResults() {
$return = [];
foreach($this->getPollOptions() as $pollOption) {
//$return[$pollOption->getId()] = $pollOption->countResponses();
$return[$pollOption->getText()] = $pollOption->countResponses();
}
return $return;
}
public function getPollResponses() {
$return = [];
foreach($this->getPollOptions() as $pollOption) {
foreach($pollOption->getPollResponses() as $pollResponse) {
$return[] = $pollResponse;
}
}
return $return;
}
public function countResponses() {
return count($this->getPollResponses());
}
/**
* @return Collection|Customer[]
*/
public function getCustomers(): Collection
{
return $this->customers;
}
//don't think Sonata is using this
public function addCustomers($customers): self
{
if(is_array($customers)) {
foreach($customers as $customer) {
$this->addCustomer($customer);
}
}
else {
$this->customers->clear();
$this->addCustomer($customers);
}
return $this;
}
public function addCustomer(Customer $customer): self
{
if (!$this->customers->contains($customer)) {
$this->customers[] = $customer;
$customer->addPoll($this);
}
return $this;
}
public function removeCustomer(Customer $customer): self
{
if ($this->customers->contains($customer)) {
$this->customers->removeElement($customer);
$customer->removePoll($this);
}
return $this;
}
public function resetCustomers(): self
{
foreach($this->customers as $customer) {
$this->removeCustomer($customer);
}
return $this;
}
public function getMedia (): ?Media
{
return $this->media;
}
public function setMedia (Media $media = null): self
{
$this->media = $media;
return $this;
}
public function getAd (): ?MediaGroupItem
{
return $this->ad;
}
public function setAd (MediaGroupItem $ad = null): self
{
$this->ad = $ad;
return $this;
}
public function getPopOutAd (): ?MediaGroupItem
{
return $this->popOutAd;
}
public function setPopOutAd (MediaGroupItem $popOutAd = null): self
{
$this->popOutAd = $popOutAd;
return $this;
}
public function getInfoUrl(): ?string
{
return $this->info_url;
}
public function setInfoUrl(string $info_url = null): self
{
$this->info_url = $info_url ? $info_url : "";
return $this;
}
public function getTotalImpressions(): ?int
{
return $this->total_impressions;
}
public function setTotalImpressions(int $total_impressions): self
{
$this->total_impressions = $total_impressions;
return $this;
}
public function incrementTotalImpressions(): self
{
$this->total_impressions = $this->total_impressions + 1;
return $this;
}
public function getRequiresLogin(): ?bool
{
return $this->requires_login == 1;
}
public function setRequiresLogin(bool $value): self
{
$this->requires_login = $value ? 1 : 0;
return $this;
}
/**
* @return Collection|Site[]
*/
public function getSite(): Collection
{
return $this->site;
}
public function getSites(): Collection
{
return $this->site;
}
public function addSite(Site $site): self
{
if (!$this->site->contains($site)) {
$this->site[] = $site;
$site->addPoll($this);
}
return $this;
}
public function resetSite(): self
{
$this->site = new ArrayCollection();
return $this;
}
public function removeSite(Site $site): self
{
if ($this->site->contains($site)) {
$this->site->removeElement($site);
$site->removePoll($this);
}
return $this;
}
public function getSiteTextShort()
{
$site_texts = [];
foreach($this->getSite() as $s) {
$site_texts[] = $s->getSiteTextShort();
}
return implode(", ", $site_texts);
}
public function getSiteIds()
{
$site_ids = [];
foreach($this->getSite() as $s) {
$site_ids[] = $s->getId();
}
return $site_ids;
}
public function getType(): ?int
{
return $this->type;
}
public function setType(int $type): self
{
$this->type = $type;
return $this;
}
public function isReallyActive(): bool {
return ($this->getStatus() == self::STATUS_ACTIVE) && $this->getTitle() && ($this->getPollOptions()->count() > 1);
}
public function __toString ()
{
return $this->title ? "Poll: " . $this->title : "";
}
}