Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kappenberg committed Jan 31, 2023
1 parent 33602a0 commit 83dee57
Show file tree
Hide file tree
Showing 15 changed files with 793 additions and 0 deletions.
31 changes: 31 additions & 0 deletions Classes/Backend/BackendJsCss.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
declare(strict_types=1);

namespace Ressourcenmangel\Simplereference\Backend;

use TYPO3\CMS\Core\Information\Typo3Version;
use TYPO3\CMS\Core\Utility\GeneralUtility;

class BackendJsCss
{
/**
* @param $params array The already used JS and CSS files and the header and footer data
* @param $ref mixed|object The back reference to the TYPO3\CMS\Core\Page\PageRenderer class
*/
public function addJsCss($params, $ref): void
{
if (TYPO3_MODE == 'BE') {

$typo3Version = (int)GeneralUtility::makeInstance(Typo3Version::class)->getMajorVersion();
$ref->addCssFile('EXT:simplereference/Resources/Public/Backend/Css/styles.css');
if ($typo3Version === 10) {
$ref->addJsFooterFile('EXT:simplereference/Resources/Public/JavaScript/Main.v10.js');
}
if ($typo3Version === 11) {
$ref->addJsFooterFile('EXT:simplereference/Resources/Public/JavaScript/Main.v11.js');
}

$ref->addInlineLanguageLabelFile('EXT:simplereference/Resources/Private/Language/locallang_modal.xlf');
}
}
}
203 changes: 203 additions & 0 deletions Classes/Backend/Helper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
<?php

declare(strict_types=1);

namespace Ressourcenmangel\Simplereference\Backend;

use Doctrine\DBAL\DBALException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Backend\Clipboard\Clipboard;
use TYPO3\CMS\Backend\Routing\Exception\RouteNotFoundException;
use TYPO3\CMS\Backend\Routing\UriBuilder;
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
use TYPO3\CMS\Core\Http\JsonResponse;
use TYPO3\CMS\Core\Utility\GeneralUtility;


class Helper
{
/**
* Processes all AJAX calls and returns a JSON formatted string
*
* Possible important incoming array values in $request->getParsedBody():
*
* Add new reference on top of a normal colPos
* [addPanelId] = colpos-1-page-50-62c20b4d2e903078051133 // insert panel HTML Id
* [addColpos] => 1 // colPos
* [addPageUid] => 50 // page uid
* [addLanguageuid] => 0 // sys_language_uid
*
* Add new reference on top of a container element
* [addPanelId] = colpos-201-page-50-62c20b4d2e903078051133 // insert panel HTML Id
* [addColpos] => 283-201
* [addPageUid] => 50 // page uid
* [addLanguageuid] => 0
*
* Add a new reference below existing content element 'addUid' : contentElementUid
* [addPanelId] = colpos-201-page-50-62c20b4d2e903078051133 // insert panel HTML Id
* [addTable] => tt_content
* [addUid] => 281
* [addPageUid] => 50 // page uid
* [addLanguageuid] => 0
*
* @param ServerRequestInterface $request
* @return ResponseInterface
* @throws RouteNotFoundException
*/
public function getData(ServerRequestInterface $request): ResponseInterface
{
$data = ['info' => 'null'];
$beUser = $this->getBackendUser();

// no backend user? return...
if (!is_object($beUser)) {
return new JsonResponse($data);
}

// the incoming get or post
$parsedBody = $request->getParsedBody();
$references = $this->getReferences($request);

// early return if we have nothing to add
if (!count($references)) {
return new JsonResponse($data);
}
$data['references'] = $references;


if (isset($parsedBody['addReference'])) {

$records = implode(',', $references);
$newUid = (microtime(true) * 10000);
$infoArray = GeneralUtility::intExplode('-',$parsedBody['addPanelId'],true);

if (isset($parsedBody['addTable'])) {
$pid = '-' . $parsedBody['addUid'];
} else {
$pid = $parsedBody['addPageUid'];
}

$data['data'] = [
'tt_content' => [
'NEW' . $newUid => [
'CType' => 'shortcut',
'header' => 'Reference',
//'header_layout' => '100',
'colPos' => (int)$infoArray[1], //end($colPosArray),
'sys_language_uid' => (int)$parsedBody['addLanguageuid'],
'pid' => (int)$pid,
'records' => $records,
],
],
];

// add EXT:container parent
if (isset($parsedBody['addTable'])) {
$currentRecord = $this->getRecordByUid((int)$parsedBody['addUid']);
if (array_key_exists('tx_container_parent', $currentRecord)) {
$data['data']['tt_content']['NEW' . $newUid]['tx_container_parent'] = $currentRecord['tx_container_parent'];
}
}

// or override if we are on top of a EXT:container colPos
if (isset($parsedBody['addColpos'])) {
$colPosArray = GeneralUtility::intExplode('-', $parsedBody['addColpos'], true);
if (count($colPosArray) === 2) {
$data['data']['tt_content']['NEW' . $newUid]['tx_container_parent'] = $colPosArray[0];
}
}

// create redirect uri
$uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
$route = 'web_layout';
$parameters = [
'id' => $parsedBody['addPageUid'],
't' => time(),
];
$referenceType = '';
$data['redirect'] = $uriBuilder->buildUriFromRoute($route, $parameters, $referenceType) . '#';

// update info
$data['info'] = 'success';
}

return new JsonResponse($data);
}

/**
* @param ServerRequestInterface $request
* @return array
*/
private function getReferences(ServerRequestInterface $request): array
{
$clipBoard = GeneralUtility::makeInstance(Clipboard::class);
$clipBoard->initializeClipboard($request);
// yes, normal pad can contain only one record, but why not prepare for other pads, too
$clipBoardNormal = $clipBoard->clipData['normal'];

$references = [];
if (isset($clipBoardNormal['el']) && count($clipBoardNormal['el'])) {
foreach ($clipBoardNormal['el'] as $key => $val) {
$tableAndUid = explode('|', $key);
if ($tableAndUid[0] === 'tt_content') {
$referenceCe = $this->getRecordByUid((int)$tableAndUid[1]);
if ($referenceCe) {
// we don't want Matroschka shortcuts
if ($referenceCe['CType'] === 'shortcut') {
$references[] = $referenceCe['records'];
} else {
$references[] = $tableAndUid[0] . '_' . $tableAndUid[1];
}
}
}
}
}

return $references;
}

/**
* Get the record where the reference
*
* @param int $uid
* @return array
* @throws DBALException
*/
protected function getRecordByUid(int $uid = 0): array
{
$data = [];

if ($uid) {
$table = 'tt_content';
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable($table);
// we need hidden ... records, too
$queryBuilder->getRestrictions()
->removeAll()
->add(GeneralUtility::makeInstance(DeletedRestriction::class));
$data = $queryBuilder
->select('*')
->from($table)
->where('uid = :theUid')
->setParameter('theUid', $uid)
->execute()
->fetch();
}

return $data;
}


/**
* Returns the current BE user.
*
* @return BackendUserAuthentication
*/
protected function getBackendUser(): BackendUserAuthentication
{
return $GLOBALS['BE_USER'];
}
}
24 changes: 24 additions & 0 deletions Classes/Backend/Preview/ShortcutPreviewRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Ressourcenmangel\Simplereference\Backend\Preview;

use TYPO3\CMS\Backend\Preview\StandardContentPreviewRenderer;
use TYPO3\CMS\Backend\View\BackendLayout\Grid\GridColumnItem;
use TYPO3\CMS\Backend\View\PageLayoutContext;

class ShortcutPreviewRenderer extends StandardContentPreviewRenderer
{
/**
* @var PageLayoutContext
*/
protected $context;

public function renderPageModulePreviewContent(GridColumnItem $item): string
{
$content = parent::renderPageModulePreviewContent($item);

return '<span class="simplereference-shortcut">' . $content . '</span>';
}
}
7 changes: 7 additions & 0 deletions Configuration/Backend/AjaxRoutes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
return [
'simplereference_get_data' => [
'path' => '/simplereference/get-data',
'target' => \Ressourcenmangel\Simplereference\Backend\Helper::class . '::getData',
]
];
12 changes: 12 additions & 0 deletions Configuration/TCA/Overrides/tt_content.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

call_user_func(static function () {
$fluidBasedPageModule = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Configuration\Features::class)->isFeatureEnabled('fluidBasedPageModule');
if (false === $fluidBasedPageModule) {
// implement Standard Preview renderer if you need it for older TYPO3 Versions,
// or use Hook in ext_localconf.php;
// $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['cms/layout/class.tx_cms_layout.php']['tt_content_drawItem']
} else {
$GLOBALS['TCA']['tt_content']['types']['shortcut']['previewRenderer'] = \Ressourcenmangel\Simplereference\Backend\Preview\ShortcutPreviewRenderer::class;
}
});
20 changes: 20 additions & 0 deletions Resources/Private/Language/de.locallang_modal.xlf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<xliff version="1.0">
<file source-language="en" datatype="plaintext" original="EXT:simplereference/Resources/Private/Language/locallang_layout.xlf" date="2022-02-02T20:22:32Z" product-name="simplereference">
<header/>
<body>
<trans-unit id="simplereference.modal.title">
<target>Referenz einfügen</target>
</trans-unit>
<trans-unit id="simplereference.modal.text">
<target>Soll eine Referenz an dieser Position eingefügt werden?</target>
</trans-unit>
<trans-unit id="simplereference.modal.button.cancel">
<target>Abbrechen</target>
</trans-unit>
<trans-unit id="simplereference.modal.button.paste">
<target>Einfügen</target>
</trans-unit>
</body>
</file>
</xliff>
20 changes: 20 additions & 0 deletions Resources/Private/Language/locallang_modal.xlf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<xliff version="1.0">
<file source-language="en" datatype="plaintext" original="EXT:simplereference/Resources/Private/Language/locallang_layout.xlf" date="2022-02-02T20:22:32Z" product-name="simplereference">
<header/>
<body>
<trans-unit id="simplereference.modal.title">
<source>Paste Reference</source>
</trans-unit>
<trans-unit id="simplereference.modal.text">
<source>Do you want to paste a reference to this position?</source>
</trans-unit>
<trans-unit id="simplereference.modal.button.cancel">
<source>Cancel</source>
</trans-unit>
<trans-unit id="simplereference.modal.button.paste">
<source>Paste</source>
</trans-unit>
</body>
</file>
</xliff>
6 changes: 6 additions & 0 deletions Resources/Public/Backend/Css/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.simplereference-shortcut {
display: block;
padding: 10px;
margin: 5px 0 10px;
background-color: rgba(80, 160, 255, 0.25);
}
15 changes: 15 additions & 0 deletions Resources/Public/Icons/Extension.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions Resources/Public/Icons/simplereference_paste.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 83dee57

Please sign in to comment.