Completed
Pull Request — master (#1810)
by
unknown
02:53
created

Tools::disableSaveButton()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin\Form;
4
5
use Encore\Admin\Facades\Admin;
6
use Encore\Admin\Form;
7
use Illuminate\Contracts\Support\Htmlable;
8
use Illuminate\Contracts\Support\Renderable;
9
use Illuminate\Support\Collection;
10
use Illuminate\Support\Str;
11
12
class Tools implements Renderable
13
{
14
    /**
15
     * @var Builder
16
     */
17
    protected $form;
18
19
    /**
20
     * Collection of tools.
21
     *
22
     * @var Collection
23
     */
24
    protected $tools;
25
26
    /**
27
     * @var array
28
     */
29
    protected $options = [
30
        'enableListButton' => true,
31
        'enableBackButton' => true,
32
        'enableSaveButton' => true
33
    ];
34
35
    /**
36
     * Create a new Tools instance.
37
     *
38
     * @param Builder $builder
39
     */
40
    public function __construct(Builder $builder)
41
    {
42
        $this->form = $builder;
43
44
        $this->tools = new Collection();
45
    }
46
47
    /**
48
     * @return string
49
     */
50
    protected function backButton()
51
    {
52
        $script = <<<'EOT'
53
$('.form-history-back').on('click', function (event) {
54
    event.preventDefault();
55
    history.back(1);
56
});
57
EOT;
58
59
        Admin::script($script);
60
61
        $text = trans('admin.back');
62
63
        return <<<EOT
64
<div class="btn-group pull-right" style="margin-right: 10px">
65
    <a class="btn btn btn-default form-history-back"><i class="fa fa-arrow-left"></i>&nbsp;$text</a>
66
</div>
67
EOT;
68
    }
69
70
    public function listButton()
71
    {
72
        $slice = Str::contains($this->form->getResource(0), '/edit') ? null : -1;
73
        $resource = $this->form->getResource($slice);
74
75
        $text = trans('admin.list');
76
77
        return <<<EOT
78
<div class="btn-group pull-right" style="margin-right: 10px">
79
    <a href="$resource" class="btn btn btn-default"><i class="fa fa-list"></i>&nbsp;$text</a>
80
</div>
81
EOT;
82
    }
83
84
    /**
85
     * Prepend a tool.
86
     *
87
     * @param string $tool
88
     *
89
     * @return $this
90
     */
91
    public function add($tool)
92
    {
93
        $this->tools->push($tool);
94
95
        return $this;
96
    }
97
98
    /**
99
     * Disable back button.
100
     *
101
     * @return $this
102
     */
103
    public function disableBackButton()
104
    {
105
        $this->options['enableBackButton'] = false;
106
107
        return $this;
108
    }
109
110
    /**
111
     * Disable list button.
112
     *
113
     * @return $this
114
     */
115
    public function disableListButton()
116
    {
117
        $this->options['enableListButton'] = false;
118
119
        return $this;
120
    }
121
122
    /**
123
     * Disasble save button.
124
     * an extra save button at the top (made specially for long forms)
125
     * @return $this
126
     */
127
    public function disableSaveButton()
128
    {
129
        $this->options['enableSaveButton'] = false;
130
131
        return $this;
132
    }
133
134
    /**
135
     * Render header tools bar.
136
     *
137
     * @return string
138
     */
139
    public function render()
140
    {
141
142
        if ($this->options['enableSaveButton']) {
143
            $this->add($this->form->submitButton());
144
        }
145
        if ($this->options['enableListButton']) {
146
            $this->add($this->listButton());
147
        }
148
149
        if ($this->options['enableBackButton']) {
150
            $this->add($this->backButton());
151
        }
152
153 View Code Duplication
        return $this->tools->map(function ($tool) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
154
            if ($tool instanceof Renderable) {
155
                return $tool->render();
156
            }
157
158
            if ($tool instanceof Htmlable) {
159
                return $tool->toHtml();
160
            }
161
162
            return (string) $tool;
163
        })->implode(' ');
164
    }
165
}
166