Skip to content

Commit 97a8372

Browse files
committed
[TASK] Use proper ajax routing for tag generation
Change-Id: I57e0b8fc9159d76387f68b5d9a796f287dfa64c8 Reviewed-on: https://review.typo3.org/49594 Reviewed-by: Georg Ringer <[email protected]> Tested-by: Georg Ringer <[email protected]>
1 parent a2f888c commit 97a8372

File tree

4 files changed

+63
-59
lines changed

4 files changed

+63
-59
lines changed

Classes/Hooks/SuggestReceiverCall.php renamed to Classes/Backend/TagEndPoint.php

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace GeorgRinger\News\Hooks;
3+
namespace GeorgRinger\News\Backend;
44

55
/**
66
* This file is part of the TYPO3 CMS project.
@@ -14,92 +14,91 @@
1414
*
1515
* The TYPO3 project - inspiring people to share!
1616
*/
17+
use Exception;
1718
use GeorgRinger\News\Utility\EmConfiguration;
19+
use Psr\Http\Message\ServerRequestInterface;
1820
use TYPO3\CMS\Backend\Utility\BackendUtility as BackendUtilityCore;
1921
use TYPO3\CMS\Core\DataHandling\DataHandler as DataHandlerCore;
22+
use TYPO3\CMS\Core\Http\Response;
2023
use TYPO3\CMS\Core\Utility\GeneralUtility;
2124

2225
/**
2326
* Ajax response for the custom suggest receiver
2427
*
2528
*/
26-
class SuggestReceiverCall
29+
class TagEndPoint
2730
{
2831

2932
const TAG = 'tx_news_domain_model_tag';
3033
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_';
3235

3336
/**
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
4040
*/
41-
public function createTag(array $params, \TYPO3\CMS\Core\Http\AjaxRequestHandler $ajaxObj)
41+
public function create(ServerRequestInterface $request, Response $response)
4242
{
43-
$request = GeneralUtility::_GET();
44-
4543
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');
4948
}
5049

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');
5453
}
5554

5655
// Get tag uid
57-
$newTagId = $this->getTagUid($request);
56+
$newTagId = $this->getTagUid($item, $newsUid);
5857

59-
$ajaxObj->setContentFormat('javascript');
60-
$ajaxObj->setContent('');
61-
$response = [
58+
$content = [
6259
$newTagId,
63-
$request['item'],
60+
$item,
6461
self::TAG,
6562
self::NEWS,
6663
'tags',
6764
'data[tx_news_domain_model_news][' . $newsUid . '][tags]',
6865
$newsUid
6966
];
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);
7471
}
72+
return $response;
7573
}
7674

7775
/**
7876
* Get the uid of the tag, either bei inserting as new or get existing
7977
*
80-
* @param array $request ajax request
78+
* @param string $title title
79+
* @param int $newsUid news uid
8180
* @return int
82-
* @throws \Exception
81+
* @throws Exception
8382
*/
84-
protected function getTagUid(array $request)
83+
protected function getTagUid($title, $newsUid)
8584
{
8685
// Get configuration from EM
8786
$configuration = EmConfiguration::getSettings();
8887

8988
$pid = $configuration->getTagPid();
9089
if ($pid === 0) {
91-
$pid = $this->getTagPidFromTsConfig($request['newsid']);
90+
$pid = $this->getTagPidFromTsConfig($newsUid);
9291
}
9392

9493
if ($pid === 0) {
95-
throw new \Exception('error_no-pid-defined');
94+
throw new Exception('error_no-pid-defined');
9695
}
9796

9897
$record = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
9998
'*',
10099
self::TAG,
101100
'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)
103102
);
104103
if (isset($record['uid'])) {
105104
$tagUid = $record['uid'];
@@ -108,7 +107,7 @@ protected function getTagUid(array $request)
108107
self::TAG => [
109108
'NEW' => [
110109
'pid' => $pid,
111-
'title' => $request['item']
110+
'title' => $title
112111
]
113112
]
114113
];
@@ -121,7 +120,7 @@ protected function getTagUid(array $request)
121120
}
122121

123122
if ($tagUid == 0) {
124-
throw new \Exception('error_no-tag-created');
123+
throw new Exception('error_no-tag-created');
125124
}
126125

127126
return $tagUid;

Classes/Hooks/SuggestReceiver.php

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* The TYPO3 project - inspiring people to share!
1616
*/
1717
use TYPO3\CMS\Backend\Form\Wizard\SuggestWizardDefaultReceiver;
18+
use TYPO3\CMS\Backend\Routing\UriBuilder;
1819
use TYPO3\CMS\Core\Imaging\Icon;
1920
use TYPO3\CMS\Core\Imaging\IconFactory;
2021
use TYPO3\CMS\Core\Utility\GeneralUtility;
@@ -42,22 +43,23 @@ public function queryTable(&$params, $recursionCounter = 0)
4243
$records = parent::queryTable($params, $recursionCounter);
4344
if ($this->checkIfTagIsNotFound($records)) {
4445
$text = GeneralUtility::quoteJSvalue($params['value']);
45-
$javaScriptCode = '
46-
var value=' . $text . ';
4746

48-
Ext.Ajax.request({
49-
url : \'ajax.php\' ,
50-
method: "GET",
51-
params : { ajaxID : \'News::createTag\', item:value,newsid:\'' . $uid . '\' },
52-
success: function ( result, request ) {
53-
var arr = result.responseText.split(\'-\');
54-
setFormValueFromBrowseWin(arr[5], arr[2] + \'_\' + arr[0], arr[1]);
55-
TBE_EDITOR.fieldChanged(arr[3], arr[6], arr[4], arr[5]);
56-
},
57-
failure: function ( result, request) {
58-
Ext.MessageBox.alert(\'Failed\', result.responseText);
59-
}
60-
});
47+
$javaScriptCode = '
48+
var value=' . $text . ';
49+
TYPO3.jQuery.ajax({
50+
url : TYPO3.settings.ajaxUrls[\'news_tag\'] ,
51+
method: \'POST\',
52+
data: {item:value,newsid:\'' . $uid . '\' },
53+
complete:function(result) {
54+
if (re.status == 200) {
55+
ar arr = result.responseText.split(\'-\');
56+
setFormValueFromBrowseWin(arr[5], arr[2] + \'_\' + arr[0], arr[1]);
57+
TBE_EDITOR.fieldChanged(arr[3], arr[6], arr[4], arr[5]);
58+
} else {
59+
alert(result.responseText);
60+
}
61+
}
62+
});
6163
';
6264

6365
$javaScriptCode = trim(str_replace('"', '\'', $javaScriptCode));
@@ -104,7 +106,7 @@ protected function checkIfTagIsNotFound(array $tags)
104106
/**
105107
* @return Icon
106108
*/
107-
private function getDummyIcon()
109+
protected function getDummyIcon()
108110
{
109111
$iconFactory = GeneralUtility::makeInstance(IconFactory::class);
110112
return $iconFactory->getIcon('tx_news_domain_model_tag', Icon::SIZE_SMALL);

Configuration/Backend/AjaxRoutes.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
/**
4+
* Definitions for routes provided by EXT:news
5+
*/
6+
return [
7+
'news_tag' => [
8+
'path' => '/news/tag',
9+
'target' => \GeorgRinger\News\Backend\TagEndPoint::class . '::create'
10+
]
11+
];

ext_tables.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,6 @@
7272
]
7373
);
7474
}
75-
76-
/* ===========================================================================
77-
Ajax call to save tags
78-
=========================================================================== */
79-
$GLOBALS['TYPO3_CONF_VARS']['BE']['AJAX']['News::createTag'] = [
80-
'callbackMethod' => \GeorgRinger\News\Hooks\SuggestReceiverCall::class . '->createTag',
81-
'csrfTokenCheck' => false
82-
];
8375
}
8476

8577
/* ===========================================================================

0 commit comments

Comments
 (0)