Skip to content

Commit

Permalink
#340 add support for security policy header
Browse files Browse the repository at this point in the history
  • Loading branch information
digedag committed Nov 18, 2023
1 parent 8358f84 commit 0f377f1
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 19 deletions.
100 changes: 100 additions & 0 deletions Classes/Backend/Form/Element/EnhancedLinkButton.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

namespace Sys25\RnBase\Backend\Form\Element;

use Sys25\RnBase\Utility\T3General;
use TYPO3\CMS\Backend\Template\Components\Buttons\LinkButton;

/***************************************************************
* Copyright notice
*
* (c) 2023 Rene Nitzsche (rene@system25.de)
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/

/**
* Erweitert den LinkButton von TYPO3 um zusätzliche Funktionen.
* - man kann links ohne Icon setzen
* - der Hover-Tooltip kann abweichend vom Label sein.
*/
class EnhancedLinkButton extends LinkButton
{
/**
* HREF attribute of the link.
*
* @var string
*/
private $hoverText = '';

/**
* Get hoverText.
*
* @return string
*/
public function getHoverText()
{
return ''.$this->hoverText;
}

/**
* Set href.
*
* @param string $href HREF attribute
*
* @return LinkButton
*/
public function setHoverText($value)
{
$this->hoverText = $value;

return $this;
}

/**
* Renders the markup for the button.
*
* @return string
*/
public function render()
{
$attributes = [
'href' => $this->getHref(),
'class' => 'btn btn-sm btn-default '.$this->getClasses(),
'title' => $this->getHoverText() ?: $this->getTitle(),
];
$labelText = '';
if ($this->showLabelText) {
$labelText = ' '.$this->title;
}
foreach ($this->dataAttributes as $attributeName => $attributeValue) {
$attributes['data-'.$attributeName] = $attributeValue;
}
if ($this->isDisabled()) {
$attributes['disabled'] = 'disabled';
$attributes['class'] .= ' disabled';
}
$attributesString = T3General::implodeAttributes($attributes, true);

$icon = $this->getIcon() ? $this->getIcon()->render() : '';

return '<a '.$attributesString.'>'
.$icon.htmlspecialchars($labelText)
.'</a>';
}
}
61 changes: 42 additions & 19 deletions Classes/Backend/Form/ToolBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Sys25\RnBase\Backend\Form;

use Sys25\RnBase\Backend\Form\Element\EnhancedLinkButton;
use Sys25\RnBase\Backend\Form\Element\InputText;
use Sys25\RnBase\Backend\Module\IModule;
use Sys25\RnBase\Backend\Template\Override\DocumentTemplate;
Expand All @@ -16,7 +17,6 @@
use Sys25\RnBase\Utility\T3General;
use Sys25\RnBase\Utility\TYPO3;
use tx_rnbase;
use TYPO3\CMS\Backend\Template\Components\ButtonBar;
use TYPO3\CMS\Core\Localization\LanguageService;
use TYPO3\CMS\Core\Page\JavaScriptModuleInstruction;

Expand Down Expand Up @@ -68,6 +68,8 @@ class ToolBox
public const OPTION_TITLE = 'title';

public const OPTION_CONFIRM = 'confirm';
public const OPTION_ICON_NAME = 'icon-name';
public const OPTION_HOVER_TEXT = 'hover';

public const OPTION_PARAMS = 'params';

Expand All @@ -81,13 +83,6 @@ class ToolBox
/** @var LanguageService */
private $lang;

/**
* not used as button bar, but as simple factory.
*
* @var ButtonBar
*/
private $buttonBar;

/** @var \TYPO3\CMS\Backend\Routing\UriBuilder */
private $uriBuilder;

Expand All @@ -103,8 +98,6 @@ public function init(DocumentTemplate $doc, IModule $module)

$this->uriBuilder = tx_rnbase::makeInstance(\TYPO3\CMS\Backend\Routing\UriBuilder::class);

$this->buttonBar = tx_rnbase::makeInstance(ButtonBar::class);

// TCEform für das Formular erstellen
$this->form = tx_rnbase::makeInstance(FormBuilder::class);
$this->form->initDefaultBEmode();
Expand Down Expand Up @@ -217,6 +210,21 @@ public function createShowLink($pid, $label, $urlParams = '', $options = [])
return '<a href="#" '.$class.' onclick="'.htmlspecialchars($jsCode).'" '.$title.'>'.$label.'</a>';
}

/**
* @return EnhancedLinkButton
*/
private function makeLinkButton($uri, $label = '')
{
$btn = new EnhancedLinkButton();
$btn->setHref($uri);
if ($label) {
$btn->setTitle($label)
->setShowLabelText(true);
}

return $btn;
}

/**
* Erstellt einen Link zur Erstellung eines neuen Datensatzes
* Possible options:
Expand All @@ -239,10 +247,7 @@ public function createNewLink($table, $pid, $label = 'New', $options = [])
$uri .= $this->buildDefVals($options);

$image = Icons::getSpriteIcon('actions-document-new', ['asIcon' => true]);
$recordButton = $this->buttonBar->makeLinkButton()
->setHref($uri)
->setTitle($label)
->setShowLabelText(true)
$recordButton = $this->makeLinkButton($uri, $label)
->setIcon($image);

$class = array_key_exists('class', $options) ? htmlspecialchars($options['class']) : '';
Expand Down Expand Up @@ -271,10 +276,7 @@ public function createEditLink($editTable, $editUid, $label = 'Edit', $options =
$uri = $this->buildEditUri($editTable, $editUid, 'edit', $options);

$image = Icons::getSpriteIcon('actions-document-open', ['asIcon' => true]);
$recordButton = $this->buttonBar->makeLinkButton()
->setHref($uri)
->setTitle($label)
->setShowLabelText(true)
$recordButton = $this->makeLinkButton($uri, $label)
->setIcon($image);

$class = array_key_exists('class', $options) ? htmlspecialchars($options['class']) : '';
Expand Down Expand Up @@ -635,7 +637,28 @@ public function createModuleLink(array $params, $pid, $label, array $options = [
// ensure pid is set even on POST requests.
$params['id'] = $pid;
}
$location = $this->getLinkThisScript(false, ['params' => $params]);
$uri = $this->getLinkThisScript(false, ['params' => $params]);

$recordButton = $this->makeLinkButton($uri, $label);

if (isset($options[self::OPTION_HOVER_TEXT])) {
$recordButton->setHoverText($options[self::OPTION_HOVER_TEXT]);
}

if (isset($options[self::OPTION_ICON_NAME])) {
$icon = Icons::getSpriteIcon($options[self::OPTION_ICON_NAME], ['asIcon' => true]);
$recordButton->setIcon($icon);
}

$class = array_key_exists('class', $options) ? htmlspecialchars($options['class']) : '';

if (isset($options[self::OPTION_CONFIRM]) && strlen($options[self::OPTION_CONFIRM]) > 0) {
$class .= ' t3js-modal-trigger';
$recordButton->setDataAttributes(['content' => $options[self::OPTION_CONFIRM]]);
}
$recordButton->setClasses($class);

return $recordButton->render();

$jsCode = "window.location.href='".$location."'; return false;";

Expand Down

0 comments on commit 0f377f1

Please sign in to comment.