Completed
Push — master ( c0e2c7...7b6048 )
by Song
02:52 queued 11s
created

StepForm::prev()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin\Widgets;
4
5
class StepForm extends Form
6
{
7
    /**
8
     * @var int|string
9
     */
10
    protected $current;
11
12
    /**
13
     * @var array
14
     */
15
    protected $steps = [];
16
17
    /**
18
     * @var string
19
     */
20
    protected $url;
21
22
    /**
23
     * @var array
24
     */
25
    protected $buttons = [];
26
27
    /**
28
     * @param array $data
29
     *
30
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
31
     */
32
    protected function next($data = [])
33
    {
34
        $this->remember($data);
35
36
        return $this->redirectToNextStep();
37
    }
38
39
    protected function prev()
40
    {
41
        return back()->withInput();
42
    }
43
44
    /**
45
     * @param array $data
46
     */
47
    protected function remember($data)
48
    {
49
        session()->put("steps.{$this->current}", $data);
50
    }
51
52
    /**
53
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
54
     */
55
    protected function redirectToNextStep()
56
    {
57
        $index = array_search($this->current, $this->steps);
58
59
        $step = $this->steps[$index + 1];
60
61
        $nextUrl = $this->url.'?'.http_build_query(compact('step'));
62
63
        return redirect($nextUrl);
64
    }
65
66
    /**
67
     * Get all data from steps.
68
     *
69
     * @return array
70
     */
71
    protected function all()
72
    {
73
        $prev = session()->get('steps', []);
74
75
        return array_merge($prev, [$this->current => request()->all()]);
76
    }
77
78
    /**
79
     * Clear all data from steps.
80
     */
81
    protected function clear()
82
    {
83
        session()->remove('steps');
84
    }
85
86
    /**
87
     * @param array $steps
88
     *
89
     * @return $this
90
     */
91
    public function setSteps($steps)
92
    {
93
        $this->steps = $steps;
94
95
        return $this;
96
    }
97
98
    /**
99
     * @param string|int $current
100
     *
101
     * @return $this
102
     */
103
    public function setCurrent($current)
104
    {
105
        $this->current = $current;
106
107
        return $this;
108
    }
109
110
    /**
111
     * @param string $url
112
     *
113
     * @return $this
114
     */
115
    public function setUrl($url)
116
    {
117
        $this->url = $url;
118
119
        return $this;
120
    }
121
122
    protected function prepareForm()
123
    {
124
        parent::prepareForm();
125
126
        $url = request()->url();
127
128
        $this->hidden('_url')->default($url);
129
        $this->hidden('_current')->default($this->current);
130
        $this->hidden('_steps')->default(implode(',', $this->steps));
131
132
        $this->divider();
133
134
        $this->addFooter();
135
    }
136
137
    protected function addFooter()
138
    {
139
        $footer = '';
140
141
        $index = array_search($this->current, $this->steps);
142
143
        $trans = [
144
            'prev'   => __('admin.prev'),
145
            'next'   => __('admin.next'),
146
            'submit' => __('admin.submit'),
147
        ];
148
149
        if ($index !== 0) {
150
            $step = $this->steps[$index - 1];
151
            $prevUrl = request()->fullUrlWithQuery(compact('step'));
152
            $footer .= "<a href=\"{$prevUrl}\" class=\"btn btn-warning pull-left\">{$trans['prev']}</a>";
153
        }
154
155
        if ($index !== count($this->steps) - 1) {
156
            $footer .= "<button class=\"btn btn-info pull-right\">{$trans['next']}</button>";
157
        }
158
159
        if ($index === count($this->steps) - 1) {
160
            $footer .= "<button class=\"btn btn-info pull-right\">{$trans['submit']}</button>";
161
        }
162
163
        $this->html($footer);
164
    }
165
166
    /**
167
     * @return $this
168
     */
169
    public function sanitize()
170
    {
171
        $this->setUrl(request('_url'))
172
            ->setCurrent(request('_current'))
173
            ->setSteps(explode(',', request('_steps')));
174
175
        foreach (['_form_', '_token', '_url', '_current', '_steps'] as $key) {
176
            request()->request->remove($key);
177
        }
178
179
        return $this;
180
    }
181
182
    /**
183
     * @return mixed
184
     */
185
    public function data()
186
    {
187
        return session()->get('steps.'.$this->current, []);
188
    }
189
}
190