-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from pagemachine/rework_settings
Rework settings and generation page
- Loading branch information
Showing
56 changed files
with
3,071 additions
and
668 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Pagemachine\AItools\Controller\Backend; | ||
|
||
use Pagemachine\AItools\Service\ImageMetaDataService; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use TYPO3\CMS\Core\Resource\AbstractFile; | ||
use TYPO3\CMS\Core\Resource\FileInterface; | ||
use TYPO3\CMS\Core\Resource\ResourceFactory; | ||
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController; | ||
|
||
class CreditsController extends ActionController | ||
{ | ||
public function __construct( | ||
private readonly ImageMetaDataService $imageMetaDataService, | ||
private readonly ResourceFactory $resourceFactory, | ||
) { | ||
} | ||
|
||
protected function imageRecognition(ServerRequestInterface $request): string | ||
{ | ||
$fileIdentifier = $this->getParameter($request, 'fileIdentifier'); | ||
if ($fileIdentifier) { | ||
$fileObject = $this->resourceFactory->retrieveFileOrFolderObject($fileIdentifier); | ||
if ($fileObject instanceof FileInterface) { | ||
if ($fileObject->getType() !== AbstractFile::FILETYPE_IMAGE) { | ||
return ''; | ||
} | ||
|
||
return $this->imageMetaDataService->priceForImageDescription($fileObject, $this->getParameter($request, 'textPrompt')); | ||
} | ||
} | ||
|
||
return ''; | ||
} | ||
|
||
|
||
public function ajaxCreditsAction(ServerRequestInterface $request): ResponseInterface | ||
{ | ||
$text = ''; | ||
try { | ||
switch ($this->getParameter($request, 'type')) { | ||
case 'imageRecognition': | ||
$text = $this->imageRecognition($request); | ||
break; | ||
case 'remaining': | ||
break; | ||
default: | ||
throw new \Exception('Invalid type'); | ||
} | ||
} catch (\Exception $e) { | ||
return $this->responseFactory->createResponse() | ||
->withHeader('Content-Type', 'application/json') | ||
->withBody($this->streamFactory->createStream(json_encode(['error' => $e->getMessage()], JSON_THROW_ON_ERROR))); | ||
} | ||
|
||
return $this->responseFactory->createResponse() | ||
->withHeader('Content-Type', 'application/json') | ||
->withBody($this->streamFactory->createStream(json_encode(['credits' => $text]))); | ||
} | ||
|
||
protected function getParameter(ServerRequestInterface $request, string $key): string | ||
{ | ||
$parsedBody = $request->getParsedBody(); | ||
$queryParams = $request->getQueryParams(); | ||
|
||
return $parsedBody[$key] ?? $queryParams[$key] ?? ''; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Pagemachine\AItools\Controller\Backend; | ||
|
||
use Pagemachine\AItools\Domain\Repository\PromptRepository; | ||
use Psr\Http\Message\ResponseInterface; | ||
use TYPO3\CMS\Backend\Routing\UriBuilder; | ||
use TYPO3\CMS\Backend\Template\Components\ButtonBar; | ||
use TYPO3\CMS\Backend\Template\ModuleTemplate; | ||
use TYPO3\CMS\Backend\Template\ModuleTemplateFactory; | ||
use TYPO3\CMS\Core\Imaging\Icon; | ||
use TYPO3\CMS\Core\Imaging\IconFactory; | ||
use TYPO3\CMS\Core\Localization\LanguageService; | ||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController; | ||
|
||
class PromptsController extends ActionController | ||
{ | ||
|
||
public function __construct( | ||
private readonly PromptRepository $promptRepository, | ||
private readonly ModuleTemplateFactory $moduleTemplateFactory, | ||
private readonly IconFactory $iconFactory, | ||
) { | ||
} | ||
|
||
private function setDocHeader(ModuleTemplate $moduleTemplate, $requestUri): void | ||
{ | ||
$uriBuilder = GeneralUtility::makeInstance(UriBuilder::class); | ||
|
||
$buttonBar = $moduleTemplate->getDocHeaderComponent()->getButtonBar(); | ||
|
||
$newRecordButton = $buttonBar->makeLinkButton() | ||
->setHref((string)$uriBuilder->buildUriFromRoute( | ||
'record_edit', | ||
[ | ||
'edit' => [ | ||
'tx_aitools_domain_model_prompt' => ['new'], | ||
], | ||
'returnUrl' => (string)$requestUri, | ||
] | ||
)) | ||
->setTitle('Add') | ||
->setIcon($this->iconFactory->getIcon('actions-add', Icon::SIZE_SMALL)); | ||
|
||
$buttonBar->addButton($newRecordButton, ButtonBar::BUTTON_POSITION_LEFT, 10); | ||
} | ||
|
||
public function listAction(): ResponseInterface | ||
{ | ||
$moduleTemplate = $this->moduleTemplateFactory->create($this->request); | ||
|
||
$requestUri = $this->request->getAttribute('normalizedParams')->getRequestUri(); | ||
|
||
$this->view->assignMultiple([ | ||
'prompts' => $this->promptRepository->listAllPrompts(), | ||
'returnUrl' => $requestUri, | ||
]); | ||
|
||
|
||
$moduleTemplate->setContent($this->view->render()); | ||
$this->setDocHeader($moduleTemplate, $requestUri); | ||
return $this->htmlResponse($moduleTemplate->renderContent()); | ||
} | ||
|
||
protected function getLanguageService(): LanguageService | ||
{ | ||
return $GLOBALS['LANG']; | ||
} | ||
} |
Oops, something went wrong.