src/Entity/Blog.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\_Trait\LocaleTrait;
  4. use App\Repository\BlogRepository;
  5. use Cocur\Slugify\Slugify;
  6. use Doctrine\Common\Collections\ArrayCollection;
  7. use Doctrine\Common\Collections\Collection;
  8. use Doctrine\ORM\Mapping as ORM;
  9. #[ORM\Entity(repositoryClass: BlogRepository::class)]
  10. class Blog
  11. {
  12. use LocaleTrait;
  13. public const CREATED = 0;
  14. public const PUBLISHED = 1;
  15. public const DELETED = -2;
  16. public const STATUS = [
  17. self::CREATED => "Brouillon",
  18. self::PUBLISHED => "Publié",
  19. self::DELETED => "Supprimé"
  20. ];
  21. public const STATUS_DATA_FORM = [
  22. 'Brouillon' => self::CREATED,
  23. 'Publié' => self::PUBLISHED,
  24. ];
  25. public const STATUS_FILTER_FORM = [
  26. 'Brouillon' => self::CREATED,
  27. 'Publié' => self::PUBLISHED,
  28. 'Supprimé' => self::DELETED,
  29. ];
  30. #[ORM\Id]
  31. #[ORM\GeneratedValue]
  32. #[ORM\Column(type: 'integer')]
  33. private $id;
  34. #[ORM\Column(type: 'string', length: 500)]
  35. private $titre;
  36. #[ORM\Column(type: 'string', length: 255, nullable: true)]
  37. private $image;
  38. #[ORM\Column(type: 'text', nullable: true)]
  39. private $description;
  40. #[ORM\Column(type: 'datetime')]
  41. private $datePublication;
  42. #[ORM\Column(type: 'string', length: 255)]
  43. private $nomAuteur;
  44. #[ORM\ManyToOne(targetEntity: User::class)]
  45. #[ORM\JoinColumn(nullable: false)]
  46. private $auteur;
  47. #[ORM\Column(type: 'text')]
  48. private $contenu;
  49. #[ORM\ManyToOne(targetEntity: CategorieBlog::class)]
  50. #[ORM\JoinColumn(nullable: false)]
  51. private $categorie;
  52. #[ORM\OneToMany(targetEntity: BlogTagBlog::class, mappedBy: 'blog')]
  53. private $tags;
  54. #[ORM\Column(type: 'integer')]
  55. private $statut;
  56. #[ORM\Column(type: 'string', length: 500, nullable: true)]
  57. private $metatitle;
  58. #[ORM\Column(type: 'text', nullable: true)]
  59. private $metadescription;
  60. private $tagsArray;
  61. #[ORM\Column(nullable: true, options:['default' => 0])]
  62. private ?int $langue = null;
  63. public function __construct()
  64. {
  65. $this->tags = new ArrayCollection();
  66. }
  67. public function getId(): ?int
  68. {
  69. return $this->id;
  70. }
  71. public function getTitre(): ?string
  72. {
  73. return $this->titre;
  74. }
  75. public function setTitre(string $titre): self
  76. {
  77. $this->titre = $titre;
  78. return $this;
  79. }
  80. public function getImage(): ?string
  81. {
  82. return $this->image;
  83. }
  84. public function setImage(?string $image): self
  85. {
  86. $this->image = $image;
  87. return $this;
  88. }
  89. public function getDescription(): ?string
  90. {
  91. return $this->description;
  92. }
  93. public function setDescription(?string $description): self
  94. {
  95. $this->description = $description;
  96. return $this;
  97. }
  98. public function getMetadescription(): ?string
  99. {
  100. return $this->metadescription;
  101. }
  102. public function setMetadescription(?string $metadescription): self
  103. {
  104. $this->metadescription = $metadescription;
  105. return $this;
  106. }
  107. public function getMetatitle(): ?string
  108. {
  109. return $this->metatitle;
  110. }
  111. public function setMetatitle(?string $metatitle): self
  112. {
  113. $this->metatitle = $metatitle;
  114. return $this;
  115. }
  116. public function getDatePublication(): ?\DateTimeInterface
  117. {
  118. return $this->datePublication;
  119. }
  120. public function setDatePublication(\DateTimeInterface $datePublication): self
  121. {
  122. $this->datePublication = $datePublication;
  123. return $this;
  124. }
  125. public function getNomAuteur(): ?string
  126. {
  127. return $this->nomAuteur;
  128. }
  129. public function setNomAuteur(string $nomAuteur): self
  130. {
  131. $this->nomAuteur = $nomAuteur;
  132. return $this;
  133. }
  134. public function getAuteur(): ?User
  135. {
  136. return $this->auteur;
  137. }
  138. public function setAuteur(?User $auteur): self
  139. {
  140. $this->auteur = $auteur;
  141. return $this;
  142. }
  143. public function getContenu(): ?string
  144. {
  145. return $this->contenu;
  146. }
  147. public function setContenu(string $contenu): self
  148. {
  149. $this->contenu = $contenu;
  150. return $this;
  151. }
  152. public function getCategorie(): ?CategorieBlog
  153. {
  154. return $this->categorie;
  155. }
  156. public function setCategorie(?CategorieBlog $categorie): self
  157. {
  158. $this->categorie = $categorie;
  159. return $this;
  160. }
  161. /**
  162. * @return Collection<int, BlogTagBlog>
  163. */
  164. public function getTags(): Collection
  165. {
  166. return $this->tags;
  167. }
  168. public function addTag(BlogTagBlog $tag): self
  169. {
  170. if (!$this->tags->contains($tag)) {
  171. $this->tags[] = $tag;
  172. $tag->setBlog($this);
  173. }
  174. return $this;
  175. }
  176. public function removeTag(BlogTagBlog $tag): self
  177. {
  178. if ($this->tags->removeElement($tag)) {
  179. // set the owning side to null (unless already changed)
  180. if ($tag->getBlog() === $this) {
  181. $tag->setBlog(null);
  182. }
  183. }
  184. return $this;
  185. }
  186. public function getStatut(): ?int
  187. {
  188. return $this->statut;
  189. }
  190. public function setStatut(int $statut): self
  191. {
  192. $this->statut = $statut;
  193. return $this;
  194. }
  195. /**
  196. * Get the value of tagsArray
  197. */
  198. public function getTagsArray()
  199. {
  200. return $this->tagsArray;
  201. }
  202. /**
  203. * Set the value of tagsArray
  204. *
  205. * @return self
  206. */
  207. public function setTagsArray($tagsArray)
  208. {
  209. $this->tagsArray = $tagsArray;
  210. return $this;
  211. }
  212. public function convertTagsToArray(){
  213. $tab = new ArrayCollection();
  214. for($i=0; $i<count($this->getTags()); $i++){
  215. $tab->add($this->getTags()[$i]->getTag());
  216. }
  217. $this->setTagsArray($tab);
  218. }
  219. public function findBlogTag(int $tagId){
  220. foreach($this->getTags() as $blogTag){
  221. if($blogTag->getTag()->getId() == $tagId) return $blogTag;
  222. }
  223. return null;
  224. }
  225. /**
  226. * Set the value of tags
  227. *
  228. * @return self
  229. */
  230. public function setTags($tags)
  231. {
  232. $this->tags = $tags;
  233. return $this;
  234. }
  235. public function getConcatTags(){
  236. $concat = '';
  237. for($i=0; $i<count($this->getTags()); $i++){
  238. $concat .= $this->getTags()->get($i)->getTag()->getNom();
  239. if($i+1 < count($this->getTags())){
  240. $concat .= ', ';
  241. }
  242. }
  243. return $concat;
  244. }
  245. public function getSlugCat()
  246. {
  247. return (new Slugify())->slugify($this->categorie->getNom()) ;
  248. }
  249. public function getSlugTitle()
  250. {
  251. return (new Slugify())->slugify($this->getTitre()) ;
  252. }
  253. public function getLangue(): ?int
  254. {
  255. return $this->langue;
  256. }
  257. public function setLangue(?int $langue): self
  258. {
  259. $this->langue = $langue;
  260. return $this;
  261. }
  262. public function getStatusStr(): ?string
  263. {
  264. return Blog::STATUS[$this->getStatut()];
  265. }
  266. }