src/Entity/News.php line 30

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Entity\Traits\LinkTrait;
  4. use App\Entity\Traits\MetaTrait;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use App\Entity\Traits\ActiveTrait;
  7. use App\Entity\Traits\DeleteTrait;
  8. use App\Repository\NewsRepository;
  9. use App\Entity\Traits\SubTitleTrait;
  10. use App\Entity\Traits\TranslateTrait;
  11. use Gedmo\Mapping\Annotation as Gedmo;
  12. use App\Entity\Traits\ImageUploadTrait;
  13. use App\Entity\Traits\SitemapableTrait;
  14. use App\Entity\Traits\TitleAndContentTrait;
  15. use App\Entity\Interfaces\DefaultLinkedEntity;
  16. use Gedmo\Timestampable\Traits\TimestampableEntity;
  17. use Symfony\Component\Validator\Constraints as Assert;
  18. /**
  19. * News.
  20. *
  21. * @Gedmo\Loggable
  22. *
  23. * @Gedmo\TranslationEntity(class="App\Entity\NewsTranslations")
  24. */
  25. #[ORM\Entity(repositoryClass: NewsRepository::class)]
  26. #[ORM\Table(name: 'news')]
  27. class News implements DefaultLinkedEntity
  28. {
  29. use TitleAndContentTrait;
  30. use SubTitleTrait;
  31. use LinkTrait;
  32. use MetaTrait;
  33. use ActiveTrait;
  34. use DeleteTrait;
  35. use ImageUploadTrait;
  36. use TimestampableEntity;
  37. use TranslateTrait;
  38. use SitemapableTrait;
  39. public $imageUpload;
  40. #[ORM\Column(name: 'id', type: 'integer')]
  41. #[ORM\Id]
  42. #[ORM\GeneratedValue(strategy: 'AUTO')]
  43. private readonly int $id;
  44. /**
  45. * @Gedmo\Versioned
  46. *
  47. * @Gedmo\Translatable
  48. */
  49. #[ORM\Column(name: 'excerpt', type: 'text', nullable: true)]
  50. private ?string $excerpt = null;
  51. /**
  52. * @Gedmo\Versioned
  53. */
  54. #[ORM\Column(name: 'publish_date', type: 'datetime')]
  55. private ?\DateTimeInterface $publishDate = null;
  56. /**
  57. * @Gedmo\Versioned
  58. */
  59. #[ORM\Column(name: 'thumbnail', type: 'string', length: 255, nullable: true)]
  60. private ?string $thumbnail = null;
  61. #[ORM\ManyToOne(targetEntity: User::class, inversedBy: 'news')]
  62. #[ORM\JoinColumn(nullable: false)]
  63. private ?\App\Entity\User $updatedBy = null;
  64. #[ORM\Column(type: 'string', length: 255, nullable: true)]
  65. private $cta_text;
  66. #[ORM\Column(type: 'string', length: 255, nullable: true)]
  67. private $cta_link;
  68. #[ORM\Column(type: 'boolean')]
  69. private $featuredOnNewsPage = false;
  70. #[ORM\Column(type: 'boolean')]
  71. private $featuredInWisdomComponent = false;
  72. /**
  73. * Override slug from LinkTrait to disable auto-update.
  74. * Slug will only be auto-generated on create, but can be manually set.
  75. *
  76. * @Gedmo\Translatable
  77. *
  78. * @Gedmo\Versioned
  79. *
  80. * @Gedmo\Slug(fields={"title"}, separator="-", updatable=false)
  81. */
  82. #[ORM\Column(length: 255, unique: true)]
  83. private ?string $slug = null;
  84. /**
  85. * Get id.
  86. *
  87. * @return int
  88. */
  89. public function getId()
  90. {
  91. return $this->id;
  92. }
  93. /**
  94. * Set excerpt.
  95. *
  96. * @param string $excerpt
  97. *
  98. * @return News
  99. */
  100. public function setExcerpt($excerpt)
  101. {
  102. $this->excerpt = $excerpt;
  103. return $this;
  104. }
  105. /**
  106. * Get excerpt.
  107. *
  108. * @return string
  109. */
  110. public function getExcerpt()
  111. {
  112. return $this->excerpt;
  113. }
  114. /**
  115. * Set publishDate.
  116. *
  117. * @param \DateTime|\DateTimeImmutable $publishDate
  118. *
  119. * @return News
  120. */
  121. public function setPublishDate(\DateTimeInterface $publishDate)
  122. {
  123. $this->publishDate = $publishDate;
  124. return $this;
  125. }
  126. /**
  127. * Get publishDate.
  128. *
  129. * @return \DateTime
  130. */
  131. public function getPublishDate()
  132. {
  133. return $this->publishDate;
  134. }
  135. /**
  136. * Set thumbnail.
  137. *
  138. * @param string $thumbnail
  139. *
  140. * @return News
  141. */
  142. public function setThumbnail($thumbnail)
  143. {
  144. $this->thumbnail = $thumbnail;
  145. return $this;
  146. }
  147. /**
  148. * Get thumbnail.
  149. *
  150. * @return string
  151. */
  152. public function getThumbnail()
  153. {
  154. return $this->thumbnail;
  155. }
  156. // REQUIRED BY THE META LINKS TRAIT
  157. public function getLinkedPageId(): int
  158. {
  159. return 3;
  160. }
  161. public function getUpdatedBy(): ?User
  162. {
  163. return $this->updatedBy;
  164. }
  165. public function setUpdatedBy(?User $updatedBy): self
  166. {
  167. $this->updatedBy = $updatedBy;
  168. return $this;
  169. }
  170. public function getCtaText(): ?string
  171. {
  172. return $this->cta_text;
  173. }
  174. public function setCtaText(?string $cta_text): self
  175. {
  176. $this->cta_text = $cta_text;
  177. return $this;
  178. }
  179. public function getCtaLink(): ?string
  180. {
  181. return $this->cta_link;
  182. }
  183. public function setCtaLink(?string $cta_link): self
  184. {
  185. $this->cta_link = $cta_link;
  186. return $this;
  187. }
  188. public function getFeaturedOnNewsPage(): ?bool
  189. {
  190. return $this->featuredOnNewsPage;
  191. }
  192. public function setFeaturedOnNewsPage(bool $featuredOnNewsPage): self
  193. {
  194. $this->featuredOnNewsPage = $featuredOnNewsPage;
  195. return $this;
  196. }
  197. public function getFeaturedInWisdomComponent(): ?bool
  198. {
  199. return $this->featuredInWisdomComponent;
  200. }
  201. public function setFeaturedInWisdomComponent(bool $featuredInWisdomComponent): self
  202. {
  203. $this->featuredInWisdomComponent = $featuredInWisdomComponent;
  204. return $this;
  205. }
  206. /**
  207. * Override setSlug from LinkTrait to allow manual setting.
  208. * If empty string is provided, set to null to allow auto-generation on create.
  209. */
  210. public function setSlug(?string $slug): void
  211. {
  212. // Convert empty string to null so Gedmo can auto-generate on create
  213. $this->slug = ($slug === '' || $slug === null) ? null : $slug;
  214. }
  215. public function getSitemapRouteParams(): array
  216. {
  217. return ['slug' => $this->getSlug()];
  218. }
  219. public function isIncludedInSitemap(): bool
  220. {
  221. $now = new \DateTime();
  222. return $this->isActive() &&
  223. ! $this->isDeleted() &&
  224. $this->getPublishDate() < $now
  225. ;
  226. }
  227. }