NewsForm.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace internship\model;
  3. use n2n\web\dispatch\Dispatchable;
  4. use internship\bo\NewsCategory;
  5. use internship\bo\NewsTag;
  6. use n2n\web\dispatch\map\bind\BindingDefinition;
  7. use n2n\impl\web\dispatch\map\val\ValNotEmpty;
  8. use n2n\impl\web\dispatch\map\val\ValImageFile;
  9. use n2n\io\managed\File;
  10. use n2n\web\dispatch\map\MappingResult;
  11. use n2n\util\io\IoUtils;
  12. use internship\bo\NewsItem;
  13. use n2n\web\http\PageNotFoundException;
  14. use n2n\util\type\ArgUtils;
  15. class NewsForm implements Dispatchable {
  16. private ?NewsItem $news = null;
  17. protected string $title;
  18. protected string $content;
  19. protected ?File $imageFile = null;
  20. protected array $tagIds = array();
  21. /**
  22. * @param NewsCategory $category
  23. * @param NewsTag[] $tags
  24. * @param NewsItem|null $news
  25. */
  26. public function __construct(private NewsCategory $category, private array $tags, NewsItem $news = null) {
  27. ArgUtils::valArray($tags, NewsTag::class);
  28. if ($news === null) {
  29. return;
  30. }
  31. $this->title = $news->getTitle();
  32. $this->content = $news->getContent();
  33. $this->id = $news->getId();
  34. $this->tagIds = array_map(fn(NewsTag $t) => $t->getId(), $news->getTags()->getArrayCopy());
  35. $this->news = $news;
  36. }
  37. public function getTitle(): ?string {
  38. return $this->title ?? null;
  39. }
  40. /**
  41. * @param string $title
  42. */
  43. public function setTitle(string $title) {
  44. $this->title = $title;
  45. }
  46. public function getImageFile(): ?File {
  47. return $this->imageFile;
  48. }
  49. public function setImageFile(?File $imageFile) {
  50. $this->imageFile = $imageFile;
  51. }
  52. /**
  53. * @return array
  54. */
  55. public function getTagIds(): array {
  56. return $this->tagIds;
  57. }
  58. /**
  59. * @param array $tagIds
  60. */
  61. public function setTagIds(array $tagIds): void {
  62. $this->tagIds = $tagIds;
  63. }
  64. /**
  65. * @return string
  66. */
  67. public function getContent(): ?string {
  68. return $this->content ?? null;
  69. }
  70. /**
  71. * @param string $content
  72. */
  73. public function setContent(string $content) {
  74. $this->content = $content;
  75. }
  76. public function getTagOptions(): array {
  77. return $this->tags;
  78. }
  79. private function _mapping(MappingResult $mr) {
  80. $mr->setLabel('title', 'Titel');
  81. $mr->setLabel('content', 'Beschreibung');
  82. $mr->setLabel('imageFile', 'Bild');
  83. $mr->setLabel('tagIds', 'zugewiesene Tags');
  84. }
  85. private function _validation(BindingDefinition $bd) {
  86. $bd->val(array('content', 'title'), new ValNotEmpty());
  87. $bd->val('imageFile', new ValImageFile(true));
  88. }
  89. public function save(NewsDao $newsDao) {
  90. $selectedTagObjects = new \ArrayObject();
  91. foreach ($this->tags as $tagObject) {
  92. if (in_array($tagObject->getId(),$this->tagIds )) {
  93. $selectedTagObjects[] = $tagObject;
  94. }
  95. }
  96. $news = $this->news ?? new NewsItem();
  97. $news->setTitle($this->title);
  98. $news->setLead(substr_replace($this->content, "...", 50));
  99. $news->setContent($this->content);
  100. $news->setImageFile($this->imageFile);
  101. $news->setUrlPart(strtolower(IoUtils::stripSpecialChars($this->title)));
  102. $news->setCategory($this->category);
  103. $news->setTags($selectedTagObjects);
  104. $newsDao->saveNews($news);
  105. }
  106. public function delete(NewsDao $newsDao) {
  107. if (!$this->news) {
  108. throw new PageNotFoundException('no id set');
  109. }
  110. $news = $this->news;
  111. $newsDao->deleteNews($news);
  112. }
  113. }