Completed
Push — master ( 3cc478...c0e2c7 )
by Song
06:54 queued 04:37
created

StepForm::setCurrent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 6
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
    /**
138
     *
139
     */
140
    protected function addFooter()
141
    {
142
        $footer = '';
143
144
        $index = array_search($this->current, $this->steps);
145
146
        $trans = [
147
            'prev'   => __('admin.prev'),
148
            'next'   => __('admin.next'),
149
            'submit' => __('admin.submit'),
150
        ];
151
152
        if ($index !== 0) {
153
            $step    = $this->steps[$index - 1];
154
            $prevUrl = request()->fullUrlWithQuery(compact('step'));;
155
            $footer .= "<a href=\"{$prevUrl}\" class=\"btn btn-warning pull-left\">{$trans['prev']}</a>";
156
        }
157
158
        if ($index !== count($this->steps) - 1) {
159
            $footer .= "<button class=\"btn btn-info pull-right\">{$trans['next']}</button>";
160
        }
161
162
        if ($index === count($this->steps) - 1) {
163
            $footer .= "<button class=\"btn btn-info pull-right\">{$trans['submit']}</button>";
164
        }
165
166
        $this->html($footer);
167
    }
168
169
    /**
170
     * @return $this
171
     */
172
    public function sanitize()
173
    {
174
        $this->setUrl(request('_url'))
175
            ->setCurrent(request('_current'))
176
            ->setSteps(explode(',', request('_steps')));
177
178
        foreach (['_form_', '_token', '_url', '_current', '_steps'] as $key) {
179
            request()->request->remove($key);
180
        }
181
182
        return $this;
183
    }
184
185
    /**
186
     * @return mixed
187
     */
188
    public function data()
189
    {
190
        return session()->get('steps.' . $this->current, []);
191
    }
192
}