-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add backend wizard ui with popup wizard method
- Loading branch information
Showing
4 changed files
with
193 additions
and
4 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,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 | ||
); | ||
} | ||
} |
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,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; | ||
} | ||
|
||
|
||
} |