src/Aviatur/EditionBundle/Entity/HomePromo.php line 16

Open in your IDE?
  1. <?php
  2. namespace Aviatur\EditionBundle\Entity;
  3. use Doctrine\ORM\Mapping as ORM;
  4. use Symfony\Component\HttpFoundation\File\UploadedFile;
  5. use Symfony\Component\Validator\Constraints as Assert;
  6. /**
  7.  * HomePromo.
  8.  *
  9.  * @ORM\Table(name="home_promo",  indexes={@ORM\Index(name="home_promo_list_id", columns={"home_promo_list_id"})})
  10.  * @ORM\Entity(repositoryClass="Aviatur\EditionBundle\Entity\HomePromoRepository")
  11.  * @ORM\HasLifecycleCallbacks
  12.  */
  13. class HomePromo
  14. {
  15.     private $em;
  16.     private $temp;
  17.     private $tempSwitch;
  18.     public function __contruct($em)
  19.     {
  20.         $this->em $em;
  21.     }
  22.     /**
  23.      * @var int
  24.      *
  25.      * @ORM\Column(name="id", type="integer")
  26.      * @ORM\Id
  27.      * @ORM\GeneratedValue(strategy="AUTO")
  28.      */
  29.     private $id;
  30.     /**
  31.      * @var string
  32.      *
  33.      * @ORM\Column(name="size", type="string", length=255)
  34.      */
  35.     private $size;
  36.     /**
  37.      * @var bool
  38.      *
  39.      * @ORM\Column(name = "switch", type = "boolean")
  40.      */
  41.     private $switch;
  42.     /**
  43.      * @var bool
  44.      *
  45.      * @ORM\Column(name = "mobile", type = "boolean")
  46.      */
  47.     private $mobile;
  48.     /**
  49.      * @var bool
  50.      *
  51.      * @ORM\Column(name = "toUpdate", type = "boolean", nullable=true)
  52.      */
  53.     private $toUpdate;
  54.     /**
  55.      * @var string
  56.      *
  57.      * @ORM\Column(name="title", type="string", length=255)
  58.      */
  59.     private $title;
  60.     /**
  61.      * @var string
  62.      *
  63.      * @ORM\Column(name="titleSwitch", type="string", length=255, nullable=true)
  64.      */
  65.     private $titleSwitch;
  66.     /**
  67.      * @var string
  68.      *
  69.      * @ORM\Column(name="price", type="string", length=255)
  70.      */
  71.     private $price;
  72.     /**
  73.      * @var string
  74.      *
  75.      * @ORM\Column(name="content", type="text")
  76.      */
  77.     private $content;
  78.     /**
  79.      * @var string
  80.      *
  81.      * @ORM\Column(name="details", type="string", length=255, nullable=true)
  82.      */
  83.     private $details;
  84.     /**
  85.      * @var string
  86.      *
  87.      * @ORM\Column(name="duration", type="string", length=255, nullable=true)
  88.      */
  89.     private $duration;
  90.     /**
  91.      * @var string
  92.      *
  93.      * @ORM\Column(name="path", type="string", length=255)
  94.      */
  95.     private $path;
  96.     /**
  97.      * @Assert\File(maxSize="6000000")
  98.      */
  99.     private ?\Symfony\Component\HttpFoundation\File\UploadedFile $file null;
  100.     /**
  101.      * @Assert\File(maxSize="6000000")
  102.      */
  103.     private ?\Symfony\Component\HttpFoundation\File\UploadedFile $fileSwitch null;
  104.     /**
  105.      * @var string
  106.      *
  107.      * @ORM\Column(name="link", type="string", length=255)
  108.      */
  109.     private $link;
  110.     /**
  111.      * @var string
  112.      *
  113.      * @ORM\Column(name="alt", type="string", length=255)
  114.      */
  115.     private $alt;
  116.     /**
  117.      * @var string
  118.      *
  119.      * @ORM\Column(name="linkSwitch", type="string", length=255)
  120.      */
  121.     private $linkSwitch;
  122.     /**
  123.      * @var int
  124.      *
  125.      * @ORM\Column(name="promoSize", type="integer")
  126.      */
  127.     private $promoSize;
  128.     /**
  129.      * @var \DateTime
  130.      * @ORM\Column(name="date", type="datetime", nullable=true)
  131.      */
  132.     private $date;
  133.     /**
  134.      *
  135.      * @ORM\ManyToOne(targetEntity="HomePromoList", inversedBy="homePromo")
  136.      * @ORM\JoinColumns({
  137.      *   @ORM\JoinColumn(name="home_promo_list_id", referencedColumnName="id")
  138.      * })
  139.      */
  140.     private ?\Aviatur\EditionBundle\Entity\HomePromoList $homePromoList null;
  141.     /**
  142.      * Get id.
  143.      *
  144.      * @return int
  145.      */
  146.     public function getId()
  147.     {
  148.         return $this->id;
  149.     }
  150.     /**
  151.      * Set size.
  152.      *
  153.      * @param string $size
  154.      *
  155.      * @return HomePromo
  156.      */
  157.     public function setSize($size)
  158.     {
  159.         $this->size $size;
  160.         return $this;
  161.     }
  162.     /**
  163.      * Get size.
  164.      *
  165.      * @return string
  166.      */
  167.     public function getSize()
  168.     {
  169.         return $this->size;
  170.     }
  171.     /**
  172.      * Set switch.
  173.      *
  174.      * @param bool $switch
  175.      *
  176.      * @return HomePromo
  177.      */
  178.     public function setSwitch($switch)
  179.     {
  180.         $this->switch $switch;
  181.         return $this;
  182.     }
  183.     /**
  184.      * Get switch.
  185.      *
  186.      * @return bool
  187.      */
  188.     public function getSwitch()
  189.     {
  190.         return $this->switch;
  191.     }
  192.     /**
  193.      * Set mobile.
  194.      *
  195.      * @param bool $mobile
  196.      *
  197.      * @return HomePromo
  198.      */
  199.     public function setMobile($mobile)
  200.     {
  201.         $this->mobile $mobile;
  202.         return $this;
  203.     }
  204.     /**
  205.      * Get mobile.
  206.      *
  207.      * @return bool
  208.      */
  209.     public function getMobile()
  210.     {
  211.         return $this->mobile;
  212.     }
  213.     /**
  214.      * Set title.
  215.      *
  216.      * @param string $title
  217.      *
  218.      * @return HomePromo
  219.      */
  220.     public function setTitle($title)
  221.     {
  222.         $this->title $title;
  223.         return $this;
  224.     }
  225.     /**
  226.      * Get title.
  227.      *
  228.      * @return string
  229.      */
  230.     public function getTitle()
  231.     {
  232.         return $this->title;
  233.     }
  234.     /**
  235.      * Set price.
  236.      *
  237.      * @param int $price
  238.      *
  239.      * @return HomePromo
  240.      */
  241.     public function setPrice($price)
  242.     {
  243.         $this->price $price;
  244.         return $this;
  245.     }
  246.     /**
  247.      * Get price.
  248.      *
  249.      * @return int
  250.      */
  251.     public function getPrice()
  252.     {
  253.         return $this->price;
  254.     }
  255.     /**
  256.      * Set content.
  257.      *
  258.      * @param string $content
  259.      *
  260.      * @return HomePromo
  261.      */
  262.     public function setContent($content)
  263.     {
  264.         $this->content $content;
  265.         return $this;
  266.     }
  267.     /**
  268.      * Get content.
  269.      *
  270.      * @return string
  271.      */
  272.     public function getContent()
  273.     {
  274.         return $this->content;
  275.     }
  276.     /**
  277.      * Set link.
  278.      *
  279.      * @param string $link
  280.      *
  281.      * @return HomePromo
  282.      */
  283.     public function setLink($link)
  284.     {
  285.         $this->link $link;
  286.         return $this;
  287.     }
  288.     /**
  289.      * Get link.
  290.      *
  291.      * @return string
  292.      */
  293.     public function getLink()
  294.     {
  295.         return $this->link;
  296.     }
  297.      /**
  298.      * Set link.
  299.      *
  300.      * @param string $alt
  301.      *
  302.      * @return HomePromo
  303.      */
  304.     public function setAlt($alt)
  305.     {
  306.         $this->alt $alt;
  307.         return $this;
  308.     }
  309.     /**
  310.      * Get alt.
  311.      *
  312.      * @return string
  313.      */
  314.     public function getAlt()
  315.     {
  316.         return $this->alt;
  317.     }
  318.     /**
  319.      * Set promoSize.
  320.      *
  321.      * @param string $promoSize
  322.      *
  323.      * @return HomePromo
  324.      */
  325.     public function setPromoSize($promoSize)
  326.     {
  327.         $this->promoSize $promoSize;
  328.         return $this;
  329.     }
  330.     /**
  331.      * Get officeId.
  332.      *
  333.      * @return string
  334.      */
  335.     public function getPromoSize()
  336.     {
  337.         return $this->promoSize;
  338.     }
  339.     /**
  340.      * Set linkSwitch.
  341.      *
  342.      * @param stringSwitch $linkSwitch
  343.      *
  344.      * @return HomePromo
  345.      */
  346.     public function setLinkSwitch($linkSwitch)
  347.     {
  348.         $this->linkSwitch $linkSwitch;
  349.         return $this;
  350.     }
  351.     /**
  352.      * Get linkSwitch.
  353.      *
  354.      * @return string
  355.      */
  356.     public function getLinkSwitch()
  357.     {
  358.         return $this->linkSwitch;
  359.     }
  360.     /**
  361.      * Set path.
  362.      *
  363.      * @param string $path
  364.      *
  365.      * @return HomePromo
  366.      */
  367.     public function setPath($path)
  368.     {
  369.         $this->path $path;
  370.         return $this;
  371.     }
  372.     /**
  373.      * Get path.
  374.      *
  375.      * @return string
  376.      */
  377.     public function getPath()
  378.     {
  379.         return $this->path;
  380.     }
  381.     /**
  382.      * Set date.
  383.      *
  384.      * @param \DateTime $date
  385.      *
  386.      * @return HomePromo
  387.      */
  388.     public function setDate($date)
  389.     {
  390.         $this->date $date;
  391.         return $this;
  392.     }
  393.     /**
  394.      * Get date.
  395.      *
  396.      * @return \DateTime
  397.      */
  398.     public function getDate()
  399.     {
  400.         return $this->date;
  401.     }
  402.     /**
  403.      * Set homePromoList.
  404.      *
  405.      * @param \Aviatur\EditionBundle\Entity\HomePromoList $homePromoList
  406.      *
  407.      * @return HomePromo
  408.      */
  409.     public function setHomePromoList(\Aviatur\EditionBundle\Entity\HomePromoList $homePromoList null)
  410.     {
  411.         $this->homePromoList $homePromoList;
  412.         return $this;
  413.     }
  414.     /**
  415.      * Get homePromoList.
  416.      *
  417.      * @return \Aviatur\EditionBundle\Entity\HomePromoList
  418.      */
  419.     public function getHomePromoList()
  420.     {
  421.         return $this->homePromoList;
  422.     }
  423.     /**
  424.      * Get file.
  425.      *
  426.      * @return UploadedFile
  427.      */
  428.     public function getFile()
  429.     {
  430.         return $this->file;
  431.     }
  432.     /**
  433.      * Get fileSwitch.
  434.      *
  435.      * @return UploadedFile
  436.      */
  437.     public function getFileSwitch()
  438.     {
  439.         return $this->fileSwitch;
  440.     }
  441.     public function getWebPath()
  442.     {
  443.         return null === $this->path null $this->getUploadDir().'/'.$this->path;
  444.     }
  445.     protected function getUploadRootDir()
  446.     {
  447.         // the absolute directory path where uploaded
  448.         // documents should be saved
  449.         return __DIR__.'/../../../../web/'.$this->getUploadDir();
  450.     }
  451.     protected function getUploadDir()
  452.     {
  453.         // get rid of the __DIR__ so it doesn't screw up
  454.         // when displaying uploaded doc/image in the view.
  455.         return 'assets/'.$this->getHomePromoList()->getAgency()->getAssetsFolder().'_assets/img/promo/box';
  456.     }
  457.     protected function getFileName($link)
  458.     {
  459.         $textTemp explode('/'strtok($link'?'));
  460.         $text is_array($textTemp) ? $textTemp[sizeof($textTemp) - 1] : $textTemp;
  461.         $text = ('' != $text) ? $text null;
  462.         if (preg_match('/.\..[^\.jpeg]/'$text)) {
  463.             $text null;
  464.         }
  465.         $finalName null != $text $text.'_'.$this->id $this->id;
  466.         return $finalName;
  467.     }
  468.     /**
  469.      * Sets file.
  470.      *
  471.      * @param UploadedFile $file
  472.      */
  473.     public function setFile(UploadedFile $file null)
  474.     {
  475.         $this->file $file;
  476.         // check if we have an old image path
  477.         if (is_file($this->getAbsolutePath())) {
  478.             // store the old name to delete after the update
  479.             $this->temp $this->getAbsolutePath();
  480.         } else {
  481.             $this->path 'initial';
  482.         }
  483.     }
  484.     /**
  485.      * Sets fileSwitch.
  486.      *
  487.      * @param UploadedFile $fileSwitch
  488.      */
  489.     public function setFileSwitch(UploadedFile $fileSwitch null)
  490.     {
  491.         $this->fileSwitch $fileSwitch;
  492.         // check if we have an old image path
  493.         if (is_file($this->getAbsolutePathSwitch())) {
  494.             // store the old name to delete after the update
  495.             $this->tempSwitch $this->getAbsolutePathSwitch();
  496.         } else {
  497.             $this->path 'initial';
  498.         }
  499.     }
  500.     /**
  501.      * @ORM\PrePersist()
  502.      * @ORM\PreUpdate()
  503.      */
  504.     public function preUpload()
  505.     {
  506.         if (null !== $this->getFile()) {
  507.             $this->path $this->getFile()->guessExtension();
  508.         }
  509.     }
  510.     /**
  511.      * @ORM\PostPersist()
  512.      * @ORM\PostUpdate()
  513.      */
  514.     public function upload()
  515.     {
  516.         if (null === $this->getFile()) {
  517.             return;
  518.         }
  519.         // check if we have an old image
  520.         if (isset($this->temp)) {
  521.             echo 'entro temp:  ';
  522.             // delete the old image
  523.             unlink($this->temp);
  524.             // clear the temp image path
  525.             $this->temp null;
  526.         }
  527.         // you must throw an exception here if the file cannot be moved
  528.         // so that the entity is not persisted to the database
  529.         // which the UploadedFile move() method does
  530.         $this->getFile()->move(
  531.             $this->getUploadRootDir(),
  532.             $this->getFileName($this->link).'.'.$this->getFile()->guessExtension()
  533.         );
  534.         $this->setFile(null);
  535.     }
  536.     /**
  537.      * @ORM\PostPersist()
  538.      * @ORM\PostUpdate()
  539.      */
  540.     public function uploadName()
  541.     {
  542.         $pathCopy $this->getUploadRootDir().'/'.$this->getFileName($this->link).'.'.$this->path;
  543.         return $pathCopy;
  544.     }
  545.     /**
  546.      * @ORM\PostPersist()
  547.      * @ORM\PostUpdate()
  548.      */
  549.     public function uploadSwitch()
  550.     {
  551.         if (null === $this->getFileSwitch()) {
  552.             return;
  553.         }
  554.         // check if we have an old image
  555.         if (isset($this->tempSwitch)) {
  556.             // delete the old image
  557.             unlink($this->tempSwitch);
  558.             // clear the temp image path
  559.             $this->tempSwitch null;
  560.         }
  561.         // you must throw an exception here if the file cannot be moved
  562.         // so that the entity is not persisted to the database
  563.         // which the UploadedFile move() method does
  564.         $this->getFileSwitch()->move(
  565.             $this->getUploadRootDir(),
  566.             $this->getFileName($this->link).'-2.'.$this->getFileSwitch()->guessExtension()
  567.         );
  568.         $this->setFileSwitch(null);
  569.     }
  570.     /**
  571.      * @ORM\PreRemove()
  572.      */
  573.     public function storeFilenameForRemove()
  574.     {
  575.         $this->temp $this->getAbsolutePath();
  576.         $this->tempSwitch $this->getAbsolutePathSwitch();
  577.     }
  578.     /**
  579.      * @ORM\PostRemove()
  580.      */
  581.     public function removeUpload()
  582.     {
  583.         if (isset($this->temp)) {
  584.             unlink($this->temp);
  585.         }
  586.         if (isset($this->tempSwitch)) {
  587.             unlink($this->tempSwitch);
  588.         }
  589.     }
  590.     public function getAbsolutePath()
  591.     {
  592.         $return null === $this->path null $this->getUploadRootDir().'/'.$this->getFileName($this->link).'.'.$this->path;
  593.         if (!file_exists($return)) {
  594.             $return null === $this->path null $this->getUploadRootDir().'/'.$this->id.'.'.$this->path;
  595.             if (!file_exists($return)) {
  596.                 $return null;
  597.             }
  598.         }
  599.         return $return;
  600.     }
  601.     public function getAbsolutePathSwitch()
  602.     {
  603.         if (true == $this->switch) {
  604.             $return $this->getUploadRootDir().'/'.$this->getFileName($this->link).'-2.'.$this->path;
  605.             if (!file_exists($return)) {
  606.                 $return $this->getUploadRootDir().'/'.$this->id.'-2.'.$this->path;
  607.                 if (!file_exists($return)) {
  608.                     $return null;
  609.                 }
  610.             }
  611.             return $return;
  612.         } else {
  613.             return null;
  614.         }
  615.     }
  616.     /**
  617.      * Set toUpdate.
  618.      *
  619.      * @param bool $toUpdate
  620.      *
  621.      * @return HomePromo
  622.      */
  623.     public function setToUpdate($toUpdate)
  624.     {
  625.         $this->toUpdate $toUpdate;
  626.         return $this;
  627.     }
  628.     /**
  629.      * Get toUpdate.
  630.      *
  631.      * @return bool
  632.      */
  633.     public function getToUpdate()
  634.     {
  635.         return $this->toUpdate;
  636.     }
  637.     /**
  638.      * Set titleSwitch.
  639.      *
  640.      * @param string $titleSwitch
  641.      *
  642.      * @return HomePromo
  643.      */
  644.     public function setTitleSwitch($titleSwitch)
  645.     {
  646.         $this->titleSwitch $titleSwitch;
  647.         return $this;
  648.     }
  649.     /**
  650.      * Get titleSwitch.
  651.      *
  652.      * @return string
  653.      */
  654.     public function getTitleSwitch()
  655.     {
  656.         return $this->titleSwitch;
  657.     }
  658.     /**
  659.      * Set details.
  660.      *
  661.      * @param string $details
  662.      *
  663.      * @return HomePromo
  664.      */
  665.     public function setDetails($details)
  666.     {
  667.         $this->details $details;
  668.         return $this;
  669.     }
  670.     /**
  671.      * Get details.
  672.      *
  673.      * @return string
  674.      */
  675.     public function getDetails()
  676.     {
  677.         return $this->details;
  678.     }
  679.     /**
  680.      * Set duration.
  681.      *
  682.      * @param string $duration
  683.      *
  684.      * @return HomePromo
  685.      */
  686.     public function setDuration($duration)
  687.     {
  688.         $this->duration $duration;
  689.         return $this;
  690.     }
  691.     /**
  692.      * Get duration.
  693.      *
  694.      * @return string
  695.      */
  696.     public function getDuration()
  697.     {
  698.         return $this->duration;
  699.     }
  700. }