Completed
Push — master ( 6c5c5c...ba14b8 )
by WEBEWEB
05:13
created

ButtonTableTwigExtension::getFunctions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
/**
4
 * This file is part of the bootstrap-bundle package.
5
 *
6
 * (c) 2018 WEBEWEB
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WBW\Bundle\BootstrapBundle\Twig\Extension\Table;
13
14
use Symfony\Component\Routing\RouterInterface;
15
use Symfony\Component\Translation\TranslatorInterface;
16
use Twig_SimpleFunction;
17
use WBW\Bundle\BootstrapBundle\Twig\Extension\Component\ButtonComponentTwigExtension;
18
use WBW\Library\Core\Utility\ArrayUtility;
19
20
/**
21
 * Button table Twig extension.
22
 *
23
 * @author webeweb <https://github.com/webeweb/>
24
 * @package WBW\Bundle\BootstrapBundle\Twig\Extension\Table
25
 * @final
26
 */
27
final class ButtonTableTwigExtension extends AbstractTableTwigExtension {
28
29
    /**
30
     * Service name.
31
     *
32
     * @var string
33
     */
34
    const SERVICE_NAME = "webeweb.bundle.bootstrapbundle.twig.extension.table.button";
35
36
    /**
37
     * Router.
38
     *
39
     * @var RouterInterface
40
     */
41
    private $router;
42
43
    /**
44
     * Translator.
45
     *
46
     * @var TranslatorInterface
47
     */
48
    private $translator;
49
50
    /**
51
     * Constructor.
52
     *
53
     * @param RouterInterface $router The router service.
54
     * @param TranslatorInterface $translator The translator service.
55
     */
56
    public function __construct(RouterInterface $router, TranslatorInterface $translator) {
57
        parent::__construct();
58
        $this->router     = $router;
59
        $this->translator = $translator;
60
    }
61
62
    /**
63
     * Displays a Bootstrap default row buttons.
64
     *
65
     * @param array $args The arguments.
66
     * @return string Returns the Bootstrap default form buttons.
67
     */
68
    public function bootstrapDefaultRowButtonsFunction(array $args = []) {
69
70
        // Initialize the buttons.
71
        $editButton   = $this->bootstrapEditRowButtonFunction(["route" => ArrayUtility::get($args, "edit_route"), "route_arguments" => ArrayUtility::get($args, "edit_arguments")]);
72
        $deleteButton = $this->bootstrapDeleteRowButtonFunction(["route" => ArrayUtility::get($args, "delete_route"), "route_arguments" => ArrayUtility::get($args, "delete_arguments")]);
73
74
        // Return the HTML.
75
        return implode(" ", [$editButton, $deleteButton]);
76
    }
77
78
    /**
79
     * Displays a Bootstrap delete row button.
80
     *
81
     * @param array $args The arguments.
82
     * @return string Returns the Bootstrap delete row button.
83
     */
84
    public function bootstrapDeleteRowButtonFunction(array $args = []) {
85
86
87
        // Translate the label.
88
        $txt = $this->translator->trans("label.delete", [], "BootstrapBundle");
89
90
        // Generate the URL.
91
        $url = $this->router->generate(ArrayUtility::get($args, "route"), ArrayUtility::get($args, "arguments"));
92
93
        // Initialize the button.
94
        $ext = new ButtonComponentTwigExtension();
95
        $but = $ext->bootstrapButtonDangerFunction(["title" => $txt, "icon" => "trash"]);
96
97
        // Return the HTML.
98
        return $ext->bootstrapButtonLinkFilter($but, $url);
99
    }
100
101
    /**
102
     * Displays a Bootstrap edit row button.
103
     *
104
     * @param array $args The arguments.
105
     * @return string Returns the Bootstrap edit row button.
106
     */
107
    public function bootstrapEditRowButtonFunction(array $args = []) {
108
109
110
        // Translate the label.
111
        $txt = $this->translator->trans("label.edit", [], "BootstrapBundle");
112
113
        // Generate the URL.
114
        $url = $this->router->generate(ArrayUtility::get($args, "route"), ArrayUtility::get($args, "arguments"));
115
116
        // Initialize the button.
117
        $ext = new ButtonComponentTwigExtension();
118
        $but = $ext->bootstrapButtonDefaultFunction(["title" => $txt, "icon" => "edit"]);
119
120
        // Return the HTML.
121
        return $ext->bootstrapButtonLinkFilter($but, $url);
122
    }
123
124
    /**
125
     * Get the Twig functions.
126
     *
127
     * @return array Returns the Twig functions.
128
     */
129
    public function getFunctions() {
130
        return [
131
            new Twig_SimpleFunction('bootstrapDefaultRowButtons', [$this, 'bootstrapDefaultRowButtonsFunction'], ['is_safe' => ['html']]),
132
            new Twig_SimpleFunction('bootstrapDeleteRowButton', [$this, 'bootstrapDeleteRowButtonFunction'], ['is_safe' => ['html']]),
133
            new Twig_SimpleFunction('bootstrapEditRowButton', [$this, 'bootstrapEditRowButtonFunction'], ['is_safe' => ['html']]),
134
        ];
135
    }
136
137
}
138