1
1
<?php
2
2
3
- namespace GeorgRinger \News \Hooks ;
3
+ namespace GeorgRinger \News \Backend ;
4
4
5
5
/**
6
6
* This file is part of the TYPO3 CMS project.
14
14
*
15
15
* The TYPO3 project - inspiring people to share!
16
16
*/
17
+ use Exception ;
17
18
use GeorgRinger \News \Utility \EmConfiguration ;
19
+ use Psr \Http \Message \ServerRequestInterface ;
18
20
use TYPO3 \CMS \Backend \Utility \BackendUtility as BackendUtilityCore ;
19
21
use TYPO3 \CMS \Core \DataHandling \DataHandler as DataHandlerCore ;
22
+ use TYPO3 \CMS \Core \Http \Response ;
20
23
use TYPO3 \CMS \Core \Utility \GeneralUtility ;
21
24
22
25
/**
23
26
* Ajax response for the custom suggest receiver
24
27
*
25
28
*/
26
- class SuggestReceiverCall
29
+ class TagEndPoint
27
30
{
28
31
29
32
const TAG = 'tx_news_domain_model_tag ' ;
30
33
const NEWS = 'tx_news_domain_model_news ' ;
31
- const LLPATH = 'LLL:EXT:news/Resources/Private/Language/locallang_be.xlf:tag_suggest_ ' ;
34
+ const LL_PATH = 'LLL:EXT:news/Resources/Private/Language/locallang_be.xlf:tag_suggest_ ' ;
32
35
33
36
/**
34
- * Create a tag
35
- *
36
- * @param array $params
37
- * @param \TYPO3\CMS\Core\Http\AjaxRequestHandler $ajaxObj
38
- * @return void
39
- * @throws \Exception
37
+ * @param ServerRequestInterface $request
38
+ * @param Response $response
39
+ * @return Response
40
40
*/
41
- public function createTag ( array $ params , \ TYPO3 \ CMS \ Core \ Http \ AjaxRequestHandler $ ajaxObj )
41
+ public function create ( ServerRequestInterface $ request , Response $ response )
42
42
{
43
- $ request = GeneralUtility::_GET ();
44
-
45
43
try {
46
- // Check if a tag is submitted
47
- if (!isset ($ request ['item ' ]) || empty ($ request ['item ' ])) {
48
- throw new \Exception ('error_no-tag ' );
44
+ $ item = isset ($ request ->getParsedBody ()['item ' ]) ? $ request ->getParsedBody ()['item ' ] : $ request ->getQueryParams ()['item ' ];
45
+
46
+ if (empty ($ item )) {
47
+ throw new Exception ('error_no-tag ' );
49
48
}
50
49
51
- $ newsUid = $ request ['newsid ' ];
52
- if ((int )$ newsUid === 0 && ( strlen ( $ newsUid ) == 16 && !GeneralUtility:: isFirstPartOfStr ( $ newsUid , ' NEW ' )) ) {
53
- throw new \ Exception ('error_no-newsid ' );
50
+ $ newsUid = isset ( $ request-> getParsedBody ()[ ' newsid ' ]) ? $ request -> getParsedBody ()[ ' newsid ' ] : $ request -> getQueryParams () ['newsid ' ];
51
+ if ((int )$ newsUid === 0 ) {
52
+ throw new Exception ('error_no-newsid ' );
54
53
}
55
54
56
55
// Get tag uid
57
- $ newTagId = $ this ->getTagUid ($ request );
56
+ $ newTagId = $ this ->getTagUid ($ item , $ newsUid );
58
57
59
- $ ajaxObj ->setContentFormat ('javascript ' );
60
- $ ajaxObj ->setContent ('' );
61
- $ response = [
58
+ $ content = [
62
59
$ newTagId ,
63
- $ request [ ' item ' ] ,
60
+ $ item ,
64
61
self ::TAG ,
65
62
self ::NEWS ,
66
63
'tags ' ,
67
64
'data[tx_news_domain_model_news][ ' . $ newsUid . '][tags] ' ,
68
65
$ newsUid
69
66
];
70
- $ ajaxObj -> setJavascriptCallbackWrap ( implode ('- ' , $ response ));
71
- } catch (\ Exception $ e ) {
72
- $ errorMsg = $ GLOBALS ['LANG ' ]->sL (self ::LLPATH . $ e ->getMessage ());
73
- $ ajaxObj -> setError ( $ errorMsg );
67
+ $ response -> getBody ()-> write ( implode ('- ' , $ content ));
68
+ } catch (Exception $ e ) {
69
+ $ message = $ GLOBALS ['LANG ' ]->sL (self ::LL_PATH . $ e ->getMessage ());
70
+ throw new \ RuntimeException ( $ message );
74
71
}
72
+ return $ response ;
75
73
}
76
74
77
75
/**
78
76
* Get the uid of the tag, either bei inserting as new or get existing
79
77
*
80
- * @param array $request ajax request
78
+ * @param string $title title
79
+ * @param int $newsUid news uid
81
80
* @return int
82
- * @throws \ Exception
81
+ * @throws Exception
83
82
*/
84
- protected function getTagUid (array $ request )
83
+ protected function getTagUid ($ title , $ newsUid )
85
84
{
86
85
// Get configuration from EM
87
86
$ configuration = EmConfiguration::getSettings ();
88
87
89
88
$ pid = $ configuration ->getTagPid ();
90
89
if ($ pid === 0 ) {
91
- $ pid = $ this ->getTagPidFromTsConfig ($ request [ ' newsid ' ] );
90
+ $ pid = $ this ->getTagPidFromTsConfig ($ newsUid );
92
91
}
93
92
94
93
if ($ pid === 0 ) {
95
- throw new \ Exception ('error_no-pid-defined ' );
94
+ throw new Exception ('error_no-pid-defined ' );
96
95
}
97
96
98
97
$ record = $ GLOBALS ['TYPO3_DB ' ]->exec_SELECTgetSingleRow (
99
98
'* ' ,
100
99
self ::TAG ,
101
100
'deleted=0 AND pid= ' . $ pid .
102
- ' AND title= ' . $ GLOBALS ['TYPO3_DB ' ]->fullQuoteStr ($ request [ ' item ' ] , self ::TAG )
101
+ ' AND title= ' . $ GLOBALS ['TYPO3_DB ' ]->fullQuoteStr ($ title , self ::TAG )
103
102
);
104
103
if (isset ($ record ['uid ' ])) {
105
104
$ tagUid = $ record ['uid ' ];
@@ -108,7 +107,7 @@ protected function getTagUid(array $request)
108
107
self ::TAG => [
109
108
'NEW ' => [
110
109
'pid ' => $ pid ,
111
- 'title ' => $ request [ ' item ' ]
110
+ 'title ' => $ title
112
111
]
113
112
]
114
113
];
@@ -121,7 +120,7 @@ protected function getTagUid(array $request)
121
120
}
122
121
123
122
if ($ tagUid == 0 ) {
124
- throw new \ Exception ('error_no-tag-created ' );
123
+ throw new Exception ('error_no-tag-created ' );
125
124
}
126
125
127
126
return $ tagUid ;
0 commit comments