src/Entity/Picture.php line 25

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\ORM\Mapping as ORM;
  4. use Symfony\Component\Validator\Constraints as Assert;
  5. use Symfony\Component\HttpFoundation\File\File;
  6. use Symfony\Component\HttpFoundation\File\UploadedFile;
  7. /**
  8. * Picture
  9. *
  10. * @ORM\Table(name="pictures")
  11. * @ORM\Entity(repositoryClass="App\Repository\PictureRepository")
  12. * @ORM\InheritanceType("JOINED")
  13. * @ORM\DiscriminatorColumn(name="type", type="string")
  14. * @ORM\DiscriminatorMap(
  15. * {
  16. * "product" = "PictureProduct",
  17. * "category" = "PictureCategory"
  18. * }
  19. * )
  20. * @ORM\HasLifecycleCallbacks
  21. */
  22. abstract class Picture extends TranslatedEntity {
  23. protected $tranlatedEntity = 'PictureDescription';
  24. /**
  25. * @var integer
  26. *
  27. * @ORM\Column(name="id", type="integer")
  28. * @ORM\Id
  29. * @ORM\GeneratedValue(strategy="AUTO")
  30. */
  31. private $id;
  32. /**
  33. * @var string
  34. *
  35. * @ORM\Column(name="url", type="string", length=255, nullable=true)
  36. */
  37. private $url;
  38. /**
  39. * @var string
  40. *
  41. * @ORM\Column(name="source_name", type="string", length=255, nullable=true)
  42. */
  43. private $sourceName;
  44. /**
  45. * @var integer
  46. *
  47. * @ORM\Column(name="`rank`", type="integer", nullable=true)
  48. */
  49. private $rank;
  50. /**
  51. * @var string
  52. *
  53. * @ORM\Column(name="`code`", type="string", nullable=true)
  54. */
  55. private $code;
  56. /**
  57. * @var string
  58. *
  59. * @ORM\Column(name="format", type="string", nullable=true)
  60. */
  61. private $format;
  62. public $file;
  63. private $temp;
  64. /**
  65. * @ORM\OneToMany(targetEntity="App\Entity\PictureDescription", mappedBy="picture", cascade={"persist", "remove"})
  66. */
  67. private $descriptions = [];
  68. public function __construct() {
  69. $this->descriptions = new \Doctrine\Common\Collections\ArrayCollection();
  70. }
  71. /**
  72. * Get id
  73. *
  74. * @return integer
  75. */
  76. public function getId() {
  77. return $this->id;
  78. }
  79. public function getUrl(): ?string {
  80. $tmp = null;
  81. // if(!empty($this->languageId)){
  82. // $description = null;
  83. // foreach($this->descriptions as $d){
  84. // if($d->getLanguage()->getId()==$this->languageId){
  85. // $description = $d;
  86. // break;
  87. // }
  88. // }
  89. // }
  90. // if(empty($description) && count($this->descriptions)){
  91. // $description = $this->descriptions[0];
  92. // $tmp = $description->getUrl();
  93. // }
  94. return empty($tmp)?$this->url:$tmp;
  95. }
  96. public function getSourceName(): ?string {
  97. // return $this->generateFilename()
  98. return $this->sourceName;
  99. }
  100. public function getRank(): ?int {
  101. return $this->rank;
  102. }
  103. public function getDescriptions() {
  104. return $this->descriptions;
  105. }
  106. public function getDescription($lang = 'fr') {
  107. foreach($this->getDescriptions() as $desc){
  108. if($desc->getLanguage()->getCode() == $lang)
  109. return $desc;
  110. }
  111. return null;
  112. }
  113. public function getPictureDescription($lang='fr') {
  114. foreach($this->getDescriptions() as $desc){
  115. if($desc->getLanguage()->getCode() == $lang)
  116. return $desc;
  117. }
  118. return null;
  119. }
  120. public function getTitle() {
  121. return $this->descriptions->first()->getTitle();
  122. }
  123. public function getAlt() {
  124. return $this->descriptions->first()->getAlt();
  125. }
  126. public function getCode(): string {
  127. return $this->code;
  128. }
  129. public function getFormat(): string {
  130. return $this->format;
  131. }
  132. public function setCode(string $code): void {
  133. $this->code = $code;
  134. }
  135. public function setFormat(string $format): void {
  136. $this->format = $format;
  137. }
  138. public function setUrl(?string $url): void {
  139. $this->url = $url;
  140. }
  141. public function setSourceName(?string $sourceName): void {
  142. $this->sourceName = $sourceName;
  143. }
  144. public function setRank(int $rank): void {
  145. $this->rank = $rank;
  146. }
  147. public function setDescriptions($descriptions): void {
  148. $this->descriptions = $descriptions;
  149. }
  150. public function addDescription(PictureDescription $description) {
  151. $this->descriptions->add($description);
  152. }
  153. /**
  154. * @return the $file
  155. */
  156. public function getFile() {
  157. return $this->file;
  158. }
  159. /**
  160. * @param field_type $file
  161. */
  162. public function setFile(UploadedFile $file) {
  163. $this->file = $file;
  164. // check if we have an old image path
  165. if (is_file($this->getAbsolutePath())) {
  166. // store the old name to delete after the update
  167. $this->temp = $this->getAbsolutePath();
  168. $this->url = null;
  169. } else {
  170. $this->url = 'initial';
  171. }
  172. }
  173. public function getAbsolutePath($lang = 'fr') {
  174. $desc = $this->getDescription('fr');
  175. if($desc)
  176. return $this->getUploadRootDir() . '/' . $desc->getUrl();
  177. return null === $this->url ? null : $this->getUploadRootDir() . '/' . $this->url;
  178. }
  179. public function getWebPath() {
  180. if (empty($this->url))
  181. return $this->url;
  182. if (substr($this->url, 0, 1) != '/')
  183. return '/' . trim($this->url);
  184. return trim($this->url);
  185. }
  186. public function getThumbnailPath() {
  187. return null === $this->thumbnailUrl ? null : $this->getUploadDir() . $this->thumbnailUrl;
  188. }
  189. public function getUploadRootDir() {
  190. // le chemin absolu du répertoire où les documents uploadés doivent être sauvegardés
  191. return __DIR__ . '/../../public/' . $this->getUploadDir();
  192. }
  193. public function getUploadDir() {
  194. return 'assets/products/';
  195. }
  196. public function generateFilename($url = '', $ext = '') {
  197. // $ext = \App\Helpers\Encoder::getExtension($this->getSourceName());
  198. if($url == ''){
  199. $title = $this->formatUrl($this->descriptions->first()->getUrl());
  200. }else{
  201. $title = \App\Helpers\Encoder::formatUrl($url);
  202. }
  203. if (!empty($title)) {
  204. $tmp = $title;
  205. } else {
  206. $tmp = sha1(uniqid(mt_rand(), true));
  207. }
  208. $cpt = 0;
  209. $f_name = $tmp . '.' . $ext;
  210. while (file_exists($this->getUploadRootDir() . $this->getUploadDir() . $f_name . '.' . $ext)) {
  211. $cpt++;
  212. $f_name = $tmp . ($cpt ? ('-' . $cpt) : '');
  213. }
  214. return $f_name;
  215. }
  216. private function formatUrl($str, $replace = array(), $delimiter = '-') {
  217. if (!empty($replace)) {
  218. $str = str_replace((array) $replace, ' ', $str);
  219. }
  220. $clean = iconv('UTF-8', 'ASCII//TRANSLIT', $str);
  221. $clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean);
  222. $clean = strtolower(trim($clean, '-'));
  223. $clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean);
  224. return $clean;
  225. }
  226. public function toArray() : array{
  227. $parent = $this->getParent();
  228. $manufacturer = $this->getManufacturer();
  229. $output = [
  230. 'id' => $this->getId(),
  231. 'code' => $this->getCode(),
  232. 'url' => $this->getWebPath(),
  233. ];
  234. foreach($this->getPictures() as $picture){
  235. $output['pictures'][] = $picture->toArray();
  236. }
  237. return $output;
  238. }
  239. /**
  240. * @ORM\PrePersist()
  241. * @ORM\PreUpdate()
  242. */
  243. public function preUpload() {
  244. if (null !== $this->file) {
  245. $this->url = $this->generateFilename();
  246. }
  247. }
  248. /**
  249. * @ORM\PostPersist()
  250. * @ORM\PostUpdate()
  251. */
  252. public function upload() {
  253. if (null === $this->file) {
  254. return;
  255. }
  256. if (isset($this->temp)) {
  257. //unlink($this->temp);
  258. $this->temp = null;
  259. }
  260. }
  261. }