Completed
Push — master ( af7b98...388b72 )
by Vitaliy
02:45
created

DateRangePicker::setSubmittedOnChange()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
rs 9.4286
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
namespace Nayjest\Grids\Components\Filters;
3
4
use Carbon\Carbon;
5
use Nayjest\Grids\Components\Filter;
6
use Nayjest\Grids\DataProvider;
7
8
/**
9
 * Class DateRangePicker
10
 *
11
 * Date Range Picker for Bootstrap.
12
 * https://github.com/dangrossman/bootstrap-daterangepicker
13
 *
14
 * This component does not includes javascript & styles required to work with bootstrap-daterangepicker.
15
 * You need to include it manually to your pages/layout.
16
 *
17
 * @package Nayjest\Grids\Components\Filters
18
 */
19
class DateRangePicker extends Filter
20
{
21
    protected $js_options;
22
23
    protected $use_clear_button;
24
25
    protected $template = '*.components.filters.date_range_picker';
26
    
27
    protected $is_submitted_on_change = false;
28
29
    /**
30
     * Returns javascript options
31
     *
32
     * Available options:
33
     * @see https://github.com/dangrossman/bootstrap-daterangepicker#options
34
     *
35
     * @return array
36
     */
37
    public function getJsOptions()
38
    {
39
        if (!$this->js_options) {
40
            $this->js_options = $this->getDefaultJsOptions();
41
        }
42
        return $this->js_options;
43
    }
44
45
    /**
46
     * Sets javascript options
47
     *
48
     * Available options:
49
     * @see https://github.com/dangrossman/bootstrap-daterangepicker#options
50
     *
51
     * @param array $options
52
     */
53
    public function setJsOptions($options)
54
    {
55
        $this->js_options = $options;
56
        return $this;
57
    }
58
    
59
    /**
60
     * Returns true if form must be submitted immediately
61
     * when filter value selected.
62
     *
63
     * @return bool
64
     */
65
    public function isSubmittedOnChange()
66
    {
67
        return $this->is_submitted_on_change;
68
    }
69
    
70
    /**
71
     * Allows to submit form immediately when filter value selected.
72
     *
73
     * @param bool $isSubmittedOnChange
74
     * @return $this
75
     */
76
    public function setSubmittedOnChange($isSubmittedOnChange)
77
    {
78
        $this->is_submitted_on_change = $isSubmittedOnChange;
79
        return $this;
80
    }
81
82 View Code Duplication
    public function getStartValue()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
83
    {
84
        $from_input = $this
85
            ->grid
86
            ->getInputProcessor()
87
            ->getFilterValue($this->name . '_start');
88
        if ($from_input === null) {
89
            return $this->getDefaultStartValue();
90
        } else {
91
            return $from_input;
92
        }
93
    }
94
95
96 View Code Duplication
    public function getEndValue()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
97
    {
98
        $from_input = $this
99
            ->grid
100
            ->getInputProcessor()
101
            ->getFilterValue($this->name . '_end');
102
        if ($from_input === null) {
103
            return $this->getDefaultEndValue();
104
        } else {
105
            return $from_input;
106
        }
107
    }
108
109
    public function getValue()
110
    {
111
        return [$this->getStartValue(), $this->getEndValue()];
112
    }
113
114
    /**
115
     * Returns true if non-empty value specified for the filter.
116
     *
117
     * @return bool
118
     */
119
    protected function hasValue()
120
    {
121
        list($start, $end) = $this->getValue();
122
        return $start !== null && $start !== '' && $end !== null && $end !== '';
123
    }
124
125
    /**
126
     * Returns default javascript options
127
     *
128
     * Available options:
129
     * @see https://github.com/dangrossman/bootstrap-daterangepicker#options
130
     *
131
     * @return array
132
     */
133
    protected function getDefaultJsOptions()
134
    {
135
        $carbon = new Carbon();
136
        $prev_month = Carbon::now()->startOfMonth()->subWeek();
137
        $today = Carbon::now();
138
        $res = [
139
            'format' => 'YYYY-MM-DD',
140
            'ranges' => [
141
                'previous_month' => [
142
                    'Previous month (' . $prev_month->format('F') . ')',
143
                    [
144
                        $prev_month->startOfMonth()->format('Y-m-d'),
145
                        $prev_month->endOfMonth()->format('Y-m-d'),
146
                    ]
147
                ],
148
                'current_month' => [
149
                    'Cur. month (' . date('F'). ')',
150
                    [
151
                        $carbon->startOfMonth()->format('Y-m-d'),
152
                        $carbon->endOfMonth()->format('Y-m-d')
153
                    ]
154
                ],
155
                'last_week' => [
156
                    'This Week',
157
                    [
158
                        $carbon->startOfWeek()->format('Y-m-d'),
159
                        $carbon->endOfWeek()->format('Y-m-d')
160
                    ]
161
                ],
162
                'last_14' => [
163
                    'Last 14 days',
164
                    [
165
                        Carbon::now()->subDays(13)->format('Y-m-d'),
166
                        $today->format('Y-m-d')
167
                    ]
168
                ],
169
170
            ],
171
        ];
172
        // will not set dates when '' passed but set default date when null passed
173
        if ($this->getStartValue()) {
174
            $res['startDate'] = $this->getStartValue();
175
        }
176
        if ($this->getEndValue()) {
177
            $res['endDate'] = $this->getEndValue();
178
        }
179
        return $res;
180
    }
181
182
    public function getDefaultStartValue()
183
    {
184
        return $this->getDefaultValue()[0];
185
    }
186
187
    public function getDefaultEndValue()
188
    {
189
        return $this->getDefaultValue()[1];
190
    }
191
192
    /**
193
     * Returns default filter value as [$startDate, $endDate]
194
     *
195
     * @return array
196
     */
197
    public function getDefaultValue()
198
    {
199
        return is_array($this->default_value) ? $this->default_value : [
200
            Carbon::now()->subWeek()->format('Y-m-d'),
201
            Carbon::now()->format('Y-m-d'),
202
        ];
203
    }
204
205
    public function getStartInputName()
206
    {
207
        $key = $this->grid->getInputProcessor()->getKey();
208
        return "{$key}[filters][{$this->name}_start]";
209
    }
210
211
    public function getEndInputName()
212
    {
213
        $key = $this->grid->getInputProcessor()->getKey();
214
        return "{$key}[filters][{$this->name}_end]";
215
    }
216
217
    public function getFilteringFunc()
218
    {
219
        if (!$this->filtering_func) {
220
            $this->filtering_func = $this->getDefaultFilteringFunc();
221
        }
222
        return $this->filtering_func;
223
    }
224
225
    protected function getDefaultFilteringFunc()
226
    {
227
        return function($value, DataProvider $provider) {
228
            $provider->filter($this->getName(), '>=', $value[0]);
229
            $provider->filter($this->getName(), '<=', $value[1]);
230
        };
231
    }
232
}
233