Skip to content

Commit

Permalink
add backend wizard ui with popup wizard method
Browse files Browse the repository at this point in the history
  • Loading branch information
koertho committed Nov 15, 2024
1 parent 777fdc0 commit 9eb5b0f
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 4 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
"require": {
"ext-dom": "*",
"ext-simplexml": "*",
"php": "^7.2|^8.0",
"php": "^7.4 || ^8.0",
"ausi/slug-generator": "^1.1",
"contao/core-bundle": "^4.4",
"contao/core-bundle": "^4.9",
"doctrine/dbal": "^2.13 || ^3.0",
"hackzilla/password-generator": "^1.4",
"psr/log": "^1.0 || ^2.0 || ^3.0",
Expand All @@ -17,7 +17,7 @@
"symfony/config": "^3.4|^4.4|^5.0",
"symfony/deprecation-contracts": "^1.0 || ^2.0 || ^3.0",
"symfony/filesystem": "^3.4|^4.4|^5.0",
"symfony/http-foundation": "^3.4|^4.4|^5.0",
"symfony/http-foundation": "^4.4 || ^5.0",
"symfony/http-kernel": "^3.4|^4.4|^5.0",
"symfony/monolog-bridge": "^3.4|^4.4|^5.0",
"symfony/string": "^5.2",
Expand Down
95 changes: 95 additions & 0 deletions src/Util/BackendUiUtil.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

namespace HeimrichHannot\UtilsBundle\Util;

use Contao\CoreBundle\Framework\ContaoFramework;
use Contao\Image;
use Contao\StringUtil;
use HeimrichHannot\UtilsBundle\Util\Html\HtmlUtil;
use HeimrichHannot\UtilsBundle\Util\Routing\RoutingUtil;
use HeimrichHannot\UtilsBundle\Util\Ui\PopupWizardLinkOptions;

class BackendUiUtil
{
private RoutingUtil $routingUtil;
private ContaoFramework $framework;
private HtmlUtil $htmlUtil;

public function __construct(
RoutingUtil $routingUtil,
ContaoFramework $framework,
HtmlUtil $htmlUtil
)
{
$this->routingUtil = $routingUtil;
$this->framework = $framework;
$this->htmlUtil = $htmlUtil;
}

/**
* Create a popup wizard link or url.
*/
public function popupWizardLink(array $parameter, PopupWizardLinkOptions $config): string
{
$parameter['popup'] = 1;
$parameter['nb'] = 1;

$url = $this->routingUtil->generateBackendRoute($parameter, true, true, $config->route);
if ($config->urlOnly) {
return $url;
}

$attributes = $config->attributes;
if (empty($config->title)) {
$title = $GLOBALS['TL_LANG']['tl_content']['edit'][0];
} else {
$title = StringUtil::specialchars($config->title);
}

if (!isset($attributes['title'])) {
$attributes['title'] = $title;
}

if (!isset($attributes['style'])) {
$attributes['style'] = $config->style;
}

// onclick
if (empty($attributes['onclick'])) {
if (empty($config->popupTitle)) {
$popupTitle = $title;
} else {
$popupTitle = $config->popupTitle;
}

$attributes['onclick'] = sprintf(
'Backend.openModalIframe({\'width\':%s,\'title\':\'%s'.'\',\'url\':this.href});return false',
$config->width,
$popupTitle
);
}

// link text and icon
$linkText = '';

if (!empty($config->icon)) {
/** @var Image $image */
$image = $this->framework->getAdapter(Image::class);
$linkText .= $image->getHtml('alias.svg', $title, 'style="vertical-align:top"');
}

if (!empty($config->linkText)) {
$linkText = trim($linkText . ' '.$config->linkText);
}

// remove href from attributes if set
unset($attributes['href']);

return sprintf(
'<a href="%s" %s>%s</a>',
$url,
$this->htmlUtil->generateAttributeString($attributes),
$linkText
);
}
}
2 changes: 1 addition & 1 deletion src/Util/Routing/RoutingUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function generateBackendRoute(array $params = [], bool $addToken = true,
/**
* @codeCoverageIgnore
*/
public static function getSubscribedServices()
public static function getSubscribedServices(): array
{
return [
'?'.ContaoCsrfTokenManager::class,
Expand Down
94 changes: 94 additions & 0 deletions src/Util/Ui/PopupWizardLinkOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace HeimrichHannot\UtilsBundle\Util\Ui;

class PopupWizardLinkOptions
{
/** @var string The route to generate the link */
public string $route = 'contao_backend';
/** @var bool If only the url should be returned instead of a complete link element */
public bool $urlOnly = false;
/** @var string The title of the link */
public string $title = '';
/** @var string Override the default css style properties */
public string $style = 'padding-left: 5px; padding-top: 2px; display: inline-block;';
/** *@var array Additional Link attributes as key value pairs. Will override title and style option. href is not allowed and will be removed from list. */
public array $attributes = [];
/** @var string Link icon to show as link text. Overrides default icon. */
public string $icon = 'alias.svg';
/** @var string A linkTitle to show as link text. Will be displayed after the link icon. Default empty. */
public string $linkText = '';
/** @var int The width of the popup */
public int $width = 991;
/** @var string The title of the popup */
public string $popupTitle = '';

public function __construct()
{
}

/**
* Set the route to generate the link.
*/
public function setRoute(string $route): PopupWizardLinkOptions
{
$this->route = $route;
return $this;
}

/**
* If only the url should be returned instead of a complete link element.
*/
public function setUrlOnly(bool $urlOnly): PopupWizardLinkOptions
{
$this->urlOnly = $urlOnly;
return $this;
}

/**
* Set the title of the link.
*/
public function setTitle(string $title): PopupWizardLinkOptions
{
$this->title = $title;
return $this;
}

/**
* Override the default css style properties.
*/
public function setStyle(string $style): PopupWizardLinkOptions
{
$this->style = $style;
return $this;
}

/**
* Set additional link attributes as key value pairs. Will override title and style option. href and onclick are not allowed and will be removed from list.
*/
public function setAttributes(array $attributes): PopupWizardLinkOptions
{
$this->attributes = $attributes;
return $this;
}

/**
* Add an additional attribute to the link.
*/
public function setIcon(string $icon): PopupWizardLinkOptions
{
$this->icon = $icon;
return $this;
}

/**
* Override the default link text. Will be displayed after the link icon.
*/
public function setLinkText(string $linkText): PopupWizardLinkOptions
{
$this->linkText = $linkText;
return $this;
}


}

0 comments on commit 9eb5b0f

Please sign in to comment.