NewsController.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. namespace internship\controller;
  3. use n2n\web\http\controller\ControllerAdapter;
  4. use internship\model\NewsDao;
  5. use internship\model\CategoryForm;
  6. use n2n\reflection\annotation\AnnoInit;
  7. use n2n\web\http\annotation\AnnoPath;
  8. use internship\model\NewsForm;
  9. use n2n\web\http\PageNotFoundException;
  10. use internship\model\TagForm;
  11. use n2n\l10n\MessageContainer;
  12. use n2n\context\attribute\Inject;
  13. use internship\bo\NewsCategory;
  14. use internship\bo\NewsItem;
  15. use internship\bo\NewsTag;
  16. class NewsController extends ControllerAdapter {
  17. private static function _annos(AnnoInit $ai) {
  18. $ai->m('categoryDetail', new AnnoPath('/urlPart:*'));
  19. $ai->m('newsDetail', new AnnoPath('newsCategoryUrlPart:*/detail/newsItemUrlPart:*/id:*'));
  20. }
  21. private NewsDao $newsDao;
  22. private function _init(NewsDao $newsDao) {
  23. $this->newsDao = $newsDao;
  24. }
  25. #[Inject]
  26. private MessageContainer $mc;
  27. public function index() {
  28. $newsCategories = $this->newsDao->getNewsCategories();
  29. $this->forward('~\view\overview.html', ['newsCategories' => $newsCategories, 'mc' => $this->mc]);
  30. }
  31. public function categoryDetail( string $urlPart) {
  32. $NewsCategory = $this->newsDao->getNewsCategory($urlPart);
  33. if ($NewsCategory === null) {
  34. throw new PageNotFoundException('Kategorie nicht gefunden');
  35. }
  36. $this->forward('~\view\categorydetail.html', ['newsCategory' => $NewsCategory, 'mc' => $this->mc]);
  37. }
  38. public function newsDetail(string $newsCategoryUrlPart, string $newsItemUrlPart, int $id) {
  39. $newsItem = $this->newsDao->getNewsItemById($id);
  40. if ($newsItem === null) {
  41. throw new PageNotFoundException('News nicht gefunden');
  42. }
  43. $newsCategory = $this->newsDao->getNewsCategory($newsCategoryUrlPart);
  44. if ($newsCategory === null || $newsCategory != $newsItem->getCategory()) {
  45. throw new PageNotFoundException('News oder News Kategorie nicht gefunden');
  46. }
  47. if ($newsItem->getUrlPart() != $newsItemUrlPart) {
  48. throw new PageNotFoundException('News-Url nicht gefunden');
  49. }
  50. $this->forward('~\view\newsdetail.html', ['newsItem' => $newsItem, 'mc' => $this->mc]);
  51. }
  52. public function doCreateCategory(int $id = null) {
  53. $category = null;
  54. $method = 'new';
  55. if ($id !== null) {
  56. $category = $this->newsDao->getNewsCategoryById($id);
  57. if ($category instanceof NewsCategory) {
  58. $method = 'edit';
  59. }
  60. }
  61. $categoryForm = new CategoryForm($category);
  62. $this->beginTransaction();
  63. if ($this->dispatch($categoryForm, 'save')) {
  64. $this->commit();
  65. $this->mc->addInfo('Kategorie erfolgreich erstellt!');
  66. $this->redirectToController();
  67. return;
  68. }
  69. if ($this->dispatch($categoryForm, 'delete')) {
  70. $this->commit();
  71. $this->mc->addInfo('Kategorie erfolgreich gelöscht!');
  72. $this->redirectToController();
  73. return;
  74. }
  75. $this->commit();
  76. $this->forward('~\view\createcategory.html',
  77. array('categoryForm'=> $categoryForm, 'mc' => $this->mc, 'method' => $method));
  78. }
  79. public function doDeleteCategory(int $id) {
  80. $category = $this->newsDao->getNewsCategoryById($id);
  81. if ($category === null) {
  82. throw new PageNotFoundException('invalid category id: ' . $id);
  83. }
  84. $this->beginTransaction();
  85. $this->newsDao->deleteCategory($category);
  86. $this->commit();
  87. $this->mc->addInfo('Kategorie, inkl. zugeordneter News erfolgreich gelöscht!');
  88. $this->redirectToController();
  89. }
  90. public function doCreateNews(string $urlPart, int $newsId = null) {
  91. $category = $this->newsDao->getNewsCategory($urlPart);
  92. $method = 'new';
  93. $news = null;
  94. if ($category === null) {
  95. throw new PageNotFoundException('invalid category name: ' . $urlPart);
  96. }
  97. if ($newsId !== null){
  98. $news = $this->newsDao->getNewsItemById($newsId);
  99. if ($news instanceof NewsItem) {
  100. $method = 'edit';
  101. }
  102. }
  103. $this->beginTransaction();
  104. $newsForm = new NewsForm($category, $this->newsDao->getTags(), $news);
  105. if ($this->dispatch($newsForm, 'save')) {
  106. $this->commit();
  107. $this->mc->addInfo('NewsItem erfolgreich erstellt!');
  108. $this->redirectToController(array($urlPart));
  109. return;
  110. }
  111. if ($this->dispatch($newsForm, 'delete')) {
  112. $this->commit();
  113. $this->mc->addInfo('NewsItem erfolgreich gelöscht!');
  114. $this->redirectToController(array($urlPart));
  115. return;
  116. }
  117. $this->commit();
  118. $this->forward('~\view\createnews.html',
  119. array('newsForm'=> $newsForm,'urlPart'=> $urlPart, 'mc' => $this->mc, 'method' => $method));
  120. }
  121. public function doDeleteNews(int $newsId) {
  122. $news = $this->newsDao->getNewsItemById($newsId);
  123. if (empty($news)) {
  124. throw new PageNotFoundException('invalid news id: ' . $newsId);
  125. }
  126. $this->beginTransaction();
  127. $this->newsDao->deleteNews($news);
  128. $this->commit();
  129. $this->mc->addInfo('NewsItem erfolgreich gelöscht!');
  130. $this->redirectToController();
  131. }
  132. public function doCreateTag(int $tagId = null) {
  133. $tag = null;
  134. $method = 'new';
  135. if ($tagId !== null) {
  136. $tag = $this->newsDao->getNewsTagById($tagId);
  137. if ($tag instanceof NewsTag) {
  138. $method = 'edit';
  139. }
  140. }
  141. $this->beginTransaction();
  142. $tagForm = new TagForm($tag);
  143. if ($this->dispatch($tagForm, 'save')) {
  144. $this->commit();
  145. $this->mc->addInfo('Tag erfolgreich erstellt!');
  146. $this->redirectToController();
  147. return;
  148. }
  149. if ($this->dispatch($tagForm, 'delete')) {
  150. $this->commit();
  151. $this->mc->addInfo('Tag erfolgreich gelöscht!');
  152. $this->redirectToController();
  153. return;
  154. }
  155. $this->commit();
  156. $this->forward('~\view\createtag.html',
  157. array('tagForm'=> $tagForm, 'mc' => $this->mc, 'method' => $method));
  158. }
  159. public function doDeleteTag(int $tagId) {
  160. $tag = $this->newsDao->getNewsTagById($tagId);
  161. if (empty($tag)) {
  162. throw new PageNotFoundException('invalid tag id: ' . $tagId);
  163. }
  164. $this->beginTransaction();
  165. $this->newsDao->deleteTag($tag);
  166. $this->commit();
  167. $this->mc->addInfo('Tag erfolgreich gelöscht!');
  168. $this->redirectToController();
  169. }
  170. public function doTagList(string $tagName = null) {
  171. $newsTags = $this->newsDao->getTags($tagName);
  172. $this->forward('~\view\taglist.html', array('newsTags'=> $newsTags, 'mc' => $this->mc));
  173. }
  174. }