<?php
namespace Uniski\CMSBundle\Entity;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Uniski\ResourceBundle\Entity\Pack;
/**
* @ORM\Entity
* @ORM\Table("offer")
*/
class Offer
{
const FRONTPAGE_SLIDER = 'slider';
const FRONTPAGE_SECTION_1 = 'top-ski';
const FRONTPAGE_SECTION_2 = 'highlight';
const FRONTPAGE_SECTION_3 = 'general';
/**
* @var integer
*
* @ORM\Id
* @ORM\Column(name="id", type="integer")
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
/**
* @var Uniski\ResourceBundle\Entity\Image
* @ORM\ManyToOne(targetEntity="Uniski\ResourceBundle\Entity\Image", cascade={"persist"})
* @ORM\JoinColumn(name="image_id", referencedColumnName="id", nullable=true)
*/
protected $image;
/**
* @var Uniski\ResourceBundle\Entity\Image
* @ORM\ManyToOne(targetEntity="Uniski\ResourceBundle\Entity\Image", cascade={"all"})
* @ORM\JoinColumn(name="header_image_id", referencedColumnName="id", nullable=true)
*/
protected $headerImage;
/**
* @var string
* @ORM\Column(name="title", type="string")
*/
protected $title;
/**
* @var string
* @ORM\Column(name="teaser", type="text")
*/
protected $teaser;
/**
* @var string
* @ORM\Column(name="description", type="text", nullable=true)
*/
protected $description;
/**
* @var string
* @ORM\ManyToMany(targetEntity="Uniski\ResourceBundle\Entity\Product")
*/
protected $products;
/**
* @var string
* @ORM\ManyToOne(targetEntity="Uniski\ConfigBundle\Entity\Category")
*/
protected $tag;
/**
* @var array
* @ORM\Column(name="period", type="json", nullable=true)
* It's a period of dateIn, dateOut or
* weekdays between or not dateIn and dateOut
*/
protected $period;
/**
* @var array
* @ORM\Column(name="seo", type="json", nullable=true)
* SEO will be used when offer is shown as a page of
* products
*/
protected $seo;
/**
* @Gedmo\Slug(fields={"title"})
* @ORM\Column(length=128, unique=true)
*/
private $slug;
/**
* @var float
* @ORM\Column(name="price", type="decimal", precision=6, scale=2, nullable=true)
*/
protected $price;
/**
* @var string
* @ORM\Column(name="frontpage", type="string", nullable=true, length=20)
*/
protected $frontpage;
/**
* @var ArrayCollection
* @ORM\ManyToOne(targetEntity="Page", inversedBy="offers")
*/
protected $page;
/**
* @var ArrayCollection
* @ORM\ManyToOne(targetEntity="Menu", inversedBy="offers")
*/
protected $menu;
/**
* @var string
* @ORM\Column(name="link", type="string", nullable=true)
*/
protected $link;
/**
* @var \DateTime $created
*
* @Gedmo\Timestampable(on="create")
* @ORM\Column(type="datetime")
*/
private $created;
/**
* @var \DateTime $updated
*
* @Gedmo\Timestampable
* @ORM\Column(type="datetime")
*/
private $updated;
/**
* @var string
* @ORM\Column(type="text", nullable=true, length=256)
*/
protected $webform;
/**
* @var integer
* @ORM\Column(type="smallint")
*/
protected $priority;
/**
* Constructor
*/
public function __construct()
{
$this->products = new \Doctrine\Common\Collections\ArrayCollection();
$this->priority = 0;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* @param string $title
*
* @return Offer
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set title
*
* @param string $teaser
*
* @return Offer
*/
public function setTeaser($teaser)
{
$this->teaser = $teaser;
return $this;
}
/**
* Get teaser
*
* @return string
*/
public function getTeaser()
{
return $this->teaser;
}
/**
* Set description
*
* @param string $description
*
* @return Offer
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set productType
*
* @param string $productType
*
* @return Offer
*/
public function setProductType($productType)
{
$this->productType = $productType;
return $this;
}
/**
* Get productType
*
* @return string
*/
public function getProductType()
{
return $this->productType;
}
/**
* Set period
*
* @param array $period
*
* @return Offer
*/
public function setPeriod($period)
{
$this->period = $period;
return $this;
}
/**
* Get period
*
* @return array
*/
public function getPeriod($field = null)
{
if ($field){
return isset($this->period[$field]) ? $this->period[$field] : null;
}
return $this->period;
}
/**
* Set seo
*
* @param array $seo
*
* @return Offer
*/
public function setSeo($seo)
{
$this->seo = $seo;
return $this;
}
/**
* Get seo
*
* @return array
*/
public function getSeo()
{
$seo = $this->seo;
if (empty($seo['title']) && $this->title) {
$seo['title'] = sprintf("Ofertas esquí %s", $this->title);
}
if (empty($seo['description']) && $this->getSingleProduct()) {
$zone = $this->getSingleProduct()
->getDestination()
->getZone()
->getName();
$destination = $this->getSingleProduct()
->getDestination()
->getName();
$seo['description'] = sprintf(
"Las mejores ofertas de esquí %s . Hotel y forfait al mejor precio para disfrutar de tu viaje de esquí en %s, %s",
$this->title,
$zone,
$destination
);
}
return $seo;
}
/**
* Set slug
*
* @param string $slug
*
* @return Offer
*/
public function setSlug($slug)
{
$this->slug = $slug;
return $this;
}
/**
* Get slug
*
* @return string
*/
public function getSlug()
{
return $this->slug;
}
/**
* Set image
*
* @param \Uniski\ResourceBundle\Entity\Image $image
*
* @return Offer
*/
public function setImage(\Uniski\ResourceBundle\Entity\Image $image = null)
{
$this->image = $image;
return $this;
}
/**
* Get image
*
* @return \Uniski\ResourceBundle\Entity\Image
*/
public function getImage()
{
return $this->image;
}
/**
* Set headerImage
*
* @param \Uniski\ResourceBundle\Entity\Image $headerImage
*
* @return Offer
*/
public function setHeaderImage(\Uniski\ResourceBundle\Entity\Image $headerImage = null)
{
$this->headerImage = $headerImage;
return $this;
}
/**
* Get headerImage
*
* @return \Uniski\ResourceBundle\Entity\Image
*/
public function getHeaderImage()
{
return $this->headerImage;
}
/**
* Add product
*
* @param \Uniski\CMSBundle\Entity\Product $product
*
* @return Offer
*/
public function addProduct($product)
{
$this->products[] = $product;
return $this;
}
/**
* Remove product
*
* @param \Uniski\CMSBundle\Entity\Product $product
*/
public function removeProduct($product)
{
$this->products->removeElement($product);
}
/**
* Get products
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getProducts()
{
return $this->products;
}
/**
* Return a single product if the offer has olny one
* product. This is useful to use in "if" stataments simplify code
* @return Product Product
*/
public function getSingleProduct()
{
if (count($this->products) == 1) return $this->products[0];
return null;
}
/**
* Return a single product price if the offer has only one
* product and this one has a price method.
* @return float
*/
public function getSingleProductPrice()
{
if (!$this->getSingleProduct()) return null;
$product = $this->getSingleProduct();
if ($product instanceof Pack &&
$product->getPrice())
return $product->getPrice();
return null;
}
/**
* Set frontpage
*
* @param string $frontpage
*
* @return Offer
*/
public function setFrontpage($frontpage)
{
$this->frontpage = $frontpage;
return $this;
}
/**
* Get frontpage
*
* @return string
*/
public function getFrontpage()
{
return $this->frontpage;
}
/**
* Set price
*
* @param string $price
*
* @return Offer
*/
public function setPrice($price)
{
$this->price = $price;
return $this;
}
/**
* Get price
*
* @return string
*/
public function getPrice()
{
if ($this->price) return $this->price;
if ($this->getSingleProductPrice())
return $this->getSingleProductPrice();
return null;
}
/**
* Set page
*
* @param string $page
*
* @return Offer
*/
public function setPage($page)
{
$this->page = $page;
return $this;
}
/**
* Get page
*
* @return string
*/
public function getPage()
{
return $this->page;
}
public function setProducts(ArrayCollection $products)
{
$this->products = $products;
}
/**
* Set title
*
* @param string $link
*
* @return Offer
*/
public function setLink($link)
{
$this->link = $link;
return $this;
}
/**
* Get Link
*
* @return string
*/
public function getLink()
{
return $this->link;
}
/**
* Set tag
*
* @param \Uniski\ConfigBundle\Entity\Category $tag
*
* @return Offer
*/
public function setTag(\Uniski\ConfigBundle\Entity\Category $tag = null)
{
$this->tag = $tag;
return $this;
}
/**
* Get tag
*
* @return \Uniski\ConfigBundle\Entity\Category
*/
public function getTag()
{
return $this->tag;
}
/**
* Set menu
*
* @param \Uniski\CMSBundle\Entity\Menu $menu
*
* @return Offer
*/
public function setMenu(\Uniski\CMSBundle\Entity\Menu $menu = null)
{
$this->menu = $menu;
return $this;
}
/**
* Get menu
*
* @return \Uniski\CMSBundle\Entity\Menu
*/
public function getMenu()
{
return $this->menu;
}
/**
* Set created
*
* @param \DateTime $created
*
* @return Offer
*/
public function setCreated($created)
{
$this->created = $created;
return $this;
}
/**
* Get created
*
* @return \DateTime
*/
public function getCreated()
{
return $this->created;
}
/**
* get the pack type product's name if it exists
* otherwise returns teaser in a single fashion line
* @return [type] [description]
*/
public function getFeatures()
{
if ($this->getProducts()[0] instanceof Pack) {
return $this->getProducts()[0]->getName();
}
if (!$this->getTeaser()) return "";
$features = $this->getTeaser();
$features = str_replace(['<br>','<br/>','<br />'], ' | ', $features);
$features = strip_tags($features);
return $features;
}
public function hasValidPeriod()
{
if ($this->period === null) return false;
if (!isset($this->period['dateIn']) ||
!isset($this->period['dateOut']) ||
!isset($this->period['weekdays']))
return false;
if ($this->period['dateIn'] == null &&
empty($this->period['weekdays']) ) return false;
return true;
}
/**
* Gets the value of updated.
*
* @return \DateTime $updated
*/
public function getUpdated()
{
return $this->updated;
}
/**
* Sets the value of updated.
*
* @param \DateTime $updated $updated the updated
*
* @return self
*/
public function setUpdated($updated)
{
$this->updated = $updated;
return $this;
}
public function getCalculatedDescription()
{
if ($this->description) return $this->description;
if (!$this->getSingleProduct()) return "";
$product = $this->getSingleProduct();
return $product->getDescription() ?: $product->getDestination()->getDescription();
}
public function getCalculatedImage()
{
if ($this->image) return $this->image;
if (!$this->getSingleProduct()) return null;
if ($this->getSingleProduct()->getMainImage()) return $this->image;
return $this->getSingleProduct()->getDestination()->getMainImage();
}
/**
* Gets the value of webform.
*
* @return string
*/
public function getWebform()
{
return $this->webform;
}
/**
* Sets the value of webform.
*
* @param string $webform the webform
*
* @return self
*/
public function setWebform($webform)
{
$this->webform = $webform;
return $this;
}
/**
* @return integer
*/
public function getPriority()
{
return $this->priority;
}
/**
* @param integer $priority
*
* @return self
*/
public function setPriority($priority)
{
$this->priority = $priority;
return $this;
}
}