Completed
Push — master ( 0295ab...9b2687 )
by WEBEWEB
02:09
created

TableButtonUtilityTwigExtension::getExtension()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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