Skip to content

Commit

Permalink
Permissions: button for inline generation
Browse files Browse the repository at this point in the history
  • Loading branch information
Michał Cygankiewicz authored and hannesbochmann committed Apr 19, 2024
1 parent e080e13 commit 3fd0768
Show file tree
Hide file tree
Showing 16 changed files with 183 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

namespace DMK\MkContentAi\Backend\EventListener;

use DMK\MkContentAi\Utility\PermissionsUtility;
use TYPO3\CMS\Backend\Form\Event\CustomFileControlsEvent;
use TYPO3\CMS\Backend\Form\NodeFactory;
use TYPO3\CMS\Core\Imaging\Icon;
Expand All @@ -36,14 +37,21 @@ final class CustomFileControlsEventListener
*/
public $iconFactory;

public function __construct()
private PermissionsUtility $permissionsUtility;

public function __construct(PermissionsUtility $permissionsUtility)
{
$this->nodeFactory = GeneralUtility::makeInstance(NodeFactory::class);
$this->iconFactory = GeneralUtility::makeInstance(IconFactory::class);
$this->permissionsUtility = $permissionsUtility;
}

public function handleEvent(CustomFileControlsEvent $event): void
{
if (!$this->permissionsUtility->userHasAccessToImageGenerationPromptButton()) {
return;
}

$translatedMessage = LocalizationUtility::translate('labelAiGenerateText', 'mkcontentai') ?? '';
$item = ' <div class="form-control-wrap"><button type="button" class="btn btn-default t3js-prompt" id="prompt">';
$item .= $this->iconFactory->getIcon('actions-image', Icon::SIZE_SMALL)->render().' ';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

namespace DMK\MkContentAi\Backend\EventListener;

use DMK\MkContentAi\Utility\PermissionsUtility;
use TYPO3\CMS\Backend\Routing\UriBuilder;
use TYPO3\CMS\Backend\Template\Components\ButtonBar;
use TYPO3\CMS\Backend\Template\Components\ModifyButtonBarEvent;
Expand All @@ -31,13 +32,20 @@ class ModifyFilelistButtonBarEventListener
*/
protected $iconFactory;

public function __construct()
private PermissionsUtility $permissionsUtility;

public function __construct(PermissionsUtility $permissionsUtility)
{
$this->iconFactory = GeneralUtility::makeInstance(IconFactory::class);
$this->permissionsUtility = $permissionsUtility;
}

public function handleEvent(ModifyButtonBarEvent $event): void
{
if (!$this->permissionsUtility->userHasAccessToImageGenerationPromptButton()) {
return;
}

$translatedMessage = LocalizationUtility::translate('labelAiGenerateText', 'mkcontentai') ?? '';
$url = $this->buildUriToControllerAction();
$buttons = $event->getButtons();
Expand Down
25 changes: 17 additions & 8 deletions Classes/Backend/Hooks/ButtonBarHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

namespace DMK\MkContentAi\Backend\Hooks;

use DMK\MkContentAi\Utility\PermissionsUtility;
use TYPO3\CMS\Backend\Routing\UriBuilder;
use TYPO3\CMS\Backend\Template\Components\ButtonBar;
use TYPO3\CMS\Core\Http\ServerRequestFactory;
Expand All @@ -25,6 +26,13 @@

class ButtonBarHook
{
private PermissionsUtility $permissionsUtility;

public function __construct(PermissionsUtility $permissionsUtility)
{
$this->permissionsUtility = $permissionsUtility;
}

/**
* Retrieves and returns buttons for the button bar.
*
Expand All @@ -40,18 +48,19 @@ public function getButtons(array $params, ButtonBar $buttonBar)
$url = $this->buildUriToControllerAction();
$request = ServerRequestFactory::fromGlobals();
$currentUri = $request->getQueryParams()['route'];
if ('/module/file/FilelistList' === $currentUri) {
$iconFactory = GeneralUtility::makeInstance(IconFactory::class);
$button = $buttonBar->makeLinkButton();
$button->setShowLabelText(true);
$button->setIcon($iconFactory->getIcon('actions-image', Icon::SIZE_SMALL));
$button->setTitle($translatedMessage);
$button->setHref($url);
$buttons[ButtonBar::BUTTON_POSITION_LEFT][1][] = $button;

if ('/module/file/FilelistList' !== $currentUri || !$this->permissionsUtility->userHasAccessToImageGenerationPromptButton()) {
return $buttons;
}

$iconFactory = GeneralUtility::makeInstance(IconFactory::class);
$button = $buttonBar->makeLinkButton();
$button->setShowLabelText(true);
$button->setIcon($iconFactory->getIcon('actions-image', Icon::SIZE_SMALL));
$button->setTitle($translatedMessage);
$button->setHref($url);
$buttons[ButtonBar::BUTTON_POSITION_LEFT][1][] = $button;

return $buttons;
}

Expand Down
84 changes: 0 additions & 84 deletions Classes/Controller/AiImageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,12 @@

namespace DMK\MkContentAi\Controller;

use DMK\MkContentAi\Domain\Model\Image;
use DMK\MkContentAi\Http\Client\ImageApiInterface;
use DMK\MkContentAi\Http\Client\OpenAiClient;
use DMK\MkContentAi\Http\Client\StabilityAiClient;
use DMK\MkContentAi\Http\Client\StableDiffusionClient;
use DMK\MkContentAi\Service\FileService;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Http\JsonResponse;
use TYPO3\CMS\Core\Messaging\AbstractMessage;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Domain\Model\File;
Expand Down Expand Up @@ -93,32 +90,6 @@ public function initializeAction(): void
parent::initializeAction();
}

/**
* @return array{client?:ImageApiInterface, clientClass?:string, error?:string}
*/
private function initializeClient(): array
{
try {
$imageEngineKey = SettingsController::getImageAiEngine();
$client = GeneralUtility::makeInstance($this::GENERATOR_ENGINE[$imageEngineKey]);
if (is_a($client, ImageApiInterface::class)) {
return [
'client' => $client,
'clientClass' => get_class($client),
];
}
$errorTranslated = LocalizationUtility::translate('labelError', 'mkcontentai') ?? '';

return [
'error' => $errorTranslated,
];
} catch (\Exception $e) {
return [
'error' => $e->getMessage(),
];
}
}

/**
* @return ResponseInterface
*
Expand Down Expand Up @@ -151,61 +122,6 @@ protected function handleResponse(): ResponseInterface
return $this->htmlResponse($moduleTemplate->renderContent());
}

/**
* @return ResponseInterface
*
* @throws \TYPO3\CMS\Core\Resource\Exception\ExistingTargetFileNameException
*/
public function promptResultAjaxAction(ServerRequestInterface $request)
{
$clientResponse = $this->initializeClient();

if (isset($clientResponse['error'])) {
return new JsonResponse(
[
'error' => $clientResponse['error'],
],
500);
}
if (!isset($clientResponse['client'])) {
$translatedMessage = LocalizationUtility::translate('labelErrorClientIsNotDefined', 'mkcontentai') ?? '';

throw new \Exception($translatedMessage, 1623345720);
}
$client = $clientResponse['client'];

if (empty($request->getParsedBody()['promptText'])) {
$translatedMessage = LocalizationUtility::translate('labelErrorPromptText', 'mkcontentai') ?? '';

return new JsonResponse(
[
'error' => $translatedMessage,
],
500);
}
$text = $request->getParsedBody()['promptText'];

try {
$images = $client->image($text);
/** @var Image[] $images */
foreach ($images as $key => $image) {
$images[$key] = $image->toArray();
}
$data = [
'name' => get_class($client),
'images' => $images,
];
} catch (\Exception $e) {
return new JsonResponse(
[
'error' => $e->getMessage(),
],
500);
}

return new JsonResponse($data, 200);
}

/**
* @return ResponseInterface
*/
Expand Down
59 changes: 58 additions & 1 deletion Classes/Controller/AjaxController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,18 @@

namespace DMK\MkContentAi\Controller;

use DMK\MkContentAi\Domain\Model\Image;
use DMK\MkContentAi\Service\AiAltTextService;
use DMK\MkContentAi\Service\FileService;
use DMK\MkContentAi\Service\SiteLanguageService;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Http\JsonResponse;
use TYPO3\CMS\Core\Http\Response;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;

class AjaxController
class AjaxController extends BaseController
{
private FileService $fileService;

Expand Down Expand Up @@ -144,4 +146,59 @@ public function altTextSaveAction(ServerRequestInterface $request): ResponseInte

return $response->withHeader('Content-Type', 'text/plain')->withStatus(200);
}

/**
* @return ResponseInterface
*
* @throws \TYPO3\CMS\Core\Resource\Exception\ExistingTargetFileNameException
*/
public function promptResultAjaxAction(ServerRequestInterface $request)
{
$clientResponse = $this->initializeClient();

if (isset($clientResponse['error'])) {
return new JsonResponse(
[
'error' => $clientResponse['error'],
],
500);
}
if (!isset($clientResponse['client'])) {
$translatedMessage = LocalizationUtility::translate('labelErrorClientIsNotDefined', 'mkcontentai') ?? '';

throw new \Exception($translatedMessage, 1623345720);
}
$client = $clientResponse['client'];

if (empty($request->getParsedBody()['promptText'])) {
$translatedMessage = LocalizationUtility::translate('labelErrorPromptText', 'mkcontentai') ?? '';

return new JsonResponse(
[
'error' => $translatedMessage,
],
500);
}
$text = $request->getParsedBody()['promptText'];

try {
$images = $client->image($text);
/** @var Image[] $images */
foreach ($images as $key => $image) {
$images[$key] = $image->toArray();
}
$data = [
'name' => get_class($client),
'images' => $images,
];
} catch (\Exception $e) {
return new JsonResponse(
[
'error' => $e->getMessage(),
],
500);
}

return new JsonResponse($data, 200);
}
}
32 changes: 31 additions & 1 deletion Classes/Controller/BaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@

namespace DMK\MkContentAi\Controller;

use DMK\MkContentAi\Http\Client\ImageApiInterface;
use TYPO3\CMS\Backend\Template\ModuleTemplateFactory;
use TYPO3\CMS\Core\Page\PageRenderer;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\PathUtility;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;

class BaseController extends ActionController
{
Expand All @@ -30,7 +34,7 @@ public function injectModuleTemplateFactory(ModuleTemplateFactory $moduleTemplat

public function initializeAction(): void
{
$pageRenderer = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Page\PageRenderer::class);
$pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
$pageRenderer->loadRequireJsModule('TYPO3/CMS/Mkcontentai/MkContentAi');
$pageRenderer->addRequireJsConfiguration(
[
Expand All @@ -43,4 +47,30 @@ public function initializeAction(): void
]
);
}

/**
* @return array{client?:ImageApiInterface, clientClass?:string, error?:string}
*/
protected function initializeClient(): array
{
try {
$imageEngineKey = SettingsController::getImageAiEngine();
$client = GeneralUtility::makeInstance(AiImageController::GENERATOR_ENGINE[$imageEngineKey]);
if (is_a($client, ImageApiInterface::class)) {
return [
'client' => $client,
'clientClass' => get_class($client),
];
}
$errorTranslated = LocalizationUtility::translate('labelError', 'mkcontentai') ?? '';

return [
'error' => $errorTranslated,
];
} catch (\Exception $e) {
return [
'error' => $e->getMessage(),
];
}
}
}
7 changes: 7 additions & 0 deletions Classes/User/InlineControl/ImageGenerationButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

namespace DMK\MkContentAi\User\InlineControl;

use DMK\MkContentAi\Utility\PermissionsUtility;
use TYPO3\CMS\Backend\Form\NodeFactory;
use TYPO3\CMS\Core\Imaging\Icon;
use TYPO3\CMS\Core\Imaging\IconFactory;
Expand Down Expand Up @@ -49,6 +50,12 @@ public function __construct()
*/
public function render(array $parameters): string
{
$permissionsUtility = GeneralUtility::makeInstance(PermissionsUtility::class);

if (!$permissionsUtility->userHasAccessToImageGenerationPromptButton()) {
return '';
}

$translatedMessage = LocalizationUtility::translate('labelAiGenerateText', 'mkcontentai') ?? '';
$item = ' <div class="form-control-wrap"><button type="button" class="btn btn-default t3js-prompt" id="prompt">';
$item .= $this->iconFactory->getIcon('actions-image', Icon::SIZE_SMALL)->render().' ';
Expand Down
Loading

0 comments on commit 3fd0768

Please sign in to comment.