Passed
Push — master ( 4961e1...3955ab )
by Peter
02:28
created

DefaultButtons::addSaveAndBack()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Framework\Form\Extra;
6
7
use AbterPhp\Framework\Constant\Html5;
8
use AbterPhp\Framework\Html\Component;
9
use AbterPhp\Framework\Html\Component\Button;
10
11
class DefaultButtons extends Component
12
{
13
    const DEFAULT_TAG = 'div';
14
15
    const BTN_CONTENT_SAVE            = 'framework:save';
16
    const BTN_CONTENT_SAVE_AND_BACK   = 'framework:saveAndBack';
17
    const BTN_CONTENT_SAVE_AND_EDIT   = 'framework:saveAndEdit';
18
    const BTN_CONTENT_SAVE_AND_CREATE = 'framework:saveAndCreate';
19
    const BTN_CONTENT_BACK_TO_GRID    = 'framework:backToGrid';
20
21
    const BTN_NAME_NEXT = 'next';
22
23
    const BTN_VALUE_NEXT_NONE   = '';
24
    const BTN_VALUE_NEXT_EDIT   = 'edit';
25
    const BTN_VALUE_NEXT_CREATE = 'create';
26
    const BTN_VALUE_NEXT_BACK   = 'back';
27
28
    /** @var Button[] */
29
    protected $components;
30
31
    /** @var array */
32
    protected $attributes = [];
33
34
    /** @var array */
35
    protected $btnAttributes = [
36
        Html5::ATTR_NAME  => [self::BTN_NAME_NEXT],
37
        Html5::ATTR_TYPE  => [Button::TYPE_SUBMIT],
38
        Html5::ATTR_VALUE => [self::BTN_VALUE_NEXT_NONE],
39
    ];
40
41
    /**
42
     * @return $this
43
     */
44
    public function addSave(): DefaultButtons
45
    {
46
        $this->nodes[] = new Button(
47
            static::BTN_CONTENT_SAVE,
48
            [Button::INTENT_PRIMARY, Button::INTENT_FORM],
49
            $this->btnAttributes
50
        );
51
52
        return $this;
53
    }
54
55
    /**
56
     * @return $this
57
     */
58
    public function addSaveAndBack(): DefaultButtons
59
    {
60
        $attributes = $this->btnAttributes;
61
62
        $attributes[Html5::ATTR_VALUE] = [static::BTN_VALUE_NEXT_BACK];
63
64
        $this->nodes[] = new Button(
65
            static::BTN_CONTENT_SAVE_AND_BACK,
66
            [Button::INTENT_PRIMARY, Button::INTENT_FORM],
67
            $attributes
68
        );
69
70
        return $this;
71
    }
72
73
    /**
74
     * @return $this
75
     */
76
    public function addSaveAndEdit(): DefaultButtons
77
    {
78
        $attributes = $this->btnAttributes;
79
80
        $attributes[Html5::ATTR_VALUE] = [static::BTN_VALUE_NEXT_EDIT];
81
82
        $this->nodes[] = new Button(
83
            static::BTN_CONTENT_SAVE_AND_EDIT,
84
            [Button::INTENT_DEFAULT, Button::INTENT_FORM],
85
            $attributes
86
        );
87
88
        return $this;
89
    }
90
91
    /**
92
     * @return $this
93
     */
94
    public function addSaveAndCreate(): DefaultButtons
95
    {
96
        $attributes = $this->btnAttributes;
97
98
        $attributes[Html5::ATTR_VALUE] = [static::BTN_VALUE_NEXT_CREATE];
99
100
        $this->nodes[] = new Button(
101
            static::BTN_CONTENT_SAVE_AND_CREATE,
102
            [Button::INTENT_DEFAULT, Button::INTENT_FORM],
103
            $attributes
104
        );
105
106
        return $this;
107
    }
108
109
    /**
110
     * @param string $showUrl
111
     *
112
     * @return $this
113
     */
114
    public function addBackToGrid(string $showUrl): DefaultButtons
115
    {
116
        $attributes = [
117
            Html5::ATTR_HREF => [$showUrl],
118
        ];
119
120
        $this->nodes[] = new Button(
121
            static::BTN_CONTENT_BACK_TO_GRID,
122
            [Button::INTENT_DANGER, Button::INTENT_FORM],
123
            $attributes,
124
            Html5::TAG_A
125
        );
126
127
        return $this;
128
    }
129
}
130