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. public $file;
  51. private $temp;
  52. /**
  53. * @ORM\OneToMany(targetEntity="App\Entity\PictureDescription", mappedBy="picture", cascade={"persist", "remove"})
  54. */
  55. private $descriptions = [];
  56. public function __construct() {
  57. $this->descriptions = new \Doctrine\Common\Collections\ArrayCollection();
  58. }
  59. /**
  60. * Get id
  61. *
  62. * @return integer
  63. */
  64. public function getId() {
  65. return $this->id;
  66. }
  67. public function getUrl(): ?string {
  68. if(!empty($this->languageId)){
  69. $description = null;
  70. foreach($this->descriptions as $d){
  71. if($d->getLanguage()->getId()==$this->languageId){
  72. $description = $d;
  73. break;
  74. }
  75. }
  76. }
  77. if(empty($description) && count($this->descriptions)){
  78. $description = $this->descriptions[0];
  79. }
  80. return empty($description)?'':$description->getUrl();
  81. }
  82. public function getSourceName(): ?string {
  83. // return $this->generateFilename()
  84. return $this->sourceName;
  85. }
  86. public function getRank(): ?int {
  87. return $this->rank;
  88. }
  89. public function getDescriptions() {
  90. return $this->descriptions;
  91. }
  92. public function getDescription($lang = 'fr') {
  93. foreach($this->getDescriptions() as $desc){
  94. if($desc->getLanguage()->getCode() == $lang)
  95. return $desc;
  96. }
  97. return null;
  98. }
  99. public function getPictureDescription($lang='fr') {
  100. foreach($this->getDescriptions() as $desc){
  101. if($desc->getLanguage()->getCode() == $lang)
  102. return $desc;
  103. }
  104. return null;
  105. }
  106. public function getTitle() {
  107. return $this->descriptions->first()->getTitle();
  108. }
  109. public function setUrl(?string $url): void {
  110. $this->url = $url;
  111. }
  112. public function setSourceName(?string $sourceName): void {
  113. $this->sourceName = $sourceName;
  114. }
  115. public function setRank(int $rank): void {
  116. $this->rank = $rank;
  117. }
  118. public function setDescriptions($descriptions): void {
  119. $this->descriptions = $descriptions;
  120. }
  121. public function addDescription(PictureDescription $description) {
  122. $this->descriptions->add($description);
  123. }
  124. /**
  125. * @return the $file
  126. */
  127. public function getFile() {
  128. return $this->file;
  129. }
  130. /**
  131. * @param field_type $file
  132. */
  133. public function setFile(UploadedFile $file) {
  134. $this->file = $file;
  135. // check if we have an old image path
  136. if (is_file($this->getAbsolutePath())) {
  137. // store the old name to delete after the update
  138. $this->temp = $this->getAbsolutePath();
  139. $this->url = null;
  140. } else {
  141. $this->url = 'initial';
  142. }
  143. }
  144. public function getAbsolutePath($lang = 'fr') {
  145. $desc = $this->getDescription('fr');
  146. if($desc)
  147. return $this->getUploadRootDir() . '/' . $desc->getUrl();
  148. return null === $this->url ? null : $this->getUploadRootDir() . '/' . $this->url;
  149. }
  150. public function getWebPath() {
  151. if (empty($this->url))
  152. return $this->url;
  153. if (substr($this->url, 0, 1) != '/')
  154. return '/' . trim($this->url);
  155. return trim($this->url);
  156. }
  157. public function getThumbnailPath() {
  158. return null === $this->thumbnailUrl ? null : $this->getUploadDir() . $this->thumbnailUrl;
  159. }
  160. public function getUploadRootDir() {
  161. // le chemin absolu du répertoire où les documents uploadés doivent être sauvegardés
  162. return __DIR__ . '/../../public/' . $this->getUploadDir();
  163. }
  164. public function getUploadDir() {
  165. return 'assets/products/';
  166. }
  167. public function generateFilename($url = '', $ext = '') {
  168. // $ext = \App\Helpers\Encoder::getExtension($this->getSourceName());
  169. if($url == ''){
  170. $title = $this->formatUrl($this->descriptions->first()->getUrl());
  171. }else{
  172. $title = \App\Helpers\Encoder::formatUrl($url);
  173. }
  174. if (!empty($title)) {
  175. $tmp = $title;
  176. } else {
  177. $tmp = sha1(uniqid(mt_rand(), true));
  178. }
  179. $cpt = 0;
  180. $f_name = $tmp . '.' . $ext;
  181. while (file_exists($this->getUploadRootDir() . $this->getUploadDir() . $f_name . '.' . $ext)) {
  182. $cpt++;
  183. $f_name = $tmp . ($cpt ? ('-' . $cpt) : '');
  184. }
  185. return $f_name;
  186. }
  187. private function formatUrl($str, $replace = array(), $delimiter = '-') {
  188. if (!empty($replace)) {
  189. $str = str_replace((array) $replace, ' ', $str);
  190. }
  191. $clean = iconv('UTF-8', 'ASCII//TRANSLIT', $str);
  192. $clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean);
  193. $clean = strtolower(trim($clean, '-'));
  194. $clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean);
  195. return $clean;
  196. }
  197. /**
  198. * @ORM\PrePersist()
  199. * @ORM\PreUpdate()
  200. */
  201. public function preUpload() {
  202. if (null !== $this->file) {
  203. $this->url = $this->generateFilename();
  204. }
  205. }
  206. /**
  207. * @ORM\PostPersist()
  208. * @ORM\PostUpdate()
  209. */
  210. public function upload() {
  211. if (null === $this->file) {
  212. return;
  213. }
  214. if (isset($this->temp)) {
  215. //unlink($this->temp);
  216. $this->temp = null;
  217. }
  218. }
  219. }