src/Entity/ProductPhoto.php line 29

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