src/Entity/Web/NewsTranslation.php line 25

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Web;
  3. use Doctrine\ORM\Event\LifecycleEventArgs;
  4. use Doctrine\ORM\Mapping as ORM;
  5. use App\Entity\Behaviors\TranslationTrait;
  6. use App\Entity\Behaviors\SluggableTrait;
  7. use Knp\DoctrineBehaviors\Contract\Entity\TranslationInterface;
  8. use Knp\DoctrineBehaviors\Contract\Entity\SluggableInterface;
  9. use Symfony\Component\HttpFoundation\File\File;
  10. use Vich\UploaderBundle\Mapping\Annotation as Vich;
  11. use Symfony\Component\Validator\Constraints as Assert;
  12. use Doctrine\ORM\Mapping\Index;
  13. /**
  14. * @ORM\Table(name="web_news_translation",uniqueConstraints={@ORM\UniqueConstraint(name="sluging", columns={"locale", "slug"})},
  15. * indexes={@Index(name="locale_slug", columns={"locale","slug"})})
  16. * @ORM\Entity(repositoryClass="App\Repository\TranslationRepository")
  17. * @ORM\EntityListeners({"App\Listener\EntityListener"})
  18. * @Vich\Uploadable()
  19. * @ORM\HasLifecycleCallbacks()
  20. */
  21. class NewsTranslation implements TranslationInterface, SluggableInterface
  22. {
  23. use TranslationTrait;
  24. use SluggableTrait;
  25. /**
  26. * @ORM\Column(type="string", length=2)
  27. */
  28. protected $locale;
  29. /**
  30. * @ORM\Column(type="string", length=255)
  31. */
  32. private string $title = '';
  33. /**
  34. * @ORM\Column(type="string", length=190)
  35. */
  36. protected $slug;
  37. /**
  38. * @ORM\Column(type="text", nullable=true)
  39. */
  40. private $description;
  41. /**
  42. * @Vich\UploadableField(mapping="news", fileNameProperty="image")
  43. * @Assert\Image(mimeTypes={"image/png", "image/jpeg", "image/jpg", "image/gif"})
  44. * @var File|null
  45. */
  46. private $imageFile;
  47. /**
  48. * @ORM\Column(type="string", length=255, nullable=true)
  49. */
  50. private $image;
  51. /**
  52. * @ORM\Column(type="datetime")
  53. */
  54. private $createts;
  55. /**
  56. * @ORM\ManyToOne(targetEntity="App\Entity\User")
  57. * @ORM\JoinColumn(nullable=false)
  58. */
  59. private $createUser;
  60. /**
  61. * @ORM\Column(type="datetime", nullable=true)
  62. */
  63. private $editts;
  64. /**
  65. * @ORM\ManyToOne(targetEntity="App\Entity\User")
  66. */
  67. private $editUser;
  68. public function __construct($locale = null) {
  69. if($locale !== null){
  70. $this->setLocale($locale);
  71. }
  72. }
  73. public function getId(): ?int
  74. {
  75. return $this->id;
  76. }
  77. public function getImage(): ?string
  78. {
  79. return $this->image;
  80. }
  81. public function setImage(?string $image): self
  82. {
  83. $this->image = $image;
  84. return $this;
  85. }
  86. /**
  87. * Get ImageFile
  88. *
  89. * @return null|File
  90. */
  91. public function getImageFile()
  92. {
  93. return $this->imageFile;
  94. }
  95. /**
  96. * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile|null $imageFile
  97. */
  98. public function setImageFile(?File $imageFile = null): void
  99. {
  100. $this->imageFile = $imageFile;
  101. if (null !== $imageFile) {
  102. $this->editts = new \DateTime();
  103. }
  104. }
  105. /**
  106. * @return mixed
  107. */
  108. public function getCreatets()
  109. {
  110. return $this->createts;
  111. }
  112. /**
  113. * @param mixed $createts
  114. */
  115. public function setCreatets($createts): void
  116. {
  117. $this->createts = $createts;
  118. }
  119. /**
  120. * @return mixed
  121. */
  122. public function getCreateUser()
  123. {
  124. return $this->createUser;
  125. }
  126. /**
  127. * @param mixed $createUser
  128. */
  129. public function setCreateUser($createUser): void
  130. {
  131. $this->createUser = $createUser;
  132. }
  133. /**
  134. * @return mixed
  135. */
  136. public function getEditts()
  137. {
  138. return $this->editts;
  139. }
  140. /**
  141. * @param mixed $editts
  142. */
  143. public function setEditts($editts): void
  144. {
  145. $this->editts = $editts;
  146. }
  147. /**
  148. * @return mixed
  149. */
  150. public function getEditUser()
  151. {
  152. return $this->editUser;
  153. }
  154. /**
  155. * @param mixed $editUser
  156. */
  157. public function setEditUser($editUser): void
  158. {
  159. $this->editUser = $editUser;
  160. }
  161. public function getSluggableFields(): array
  162. {
  163. return ['title'];
  164. }
  165. /**
  166. * @return mixed
  167. */
  168. public function getLocale(): string
  169. {
  170. return $this->locale;
  171. }
  172. /**
  173. * @param mixed $locale
  174. */
  175. public function setLocale($locale): void
  176. {
  177. $this->locale = $locale;
  178. }
  179. /**
  180. * @return string
  181. */
  182. public function getTitle(): string
  183. {
  184. return $this->title;
  185. }
  186. /**
  187. * @param string $title
  188. */
  189. public function setTitle(string $title): void
  190. {
  191. $this->title = $title;
  192. }
  193. /**
  194. * @return mixed
  195. */
  196. public function getSlug(): string
  197. {
  198. return (string) $this->slug;
  199. }
  200. /**
  201. * @param mixed $slug
  202. */
  203. public function setSlug($slug): void
  204. {
  205. $this->slug = $slug;
  206. }
  207. /**
  208. * @return mixed
  209. */
  210. public function getDescription()
  211. {
  212. return $this->description;
  213. }
  214. /**
  215. * @param mixed $description
  216. */
  217. public function setDescription($description): void
  218. {
  219. $this->description = $description;
  220. }
  221. public function generateSlug(): void { }
  222. /**
  223. * @ORM\PreUpdate()
  224. * @ORM\PrePersist()
  225. */
  226. public function updateSlug( LifecycleEventArgs $eventArgs )
  227. {
  228. $translatable = $this->getTranslatable();
  229. $oldSlug = $translatable->getSlug();
  230. if ( $this->getRegenerateSlugOnUpdate() || empty( $this->slug ) ) {
  231. $fields = $this->getSluggableFields();
  232. $values = [];
  233. foreach ( $fields as $field ) {
  234. if ( property_exists( $this, $field ) ) {
  235. $val = $this->{$field};
  236. } else {
  237. $methodName = 'get' . ucfirst( $field );
  238. if ( method_exists( $this, $methodName ) ) {
  239. $val = $this->{$methodName}();
  240. } else {
  241. $val = null;
  242. }
  243. }
  244. $values[] = $val;
  245. }
  246. $this->slug = $this->generateSlugValue( $values );
  247. if ( $oldSlug !== $this->slug ) {
  248. $repo = $eventArgs->getEntityManager()->getRepository( get_class( $this ) );
  249. $repo->updateSlug( $this );
  250. }
  251. }
  252. }
  253. }