Skip to content

Commit

Permalink
T3v12 (#3)
Browse files Browse the repository at this point in the history
* [WIP] Change t3 require verstion

* [BUGFIX] TYPO3_MODE

* [TASK] rector fix

* [WIP] Change array indexes to assoc values

* [WIP] Drop support for api v1, v2, add v3

* [BUGFIX] Select item in backend

* [TASK] Set up tests

* [TASK] Fix rendering

* [TASK] Fix BackendPreview

* [TASK] Fix test

* [BUGFIX] Select external icons in plugin

* [TASK] major version up

* [TASK] Change ci github

* [TASK] Update dev dependencies

---------

Co-authored-by: Rafal Jania <[email protected]>
  • Loading branch information
80Quattro and 80Quattro authored Sep 20, 2023
1 parent 9d5294b commit b478083
Show file tree
Hide file tree
Showing 24 changed files with 176 additions and 302 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
typo3: [^10.4, ^9.5]
php: ['7.4', '7.3']
typo3: [^11.5, ^12.4]
php: ['8.1', '8.2']
os: [ubuntu-latest]

steps:
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
Todos.md
.php_cs.cache
/Documentation-GENERATED-temp
.build/
Build/.phpunit.result.cache
composer.lock
57 changes: 13 additions & 44 deletions Classes/Api/RiddleApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@
use GuzzleHttp\HandlerStack;
use StudioMitte\Riddle\ExtensionConfiguration;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\StringUtility;

class RiddleApi
{
private const ENDPOINT = 'https://www.riddle.com/api/v2/';
private const ENDPOINTV2 = 'https://www.riddle.com/creator/v2/api/';
private const ENDPOINT = 'https://www.riddle.com/api/v3/';

/** @var ExtensionConfiguration */
protected $extensionConfiguration;
Expand All @@ -33,75 +31,46 @@ public function __construct(ExtensionConfiguration $extensionConfiguration = nul

public function getRiddleList(): array
{
if (!$this->extensionConfiguration->isV1Enabled()) {
return [];
}
return $this->request('riddle/get/list');
return $this->request('riddle/list');
}

public function getRiddleListV2(): array
public function getRiddleItem(string $id): array
{
if (!$this->extensionConfiguration->isV2Enabled()) {
return [];
}
return $this->request('riddle/list', 'POST', true);
}

public function getRiddleItem(int $id): array
{
$all = $this->getRiddleList();
foreach ($all['response']['items'] as $item) {
if ((int)$item['id'] === $id) {
foreach ($this->getRiddleList() as $item) {
if ($item['UUID'] === $id) {
return $item;
}
}
return [];
}

public function getRiddleItemV2(string $id): array
public function getEmbedCode(string $id): ?string
{
$data = $this->request('riddle/' . $id, 'GET', true);
return $data['data'] ?? [];
}

public function getEmbedCode(int $id): ?array
{
return $this->request('riddle/get/embed-code?riddleId=' . $id);
}

public function getEmbedCodeV2(string $id): string
{
return (string)$this->request('riddle/embed-code/' . $id, 'GET', true);
return $this->request('riddle/embed-code/' . $id, 'GET');
}

/**
* @return array|string
*/
protected function request(string $action, $method = 'GET', $isV2 = false)
protected function request(string $action, $method = 'POST')
{
$client = $this->getClient();
$headers = [
'Accept' => 'application/json',
];
if ($isV2) {
$uri = self::ENDPOINTV2 . trim($action, '/');
$headers['X-RIDDLE-BEARER'] = 'Bearer ' . $this->extensionConfiguration->getApiTokenV2();
} else {
$uri = self::ENDPOINT . trim($action, '/');
$headers['Authorization'] = 'Bearer ' . $this->extensionConfiguration->getApiToken();
$headers['Key'] = $this->extensionConfiguration->getApiKey();
}

$uri = self::ENDPOINT . trim($action, '/');
$headers['X-RIDDLE-BEARER'] = 'Bearer ' . $this->extensionConfiguration->getApiKey();

$options['headers'] = $headers;
$response = $client->request($method, $uri, $options);

if ($response->getStatusCode() === 200) {
if ($isV2 && StringUtility::beginsWith($action, 'riddle/embed-code')) {
if (\str_starts_with($action, 'riddle/embed-code')) {
return $response->getBody()->getContents();
} else {
return json_decode($response->getBody()->getContents(), true, 512, JSON_THROW_ON_ERROR);
return json_decode($response->getBody()->getContents(), true, 512, JSON_THROW_ON_ERROR)['data'];
}
return json_decode($response->getBody()->getContents(), true, 512, JSON_THROW_ON_ERROR);
}

throw new \UnexpectedValueException('Error for riddle request: ' . $response->getStatusCode(), 1597956586);
Expand Down
67 changes: 67 additions & 0 deletions Classes/Backend/EventListener/BackendPreviewEventListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

declare(strict_types=1);

namespace StudioMitte\Riddle\Backend\EventListener;

use TYPO3\CMS\Backend\View\Event\PageContentPreviewRenderingEvent;
use TYPO3\CMS\Core\Attribute\AsEventListener;
use StudioMitte\Riddle\Api\RiddleApi;
use StudioMitte\Riddle\Exception\ApiConfigurationMissingException;
use StudioMitte\Riddle\Utility\RiddleUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Fluid\View\StandaloneView;

#[AsEventListener(
identifier: 'riddle/riddle'
)]
final class BackendPreviewEventListener
{
/** @var string */
private $template = 'EXT:riddle/Resources/Private/Templates/BackendPreview.html';

public function __invoke(PageContentPreviewRenderingEvent $event): void
{
if ($event->getTable() !== 'tt_content') {
return;
}

if ($event->getRecord()['CType'] === 'list' && $event->getRecord()['list_type'] === 'riddle_riddle') {
$riddleId = RiddleUtility::getRiddleId($event->getRecord()['pi_flexform']);

$standaloneView = $this->getView();
try {
$standaloneView->assignMultiple([
'riddleId' => $riddleId,
'riddle' => $this->getRiddleData($riddleId),
]);
} catch (ApiConfigurationMissingException $exception) {
$standaloneView->assign('apiNotConfigured', true);
}

$event->setPreviewContent($standaloneView->render());
}
}

protected function getView(): StandaloneView
{
$standaloneView = GeneralUtility::makeInstance(StandaloneView::class);
$standaloneView->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName($this->template));
$standaloneView->assignMultiple([
'dateTime' => $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'] . ' ' . $GLOBALS['TYPO3_CONF_VARS']['SYS']['hhmm'],
'lll' => 'LLL:EXT:riddle/Resources/Private/Language/locallang.xlf:',
]);
return $standaloneView;
}

/**
* @param string|int $id
* @return array
*/
protected function getRiddleData($id): array
{
$api = GeneralUtility::makeInstance(RiddleApi::class);
$item = $api->getRiddleItem($id);
return RiddleUtility::enrichRiddleData($item);
}
}
20 changes: 9 additions & 11 deletions Classes/Backend/FieldWizard/ExternalIcons.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,38 +32,36 @@ public function render(): array
$selectItems = $parameterArray['fieldConf']['config']['items'];
$selectItemCounter = 0;
foreach ($selectItems as $item) {
if ($item[1] === '--div--') {
if ($item['value'] === '') {
continue;
}
$icon = $item[2] ?? '';
$icon = $item['icon'] ?? '';
if ($icon) {
$fieldValue = $this->data['databaseRow'][$this->data['fieldName']];
$fieldValue = $this->data['databaseRow'][$this->data['fieldName']]['data']['sDEF']['lDEF']['riddle']['vDEF'][0] ?? '';
$selectIcons[] = [
'title' => $item[0],
'active' => ($fieldValue[0] === (string)$item[1]) ? true : false,
'title' => $item['label'],
'active' => ($fieldValue === (string)$item['value']) ? true : false,
'icon' => $icon,
'index' => $selectItemCounter,
'index' => $selectItemCounter + 1,
];
}
$selectItemCounter++;
}

$html = [];
if (!empty($selectIcons)) {
$html[] = '<div class="t3js-forms-select-single-icons icon-list">';
$html[] = '<div class="row">';
$html[] = '<div class="t3js-forms-select-single-icons form-wizard-icon-list">';
foreach ($selectIcons as $i => $selectIcon) {
$active = $selectIcon['active'] ? ' active' : '';
$html[] = '<div class="item' . $active . '">';
$html[] = '<div class="form-wizard-icon-list-item">';
if (is_array($selectIcon)) {
$html[] = '<a href="#" title="' . htmlspecialchars($selectIcon['title'], ENT_COMPAT, 'UTF-8', false) . '" data-select-index="' . htmlspecialchars((string)$selectIcon['index']) . '">';
$html[] = '<a href="#" class="' . $active . '"title="' . htmlspecialchars($selectIcon['title'], ENT_COMPAT, 'UTF-8', false) . '" data-select-index="' . htmlspecialchars((string)$selectIcon['index']) . '">';
$html[] = sprintf('<span class="t3js-icon"><img src="%s" /></span>', $selectIcon['icon']);
$html[] = '</a>';
}
$html[] = '</div>';
}
$html[] = '</div>';
$html[] = '</div>';
}

$result['html'] = implode(LF, $html);
Expand Down
5 changes: 3 additions & 2 deletions Classes/Controller/ManagementController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace StudioMitte\Riddle\Controller;

use TYPO3\CMS\Core\Page\PageRenderer;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use StudioMitte\Riddle\Domain\Dto\LogDemand;
Expand Down Expand Up @@ -38,10 +39,10 @@ class ManagementController
/** @var LogRepository */
protected $logRepository;

public function __construct()
public function __construct(private PageRenderer $pageRenderer)
{
$this->moduleTemplate = GeneralUtility::makeInstance(ModuleTemplate::class);
$this->moduleTemplate->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Backend/Modal');
$this->pageRenderer->loadRequireJsModule('TYPO3/CMS/Backend/Modal');
$this->getLanguageService()->includeLLFile('EXT:riddle/Resources/Private/Language/locallang.xlf');
$this->logRepository = GeneralUtility::makeInstance(LogRepository::class);
}
Expand Down
21 changes: 8 additions & 13 deletions Classes/Controller/PluginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@

class PluginController extends ContentObjectRenderer
{
protected ContentObjectRenderer $cObj;

/** @var ContentObjectRenderer */
protected $cObj;
public function setContentObjectRenderer(ContentObjectRenderer $cObj): void
{
$this->cObj = $cObj;
}

public function run(): string
{
Expand All @@ -34,20 +37,12 @@ public function run(): string
return '';
}

if (MathUtility::canBeInterpretedAsInteger($riddleId)) {
return $this->getRiddleHtml((int)$riddleId);
}
return $this->getRiddleHtmlV2($riddleId);
return $this->getRiddleHtml($riddleId);
}

protected function getRiddleHtml(int $id): string
protected function getRiddleHtml(string $id): string
{
$response = GeneralUtility::makeInstance(RiddleApi::class)->getEmbedCode($id);
return (string)($response['response'] ?? '');
return GeneralUtility::makeInstance(RiddleApi::class)->getEmbedCode($id);
}

protected function getRiddleHtmlV2(string $id): string
{
return GeneralUtility::makeInstance(RiddleApi::class)->getEmbedCodeV2($id);
}
}
4 changes: 1 addition & 3 deletions Classes/Domain/Repository/LogRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,7 @@ public function getGroupingInformation(): array

$rows = $queryBuilder
->select($field)
->from(self::TABLE)
->groupBy($field)
->execute()
->from(self::TABLE)->groupBy($field)->executeQuery()
->fetchAll();

foreach ($rows as $row) {
Expand Down
24 changes: 0 additions & 24 deletions Classes/ExtensionConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ class ExtensionConfiguration implements SingletonInterface
{

protected string $apiKey = '';
protected string $apiToken = '';
protected string $apiTokenV2 = '';

public function __construct()
{
Expand All @@ -28,8 +26,6 @@ public function __construct()

if (is_array($configuration)) {
$this->apiKey = (string)($configuration['apiKey'] ?? '');
$this->apiToken = (string)($configuration['apiToken'] ?? '');
$this->apiTokenV2 = (string)($configuration['apiTokenV2'] ?? '');
}
} catch (\Exception $e) {
}
Expand All @@ -40,24 +36,4 @@ public function getApiKey(): string
return $this->apiKey;
}

public function getApiToken(): string
{
return $this->apiToken;
}

public function getApiTokenV2(): string
{
return $this->apiTokenV2;
}

public function isV1Enabled(): bool
{
return $this->apiKey && $this->apiToken;
}

public function isV2Enabled(): bool
{
return (bool)$this->apiTokenV2;
}

}
Loading

0 comments on commit b478083

Please sign in to comment.