| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- <?php
- namespace internship\controller;
- use n2n\web\http\controller\ControllerAdapter;
- use internship\model\NewsDao;
- use internship\model\CategoryForm;
- use n2n\reflection\annotation\AnnoInit;
- use n2n\web\http\annotation\AnnoPath;
- use internship\model\NewsForm;
- use n2n\web\http\PageNotFoundException;
- use internship\model\TagForm;
- use n2n\l10n\MessageContainer;
- use n2n\context\attribute\Inject;
- use internship\bo\NewsCategory;
- use internship\bo\NewsItem;
- use internship\bo\NewsTag;
- class NewsController extends ControllerAdapter {
- private static function _annos(AnnoInit $ai) {
- $ai->m('categoryDetail', new AnnoPath('/urlPart:*'));
- $ai->m('newsDetail', new AnnoPath('newsCategoryUrlPart:*/detail/newsItemUrlPart:*/id:*'));
- }
- private NewsDao $newsDao;
- private function _init(NewsDao $newsDao) {
- $this->newsDao = $newsDao;
- }
- #[Inject]
- private MessageContainer $mc;
- public function index() {
- $newsCategories = $this->newsDao->getNewsCategories();
- $this->forward('~\view\overview.html', ['newsCategories' => $newsCategories, 'mc' => $this->mc]);
- }
- public function categoryDetail( string $urlPart) {
- $NewsCategory = $this->newsDao->getNewsCategory($urlPart);
- if ($NewsCategory === null) {
- throw new PageNotFoundException('Kategorie nicht gefunden');
- }
- $this->forward('~\view\categorydetail.html', ['newsCategory' => $NewsCategory, 'mc' => $this->mc]);
- }
- public function newsDetail(string $newsCategoryUrlPart, string $newsItemUrlPart, int $id) {
- $newsItem = $this->newsDao->getNewsItemById($id);
- if ($newsItem === null) {
- throw new PageNotFoundException('News nicht gefunden');
- }
- $newsCategory = $this->newsDao->getNewsCategory($newsCategoryUrlPart);
- if ($newsCategory === null || $newsCategory != $newsItem->getCategory()) {
- throw new PageNotFoundException('News oder News Kategorie nicht gefunden');
- }
- if ($newsItem->getUrlPart() != $newsItemUrlPart) {
- throw new PageNotFoundException('News-Url nicht gefunden');
- }
- $this->forward('~\view\newsdetail.html', ['newsItem' => $newsItem, 'mc' => $this->mc]);
- }
- public function doCreateCategory(int $id = null) {
- $category = null;
- $method = 'new';
- if ($id !== null) {
- $category = $this->newsDao->getNewsCategoryById($id);
- if ($category instanceof NewsCategory) {
- $method = 'edit';
- }
- }
- $categoryForm = new CategoryForm($category);
- $this->beginTransaction();
- if ($this->dispatch($categoryForm, 'save')) {
- $this->commit();
- $this->mc->addInfo('Kategorie erfolgreich erstellt!');
- $this->redirectToController();
- return;
- }
- if ($this->dispatch($categoryForm, 'delete')) {
- $this->commit();
- $this->mc->addInfo('Kategorie erfolgreich gelöscht!');
- $this->redirectToController();
- return;
- }
- $this->commit();
- $this->forward('~\view\createcategory.html',
- array('categoryForm'=> $categoryForm, 'mc' => $this->mc, 'method' => $method));
- }
- public function doDeleteCategory(int $id) {
- $category = $this->newsDao->getNewsCategoryById($id);
- if ($category === null) {
- throw new PageNotFoundException('invalid category id: ' . $id);
- }
- $this->beginTransaction();
- $this->newsDao->deleteCategory($category);
- $this->commit();
- $this->mc->addInfo('Kategorie, inkl. zugeordneter News erfolgreich gelöscht!');
- $this->redirectToController();
- }
- public function doCreateNews(string $urlPart, int $newsId = null) {
- $category = $this->newsDao->getNewsCategory($urlPart);
- $method = 'new';
- $news = null;
- if ($category === null) {
- throw new PageNotFoundException('invalid category name: ' . $urlPart);
- }
- if ($newsId !== null){
- $news = $this->newsDao->getNewsItemById($newsId);
- if ($news instanceof NewsItem) {
- $method = 'edit';
- }
- }
- $this->beginTransaction();
- $newsForm = new NewsForm($category, $this->newsDao->getTags(), $news);
- if ($this->dispatch($newsForm, 'save')) {
- $this->commit();
- $this->mc->addInfo('NewsItem erfolgreich erstellt!');
- $this->redirectToController(array($urlPart));
- return;
- }
- if ($this->dispatch($newsForm, 'delete')) {
- $this->commit();
- $this->mc->addInfo('NewsItem erfolgreich gelöscht!');
- $this->redirectToController(array($urlPart));
- return;
- }
- $this->commit();
- $this->forward('~\view\createnews.html',
- array('newsForm'=> $newsForm,'urlPart'=> $urlPart, 'mc' => $this->mc, 'method' => $method));
- }
- public function doDeleteNews(int $newsId) {
- $news = $this->newsDao->getNewsItemById($newsId);
- if (empty($news)) {
- throw new PageNotFoundException('invalid news id: ' . $newsId);
- }
- $this->beginTransaction();
- $this->newsDao->deleteNews($news);
- $this->commit();
- $this->mc->addInfo('NewsItem erfolgreich gelöscht!');
- $this->redirectToController();
- }
- public function doCreateTag(int $tagId = null) {
- $tag = null;
- $method = 'new';
- if ($tagId !== null) {
- $tag = $this->newsDao->getNewsTagById($tagId);
- if ($tag instanceof NewsTag) {
- $method = 'edit';
- }
- }
- $this->beginTransaction();
- $tagForm = new TagForm($tag);
- if ($this->dispatch($tagForm, 'save')) {
- $this->commit();
- $this->mc->addInfo('Tag erfolgreich erstellt!');
- $this->redirectToController();
- return;
- }
- if ($this->dispatch($tagForm, 'delete')) {
- $this->commit();
- $this->mc->addInfo('Tag erfolgreich gelöscht!');
- $this->redirectToController();
- return;
- }
- $this->commit();
- $this->forward('~\view\createtag.html',
- array('tagForm'=> $tagForm, 'mc' => $this->mc, 'method' => $method));
- }
- public function doDeleteTag(int $tagId) {
- $tag = $this->newsDao->getNewsTagById($tagId);
- if (empty($tag)) {
- throw new PageNotFoundException('invalid tag id: ' . $tagId);
- }
- $this->beginTransaction();
- $this->newsDao->deleteTag($tag);
- $this->commit();
- $this->mc->addInfo('Tag erfolgreich gelöscht!');
- $this->redirectToController();
- }
- public function doTagList(string $tagName = null) {
- $newsTags = $this->newsDao->getTags($tagName);
- $this->forward('~\view\taglist.html', array('newsTags'=> $newsTags, 'mc' => $this->mc));
- }
- }
|