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

bootstrapDefaultFormButtonsFunction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
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\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
 * Form button utility Twig extension.
21
 *
22
 * @author webeweb <https://github.com/webeweb/>
23
 * @package WBW\Bundle\BootstrapBundle\Twig\Extension\Utility
24
 */
25
class FormButtonUtilityTwigExtension extends AbstractUtilityTwigExtension {
26
27
    /**
28
     * Service name.
29
     *
30
     * @var string
31
     */
32
    const SERVICE_NAME = "webeweb.bootstrapbundle.twig.extension.utility.formbutton";
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 cancel form button.
54
     *
55
     * @param array $args The arguments.
56
     * @return string Returns the Bootstrap cancel form button.
57
     */
58
    public function bootstrapCancelFormButtonFunction(array $args = []) {
59
60
        // Translate the label.
61
        $txt = $this->getTranslator()->trans("label.cancel", [], "BootstrapBundle");
62
63
        // Initialize the button.
64
        $but = $this->getExtension()->bootstrapButtonDefaultFunction(["content" => $txt, "title" => $txt, "icon" => "remove"]);
65
66
        // Return the HTML.
67
        return $this->getExtension()->bootstrapButtonLinkFilter($but, ArrayUtility::get($args, "href", self::DEFAULT_HREF));
68
    }
69
70
    /**
71
     * Displays a Bootstrap default form buttons.
72
     *
73
     * @param array $args The arguments.
74
     * @return string Returns the Bootstrap default form buttons.
75
     */
76
    public function bootstrapDefaultFormButtonsFunction(array $args = []) {
77
78
        // Initialize the buttons.
79
        $cancelButton = $this->bootstrapCancelFormButtonFunction(["href" => ArrayUtility::get($args, "cancel_href")]);
80
        $submitButton = $this->bootstrapSubmitFormButtonFunction();
81
82
        // Return the HTML.
83
        return implode(" ", [$cancelButton, $submitButton]);
84
    }
85
86
    /**
87
     * Displays a Bootstrap submit form button.
88
     *
89
     * @return string Returns the Bootstrap submit form button.
90
     */
91
    public function bootstrapSubmitFormButtonFunction() {
92
93
        // Translate the label.
94
        $txt = $this->getTranslator()->trans("label.submit", [], "BootstrapBundle");
95
96
        // Initialize the button.
97
        $but = $this->getExtension()->bootstrapButtonPrimaryFunction(["content" => $txt, "title" => $txt, "icon" => "ok"]);
98
99
        // Return the HTML.
100
        return $this->getExtension()->bootstrapButtonSubmitFilter($but);
101
    }
102
103
    /**
104
     * Get the extension.
105
     *
106
     * @return ButtonComponentTwigExtension Returns the extension.
107
     */
108
    public function getExtension() {
109
        return $this->extension;
110
    }
111
112
    /**
113
     * Get the Twig functions.
114
     *
115
     * @return array Returns the Twig functions.
116
     */
117
    public function getFunctions() {
118
        return [
119
            new Twig_SimpleFunction("bootstrapCancelFormButton", [$this, "bootstrapCancelFormButtonFunction"], ["is_safe" => ["html"]]),
120
            new Twig_SimpleFunction("bootstrapDefaultFormButtons", [$this, "bootstrapDefaultFormButtonsFunction"], ["is_safe" => ["html"]]),
121
            new Twig_SimpleFunction("bootstrapSubmitFormButton", [$this, "bootstrapSubmitFormButtonFunction"], ["is_safe" => ["html"]]),
122
        ];
123
    }
124
125
    /**
126
     * Set the extension.
127
     *
128
     * @param ButtonComponentTwigExtension $extension The extension.
129
     * @return FormButtonTwigExtension Returns this form button Twig extension.
130
     */
131
    protected function setExtension(ButtonComponentTwigExtension $extension) {
132
        $this->extension = $extension;
133
        return $this;
134
    }
135
136
}
137