src/AdminBundle/Entity/News.php line 203

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