<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
use Doctrine\Common\Collections\Criteria;
/**
* @ORM\Entity(repositoryClass="App\Repository\MediaGroupItemRepository")
*/
class MediaGroupItem
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="bigint")
*/
private $id;
/**
* @ORM\Version @ORM\Column(type="integer")
*/
private $version;
/**
* @ORM\Column(type="string", length=255)
*/
private $title;
/**
* @ORM\Column(type="string", length=255)
*/
private $displayTitle;
/**
* @ORM\Column(type="text")
*/
private $description;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Media", cascade={"persist"})
* @ORM\JoinColumn(nullable=true)
*/
private $media;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Media", cascade={"persist"})
* @ORM\JoinColumn(nullable=true)
*/
private $secondary_media;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Media", cascade={"persist"})
* @ORM\JoinColumn(nullable=true)
*/
private $french_media;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Media", cascade={"persist"})
* @ORM\JoinColumn(nullable=true)
*/
private $spanish_media;
/**
* @ O R M\ManyToMany(targetEntity="MediaGroup", inversedBy="media")
* @ O R M\JoinTable(
* name="media_group_media",
* joinColumns={
* @ORM\JoinColumn(name="media_id", referencedColumnName="id")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="media_group_id", referencedColumnName="id")
* }
* )
* /
private $mediaGroup;
*/
/**
* @ORM\Column(type="integer")
*/
private $sort;
/**
* @ORM\Column(type="datetime")
*/
private $modifiedAt;
/**
* @ORM\Column(type="datetime")
*/
private $createdAt;
/**
* @ORM\Column(type="datetime")
*/
private $published_at;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $expires_at;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\MediaGroup", inversedBy="mediaGroupItems")
*/
private $mediaGroup;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Content", inversedBy="galleryMediaItems")
*/
private $gallery;
/**
* @ORM\Column(type="integer")
*/
private $status;
/**
* @ORM\Column(type="integer")
*/
private $total_impressions;
/**
* @ORM\Column(type="integer")
*/
private $total_clicks;
/**
* @ORM\Column(type="text")
*/
private $attributes;
/**
* @ORM\Column(type="string", length=255)
*/
private $external_url;
/**
* @ORM\Column(type="string", length=255)
*/
private $youtube_url;
/**
* @ORM\Column(type="string", length=255)
*/
private $french_url;
/**
* @ORM\Column(type="string", length=255)
*/
private $spanish_url;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Customer", mappedBy="mediaGroupItems", cascade={"persist"})
*/
private $customers;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Traffic", mappedBy="media_group_item", orphanRemoval=true, cascade={"persist", "remove"})
*/
private $traffic;
/**
* @ORM\OneToMany(targetEntity="App\Entity\AdImpression", mappedBy="media_group_item", orphanRemoval=true, cascade={"persist", "remove"})
*/
private $impressions;
/**
* @ORM\OneToMany(targetEntity="App\Entity\AdClick", mappedBy="media_group_item", orphanRemoval=true, cascade={"persist", "remove"})
*/
private $clicks;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $countdown;
/**
* @ORM\OneToMany(targetEntity="App\Entity\AdAnalyticsDaily", mappedBy="media_group_item", orphanRemoval=true, cascade={"persist", "remove"})
*/
private $ad_analytics_daily;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Site", inversedBy="media_group_items")
* @ORM\JoinTable(name="ad_site")
*/
private $site;
/**
* @ORM\OneToOne(targetEntity="App\Entity\Poll", mappedBy="ad", cascade={"persist", "remove"})
*/
private $poll;
/**
* @ORM\Column(type="integer")
*/
private $question_slider;
/**
* @ORM\Column(type="integer")
*/
private $site_default;
/**
* @ORM\OneToOne(targetEntity="App\Entity\Poll", inversedBy="popOutAd", cascade={"persist", "remove"})
*/
private $popOutPoll;
// The pollOptions (pop-out) field is temporary until I can figure out how to properly display in SonataAdmin without this hack.
/**
* @ORM\OneToMany(targetEntity="App\Entity\PollOption", mappedBy="ad")
*/
private $pollOptions;
//The UUID of the mega menu item that this ad is pinned to.
/**
* @ORM\Column(type="string", length=150)
*/
private $pinned_uuid;
/**
* @ORM\OneToMany(targetEntity="App\Entity\AdPin", mappedBy="media_group_item", orphanRemoval=true, cascade={"persist", "remove"})
*/
private $pins;
/**
* @ORM\OneToMany(targetEntity="App\Entity\AdPriority", mappedBy="media_group_item", orphanRemoval=true, cascade={"persist", "remove"})
*/
private $priorities;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Content", mappedBy="popout_ad", orphanRemoval=true, cascade={"persist", "remove"})
*/
private $popout_contents;
public function __construct ()
{
// $this->media_groups = new ArrayCollection();
$this->external_url = "";
$this->french_url = "";
$this->spanish_url = "";
$this->attributes = "";
$this->description = "";
$this->title = "";
$this->youtube_url = "";
$this->sort = 0;
$this->status = 1;
$this->total_impressions = 0;
$this->total_clicks = 0;
$this->modifiedAt = new \DateTime("now");
$this->createdAt = new \DateTime("now");
$this->published_at = new \DateTime("now");
$this->customers = new ArrayCollection();
$this->site = new ArrayCollection();
$this->pollOptions = new ArrayCollection();
$this->question_slider = 0;
$this->displayTitle = "";
}
public function __clone() {
$this->id = null;
$this->title = $this->title . " - copy";
$this->displayTitle = $this->displayTitle;
$this->status = 0;
$this->total_impressions = 0;
$this->total_clicks = 0;
$this->customers = new ArrayCollection();
$this->modifiedAt = new \DateTime("now");
$this->createdAt = new \DateTime("now");
$this->published_at = new \DateTime("now");
}
public function getId()
{
return $this->id;
}
public function getVersion() {
return $this->version;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(string $title = null): self
{
$this->title = $title ? $title : "";
return $this;
}
public function getDisplayTitle(): ?string
{
return $this->displayTitle;
}
public function setDisplayTitle(string $displayTitle = null): self
{
$this->displayTitle = $displayTitle ? $displayTitle : "";
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(string $description = null): self
{
$this->description = $description ? $description : "";
return $this;
}
//returns the description with a link if the ad includes a link
public function getDescriptionLink(string $target = "_blank"): ?string
{
$return = "";
if($this->getExternalUrl()) {
$return .= "<a href='". $this->getExternalUrl() ."' target='". $target ."' data-mitem-id='". $this->getId() ."'>";
}
$return .= $this->description;
if($this->getExternalUrl()) {
$return .= "</a>";
}
return $return;
}
/*
public function getFile(): ?File
{
return $this->file;
}
public function setFile(?File $file): self
{
$this->file = $file;
return $this;
}
*/
public function getModifiedAt(): ?\DateTimeInterface
{
return $this->modifiedAt;
}
public function setModifiedAt(\DateTimeInterface $modifiedAt): self
{
$this->modifiedAt = $modifiedAt;
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getPublishedAt(): ?\DateTimeInterface
{
return $this->published_at;
}
public function setPublishedAt(\DateTimeInterface $scheduled_for): self
{
$this->published_at = $scheduled_for;
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 getCountdown(): ?\DateTimeInterface
{
return $this->countdown;
}
public function setCountdown(?\DateTimeInterface $countdown): self
{
$this->countdown = $countdown;
return $this;
}
public function getSort(): ?int
{
return $this->sort;
}
public function setSort(int $sort): self
{
$this->sort = $sort;
return $this;
}
/*
public function addMediaGroup (MediaGroup $mediaGroup)
{
if (!$this->media_groups->contains($mediaGroup)) {
$this->media_groups->add($mediaGroup);
$mediaGroup->addMedia($this);
}
return $this;
}
public function removeMediaGroup (MediaGroup $mediaGroup)
{
if ($this->media_groups->contains($mediaGroup)) {
$this->media_groups->removeElement($mediaGroup);
$mediaGroup->removeMedia($this);
}
return $this;
}
*/
// Helper function for the admin page
public $mediaurl;
public function getMediaURL (): ?string
{
if($this->media) {
return $this->media->getURL();
}
return "";
}
public function setMediaURL (): self
{
return $this;
}
public function getFrenchMediaURL (): ?string
{
if($this->french_media) {
return $this->french_media->getURL();
}
return "";
}
public function getSpanishMediaURL (): ?string
{
if($this->spanish_media) {
return $this->spanish_media->getURL();
}
return "";
}
public function getSecondaryMediaURL (): ?string
{
if($this->secondary_media) {
return $this->secondary_media->getURL();
}
return "";
}
public function getMediaImageTag(string $class = "", string $style = "", string $target = "")
{
$return = "";
if($this->media) {
if($this->getExternalUrl()) {
$rel = " rel='nofollow'";
if(substr($this->getExternalUrl(), 0, 1) == "/" || (isset($_SERVER['HTTP_HOST']) && (strpos($this->getExternalUrl(), $_SERVER['HTTP_HOST']) !== false) )) {
$rel = "";
}
$return .= "<a href='". $this->getExternalUrl() ."' target='". $target ."' data-mitem-id='". $this->getId() . "'" .$rel . " >";
}
if($this->french_media || $this->spanish_media) {
$return .= $this->media->getImageTag($class. " english_media", $style, $this->getId(), $this->getExternalUrl());
}
else {
$return .= $this->media->getImageTag($class, $style, $this->getId(), $this->getExternalUrl());
}
$style .= "display:none;";
if($this->french_media) {
$return .= $this->french_media->getImageTag($class." french_media secondary_media", $style, $this->getId(), $this->getFrenchUrl());
}
if($this->spanish_media) {
$return .= $this->spanish_media->getImageTag($class." spanish_media secondary_media", $style, $this->getId(), $this->getSpanishUrl());
}
if($this->getExternalUrl()) {
$return .= "</a>";
}
}
return $return;
}
public function getMedia (): ?Media
{
return $this->media;
}
public function setMedia (Media $media = null): self
{
$this->media = $media;
return $this;
}
public function getFrenchMedia (): ?Media
{
return $this->french_media;
}
public function setFrenchMedia (Media $french_media = null): self
{
$this->french_media = $french_media;
return $this;
}
public function getSecondaryMedia (): ?Media
{
return $this->secondary_media;
}
public function setSecondaryMedia (Media $secondary_media = null): self
{
$this->secondary_media = $secondary_media;
return $this;
}
public function getSpanishMedia (): ?Media
{
return $this->spanish_media;
}
public function setSpanishMedia (Media $spanish_media = null): self
{
$this->spanish_media = $spanish_media;
return $this;
}
public function getmediaGroup(): ?MediaGroup
{
return $this->mediaGroup;
}
public function setmediaGroup(?MediaGroup $mediaGroup): self
{
$this->mediaGroup = $mediaGroup;
return $this;
}
public function getGallery(): ?Content
{
$mGroup = $this->getmediaGroup();
$mgContainers = $mGroup->getMediaGroupContainers();
$mgContainer = $mgContainers->first();
$gallery = $mgContainer->getContent();
return $gallery;
}
public function setGallery(?Content $gallery): self
{
$this->gallery = $gallery;
return $this;
}
public function getStatus(): ?int
{
return $this->status;
}
public function setStatus(int $status): self
{
$this->status = $status;
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 getTotalClicks(): ?int
{
return $this->total_clicks;
}
public function setTotalClicks(int $total_clicks): self
{
$this->total_clicks = $total_clicks;
return $this;
}
public function incrementTotalClicks(): self
{
$this->total_clicks = $this->total_clicks + 1;
return $this;
}
public function isActive(): ?bool
{
return ($this->status == 1);
}
public function getAttributes(): ?string
{
return $this->attributes;
}
public function setAttributes(string $attributes = null): self
{
$this->attributes = $attributes ? $attributes : "";
return $this;
}
public function __toString ()
{
return $this->title ? $this->title : "";
}
public function getExternalUrl(): ?string
{
return $this->external_url;
}
public function setExternalUrl(string $external_url = null): self
{
$this->external_url = $external_url ? $external_url : "";
return $this;
}
public function getYoutubeUrl(): ?string
{
return $this->youtube_url;
}
public function setYoutubeUrl(string $youtube_url = null): self
{
$this->youtube_url = $youtube_url ? $youtube_url : "";
return $this;
}
//Returns the YouTube video ID for video ads based on the URL
public function getYTVideoId() {
$url = $this->youtube_url;
$url_components = parse_url($url);
if(array_key_exists("query", $url_components)) {
parse_str($url_components['query'], $params);
if(array_key_exists("v", $params) && $params['v']) { //regular YT link
return $params['v'];
}
}
else { //short 'share' YT link
if(strrpos($url, '/')) {
return substr($url, strrpos($url, '/') + 1);
}
}
//otherwise just return the URL (may have just posted the YT video ID)
return $url;
}
public function getFrenchUrl(): ?string
{
return $this->french_url;
}
public function setFrenchUrl(string $french_url = null): self
{
$this->french_url = $french_url ? $french_url : "";
return $this;
}
public function getSpanishUrl(): ?string
{
return $this->spanish_url;
}
public function setSpanishUrl(string $spanish_url = null): self
{
$this->spanish_url = $spanish_url ? $spanish_url : "";
return $this;
}
/**
* @return Collection|Customer[]
*/
public function getCustomers(): Collection
{
return $this->customers;
}
public function addCustomer(Customer $customer): self
{
if (!$this->customers->contains($customer)) {
$this->customers[] = $customer;
$customer->addMediaGroupItem($this);
}
return $this;
}
public function removeCustomer(Customer $customer): self
{
if ($this->customers->contains($customer)) {
$this->customers->removeElement($customer);
$customer->removeMediaGroupItem($this);
}
return $this;
}
public function resetCustomers(): self
{
foreach($this->customers as $customer) {
$this->removeCustomer($customer);
}
return $this;
}
/**
* @return Collection|Traffic[]
*/
public function getTraffic(): Collection
{
return $this->traffic;
}
public function addTraffic(Traffic $traffic): self
{
if (!$this->traffic->contains($traffic)) {
$this->traffic[] = $traffic;
$traffic->setMediaGroupItem($this);
}
return $this;
}
public function removeTraffic(Traffic $traffic): self
{
if ($this->traffic->contains($traffic)) {
$this->traffic->removeElement($traffic);
// set the owning side to null (unless already changed)
if ($traffic->getMediaGroupItem() === $this) {
$traffic->setMediaGroupItem(null);
}
}
return $this;
}
public function getImpressions(): Collection
{
return $this->impressions;
}
public function getClicks(): Collection
{
return $this->clicks;
}
public function getAdAnalyticsDaily(): Collection
{
return $this->ad_analytics_daily;
}
public function getPins(): Collection
{
return $this->pins;
}
public function addPin(AdPin $pin): self
{
if (!$this->pins->contains($pin)) {
$this->pins[] = $pin;
$pin->setMediaGroupItem($this);
}
return $this;
}
public function resetPins(): self
{
$this->pins = new ArrayCollection();
return $this;
}
public function getPriorities(): Collection
{
return $this->priorities;
}
public function addPriority(AdPriority $priority): self
{
if (!$this->priorities->contains($priority)) {
$this->priorities[] = $priority;
$priority->setMediaGroupItem($this);
}
return $this;
}
public function resetPriorities(): self
{
$this->priorities = new ArrayCollection();
return $this;
}
public function getPopoutContents(): Collection
{
return $this->popout_contents;
}
public function addPopoutContent(Content $content): self
{
if (!$this->popout_contents->contains($content)) {
$this->popout_contents[] = $content;
$content->setPopoutAd($this);
}
return $this;
}
public function resetPopoutContents(): self
{
$this->popout_contents = new ArrayCollection();
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->addMediaGroupItem($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->removeMediaGroupItem($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;
}
//Returns the front page poll that is using this ad
public function getPoll(): ?Poll
{
return $this->poll;
}
public function setPoll(?Poll $poll): self
{
$this->poll = $poll;
return $this;
}
public function getPopOutPoll(): ?Poll
{
return $this->popOutPoll;
}
public function setPopOutPollPoll(?Poll $popOutPoll): self
{
$this->popOutPoll = $popOutPoll;
return $this;
}
/*
public function getPollOptions()
{
if($this->poll) {
return $this->poll->getPollOptions();
}
return null;
}
public function setPollOptions(Collection $pollOptions)
{
if($this->poll) {
return $this->poll->setPollOptions($pollOptions);
}
return null;
}
public function addPollOption(PollOption $pollOption)
{
if($this->poll) {
return $this->poll->addPollOption($pollOption);
}
return null;
}
public function removePollOption(PollOption $pollOption)
{
if($this->poll) {
return $this->poll->removePollOption($pollOption);
}
return null;
}
*/
/**
* @return Collection|PollOption[]
*/
public function getPollOptions(): Collection
{
if($this->poll) {
$this->setPollOptions($this->poll->getPollOptions());
}
return $this->pollOptions;
}
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->setAd($this);
}
if($this->poll) {
$this->poll->addPollOption($pollOption);
}
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->getAd() === $this) {
$pollOption->setAd(null);
}
}
if($this->poll) {
$this->poll->removePollOption($pollOption);
}
return $this;
}
public function getQuestionSlider(): ?bool
{
return $this->question_slider == 1;
}
public function setQuestionSlider(bool $value): self
{
$this->question_slider = $value ? 1 : 0;
return $this;
}
public function getPinnedUuid(): ?string
{
return $this->pinned_uuid;
}
public function setPinnedUuid(string $pinned_uuid = null): self
{
$this->pinned_uuid = $pinned_uuid;
return $this;
}
public function getSiteDefault(): ?int
{
return $this->site_default;
}
public function setSiteDefault(int $site_default = null): self
{
$this->site_default = $site_default;
return $this;
}
/*
public function getImpressions($year=0, $month=0, $day=0)
{
$criteria = Criteria::create()
->where(Criteria::expr()->eq("type", "4"))
;
return $this->getTraffic()->matching($criteria)->count();
}
public function getClicks($year=0, $month=0, $day=0)
{
$criteria = Criteria::create()
->where(Criteria::expr()->eq("type", "5"))
;
return $this->getTraffic()->matching($criteria)->count();
}
*/
}