src/AdminBundle/Entity/MobileAds.php line 172

Open in your IDE?
  1. <?php
  2. namespace AdminBundle\Entity;
  3. use DateTime;
  4. use Doctrine\ORM\Mapping as ORM;
  5. use Symfony\Component\HttpFoundation\File\UploadedFile;
  6. use Symfony\Component\Validator\Constraints as Assert;
  7. /**
  8. * MobileAds
  9. */
  10. #[ORM\Table(name: 'mobile_ads')]
  11. #[ORM\Entity(repositoryClass: \AdminBundle\Repository\MobileAdsRepository::class)]
  12. #[ORM\HasLifecycleCallbacks]
  13. class MobileAds
  14. {
  15. const TYPE_CLIENT = 0;
  16. const TYPE_DRIVER = 1;
  17. const TYPES = [
  18. self::TYPE_CLIENT => 'Client',
  19. self::TYPE_DRIVER => 'Driver',
  20. ];
  21. const FILE_FOLDER = 'upload/mobile-ads/';
  22. /**
  23. * @var int
  24. */
  25. #[ORM\Column(name: 'id', type: 'integer')]
  26. #[ORM\Id]
  27. #[ORM\GeneratedValue(strategy: 'AUTO')]
  28. private $id;
  29. /**
  30. * @var string
  31. */
  32. #[ORM\Column(name: 'title', type: 'string', length: 255)]
  33. protected $title;
  34. /**
  35. * @var string
  36. */
  37. #[ORM\Column(name: 'description', type: 'text', nullable: true)]
  38. protected $description;
  39. /**
  40. * @var int
  41. */
  42. #[ORM\Column(name: 'type', type: 'integer', nullable: false)]
  43. protected $type;
  44. /**
  45. * @var string
  46. */
  47. #[ORM\Column(name: 'picture', type: 'string', length: 255, nullable: true)]
  48. private $picture;
  49. /**
  50. * @var UploadedFile
  51. */
  52. #[Assert\File(mimeTypes: ['image/png', 'image/jpeg', 'image/jpg', 'image/gif'])]
  53. private $pictureFile;
  54. /**
  55. * @var string
  56. */
  57. #[ORM\Column(name: 'url', type: 'string', length: 255, nullable: true)]
  58. protected $url;
  59. /**
  60. * @var DateTime
  61. */
  62. #[ORM\Column(name: 'start_date', type: 'datetime')]
  63. protected $startDate;
  64. /**
  65. * @var DateTime
  66. */
  67. #[ORM\Column(name: 'end_date', type: 'datetime')]
  68. protected $endDate;
  69. /**
  70. * @var DateTime
  71. */
  72. #[ORM\Column(name: 'updated_at', type: 'datetime')]
  73. protected $updatedAt;
  74. public function __construct()
  75. {
  76. $this->updatedAt = new DateTime();
  77. }
  78. /**
  79. * Get id
  80. *
  81. * @return int
  82. */
  83. public function getId()
  84. {
  85. return $this->id;
  86. }
  87. /**
  88. * Set title
  89. *
  90. * @param string $title
  91. *
  92. * @return MobileAds
  93. */
  94. public function setTitle($title)
  95. {
  96. $this->title = $title;
  97. return $this;
  98. }
  99. /**
  100. * Get title
  101. *
  102. * @return string
  103. */
  104. public function getTitle()
  105. {
  106. return $this->title;
  107. }
  108. /**
  109. * @return string
  110. */
  111. public function getDescription()
  112. {
  113. return $this->description;
  114. }
  115. /**
  116. * @param string $description
  117. *
  118. * @return MobileAds
  119. */
  120. public function setDescription($description)
  121. {
  122. $this->description = $description;
  123. return $this;
  124. }
  125. /**
  126. * @return int
  127. */
  128. public function getType()
  129. {
  130. return $this->type;
  131. }
  132. /**
  133. * @param int $type
  134. *
  135. * @return MobileAds
  136. */
  137. public function setType($type)
  138. {
  139. $this->type = $type;
  140. return $this;
  141. }
  142. /**
  143. * @param UploadedFile $pictureFile
  144. */
  145. public function setPictureFile(UploadedFile $pictureFile = null)
  146. {
  147. $this->pictureFile = $pictureFile;
  148. return $this;
  149. }
  150. /**
  151. * @return UploadedFile
  152. */
  153. public function getPictureFile()
  154. {
  155. return $this->pictureFile;
  156. }
  157. public function getUploadFilePath()
  158. {
  159. if (!is_dir(self::FILE_FOLDER . $this->id)) {
  160. mkdir(self::FILE_FOLDER . $this->id, 0777, true);
  161. }
  162. return self::FILE_FOLDER . $this->id . '/';
  163. }
  164. public function getPictureFilePath()
  165. {
  166. if (!$this->picture) {
  167. return;
  168. }
  169. return $this->getUploadFilePath() . $this->picture;
  170. }
  171. public function uploadFile()
  172. {
  173. $pictureFile = $this->getPictureFile();
  174. if (!$pictureFile) {
  175. return;
  176. }
  177. $this->removePictureFile();
  178. $nDate = new DateTime();
  179. $filename = 'MOBILE_ADS_FILE_' . $nDate->format('Y-m-d-His');
  180. $this->picture = $filename . '.' . $pictureFile->guessExtension();
  181. $pictureFile->move(
  182. $this->getUploadFilePath(),
  183. $this->picture
  184. );
  185. $this->setPictureFile(null);
  186. }
  187. #[ORM\PreUpdate]
  188. #[ORM\PrePersist]
  189. public function lifecycleFileUpload()
  190. {
  191. $this->uploadFile();
  192. }
  193. #[ORM\PostRemove]
  194. public function removeUpload()
  195. {
  196. $this->removePictureFile();
  197. }
  198. #[ORM\PostPersist]
  199. public function onPostPersist()
  200. {
  201. rename(
  202. self::FILE_FOLDER . $this->picture,
  203. $this->getUploadFilePath() . $this->picture
  204. );
  205. }
  206. public function removePictureFile()
  207. {
  208. if ($pictureFile = $this->getPictureFilePath()) {
  209. @unlink($pictureFile);
  210. }
  211. }
  212. /**
  213. * Set url
  214. *
  215. * @param string $url
  216. *
  217. * @return MobileAds
  218. */
  219. public function setUrl($url)
  220. {
  221. $this->url = $url;
  222. return $this;
  223. }
  224. /**
  225. * Get url
  226. *
  227. * @return string
  228. */
  229. public function getUrl()
  230. {
  231. return $this->url;
  232. }
  233. /**
  234. * @return DateTime
  235. */
  236. public function getStartDate()
  237. {
  238. return $this->startDate;
  239. }
  240. /**
  241. * @param DateTime $startDate
  242. *
  243. * @return MobileAds
  244. */
  245. public function setStartDate(DateTime $startDate)
  246. {
  247. $this->startDate = $startDate;
  248. return $this;
  249. }
  250. /**
  251. * @return DateTime
  252. */
  253. public function getEndDate()
  254. {
  255. return $this->endDate;
  256. }
  257. /**
  258. * @param DateTime $endDate
  259. *
  260. * @return MobileAds
  261. */
  262. public function setEndDate(DateTime $endDate)
  263. {
  264. $this->endDate = $endDate;
  265. return $this;
  266. }
  267. /**
  268. * @return DateTime
  269. */
  270. public function getUpdatedAt()
  271. {
  272. return $this->updatedAt;
  273. }
  274. /**
  275. * @param DateTime $updatedAt
  276. *
  277. * @return MobileAds
  278. */
  279. public function setUpdatedAt(DateTime $updatedAt)
  280. {
  281. $this->updatedAt = $updatedAt;
  282. return $this;
  283. }
  284. }