1919
2020use Psr \Http \Message \ResponseInterface ;
2121use Psr \Http \Message \ServerRequestInterface ;
22+ use T3docs \BlogExample \Domain \Model \Administrator ;
2223use T3docs \BlogExample \Domain \Model \Blog ;
2324use T3docs \BlogExample \Domain \Model \Post ;
25+ use T3docs \BlogExample \Domain \Repository \AdministratorRepository ;
2426use T3docs \BlogExample \Domain \Repository \BlogRepository ;
2527use T3docs \BlogExample \Domain \Repository \CommentRepository ;
28+ use T3docs \BlogExample \Domain \Repository \PersonRepository ;
2629use T3docs \BlogExample \Domain \Repository \PostRepository ;
2730use T3docs \BlogExample \Service \BlogFactory ;
2831use TYPO3 \CMS \Backend \Template \Components \ButtonBar ;
3134use TYPO3 \CMS \Backend \Template \ModuleTemplate ;
3235use TYPO3 \CMS \Backend \Template \ModuleTemplateFactory ;
3336use TYPO3 \CMS \Backend \Utility \BackendUtility ;
37+ use TYPO3 \CMS \Core \Domain \Repository \PageRepository ;
3438use TYPO3 \CMS \Core \Imaging \IconFactory ;
3539use TYPO3 \CMS \Core \Imaging \IconSize ;
3640use TYPO3 \CMS \Core \Localization \LanguageService ;
@@ -56,12 +60,22 @@ public function __construct(
5660 protected readonly BlogFactory $ blogFactory ,
5761 protected readonly PostRepository $ postRepository ,
5862 protected readonly CommentRepository $ commentRepository ,
63+ protected readonly PersonRepository $ personRepository ,
64+ protected readonly AdministratorRepository $ administratorRepository ,
5965 protected readonly ModuleTemplateFactory $ moduleTemplateFactory ,
6066 private readonly IconFactory $ iconFactory ,
6167 private readonly LanguageServiceFactory $ languageServiceFactory ,
6268 protected readonly ComponentFactory $ componentFactory ,
6369 ) {}
6470
71+ /**
72+ * Function will be called before every other action
73+ */
74+ protected function initializeAction (): void
75+ {
76+ $ this ->pageUid = (int )($ this ->request ->getQueryParams ()['id ' ] ?? 0 );
77+ }
78+
6579 public function addPopulateButton (ButtonBar $ buttonBar ): void
6680 {
6781 $ populateButton = $ this ->componentFactory ->createLinkButton ()
@@ -91,20 +105,19 @@ public function getMetaInformation(): array|false
91105 );
92106 }
93107
94- /**
95- * Function will be called before every other action
96- */
97- protected function initializeAction (): void
98- {
99- $ this ->pageUid = (int )($ this ->request ->getQueryParams ()['id ' ] ?? 0 );
100- }
101-
102108 /**
103109 * Index action for this controller. Displays a list of blogs.
104110 */
105111 public function indexAction (int $ currentPage = 1 ): ResponseInterface
106112 {
107113 $ view = $ this ->initializeModuleTemplate ($ this ->request );
114+ if (!$ this ->isCurrentPageSysfolder ()) {
115+
116+ $ view ->assignMultiple ([
117+ 'error ' => true ,
118+ ]);
119+ return $ view ->renderResponse ('Index ' );
120+ }
108121 $ allAvailableBlogs = $ this ->blogRepository ->findAll ();
109122 $ paginator = new QueryResultPaginator (
110123 $ allAvailableBlogs ,
@@ -124,18 +137,29 @@ public function indexAction(int $currentPage = 1): ResponseInterface
124137 }
125138
126139 /**
127- * Deletes all blogs
140+ * Deletes all blog data from the current backend page
128141 */
129142 public function deleteAllAction (): ResponseInterface
130143 {
144+ if (!$ this ->isCurrentPageSysfolder ()) {
145+ return $ this ->redirect ('index ' );
146+ }
147+ // Deleting a blog deletes the posts and comments by cascade delete
131148 $ this ->blogRepository ->removeAll ();
149+ // Persons and administrators must be deleted explicitly
150+ $ this ->personRepository ->removeAll ();
151+ $ this ->administratorRepository ->removeAll ();
132152 return $ this ->redirect ('index ' );
133153 }
154+
134155 /**
135156 * Deletes a blog
136157 */
137158 public function deleteBlogAction (Blog $ blog ): ResponseInterface
138159 {
160+ if (!$ this ->isCurrentPageSysfolder ()) {
161+ return $ this ->redirect ('index ' );
162+ }
139163 $ this ->blogRepository ->remove ($ blog );
140164 $ this ->addFlashMessage (sprintf ('Blog "%s" was deleted. ' , $ blog ->title ), 'Blog was deleted ' );
141165 return $ this ->redirect ('index ' );
@@ -146,9 +170,22 @@ public function deleteBlogAction(Blog $blog): ResponseInterface
146170 */
147171 public function populateAction (): ResponseInterface
148172 {
173+ if (!$ this ->isCurrentPageSysfolder ()) {
174+ return $ this ->redirect ('index ' );
175+ }
149176 $ numberOfExistingBlogs = $ this ->blogRepository ->countAll ();
177+
178+ // fetch administrator if they exist
179+ $ administrator = $ this ->administratorRepository ->findOneBy (['email ' => 'john.doe@example.com ' ]);
180+ if ($ administrator === null ) {
181+ // create administrator
182+ $ administrator = new Administrator ();
183+ $ administrator ->name = 'John Doe ' ;
184+ $ administrator ->email = 'john.doe@example.com ' ;
185+ $ this ->administratorRepository ->add ($ administrator );
186+ }
150187 for ($ blogNumber = $ numberOfExistingBlogs + 1 ; $ blogNumber < ($ numberOfExistingBlogs + 5 ); $ blogNumber ++) {
151- $ blog = $ this ->blogFactory ->createBlog ($ blogNumber );
188+ $ blog = $ this ->blogFactory ->createBlog ($ administrator , $ blogNumber, new \ DateTime () );
152189 $ this ->blogRepository ->add ($ blog );
153190 }
154191 $ this ->addFlashMessage ('populated ' );
@@ -163,6 +200,9 @@ public function showBlogAction(
163200 string $ tag = '' ,
164201 int $ currentPage = 1 ,
165202 ): ResponseInterface {
203+ if (!$ this ->isCurrentPageSysfolder ()) {
204+ return $ this ->redirect ('index ' );
205+ }
166206 $ view = $ this ->initializeModuleTemplate ($ this ->request );
167207 if ($ blog === null ) {
168208 $ this ->addFlashMessage ('Blog was not found ' , 'Warning ' , ContextualFeedbackSeverity::WARNING );
@@ -193,13 +233,19 @@ public function showBlogAction(
193233 */
194234 public function showPostAction (Post $ post ): ResponseInterface
195235 {
236+ if (!$ this ->isCurrentPageSysfolder ()) {
237+ return $ this ->redirect ('index ' );
238+ }
196239 $ view = $ this ->initializeModuleTemplate ($ this ->request );
197240 $ view ->assign ('post ' , $ post );
198241 return $ view ->renderResponse ('ShowPost ' );
199242 }
200243
201244 public function showAllCommentsAction (): ResponseInterface
202245 {
246+ if (!$ this ->isCurrentPageSysfolder ()) {
247+ return $ this ->redirect ('index ' );
248+ }
203249 $ view = $ this ->initializeModuleTemplate ($ this ->request );
204250 $ comments = $ this ->commentRepository ->findAll ();
205251 $ view ->assign ('comments ' , $ comments );
@@ -300,4 +346,29 @@ private function buildMenu(ModuleTemplate $view, string &$context): Menu
300346 }
301347 return $ menu ;
302348 }
349+
350+ protected function isCurrentPageSysfolder (): bool
351+ {
352+ if ($ this ->pageUid <= 0 ) {
353+ $ this ->addFlashMessage (
354+ 'Please open the module on a storage folder (sysfolder). ' ,
355+ 'Missing page context ' ,
356+ ContextualFeedbackSeverity::ERROR ,
357+ );
358+ return false ;
359+ }
360+
361+ $ page = BackendUtility::readPageAccess ($ this ->pageUid , $ GLOBALS ['BE_USER ' ]->getPagePermsClause (1 ));
362+
363+ // sysfolder check
364+ if (!is_array ($ page ) || (int )$ page ['doktype ' ] !== PageRepository::DOKTYPE_SYSFOLDER ) {
365+ $ this ->addFlashMessage (
366+ 'This module must be used on a storage folder (sysfolder). Please select a folder in the page tree. ' ,
367+ 'Wrong page type ' ,
368+ ContextualFeedbackSeverity::ERROR ,
369+ );
370+ return false ;
371+ }
372+ return true ;
373+ }
303374}
0 commit comments