Completed
Push — master ( 332a47...cbc99e )
by WEBEWEB
03:08
created

ButtonTableTwigExtension::setTranslator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
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\Translation\TranslatorInterface;
15
use Twig_SimpleFunction;
16
use WBW\Bundle\BootstrapBundle\Twig\Extension\Component\ButtonComponentTwigExtension;
17
use WBW\Library\Core\Utility\Argument\ArrayUtility;
18
19
/**
20
 * Button table Twig extension.
21
 *
22
 * @author webeweb <https://github.com/webeweb/>
23
 * @package WBW\Bundle\BootstrapBundle\Twig\Extension\Table
24
 */
25
class ButtonTableTwigExtension extends AbstractTableTwigExtension {
26
27
    /**
28
     * Service name.
29
     *
30
     * @var string
31
     */
32
    const SERVICE_NAME = "webeweb.bundle.bootstrapbundle.twig.extension.table.button";
33
34
    /**
35
     * Translator.
36
     *
37
     * @var TranslatorInterface
38
     */
39
    private $translator;
40
41
    /**
42
     * Constructor.
43
     *
44
     * @param TranslatorInterface $translator The translator service.
45
     */
46
    public function __construct(TranslatorInterface $translator) {
47
        parent::__construct();
48
        $this->setTranslator($translator);
49
    }
50
51
    /**
52
     * Displays a Bootstrap default row buttons.
53
     *
54
     * @param array $args The arguments.
55
     * @return string Returns the Bootstrap default form buttons.
56
     */
57
    public function bootstrapDefaultRowButtonsFunction(array $args = []) {
58
59
        // Initialize the buttons.
60
        $editButton   = $this->bootstrapEditRowButtonFunction(["href" => ArrayUtility::get($args, "edit_href")]);
61
        $deleteButton = $this->bootstrapDeleteRowButtonFunction(["href" => ArrayUtility::get($args, "delete_href")]);
62
63
        // Return the HTML.
64
        return implode(" ", [$editButton, $deleteButton]);
65
    }
66
67
    /**
68
     * Displays a Bootstrap delete row button.
69
     *
70
     * @param array $args The arguments.
71
     * @return string Returns the Bootstrap delete row button.
72
     */
73
    public function bootstrapDeleteRowButtonFunction(array $args = []) {
74
75
        // Translate the label.
76
        $txt = $this->getTranslator()->trans("label.delete", [], "BootstrapBundle");
77
78
        // Initialize the button.
79
        $ext = new ButtonComponentTwigExtension();
80
        $but = $ext->bootstrapButtonDangerFunction(["title" => $txt, "icon" => "trash"]);
81
82
        // Return the HTML.
83
        return $ext->bootstrapButtonLinkFilter($but, ArrayUtility::get($args, "href", self::DEFAULT_HREF));
84
    }
85
86
    /**
87
     * Displays a Bootstrap edit row button.
88
     *
89
     * @param array $args The arguments.
90
     * @return string Returns the Bootstrap edit row button.
91
     */
92
    public function bootstrapEditRowButtonFunction(array $args = []) {
93
94
        // Translate the label.
95
        $txt = $this->getTranslator()->trans("label.edit", [], "BootstrapBundle");
96
97
        // Initialize the button.
98
        $ext = new ButtonComponentTwigExtension();
99
        $but = $ext->bootstrapButtonDefaultFunction(["title" => $txt, "icon" => "pencil"]);
100
101
        // Return the HTML.
102
        return $ext->bootstrapButtonLinkFilter($but, ArrayUtility::get($args, "href", self::DEFAULT_HREF));
103
    }
104
105
    /**
106
     * Get the Twig functions.
107
     *
108
     * @return array Returns the Twig functions.
109
     */
110
    public function getFunctions() {
111
        return [
112
            new Twig_SimpleFunction("bootstrapDefaultRowButtons", [$this, "bootstrapDefaultRowButtonsFunction"], ["is_safe" => ["html"]]),
113
            new Twig_SimpleFunction("bootstrapDeleteRowButton", [$this, "bootstrapDeleteRowButtonFunction"], ["is_safe" => ["html"]]),
114
            new Twig_SimpleFunction("bootstrapEditRowButton", [$this, "bootstrapEditRowButtonFunction"], ["is_safe" => ["html"]]),
115
        ];
116
    }
117
118
    /**
119
     * Get the translator.
120
     *
121
     * @return TranslatorInterface Returns the translator.
122
     */
123
    public function getTranslator() {
124
        return $this->translator;
125
    }
126
127
    /**
128
     * Set the translator.
129
     *
130
     * @param TranslatorInterface $translator The translator.
131
     * @return ButtonTableTwigExtension Returns this button table Twig extension.
132
     */
133
    protected function setTranslator(TranslatorInterface $translator) {
134
        $this->translator = $translator;
135
        return $this;
136
    }
137
138
}
139