src/Uniski/CMSBundle/Entity/Offer.php line 15

Open in your IDE?
  1. <?php
  2. namespace Uniski\CMSBundle\Entity;
  3. use DateTime;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Gedmo\Mapping\Annotation as Gedmo;
  7. use Uniski\ResourceBundle\Entity\Pack;
  8. /**
  9. * @ORM\Entity
  10. * @ORM\Table("offer")
  11. */
  12. class Offer
  13. {
  14.     const FRONTPAGE_SLIDER     'slider';
  15.     const FRONTPAGE_SECTION_1  'top-ski';
  16.     const FRONTPAGE_SECTION_2  'highlight';
  17.     const FRONTPAGE_SECTION_3  'general';
  18.   /**
  19.    * @var integer
  20.    *
  21.    * @ORM\Id
  22.    * @ORM\Column(name="id", type="integer")
  23.    * @ORM\GeneratedValue(strategy="IDENTITY")
  24.    */
  25.   protected $id;
  26.   /**
  27.    * @var  Uniski\ResourceBundle\Entity\Image
  28.    * @ORM\ManyToOne(targetEntity="Uniski\ResourceBundle\Entity\Image", cascade={"persist"})
  29.    * @ORM\JoinColumn(name="image_id", referencedColumnName="id", nullable=true)
  30.    */
  31.   protected $image;
  32.   /**
  33.    * @var   Uniski\ResourceBundle\Entity\Image
  34.    * @ORM\ManyToOne(targetEntity="Uniski\ResourceBundle\Entity\Image", cascade={"all"})
  35.    * @ORM\JoinColumn(name="header_image_id", referencedColumnName="id", nullable=true)
  36.    */
  37.   protected $headerImage;
  38.   /**
  39.    * @var   string
  40.    * @ORM\Column(name="title", type="string")
  41.    */
  42.   protected $title;
  43.   /**
  44.    * @var   string
  45.    * @ORM\Column(name="teaser", type="text")
  46.    */
  47.   protected $teaser;
  48.   /**
  49.    * @var   string
  50.    * @ORM\Column(name="description", type="text", nullable=true)
  51.    */
  52.   protected $description;
  53.   /**
  54.    * @var   string
  55.    * @ORM\ManyToMany(targetEntity="Uniski\ResourceBundle\Entity\Product")
  56.    */
  57.   protected $products;
  58.   /**
  59.    * @var   string
  60.    * @ORM\ManyToOne(targetEntity="Uniski\ConfigBundle\Entity\Category")
  61.    */
  62.   protected $tag;
  63.   /**
  64.    * @var  array
  65.    * @ORM\Column(name="period", type="json", nullable=true)
  66.    * It's a period of dateIn, dateOut or
  67.    * weekdays between or not dateIn and dateOut
  68.    */
  69.   protected $period;
  70.   /**
  71.    * @var  array
  72.    * @ORM\Column(name="seo", type="json", nullable=true)
  73.    * SEO will be used when offer is shown as a page of
  74.    * products
  75.    */
  76.   protected $seo;
  77.   /**
  78.    * @Gedmo\Slug(fields={"title"})
  79.    * @ORM\Column(length=128, unique=true)
  80.    */
  81.   private $slug;
  82.   /**
  83.    * @var  float
  84.    * @ORM\Column(name="price", type="decimal", precision=6, scale=2, nullable=true)
  85.    */
  86.   protected $price;
  87.   /**
  88.    * @var string
  89.    * @ORM\Column(name="frontpage", type="string", nullable=true, length=20)
  90.    */
  91.   protected $frontpage;
  92.   /**
  93.    * @var  ArrayCollection
  94.    * @ORM\ManyToOne(targetEntity="Page", inversedBy="offers")
  95.    */
  96.   protected $page;
  97.   /**
  98.    * @var  ArrayCollection
  99.    * @ORM\ManyToOne(targetEntity="Menu", inversedBy="offers")
  100.    */
  101.   protected $menu;
  102.     /**
  103.     * @var string
  104.     * @ORM\Column(name="link", type="string", nullable=true)
  105.     */
  106.     protected $link;
  107.     /**
  108.      * @var \DateTime $created
  109.      *
  110.      * @Gedmo\Timestampable(on="create")
  111.      * @ORM\Column(type="datetime")
  112.      */
  113.     private $created;
  114.     /**
  115.      * @var \DateTime $updated
  116.      *
  117.      * @Gedmo\Timestampable
  118.      * @ORM\Column(type="datetime")
  119.      */
  120.     private $updated;
  121.     /**
  122.      * @var   string
  123.      * @ORM\Column(type="text", nullable=true, length=256)
  124.      */
  125.     protected $webform;
  126.     /**
  127.      * @var integer
  128.      * @ORM\Column(type="smallint")
  129.      */
  130.     protected $priority;
  131.     /**
  132.      * Constructor
  133.      */
  134.     public function __construct()
  135.     {
  136.         $this->products = new \Doctrine\Common\Collections\ArrayCollection();
  137.         $this->priority 0
  138.     }
  139.     /**
  140.      * Get id
  141.      *
  142.      * @return integer
  143.      */
  144.     public function getId()
  145.     {
  146.         return $this->id;
  147.     }
  148.     /**
  149.      * Set title
  150.      *
  151.      * @param string $title
  152.      *
  153.      * @return Offer
  154.      */
  155.     public function setTitle($title)
  156.     {
  157.         $this->title $title;
  158.         return $this;
  159.     }
  160.     /**
  161.      * Get title
  162.      *
  163.      * @return string
  164.      */
  165.     public function getTitle()
  166.     {
  167.         return $this->title;
  168.     }
  169.     /**
  170.      * Set title
  171.      *
  172.      * @param string $teaser
  173.      *
  174.      * @return Offer
  175.      */
  176.     public function setTeaser($teaser)
  177.     {
  178.         $this->teaser $teaser;
  179.         return $this;
  180.     }
  181.     /**
  182.      * Get teaser
  183.      *
  184.      * @return string
  185.      */
  186.     public function getTeaser()
  187.     {
  188.         return $this->teaser;
  189.     }
  190.     /**
  191.      * Set description
  192.      *
  193.      * @param string $description
  194.      *
  195.      * @return Offer
  196.      */
  197.     public function setDescription($description)
  198.     {
  199.         $this->description $description;
  200.         return $this;
  201.     }
  202.     /**
  203.      * Get description
  204.      *
  205.      * @return string
  206.      */
  207.     public function getDescription()
  208.     {
  209.         return $this->description;
  210.     }
  211.     /**
  212.      * Set productType
  213.      *
  214.      * @param string $productType
  215.      *
  216.      * @return Offer
  217.      */
  218.     public function setProductType($productType)
  219.     {
  220.         $this->productType $productType;
  221.         return $this;
  222.     }
  223.     /**
  224.      * Get productType
  225.      *
  226.      * @return string
  227.      */
  228.     public function getProductType()
  229.     {
  230.         return $this->productType;
  231.     }
  232.     /**
  233.      * Set period
  234.      *
  235.      * @param array $period
  236.      *
  237.      * @return Offer
  238.      */
  239.     public function setPeriod($period)
  240.     {
  241.         $this->period $period;
  242.         return $this;
  243.     }
  244.     /**
  245.      * Get period
  246.      *
  247.      * @return array
  248.      */
  249.     public function getPeriod($field null)
  250.     {
  251.         if ($field){
  252.             return isset($this->period[$field]) ? $this->period[$field] : null;
  253.         }
  254.         return $this->period;
  255.     }
  256.     /**
  257.      * Set seo
  258.      *
  259.      * @param array $seo
  260.      *
  261.      * @return Offer
  262.      */
  263.     public function setSeo($seo)
  264.     {
  265.         $this->seo $seo;
  266.         return $this;
  267.     }
  268.     /**
  269.      * Get seo
  270.      *
  271.      * @return array
  272.      */
  273.     public function getSeo()
  274.     {
  275.         $seo $this->seo;
  276.         
  277.         if (empty($seo['title']) && $this->title) {
  278.             $seo['title'] = sprintf("Ofertas esquí %s"$this->title);
  279.         }
  280.         if (empty($seo['description']) && $this->getSingleProduct()) {
  281.             
  282.             $zone $this->getSingleProduct()
  283.                 ->getDestination()
  284.                 ->getZone()
  285.                 ->getName();
  286.             $destination $this->getSingleProduct()
  287.                 ->getDestination()
  288.                 ->getName();
  289.             $seo['description'] = sprintf(
  290.                 "Las mejores ofertas de esquí %s . Hotel y forfait al mejor precio para disfrutar de tu viaje de esquí  en %s, %s",
  291.                 $this->title,
  292.                 $zone,
  293.                 $destination
  294.             );
  295.         }
  296.         return $seo;
  297.     }
  298.     /**
  299.      * Set slug
  300.      *
  301.      * @param string $slug
  302.      *
  303.      * @return Offer
  304.      */
  305.     public function setSlug($slug)
  306.     {
  307.         $this->slug $slug;
  308.         return $this;
  309.     }
  310.     /**
  311.      * Get slug
  312.      *
  313.      * @return string
  314.      */
  315.     public function getSlug()
  316.     {
  317.         return $this->slug;
  318.     }
  319.     /**
  320.      * Set image
  321.      *
  322.      * @param \Uniski\ResourceBundle\Entity\Image $image
  323.      *
  324.      * @return Offer
  325.      */
  326.     public function setImage(\Uniski\ResourceBundle\Entity\Image $image null)
  327.     {
  328.         $this->image $image;
  329.         return $this;
  330.     }
  331.     /**
  332.      * Get image
  333.      *
  334.      * @return \Uniski\ResourceBundle\Entity\Image
  335.      */
  336.     public function getImage()
  337.     {
  338.         return $this->image;
  339.     }
  340.     /**
  341.      * Set headerImage
  342.      *
  343.      * @param \Uniski\ResourceBundle\Entity\Image $headerImage
  344.      *
  345.      * @return Offer
  346.      */
  347.     public function setHeaderImage(\Uniski\ResourceBundle\Entity\Image $headerImage null)
  348.     {
  349.         $this->headerImage $headerImage;
  350.         return $this;
  351.     }
  352.     /**
  353.      * Get headerImage
  354.      *
  355.      * @return \Uniski\ResourceBundle\Entity\Image
  356.      */
  357.     public function getHeaderImage()
  358.     {
  359.         return $this->headerImage;
  360.     }
  361.     /**
  362.      * Add product
  363.      *
  364.      * @param \Uniski\CMSBundle\Entity\Product $product
  365.      *
  366.      * @return Offer
  367.      */
  368.     public function addProduct($product)
  369.     {
  370.         $this->products[] = $product;
  371.         return $this;
  372.     }
  373.     /**
  374.      * Remove product
  375.      *
  376.      * @param \Uniski\CMSBundle\Entity\Product $product
  377.      */
  378.     public function removeProduct($product)
  379.     {
  380.         $this->products->removeElement($product);
  381.     }
  382.     /**
  383.      * Get products
  384.      *
  385.      * @return \Doctrine\Common\Collections\Collection
  386.      */
  387.     public function getProducts()
  388.     {
  389.         return $this->products;
  390.     }
  391.     /**
  392.      * Return a single product if the offer has olny one
  393.      * product. This is useful to use in "if" stataments simplify code
  394.      * @return Product Product
  395.      */
  396.     public function getSingleProduct()
  397.     {
  398.         if (count($this->products) == 1) return $this->products[0];
  399.         return null;
  400.     }
  401.     /**
  402.      * Return a single product price if the offer has only one
  403.      * product and this one has a price method.
  404.      * @return float
  405.      */
  406.     public function getSingleProductPrice()
  407.     {
  408.         if (!$this->getSingleProduct()) return null;
  409.         $product $this->getSingleProduct();
  410.         if ($product instanceof Pack &&
  411.             $product->getPrice())
  412.             return $product->getPrice();
  413.         return null;
  414.     }
  415.     /**
  416.      * Set frontpage
  417.      *
  418.      * @param string $frontpage
  419.      *
  420.      * @return Offer
  421.      */
  422.     public function setFrontpage($frontpage)
  423.     {
  424.         $this->frontpage $frontpage;
  425.         return $this;
  426.     }
  427.     /**
  428.      * Get frontpage
  429.      *
  430.      * @return string
  431.      */
  432.     public function getFrontpage()
  433.     {
  434.         return $this->frontpage;
  435.     }
  436.     /**
  437.      * Set price
  438.      *
  439.      * @param string $price
  440.      *
  441.      * @return Offer
  442.      */
  443.     public function setPrice($price)
  444.     {
  445.         $this->price $price;
  446.         return $this;
  447.     }
  448.     /**
  449.      * Get price
  450.      *
  451.      * @return string
  452.      */
  453.     public function getPrice()
  454.     {
  455.         if ($this->price) return $this->price;
  456.         if ($this->getSingleProductPrice())
  457.             return $this->getSingleProductPrice();
  458.         return null;
  459.     }
  460.     /**
  461.      * Set page
  462.      *
  463.      * @param string $page
  464.      *
  465.      * @return Offer
  466.      */
  467.     public function setPage($page)
  468.     {
  469.         $this->page $page;
  470.         return $this;
  471.     }
  472.     /**
  473.      * Get page
  474.      *
  475.      * @return string
  476.      */
  477.     public function getPage()
  478.     {
  479.         return $this->page;
  480.     }
  481.     public function setProducts(ArrayCollection $products)
  482.     {
  483.       $this->products $products;
  484.     }
  485.     /**
  486.      * Set title
  487.      *
  488.      * @param string $link
  489.      *
  490.      * @return Offer
  491.      */
  492.     public function setLink($link)
  493.     {
  494.         $this->link $link;
  495.         return $this;
  496.     }
  497.     /**
  498.      * Get Link
  499.      *
  500.      * @return string
  501.      */
  502.     public function getLink()
  503.     {
  504.         return $this->link;
  505.     }
  506.     /**
  507.      * Set tag
  508.      *
  509.      * @param \Uniski\ConfigBundle\Entity\Category $tag
  510.      *
  511.      * @return Offer
  512.      */
  513.     public function setTag(\Uniski\ConfigBundle\Entity\Category $tag null)
  514.     {
  515.         $this->tag $tag;
  516.         return $this;
  517.     }
  518.     /**
  519.      * Get tag
  520.      *
  521.      * @return \Uniski\ConfigBundle\Entity\Category
  522.      */
  523.     public function getTag()
  524.     {
  525.         return $this->tag;
  526.     }
  527.     /**
  528.      * Set menu
  529.      *
  530.      * @param \Uniski\CMSBundle\Entity\Menu $menu
  531.      *
  532.      * @return Offer
  533.      */
  534.     public function setMenu(\Uniski\CMSBundle\Entity\Menu $menu null)
  535.     {
  536.         $this->menu $menu;
  537.         return $this;
  538.     }
  539.     /**
  540.      * Get menu
  541.      *
  542.      * @return \Uniski\CMSBundle\Entity\Menu
  543.      */
  544.     public function getMenu()
  545.     {
  546.         return $this->menu;
  547.     }
  548.     /**
  549.      * Set created
  550.      *
  551.      * @param \DateTime $created
  552.      *
  553.      * @return Offer
  554.      */
  555.     public function setCreated($created)
  556.     {
  557.         $this->created $created;
  558.         return $this;
  559.     }
  560.     /**
  561.      * Get created
  562.      *
  563.      * @return \DateTime
  564.      */
  565.     public function getCreated()
  566.     {
  567.         return $this->created;
  568.     }
  569.     /**
  570.      * get the pack type product's name if it exists
  571.      * otherwise returns teaser in a single fashion line
  572.      * @return [type] [description]
  573.      */
  574.     public function getFeatures()
  575.     {
  576.         if ($this->getProducts()[0] instanceof Pack) {
  577.             return $this->getProducts()[0]->getName();
  578.         }
  579.         if (!$this->getTeaser()) return "";
  580.         $features $this->getTeaser();
  581.         $features str_replace(['<br>','<br/>','<br />'], ' | '$features);
  582.         $features strip_tags($features);
  583.         return $features;
  584.     }
  585.     public function hasValidPeriod()
  586.     {
  587.         if ($this->period === null) return false;
  588.         if (!isset($this->period['dateIn']) ||
  589.             !isset($this->period['dateOut']) ||
  590.             !isset($this->period['weekdays']))
  591.             return false;
  592.         if ($this->period['dateIn'] == null &&
  593.             empty($this->period['weekdays']) ) return false;
  594.         return true;
  595.     }
  596.     /**
  597.      * Gets the value of updated.
  598.      *
  599.      * @return \DateTime $updated
  600.      */
  601.     public function getUpdated()
  602.     {
  603.         return $this->updated;
  604.     }
  605.     /**
  606.      * Sets the value of updated.
  607.      *
  608.      * @param \DateTime $updated $updated the updated
  609.      *
  610.      * @return self
  611.      */
  612.     public function setUpdated($updated)
  613.     {
  614.         $this->updated $updated;
  615.         return $this;
  616.     }
  617.     public function getCalculatedDescription()
  618.     {
  619.         if ($this->description) return $this->description;
  620.         if (!$this->getSingleProduct()) return "";
  621.         $product $this->getSingleProduct();
  622.         return $product->getDescription() ?: $product->getDestination()->getDescription();
  623.     }
  624.     public function getCalculatedImage()
  625.     {
  626.         
  627.         if ($this->image) return $this->image;
  628.         if (!$this->getSingleProduct()) return null;
  629.         if ($this->getSingleProduct()->getMainImage()) return $this->image;
  630.         return $this->getSingleProduct()->getDestination()->getMainImage();
  631.     }
  632.     /**
  633.      * Gets the value of webform.
  634.      *
  635.      * @return   string
  636.      */
  637.     public function getWebform()
  638.     {
  639.         return $this->webform;
  640.     }
  641.     /**
  642.      * Sets the value of webform.
  643.      *
  644.      * @param   string $webform the webform
  645.      *
  646.      * @return self
  647.      */
  648.     public function setWebform($webform)
  649.     {
  650.         $this->webform $webform;
  651.         return $this;
  652.     }
  653.     /**
  654.      * @return integer
  655.      */
  656.     public function getPriority()
  657.     {
  658.         return $this->priority;
  659.     }
  660.     /**
  661.      * @param integer $priority
  662.      *
  663.      * @return self
  664.      */
  665.     public function setPriority($priority)
  666.     {
  667.         $this->priority $priority;
  668.         return $this;
  669.     }
  670. }