Spaces:
No application file
No application file
File size: 7,345 Bytes
d2897cd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
<?php
declare(strict_types=1);
namespace Mautic\CoreBundle\Twig\Extension;
use Mautic\CoreBundle\Twig\Helper\ButtonHelper;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
class ButtonExtension extends AbstractExtension
{
public function __construct(
protected ButtonHelper $buttonHelper,
protected RequestStack $requestStack,
protected UrlGeneratorInterface $router,
protected TranslatorInterface $translator
) {
}
public function getFunctions()
{
return [
new TwigFunction('buttonReset', [$this, 'reset'], ['is_safe' => ['all']]),
new TwigFunction('buttonAdd', [$this, 'addButton'], ['is_safe' => ['all']]),
new TwigFunction('buttonSetMenuLink', [$this, 'setMenuLink'], ['is_safe' => ['all']]),
new TwigFunction('buttonSetWrappingTags', [$this, 'setWrappingTags'], ['is_safe' => ['all']]),
new TwigFunction('buttonSetGroupType', [$this, 'setGroupType'], ['is_safe' => ['all']]),
new TwigFunction('buttonGetCount', [$this, 'getButtonCount']),
new TwigFunction('buttonsRender', [$this, 'render'], ['is_safe' => ['all']]),
new TwigFunction('buttonsAdd', [$this, 'addButtons'], ['is_safe' => ['all']]),
new TwigFunction('buttonsAddFromTemplate', [$this, 'addButtonsFromTemplate'], ['is_safe' => ['all']]),
];
}
public function reset(string $location, string $groupType = ButtonHelper::TYPE_GROUP, $item = null): void
{
$this->buttonHelper->reset(
$this->requestStack->getCurrentRequest(),
$location,
$groupType,
$item
);
}
/**
* @param array<string,mixed> $button
*/
public function addButton(array $button): void
{
$this->buttonHelper->addButton($button);
}
public function setMenuLink(?string $menuLink): void
{
$this->buttonHelper->setMenuLink($menuLink);
}
public function setWrappingTags(?string $wrapOpeningTag, ?string $wrapClosingTag): void
{
$this->buttonHelper->setWrappingTags($wrapOpeningTag, $wrapClosingTag);
}
public function setGroupType(string $groupType): void
{
$this->buttonHelper->setGroupType($groupType);
}
public function getButtonCount(): int
{
return $this->buttonHelper->getButtonCount();
}
/**
* @param array<array<string,mixed>> $buttons
*/
public function addButtons(array $buttons): void
{
$this->buttonHelper->addButtons($buttons);
}
public function render(string $dropdownHtml = '', string $closingDropdownHtml = ''): string
{
return $this->buttonHelper->renderButtons($dropdownHtml, $closingDropdownHtml);
}
/**
* @param array<string,bool> $templateButtons
* @param array<string,string> $query
* @param array<string,string> $editAttr
* @param array<string,string> $routeVars
* @param mixed $item
*/
public function addButtonsFromTemplate(
array $templateButtons,
array $query,
string $actionRoute,
string $indexRoute,
string $langVar,
string $nameGetter,
array $editAttr = [],
array $routeVars = [],
$item = null,
?string $tooltip = null
): void {
foreach ($templateButtons as $action => $enabled) {
if (!$enabled) {
continue;
}
$path = false;
$primary = false;
$priority = 0;
switch ($action) {
case 'clone':
case 'abtest':
$actionQuery = [
/**
* If the item has the getVariantParent(), it probably implements VariantEntityInterface,
* but that doesn't have a getId() method so we can't do $item instanceof VariantEntityInterface here.
*/
'objectId' => ('abtest' == $action && method_exists($item, 'getVariantParent') && $item->getVariantParent())
? $item->getVariantParent()->getId() : $item->getId(),
];
$icon = ('clone' == $action) ? 'file-copy-line' : 'a-b';
$path = $this->router->generate($actionRoute, array_merge(['objectAction' => $action], $actionQuery, $query));
break;
case 'close':
$closeParameters = $routeVars['close'] ?? [];
$icon = 'close-line';
$path = $this->router->generate($indexRoute, $closeParameters);
$primary = true;
$priority = 200;
break;
case 'new':
case 'edit':
$actionQuery = ('edit' == $action) ? ['objectId' => $item->getId()] : [];
$icon = ('edit' == $action) ? 'edit-line' : 'add-line';
$path = $this->router->generate($actionRoute, array_merge(['objectAction' => $action], $actionQuery, $query));
$primary = true;
break;
case 'delete':
$this->buttonHelper->addButton(
[
'confirm' => [
'message' => $this->translator->trans(
'mautic.'.$langVar.'.form.confirmdelete',
['%name%' => $item->$nameGetter().' ('.$item->getId().')']
),
'confirmAction' => $this->router->generate(
$actionRoute,
array_merge(['objectAction' => 'delete', 'objectId' => $item->getId()], $query)
),
'template' => 'delete',
'btnClass' => false,
],
'priority' => -1,
]
);
break;
}
if ($path) {
$mergeAttr = (!in_array($action, ['edit', 'new'])) ? [] : $editAttr;
$this->buttonHelper->addButton(
[
'attr' => array_merge(
[
'class' => 'btn btn-default',
'href' => $path,
'data-toggle' => 'ajax',
],
$mergeAttr
),
'iconClass' => 'ri-'.$icon,
'btnText' => $this->translator->trans('mautic.core.form.'.$action),
'priority' => $priority,
'primary' => $primary,
'tooltip' => $tooltip,
]
);
}
}
}
}
|