src/Entity/ProductPhoto.php line 32

Open in your IDE?
  1. <?php
  2. // src/Acme/UserBundle/Entity/User.php
  3. namespace App\Entity;
  4. use Knp\DoctrineBehaviors\Contract\Entity\SoftDeletableInterface;
  5. use Knp\DoctrineBehaviors\Model\SoftDeletable\SoftDeletableTrait;
  6. use Knp\DoctrineBehaviors\Contract\Entity\TimestampableInterface;
  7. use Knp\DoctrineBehaviors\Model\Timestampable\TimestampableTrait;
  8. use Knp\DoctrineBehaviors\Contract\Entity\BlameableInterface;
  9. use Knp\DoctrineBehaviors\Model\Blameable\BlameableTrait;
  10. use Knp\DoctrineBehaviors\Model\Translatable\TranslatableTrait;
  11. use Knp\DoctrineBehaviors\Contract\Entity\TranslatableInterface;
  12. use Doctrine\Common\Collections\ArrayCollection;
  13. use Doctrine\Common\Collections\Collection;
  14. use Symfony\Component\Filesystem\Exception\FileNotFoundException;
  15. use Symfony\Component\Validator\Constraints as Assert;
  16. use Doctrine\ORM\Mapping as ORM;
  17. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  18. use Symfony\Component\HttpFoundation\File\File;
  19. /**
  20.  * @Doctrine\ORM\Mapping\Entity
  21.  * @Doctrine\ORM\Mapping\Table(name="product_photo")
  22.  * @Doctrine\ORM\Mapping\Entity()
  23.  */
  24. class ProductPhoto implements TranslatableInterfaceBlameableInterfaceTimestampableInterfaceSoftDeletableInterface  {
  25.     use BlameableTrait;
  26.     use TimestampableTrait;
  27.     use TranslatableTrait;
  28.     use SoftDeletableTrait;
  29.     
  30. /**
  31.      * @var integer
  32.      *
  33.      * @ORM\Column(name="id", type="integer", nullable=false)
  34.      * @ORM\Id
  35.      * @ORM\GeneratedValue(strategy="IDENTITY")
  36.      */
  37.     private $id;
  38.     /**
  39.      * @ORM\ManyToOne(targetEntity="App\Entity\Product", inversedBy="photos")
  40.      */
  41.     private $product;
  42.     /**
  43.      * @Doctrine\ORM\Mapping\Column(type="boolean", nullable=true)
  44.      */
  45.     protected $main false;
  46.     /**
  47.      * @Doctrine\ORM\Mapping\Column(type="boolean", nullable=true)
  48.      */
  49.     protected $lifestyle false;
  50.     /**
  51.      * @Doctrine\ORM\Mapping\Column(type="boolean", nullable=true)
  52.      */
  53.     protected $hover false;
  54.     /**
  55.      * @var string
  56.      *
  57.      * @ORM\Column(name="name", type="string", length=255, nullable=true)
  58.      */
  59.     private $name;
  60.     /**
  61.      * To which locale photo is associated to
  62.      * @Doctrine\ORM\Mapping\ManyToMany(targetEntity="App\Entity\Language")
  63.      */
  64.     protected $languages;
  65.     /**
  66.      *
  67.      * note This is not a mapped field of entity metadata, just a simple property.
  68.      *
  69.      * @var File $imageFile
  70.      */
  71.     protected $imageFile;
  72.     /**
  73.      * @ORM\Column(type="string", length=255, name="image_name", nullable=true)
  74.      * @var string $imageName
  75.      */
  76.     protected $imageName;
  77.     /**
  78.      * @ORM\Column(type="integer", length=12, name="position", nullable=true)
  79.      * @var string $imageName
  80.      */
  81.     protected $position;
  82.     /**
  83.      * @var string
  84.      *
  85.      * @ORM\Column(name="alt", type="string", length=255, nullable=true)
  86.      */
  87.     private $alt;
  88.     /**
  89.      * @var string
  90.      *
  91.      * @ORM\Column(name="image_custom_name", type="string", length=255, nullable=true)
  92.      */
  93.     private $imageCustomName;
  94.     /**
  95.      * @return string
  96.      */
  97.     public function getPosition(): string
  98.     {
  99.         return $this->position;
  100.     }
  101.     /**
  102.      * @param string $position
  103.      */
  104.     public function setPosition(string $position): void
  105.     {
  106.         $this->position $position;
  107.     }
  108.     public function setImageFile(File $image null)
  109.     {
  110.         $this->imageFile $image;
  111.         if ($image) {
  112.             try {
  113.                 if ($this->getImageCustomName()) {
  114.                     $fileName $this->getImageCustomName();
  115.                 } else {
  116.                     $fileName md5(uniqid()).'.'.$image->guessExtension();
  117.                 }
  118.                 // Move the file to the directory where brochures are stored
  119.                 $brochuresDir getcwd().'/images/product/';
  120.                 $image->move($brochuresDir$fileName);
  121.                 // Update the 'brochure' property to store the PDF file name
  122.                 // instead of its contents
  123.                 $this->setImageName($fileName);
  124.                 $this->updatedAt = new \DateTime('now');
  125.             } catch (\Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException $e) {
  126.             }
  127.         }
  128.         return $this;
  129.     }
  130.     /**
  131.      * @return File
  132.      */
  133.     public function getImageFile()
  134.     {
  135.         return $this->imageFile;
  136.     }
  137.     /**
  138.      * @param string $imageName
  139.      */
  140.     public function setImageName($imageName)
  141.     {
  142.         $this->imageName $imageName;
  143.     }
  144.     /**
  145.      * @return string
  146.      */
  147.     public function getImageName()
  148.     {
  149.         return $this->imageName;
  150.     }
  151.     /**
  152.      * Constructor
  153.      */
  154.     public function __construct()
  155.     {
  156.         $this->languages = new \Doctrine\Common\Collections\ArrayCollection();
  157.     }
  158.     /**
  159.      * Get id
  160.      *
  161.      * @return integer
  162.      */
  163.     public function getId()
  164.     {
  165.         return $this->id;
  166.     }
  167.     /**
  168.      * Set main
  169.      *
  170.      * @param boolean $main
  171.      *
  172.      * @return ProductPhoto
  173.      */
  174.     public function setMain($main)
  175.     {
  176.         $this->main $main;
  177.         return $this;
  178.     }
  179.     /**
  180.      * Get main
  181.      *
  182.      * @return boolean
  183.      */
  184.     public function getMain()
  185.     {
  186.         return $this->main;
  187.     }
  188.     /**
  189.      * Set name
  190.      *
  191.      * @param string $name
  192.      *
  193.      * @return ProductPhoto
  194.      */
  195.     public function setName($name)
  196.     {
  197.         $this->name $name;
  198.         return $this;
  199.     }
  200.     /**
  201.      * Get name
  202.      *
  203.      * @return string
  204.      */
  205.     public function getName()
  206.     {
  207.         return $this->name;
  208.     }
  209.     /**
  210.      * Set product
  211.      *
  212.      * @param \App\Entity\Product $product
  213.      *
  214.      * @return ProductPhoto
  215.      */
  216.     public function setProduct(\App\Entity\Product $product null)
  217.     {
  218.         $this->product $product;
  219.         return $this;
  220.     }
  221.     /**
  222.      * Get product
  223.      *
  224.      * @return \App\Entity\Product
  225.      */
  226.     public function getProduct()
  227.     {
  228.         return $this->product;
  229.     }
  230.     /**
  231.      * Add language
  232.      *
  233.      * @param \App\Entity\Language $language
  234.      *
  235.      * @return ProductPhoto
  236.      */
  237.     public function addLanguage(\App\Entity\Language $language)
  238.     {
  239.         $this->languages[] = $language;
  240.         return $this;
  241.     }
  242.     /**
  243.      * Remove language
  244.      *
  245.      * @param \App\Entity\Language $language
  246.      */
  247.     public function removeLanguage(\App\Entity\Language $language)
  248.     {
  249.         $this->languages->removeElement($language);
  250.     }
  251.     /**
  252.      * Get languages
  253.      *
  254.      * @return \Doctrine\Common\Collections\Collection
  255.      */
  256.     public function getLanguages()
  257.     {
  258.         return $this->languages;
  259.     }
  260.     /**
  261.      * Set hover
  262.      *
  263.      * @param boolean $hover
  264.      *
  265.      * @return ProductPhoto
  266.      */
  267.     public function setHover($hover)
  268.     {
  269.         $this->hover $hover;
  270.         return $this;
  271.     }
  272.     /**
  273.      * Get hover
  274.      *
  275.      * @return boolean
  276.      */
  277.     public function getHover()
  278.     {
  279.         return $this->hover;
  280.     }
  281.     /**
  282.      * Obsługa tłumaczeń
  283.      * @param $method
  284.      * @param $arguments
  285.      * @return mixed
  286.      */
  287.     public function __call($method$arguments)
  288.     {
  289.         return $this->proxyCurrentLocaleTranslation($method$arguments);
  290.     }
  291.     public function getDescription(){
  292.         return $this->translate()->getDescription();
  293.     }
  294.     public function getTranslatedDescription($locale) {
  295.         if ($this->translate($localefalse)->getDescription()) {
  296.             return $this->translate($localefalse)->getDescription();
  297.         } else {
  298.             return '';
  299.         }
  300.     }
  301.     /**
  302.      * @return string
  303.      */
  304.     public function getAlt()
  305.     {
  306.         return $this->alt;
  307.     }
  308.     /**
  309.      * @param string $alt
  310.      */
  311.     public function setAlt($alt)
  312.     {
  313.         $this->alt $alt;
  314.     }
  315.     /**
  316.      * @return string
  317.      */
  318.     public function getImageCustomName()
  319.     {
  320.         return $this->imageCustomName;
  321.     }
  322.     /**
  323.      * @param string $imageCustomName
  324.      */
  325.     public function setImageCustomName($imageCustomName)
  326.     {
  327.         $this->imageCustomName $imageCustomName;
  328.     }
  329.     public function getIsAvailableForLang(Language $language) {
  330.         if ($this->getLanguages()->contains($language)) {
  331.             return true;
  332.         }
  333.         return false;
  334.     }
  335.     /**
  336.      * @return bool
  337.      */
  338.     public function isLifestyle()
  339.     {
  340.         return $this->lifestyle;
  341.     }
  342.     /**
  343.      * @param bool $lifestyle
  344.      */
  345.     public function setLifestyle(bool $lifestyle)
  346.     {
  347.         $this->lifestyle $lifestyle;
  348.     }
  349.     public function getLifestyle(): ?bool
  350.     {
  351.         return $this->lifestyle;
  352.     }
  353. }