src/Entity/ModuleCarouselItem.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\ORM\Mapping as ORM;
  4. use Symfony\Component\HttpFoundation\File\File;
  5. use Symfony\Component\HttpFoundation\File\UploadedFile;
  6. /**
  7. * ModuleTextItem
  8. *
  9. * @ORM\Table(name="module_carousel_items")
  10. * @ORM\Entity(repositoryClass="App\Repository\ModuleCarouselItemRepository")
  11. * @ORM\HasLifecycleCallbacks
  12. */
  13. class ModuleCarouselItem {
  14. /**
  15. * @var integer
  16. *
  17. * @ORM\Column(name="id", type="integer")
  18. * @ORM\Id
  19. * @ORM\GeneratedValue(strategy="AUTO")
  20. */
  21. private $id;
  22. /**
  23. * @var string
  24. *
  25. * @ORM\Column(name="img_fr", type="text", nullable=true)
  26. */
  27. private $imgFr;
  28. /**
  29. * @var string
  30. *
  31. * @ORM\Column(name="img_mobile_fr", type="text", nullable=true)
  32. */
  33. private $imgMobileFr;
  34. /**
  35. * @var string
  36. *
  37. * @ORM\Column(name="url_fr", type="text", nullable=true)
  38. */
  39. private $urlFr;
  40. /**
  41. * @var string
  42. *
  43. * @ORM\Column(name="img_en", type="text", nullable=true)
  44. */
  45. private $imgEn;
  46. /**
  47. * @var string
  48. *
  49. * @ORM\Column(name="img_mobile_en", type="text", nullable=true)
  50. */
  51. private $imgMobileEn;
  52. /**
  53. * @var string
  54. *
  55. * @ORM\Column(name="url_en", type="text", nullable=true)
  56. */
  57. private $urlEn;
  58. /**
  59. * @var integer
  60. *
  61. * @ORM\Column(name="order_num", type="integer")
  62. */
  63. private $orderNum;
  64. /**
  65. * @var integer
  66. *
  67. * @ORM\ManyToOne(targetEntity="ModuleCarousel", inversedBy="items", cascade={"persist"})
  68. */
  69. private $moduleCarousel;
  70. /**
  71. * @ORM\ManyToOne(targetEntity="App\Entity\PriceGroup")
  72. * @ORM\JoinColumn(name="price_group_id", referencedColumnName="id", nullable=true, onDelete="CASCADE")
  73. */
  74. private $priceContext;
  75. public $fileFr;
  76. public $fileMobileFr;
  77. public $fileEn;
  78. public $fileMobileEn;
  79. private $tempEn;
  80. private $tempMobileEn;
  81. private $tempFr;
  82. private $tempMobileFr;
  83. protected $widthFr;
  84. protected $heightFr;
  85. protected $mobileFrWidth;
  86. protected $mobileFrHeight;
  87. protected $widthEn;
  88. protected $heightEn;
  89. protected $mobileEnWidth;
  90. protected $mobileEnHeight;
  91. public function getId() {
  92. return $this->id;
  93. }
  94. public function getImgFr(): ?string {
  95. return $this->imgFr;
  96. }
  97. public function getImgMobileFr(): ?string {
  98. return $this->imgMobileFr;
  99. }
  100. public function getUrlFr(): ?string {
  101. return $this->urlFr;
  102. }
  103. public function getImgEn(): ?string {
  104. return $this->imgEn;
  105. }
  106. public function getImgMobileEn(): ?string {
  107. return $this->imgMobileEn;
  108. }
  109. public function getUrlEn(): ?string {
  110. return $this->urlEn;
  111. }
  112. public function getOrderNum() {
  113. return $this->orderNum;
  114. }
  115. public function getModuleCarousel() {
  116. return $this->moduleCarousel;
  117. }
  118. public function getFile() {
  119. return $this->file;
  120. }
  121. public function getFileMobile() {
  122. return $this->fileMobile;
  123. }
  124. public function getTemp() {
  125. return $this->temp;
  126. }
  127. public function getTempMobile() {
  128. return $this->tempMobile;
  129. }
  130. public function getWidth() {
  131. return $this->width;
  132. }
  133. public function getHeight() {
  134. return $this->height;
  135. }
  136. public function getMobileWidth() {
  137. return $this->mobileWidth;
  138. }
  139. public function getMobileHeight() {
  140. return $this->mobileHeight;
  141. }
  142. public function setId($id): void {
  143. $this->id = $id;
  144. }
  145. public function getPriceContext() {
  146. return $this->priceContext;
  147. }
  148. public function setImgFr(string $imgFr): void {
  149. $this->imgFr = $imgFr;
  150. }
  151. public function setImgMobileFr(string $imgMobileFr): void {
  152. $this->imgMobileFr = $imgMobileFr;
  153. }
  154. public function setUrlFr(string $urlFr): void {
  155. $this->urlFr = $urlFr;
  156. }
  157. public function setImgEn(string $imgEn): void {
  158. $this->imgEn = $imgEn;
  159. }
  160. public function setImgMobileEn(string $imgMobileEn): void {
  161. $this->imgMobileEn = $imgMobileEn;
  162. }
  163. public function setUrlEn(string $urlEn): void {
  164. $this->urlEn = $urlEn;
  165. }
  166. public function setOrderNum($orderNum): void {
  167. $this->orderNum = $orderNum;
  168. }
  169. public function setModuleCarousel($moduleCarousel): void {
  170. $this->moduleCarousel = $moduleCarousel;
  171. }
  172. public function setFile($file): void {
  173. $this->file = $file;
  174. }
  175. public function setFileMobile($fileMobile): void {
  176. $this->fileMobile = $fileMobile;
  177. }
  178. public function setTemp($temp): void {
  179. $this->temp = $temp;
  180. }
  181. public function setTempMobile($tempMobile): void {
  182. $this->tempMobile = $tempMobile;
  183. }
  184. public function setWidth($width): void {
  185. $this->width = $width;
  186. }
  187. public function setHeight($height): void {
  188. $this->height = $height;
  189. }
  190. public function setMobileWidth($mobileWidth): void {
  191. $this->mobileWidth = $mobileWidth;
  192. }
  193. public function setMobileHeight($mobileHeight): void {
  194. $this->mobileHeight = $mobileHeight;
  195. }
  196. public function setPriceContext(PriceGroup $priceContext): void {
  197. $this->priceContext = $priceContext;
  198. }
  199. public function matchPriceContext(?PriceGroup $priceContext) {
  200. return empty($this->priceContext) || ($priceContext == $this->priceContext);
  201. }
  202. public function getUpdatedAt($type = 'desktop') {
  203. $dir = __DIR__ . '/../../../public/';
  204. if($type == 'desktop'){
  205. return filemtime($dir.$this->imgFr);
  206. }
  207. if($type == 'mobile'){
  208. return filemtime($dir.$this->imgMobileFr);
  209. }
  210. return '';
  211. }
  212. public function getUploadDir() {
  213. return 'assets/modules/carousel/';
  214. }
  215. public function getUploadRootDir() {
  216. // le chemin absolu du répertoire où les documents uploadés doivent être sauvegardés
  217. return __DIR__ . '/../../public/' . $this->getUploadDir();
  218. }
  219. /**
  220. * @ORM\PrePersist()
  221. * @ORM\PreUpdate()
  222. */
  223. public function preUpload() {
  224. if (!empty($this->fileFr)) {
  225. $this->imgFr = $this->generateFilename($this->fileFr);
  226. }
  227. if (!empty($this->fileMobileFr)) {
  228. $this->imgMobileFr = $this->generateFilename($this->fileMobileFr,'-mobile');
  229. }
  230. if (!empty($this->fileEn)) {
  231. $this->imgEn = $this->generateFilename($this->fileEn);
  232. }
  233. if (!empty($this->fileMobileEn)) {
  234. $this->imgMobileEn = $this->generateFilename($this->fileMobileEn,'-mobile');
  235. }
  236. }
  237. /**
  238. * @ORM\PostPersist()
  239. * @ORM\PostUpdate()
  240. */
  241. public function upload()
  242. {
  243. if (null !== $this->fileFr) {
  244. if (isset($this->tempFr)) {
  245. $this->tempFr = null;
  246. }
  247. $this->fileFr->move($this->getUploadRootDir(), $this->imgFr);
  248. unset($this->fileFr);
  249. }
  250. if (null !== $this->fileMobileFr) {
  251. if (isset($this->tempMobileFr)) {
  252. $this->tempMobileFr = null;
  253. }
  254. $this->fileMobileFr->move($this->getUploadRootDir(), $this->imgMobileFr);
  255. unset($this->fileMobileFr);
  256. }
  257. if (null !== $this->fileEn) {
  258. if (isset($this->tempEn)) {
  259. $this->tempEn = null;
  260. }
  261. $this->fileEn->move($this->getUploadRootDir(), $this->imgEn);
  262. unset($this->fileEn);
  263. }
  264. if (null !== $this->fileMobileEn) {
  265. if (isset($this->tempMobileEn)) {
  266. $this->tempMobileEn = null;
  267. }
  268. $this->fileMobileEn->move($this->getUploadRootDir(), $this->imgMobileEn);
  269. unset($this->fileMobileEn);
  270. }
  271. }
  272. /**
  273. * @ORM\PostLoad()
  274. */
  275. public function postLoad()
  276. {
  277. if(is_file($this->imgFr)){
  278. $infos = getimagesize($this->imgFr);
  279. if(is_array($infos)){
  280. $this->widthFr = isset($infos[0])?$infos[0]:0;
  281. $this->heightFr = isset($infos[1])?$infos[1]:0;
  282. }
  283. }
  284. if(is_file($this->imgMobileFr)){
  285. $infos = getimagesize($this->imgMobileFr);
  286. if(is_array($infos)){
  287. $this->mobileFrWidth = isset($infos[0])?$infos[0]:0;
  288. $this->mobileFrHeight = isset($infos[1])?$infos[1]:0;
  289. }
  290. }
  291. if(is_file($this->imgEn)){
  292. $infos = getimagesize($this->imgEn);
  293. if(is_array($infos)){
  294. $this->widthEn = isset($infos[0])?$infos[0]:0;
  295. $this->heightEn = isset($infos[1])?$infos[1]:0;
  296. }
  297. }
  298. if(is_file($this->imgMobileEn)){
  299. $infos = getimagesize($this->imgMobileEn);
  300. if(is_array($infos)){
  301. $this->mobileEnWidth = isset($infos[0])?$infos[0]:0;
  302. $this->mobileEnHeight = isset($infos[1])?$infos[1]:0;
  303. }
  304. }
  305. }
  306. private function generateFilename($file,$suffix='') {
  307. // $title = $this->title;
  308. // if (!empty($title)) {
  309. // $tmp = $this->getUploadDir() . \App\Helpers\CustomEncoder::formatPictureUrl($title);
  310. // } else {
  311. $tmp = $this->getUploadDir() . sha1(uniqid(mt_rand(), true));
  312. // }
  313. $cpt = 0;
  314. //$suffix = '-v2';
  315. $f_name = $tmp . $suffix . '.' . $file->guessExtension();
  316. while (file_exists($this->getUploadRootDir() . $f_name)) {
  317. $cpt++;
  318. $f_name = $tmp . ($cpt ? ('-' . $cpt) : '') . $suffix . '.' . $file->guessExtension();
  319. }
  320. return $f_name;
  321. }
  322. public function isGif() {
  323. $ext = strtolower(substr($this->getImg(), -3));
  324. return $ext == 'gif';
  325. }
  326. }