11<?php
2+
23namespace BeechIt \NewsImporter \Controller ;
34
45/*
89 */
910use BeechIt \NewsImporter \Domain \Model \ExtractedItem ;
1011use BeechIt \NewsImporter \Domain \Model \ImportSource ;
12+ use TYPO3 \CMS \Backend \Template \Components \ButtonBar ;
13+ use TYPO3 \CMS \Backend \View \BackendTemplateView ;
14+ use TYPO3 \CMS \Core \Imaging \Icon ;
15+ use TYPO3 \CMS \Core \Imaging \IconFactory ;
16+ use TYPO3 \CMS \Core \Utility \GeneralUtility ;
1117use TYPO3 \CMS \Extbase \Mvc \View \ViewInterface ;
1218use TYPO3 \CMS \Backend \Utility \BackendUtility ;
1319use TYPO3 \CMS \Core \Messaging \AbstractMessage ;
14- use TYPO3 \CMS \Core \Utility \GeneralUtility ;
1520use TYPO3 \CMS \Extbase \Utility \LocalizationUtility ;
1621
1722
1823/**
1924 * Class AdminController
2025 */
21- class AdminController extends \TYPO3 \CMS \Extbase \Mvc \Controller \ActionController {
22-
23- /**
24- * @var \BeechIt\NewsImporter\Domain\Repository\ImportSourceRepository
25- * @inject
26- */
27- protected $ importSourceRepository ;
28-
29- /**
30- * @var \BeechIt\NewsImporter\Service\ExtractorService
31- * @inject
32- */
33- protected $ extractorService ;
34-
35- /**
36- * @var \BeechIt\NewsImporter\Service\ImportService
37- * @inject
38- */
39- protected $ importService ;
40-
41- /**
42- * @return bool|string
43- */
44- protected function getErrorFlashMessage () {
45- return FALSE ;
46- }
47-
48- /**
49- * initialize view
50- */
51- public function initializeView (ViewInterface $ view ) {
52- parent ::initializeView ($ view );
53- if ($ this ->getBackendUser ()) {
54- $ lang = $ this ->getBackendUser ()->uc ['lang ' ] ?: 'en ' ;
55- $ locale = $ lang . '_ ' . strtoupper ($ lang );
56- setlocale (LC_ALL , $ lang , $ locale , $ locale . '.utf8 ' , $ this ->getBackendUser ()->uc ['lang ' ], $ GLOBALS ['TYPO3_CONF_VARS ' ]['SYS ' ]['systemLocale ' ]);
57- $ view ->assign ('locale ' , $ locale );
58- }
59- }
60-
61- /**
62- * Add flash message (with auto translation handling title and body)
63- *
64- * @param string $messageBody
65- * @param string $messageTitle
66- * @param int $severity
67- * @param array $arguments the arguments of the extension, being passed over to vsprintf
68- * @param bool $storeInSession
69- */
70- public function addFlashMessage ($ messageBody , $ messageTitle = '' , $ severity = AbstractMessage::OK , array $ arguments = NULL , $ storeInSession = TRUE ) {
71- parent ::addFlashMessage (
72- $ messageBody ? LocalizationUtility::translate ($ messageBody , $ this ->extensionName , $ arguments ) ?: $ messageBody : '' ,
73- $ messageTitle ? LocalizationUtility::translate ($ messageTitle , $ this ->extensionName , $ arguments ) ?: $ messageTitle : '' ,
74- $ severity ,
75- $ storeInSession
76- );
77- }
78-
79- /**
80- * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
81- */
82- protected function getBackendUser () {
83- return $ GLOBALS ['BE_USER ' ];
84- }
85-
86- /**
87- * Index action
88- */
89- public function indexAction () {
90- $ importSources = $ this ->importSourceRepository ->findByPid ((int )$ _GET ['id ' ]);
91- if ($ importSources ->count () === 0 ) {
92- $ this ->addFlashMessage ('select-page-with-importsources ' , '' , AbstractMessage::WARNING );
93- }
94- if ($ importSources ->count () === 1 ) {
95- $ this ->redirect ('show ' , NULL , NULL , array ('importSource ' => $ importSources ->getFirst ()));
96- }
97- $ this ->view ->assign ('importSources ' , $ importSources );
98- }
99-
100- /**
101- * @param ImportSource $importSource
102- */
103- public function showAction (ImportSource $ importSource ) {
104- $ this ->view ->assign ('importSource ' , $ importSource );
105-
106- $ this ->extractorService ->setSource ($ importSource ->getUrl ());
107- $ this ->extractorService ->setMapping ($ importSource ->getMapping ());
108- $ extractedItems = $ this ->extractorService ->getItems ();
109-
110- $ items = array ();
111- /** @var ExtractedItem $item */
112- foreach ($ extractedItems as $ item ) {
113- $ items [] = array (
114- 'guid ' => $ item ->getGuid (),
115- 'title ' => $ item ->extractValue ('title ' ),
116- 'link ' => $ item ->extractValue ('link ' ),
117- 'datetime ' => $ item ->extractValue ('datetime ' ),
118- 'newsUid ' => $ this ->importService ->alreadyImported ($ importSource ->getPid (), $ item ->getGuid ())
119- );
120- }
121-
122- $ this ->view ->assign ('items ' , $ items );
123- }
124-
125- /**
126- * Import item
127- *
128- * @param ImportSource $importSource
129- * @param string $guid
130- * @return string
131- * @throws \TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException
132- */
133- public function importAction (ImportSource $ importSource , $ guid ) {
134- $ this ->extractorService ->setSource ($ importSource ->getUrl ());
135- $ this ->extractorService ->setMapping ($ importSource ->getMapping ());
136- $ extractedItems = $ this ->extractorService ->getItems ();
137-
138- foreach ($ extractedItems as $ item ) {
139- if ($ item ->getGuid () === $ guid ) {
140- $ this ->importService ->importItem ($ importSource , $ item );
141- $ itemUid = $ this ->importService ->alreadyImported ($ importSource ->getPid (), $ guid );
142-
143- $ this ->uriBuilder ->reset ()->setCreateAbsoluteUri (TRUE );
144- if (\TYPO3 \CMS \Core \Utility \GeneralUtility::getIndpEnv ('TYPO3_SSL ' )) {
145- $ this ->uriBuilder ->setAbsoluteUriScheme ('https ' );
146- }
147- $ returnUrl = $ this ->uriBuilder ->uriFor ('show ' , array ('importSource ' => $ importSource ), $ this ->request ->getControllerName ());
148- $ this ->redirectToUri ('alt_doc.php?returnUrl= ' . rawurlencode ($ returnUrl ) . '&edit[tx_news_domain_model_news][ ' . $ itemUid . ']=edit&disHelp=1 ' );
149- }
150- }
151-
152- $ this ->addFlashMessage ('requested-item-not-found ' , '' , AbstractMessage::ERROR );
153- $ this ->redirect ('show ' , NULL , NULL , array ('importSource ' => $ importSource ));
154- }
26+ class AdminController extends \TYPO3 \CMS \Extbase \Mvc \Controller \ActionController
27+ {
28+
29+ /**
30+ * @var \BeechIt\NewsImporter\Domain\Repository\ImportSourceRepository
31+ * @inject
32+ */
33+ protected $ importSourceRepository ;
34+
35+ /**
36+ * @var \BeechIt\NewsImporter\Service\ExtractorService
37+ * @inject
38+ */
39+ protected $ extractorService ;
40+
41+ /**
42+ * @var \BeechIt\NewsImporter\Service\ImportService
43+ * @inject
44+ */
45+ protected $ importService ;
46+
47+ /**
48+ * @var BackendTemplateView
49+ */
50+ protected $ view ;
51+
52+ /**
53+ * BackendTemplateView Container
54+ *
55+ * @var BackendTemplateView
56+ */
57+ protected $ defaultViewObjectName = BackendTemplateView::class;
58+
59+
60+ /**
61+ * The module name of this BE module
62+ */
63+ const MODULE_NAME = 'web_NewsImporterNewsimporter ' ;
64+
65+ /**
66+ * @return bool|string
67+ */
68+ protected function getErrorFlashMessage ()
69+ {
70+ return false ;
71+ }
72+
73+ /**
74+ * initialize view
75+ */
76+ public function initializeView (ViewInterface $ view )
77+ {
78+ /** @var BackendTemplateView $view */
79+ parent ::initializeView ($ view );
80+ if ($ this ->getBackendUser ()) {
81+ $ lang = $ this ->getBackendUser ()->uc ['lang ' ] ?: 'en ' ;
82+ $ locale = $ lang . '_ ' . strtoupper ($ lang );
83+ setlocale (LC_ALL , $ lang , $ locale , $ locale . '.utf8 ' , $ this ->getBackendUser ()->uc ['lang ' ],
84+ $ GLOBALS ['TYPO3_CONF_VARS ' ]['SYS ' ]['systemLocale ' ]);
85+ $ view ->assign ('locale ' , $ locale );
86+ }
87+ }
88+
89+ /**
90+ * Add flash message (with auto translation handling title and body)
91+ *
92+ * @param $messageBody
93+ * @param string $messageTitle
94+ * @param int $severity
95+ * @param array|null $arguments
96+ * @param bool $storeInSession
97+ */
98+ public function addTranslatedFlashMessage (
99+ $ messageBody ,
100+ $ messageTitle = '' ,
101+ $ severity = AbstractMessage::OK ,
102+ array $ arguments = null ,
103+ $ storeInSession = true
104+ ) {
105+ $ this ->addFlashMessage ($ this ->getTranslatedString ($ messageBody , $ arguments ),
106+ $ this ->getTranslatedString ($ messageTitle , $ arguments ),
107+ $ severity ,
108+ $ storeInSession
109+ );
110+ }
111+
112+ /**
113+ * Translate input string with additional arguments
114+ *
115+ * @param $input
116+ * @param $arguments
117+ * @return string
118+ */
119+ public function getTranslatedString ($ input , $ arguments ): string
120+ {
121+ if (!$ input ) {
122+ return '' ;
123+ }
124+ $ translated = LocalizationUtility::translate ($ input , $ this ->extensionName , $ arguments );
125+ return $ translated ?: '' ;
126+ }
127+
128+ /**
129+ * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
130+ */
131+ protected function getBackendUser ()
132+ {
133+ return $ GLOBALS ['BE_USER ' ];
134+ }
135+
136+ /**
137+ * Index action
138+ */
139+ public function indexAction ()
140+ {
141+ $ importSources = $ this ->importSourceRepository ->findByPid ((int )$ _GET ['id ' ]);
142+ if ($ importSources ->count () === 0 ) {
143+ $ this ->addTranslatedFlashMessage ('select-page-with-importsources ' , '' , AbstractMessage::WARNING );
144+ }
145+ if ($ importSources ->count () === 1 ) {
146+ $ this ->redirect ('show ' , null , null , ['importSource ' => $ importSources ->getFirst ()]);
147+ }
148+ $ this ->view ->assign ('importSources ' , $ importSources );
149+ }
150+
151+ /**
152+ * @param ImportSource $importSource
153+ */
154+ public function showAction (ImportSource $ importSource )
155+ {
156+ $ this ->registerButtons ();
157+
158+ $ this ->view ->assign ('importSource ' , $ importSource );
159+
160+ $ this ->extractorService ->setSource ($ importSource ->getUrl ());
161+ $ this ->extractorService ->setMapping ($ importSource ->getMapping ());
162+ $ extractedItems = $ this ->extractorService ->getItems ();
163+
164+ $ items = [];
165+ /** @var ExtractedItem $item */
166+ foreach ($ extractedItems as $ item ) {
167+ $ items [] = [
168+ 'guid ' => $ item ->getGuid (),
169+ 'title ' => $ item ->extractValue ('title ' ),
170+ 'link ' => $ item ->extractValue ('link ' ),
171+ 'datetime ' => $ item ->extractValue ('datetime ' ),
172+ 'newsUid ' => $ this ->importService ->alreadyImported ($ importSource ->getPid (), $ item ->getGuid ())
173+ ];
174+ }
175+
176+ $ this ->view ->assign ('items ' , $ items );
177+ }
178+
179+ /**
180+ * @param ImportSource $importSource
181+ * @param string $guid
182+ * @throws \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException
183+ * @throws \TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException
184+ */
185+ public function importAction (ImportSource $ importSource , $ guid )
186+ {
187+ $ this ->extractorService ->setSource ($ importSource ->getUrl ());
188+ $ this ->extractorService ->setMapping ($ importSource ->getMapping ());
189+ $ extractedItems = $ this ->extractorService ->getItems ();
190+
191+ foreach ($ extractedItems as $ item ) {
192+ if ($ item ->getGuid () === $ guid ) {
193+ $ this ->importService ->importItem ($ importSource , $ item );
194+ $ itemUid = $ this ->importService ->alreadyImported ($ importSource ->getPid (), $ guid );
195+
196+ $ this ->uriBuilder ->reset ()->setCreateAbsoluteUri (true );
197+ if (\TYPO3 \CMS \Core \Utility \GeneralUtility::getIndpEnv ('TYPO3_SSL ' )) {
198+ $ this ->uriBuilder ->setAbsoluteUriScheme ('https ' );
199+ }
200+ $ uri = BackendUtility::getModuleUrl ('record_edit ' , [
201+ 'edit ' => [
202+ 'tx_news_domain_model_news ' => [
203+ $ itemUid => 'edit '
204+ ]
205+ ],
206+ 'returnUrl ' => $ this ->uriBuilder ->uriFor ('show ' , ['importSource ' => $ importSource ],
207+ $ this ->request ->getControllerName ())
208+ ]);
209+ $ this ->redirectToUri ($ uri );
210+ }
211+ }
212+
213+ $ this ->addTranslatedFlashMessage ('requested-item-not-found ' , '' , AbstractMessage::ERROR );
214+ $ this ->redirect ('show ' , null , null , ['importSource ' => $ importSource ]);
215+ }
216+
217+
218+ /**
219+ * Create the panel of buttons for submitting the form or otherwise perform operations.
220+ *
221+ * @return array All available buttons as an assoc. array
222+ */
223+ protected function registerButtons ()
224+ {
225+ /** @var ButtonBar $buttonBar */
226+ $ buttonBar = $ this ->view ->getModuleTemplate ()->getDocHeaderComponent ()->getButtonBar ();
227+
228+ /** @var IconFactory $iconFactory */
229+ $ iconFactory = $ this ->view ->getModuleTemplate ()->getIconFactory ();
230+
231+ $ lang = $ this ->getLanguageService ();
232+
233+ // Refresh page
234+ $ refreshLink = GeneralUtility::linkThisScript (
235+ [
236+ 'target ' => rawurlencode ('# ' ),
237+ ]
238+ );
239+ $ refreshButton = $ buttonBar ->makeLinkButton ()
240+ ->setHref ($ refreshLink )
241+ ->setTitle ($ lang ->sL ('LLL:EXT:lang/locallang_core.xlf:labels.reload ' ))
242+ ->setIcon ($ iconFactory ->getIcon ('actions-refresh ' , Icon::SIZE_SMALL ));
243+ $ buttonBar ->addButton ($ refreshButton , ButtonBar::BUTTON_POSITION_RIGHT );
244+
245+ // Shortcut
246+ if ($ this ->getBackendUser ()->mayMakeShortcut ()) {
247+ $ shortCutButton = $ buttonBar ->makeShortcutButton ()->setModuleName (self ::MODULE_NAME );
248+ $ buttonBar ->addButton ($ shortCutButton , ButtonBar::BUTTON_POSITION_RIGHT );
249+ }
250+ }
251+
252+ /**
253+ * Returns an instance of LanguageService
254+ *
255+ * @return \TYPO3\CMS\Lang\LanguageService
256+ */
257+ protected function getLanguageService ()
258+ {
259+ return $ GLOBALS ['LANG ' ];
260+ }
155261}
0 commit comments