Advertisement
Guest User

marie lddt User Entity

a guest
Apr 18th, 2014
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Lddt\UserBundle\Entity;
  4.  
  5. use Doctrine\ORM\Mapping as ORM;
  6. use FOS\UserBundle\Entity\User as BaseUser;
  7.  
  8. use Symfony\Component\Validator\Constraints as Assert;
  9. use Symfony\Component\HttpFoundation\File\UploadedFile;
  10.  
  11. /**
  12. * User
  13. *
  14. * @ORM\Table()
  15. * @ORM\Entity(repositoryClass="Lddt\UserBundle\Entity\UserRepository")
  16. * @ORM\HasLifecycleCallbacks
  17. */
  18. class User extends BaseUser
  19. {
  20. /**
  21. * @var integer
  22. *
  23. * @ORM\Column(name="id", type="integer")
  24. * @ORM\Id
  25. * @ORM\GeneratedValue(strategy="AUTO")
  26. */
  27. protected $id;
  28.  
  29. /**
  30. * @var string
  31. *
  32. * @ORM\Column(name="authorAvatarPath", type="string", length=255, nullable=true)
  33. */
  34. private $authorAvatarPath;
  35.  
  36.  
  37. /**
  38. *
  39. * @Assert\File(
  40. * maxSize="1024k",
  41. * maxSizeMessage="Votre fichier ne doit pas dépasser 1Mo",
  42. * mimeTypes ={"image/jpeg","image/png","image/gif"},
  43. * mimeTypesMessage = "Merci d'uploader un fichier jpg, png ou gif"
  44. * )
  45. */
  46. private $avatarFile;
  47.  
  48. /**
  49. * Get id
  50. *
  51. * @return integer
  52. */
  53. public function getId()
  54. {
  55. return $this->id;
  56. }
  57.  
  58. /**
  59. * Set authorAvatarPath
  60. *
  61. * @param string $authorAvatarPath
  62. * @return User
  63. */
  64. public function setAuthorAvatarPath($authorAvatarPath)
  65. {
  66. $this->authorAvatarPath = $authorAvatarPath;
  67.  
  68. return $this;
  69. }
  70.  
  71. /**
  72. * Get authorAvatarPath
  73. *
  74. * @return string
  75. */
  76. public function getAuthorAvatarPath()
  77. {
  78. return $this->authorAvatarPath;
  79. }
  80.  
  81. public function getAvatarFile() {
  82. return $this->avatarFile;
  83. }
  84.  
  85. public function setAvatarFile(UploadedFile $avatarFile) {
  86. $this->avatarFile = $avatarFile;
  87. return $this;
  88. }
  89.  
  90. // Chemin absolu du fichier
  91. public function getAbsolutePath() {
  92. return null === $this->authorAvatarPath ? null: $this->getUploadRootDir().'/'.$this->authorAvatarPath;
  93. }
  94.  
  95. // Récupérer le chemin relatif d'un fichier uploadé via les templates Twig
  96. public function getWebPath() {
  97. return null === $this->authorAvatarPath ? null: $this->getUploadDir().'/'.$this->authorAvatarPath;
  98. }
  99.  
  100.  
  101. protected function getUploadRootDir() {
  102. return __DIR__.'/../../../../web/'.$this->getUploadDir();
  103. }
  104.  
  105. protected function getUploadDir() {
  106. return 'uploads/avatars';
  107. }
  108.  
  109.  
  110. /**
  111. * @ORM\PrePersist()
  112. * @ORM\PreUpdate()
  113. */
  114. public function preUpload() {
  115. // On crypte en sha1 le nom du fichier avant de le déplacer dans le rep
  116. // d'upload final
  117. if(null !== $this->avatarFile) {
  118. $this->authorAvatarPath =
  119. sha1(uniqid(mt_rand(),true)).'.'.$this->avatarFile->guessExtension();
  120. }
  121. }
  122.  
  123. /**
  124. * @ORM\PostPersist()
  125. * @ORM\PostUpdate()
  126. */
  127. public function upload() {
  128. if(null === $this->avatarFile) {
  129. return;
  130. }
  131. $this->avatarFile->move($this->getUploadRootDir(),$this->authorAvatarPath);
  132. unset($this->avatarFile);
  133. }
  134.  
  135. /**
  136. * @ORM\PostRemove()
  137. */
  138. public function removeUpload() {
  139. if($file = $this->getAbsolutePath()) {
  140. unlink($file);
  141. }
  142. }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement