src/AdminBundle/Entity/Images.php line 128

Open in your IDE?
  1. <?php
  2. namespace AdminBundle\Entity;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\ORM\Mapping as ORM;
  5. use Symfony\Component\HttpFoundation\File\UploadedFile;
  6. use Symfony\Component\Validator\Constraints as Assert;
  7. use Symfony\Component\VarDumper\VarDumper;
  8. /**
  9. * Images
  10. */
  11. #[ORM\Table(name: 'images')]
  12. #[ORM\Entity(repositoryClass: \AdminBundle\Repository\ImagesRepository::class)]
  13. #[ORM\HasLifecycleCallbacks]
  14. class Images
  15. {
  16. const IMAGE_UPLOAD_FOLDER = 'upload/tours-objectives/';
  17. /**
  18. * @var int
  19. */
  20. #[ORM\Column(name: 'id', type: 'integer')]
  21. #[ORM\Id]
  22. #[ORM\GeneratedValue(strategy: 'AUTO')]
  23. private $id;
  24. /**
  25. * @var string
  26. */
  27. #[ORM\Column(name: 'title', type: 'string', length: 255, nullable: true)]
  28. private $title;
  29. /**
  30. * @var string
  31. */
  32. #[ORM\Column(name: 'path', type: 'string', length: 255)]
  33. private $path;
  34. /**
  35. * @var string
  36. */
  37. #[ORM\Column(name: 'url', type: 'string', length: 255, nullable: true)]
  38. private $url;
  39. /**
  40. * @var \DateTime
  41. */
  42. #[ORM\Column(name: 'updated', type: 'datetime', nullable: true)]
  43. private $updated;
  44. #[ORM\JoinColumn(name: 'objective', referencedColumnName: 'id')]
  45. #[ORM\ManyToOne(targetEntity: \Objectives::class, inversedBy: 'images', cascade: ['persist', 'remove'])]
  46. private $objectives;
  47. /**
  48. * @var Tours
  49. */
  50. #[ORM\OneToOne(targetEntity: \AdminBundle\Entity\Tours::class, mappedBy: 'image', cascade: ['persist', 'remove'])]
  51. protected $tour;
  52. #[Assert\File(mimeTypes: ['image/png', 'image/jpeg', 'image/jpg', 'image/gif'])]
  53. protected $pictureFile;
  54. /**
  55. * Get id
  56. *
  57. * @return int
  58. */
  59. public function getId()
  60. {
  61. return $this->id;
  62. }
  63. /**
  64. * Set title
  65. *
  66. * @param string $title
  67. *
  68. * @return Images
  69. */
  70. public function setTitle($title)
  71. {
  72. $this->title = $title;
  73. return $this;
  74. }
  75. /**
  76. * Get title
  77. *
  78. * @return string
  79. */
  80. public function getTitle()
  81. {
  82. return $this->title;
  83. }
  84. /**
  85. * Set path
  86. *
  87. * @param string $path
  88. *
  89. * @return Images
  90. */
  91. public function setPath($path)
  92. {
  93. $this->path = $path;
  94. return $this;
  95. }
  96. /**
  97. * Get path
  98. *
  99. * @return string
  100. */
  101. public function getPath()
  102. {
  103. return $this->path;
  104. }
  105. /**
  106. * @param UploadedFile $picture
  107. */
  108. public function setPictureFile(UploadedFile $pictureFile = null)
  109. {
  110. $this->pictureFile = $pictureFile;
  111. }
  112. public function removePictureFile()
  113. {
  114. if ($pictureFile = $this->getPath()) {
  115. @unlink($pictureFile);
  116. }
  117. }
  118. /**
  119. * @return UploadedFile
  120. */
  121. public function getPictureFile()
  122. {
  123. return $this->pictureFile;
  124. }
  125. protected function getUploadFilePath()
  126. {
  127. return self::IMAGE_UPLOAD_FOLDER;
  128. }
  129. public function getPicturePath()
  130. {
  131. if (!$this->title) {
  132. return ;
  133. }
  134. return $this->getUploadFilePath() . $this->title;
  135. }
  136. public function uploadPicture()
  137. {
  138. if (null === $this->getPictureFile()) {
  139. return;
  140. }
  141. $this->removePictureFile();
  142. $filename = sha1(uniqid(mt_rand(), true));
  143. $this->title = $filename.'.'.$this->getPictureFile()->guessExtension();
  144. $this->getPictureFile()->move(
  145. $this->getUploadFilePath(),
  146. $this->title
  147. );
  148. $this->setPath($this->getPicturePath());
  149. $this->setPictureFile(null);
  150. }
  151. #[ORM\PreUpdate]
  152. #[ORM\PrePersist]
  153. public function lifecycleFileUpload()
  154. {
  155. $this->uploadPicture();
  156. }
  157. /**
  158. * Set url
  159. *
  160. * @param string $url
  161. *
  162. * @return Images
  163. */
  164. public function setUrl($url)
  165. {
  166. $this->url = $url;
  167. return $this;
  168. }
  169. /**
  170. * Get url
  171. *
  172. * @return string
  173. */
  174. public function getUrl()
  175. {
  176. return $this->url;
  177. }
  178. /**
  179. * @return Tours
  180. */
  181. public function getTour()
  182. {
  183. return $this->tour;
  184. }
  185. /**
  186. * @param Tours $tour
  187. */
  188. public function setTour($tour)
  189. {
  190. $this->tour = $tour;
  191. }
  192. /**
  193. * @return mixed
  194. */
  195. public function getUpdated()
  196. {
  197. return $this->updated;
  198. }
  199. /**
  200. * @param mixed $updated
  201. */
  202. public function setUpdated($updated)
  203. {
  204. $this->updated = $updated;
  205. }
  206. public function refreshUpdated()
  207. {
  208. $this->setUpdated(new \DateTime());
  209. }
  210. /**
  211. * @return mixed
  212. */
  213. public function getObjectives()
  214. {
  215. return $this->objectives;
  216. }
  217. /**
  218. * @param mixed $objectives
  219. */
  220. public function setObjectives($objectives)
  221. {
  222. $this->objectives = $objectives;
  223. }
  224. }