Completed
Pull Request — master (#106)
by
unknown
03:48
created

DateRangePicker::isSubmittedOnChange()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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
    /**
28
     * Returns javascript options
29
     *
30
     * Available options:
31
     * @see https://github.com/dangrossman/bootstrap-daterangepicker#options
32
     *
33
     * @return array
34
     */
35
    public function getJsOptions()
36
    {
37
        if (!$this->js_options) {
38
            $this->js_options = $this->getDefaultJsOptions();
39
        }
40
        return $this->js_options;
41
    }
42
43
    /**
44
     * Sets javascript options
45
     *
46
     * Available options:
47
     * @see https://github.com/dangrossman/bootstrap-daterangepicker#options
48
     *
49
     * @param array $options
50
     */
51
    public function setJsOptions($options)
52
    {
53
        $this->js_options = $options;
54
        return $this;
55
    }
56
    
57
    /**
58
     * Returns true if form must be submitted immediately
59
     * when filter value selected.
60
     *
61
     * @return bool
62
     */
63
    public function isSubmittedOnChange()
64
    {
65
        return $this->is_submitted_on_change;
0 ignored issues
show
Bug introduced by
The property is_submitted_on_change does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
66
    }
67
    
68
    /**
69
     * Allows to submit form immediately when filter value selected.
70
     *
71
     * @param bool $isSubmittedOnChange
72
     * @return $this
73
     */
74
    public function setSubmittedOnChange($isSubmittedOnChange)
75
    {
76
        $this->is_submitted_on_change = $isSubmittedOnChange;
77
        return $this;
78
    }
79
80 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...
81
    {
82
        $from_input = $this
83
            ->grid
84
            ->getInputProcessor()
85
            ->getFilterValue($this->name . '_start');
86
        if ($from_input === null) {
87
            return $this->getDefaultStartValue();
88
        } else {
89
            return $from_input;
90
        }
91
    }
92
93
94 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...
95
    {
96
        $from_input = $this
97
            ->grid
98
            ->getInputProcessor()
99
            ->getFilterValue($this->name . '_end');
100
        if ($from_input === null) {
101
            return $this->getDefaultEndValue();
102
        } else {
103
            return $from_input;
104
        }
105
    }
106
107
    public function getValue()
108
    {
109
        return [$this->getStartValue(), $this->getEndValue()];
110
    }
111
112
    /**
113
     * Returns true if non-empty value specified for the filter.
114
     *
115
     * @return bool
116
     */
117
    protected function hasValue()
118
    {
119
        list($start, $end) = $this->getValue();
120
        return $start !== null && $start !== '' && $end !== null && $end !== '';
121
    }
122
123
    /**
124
     * Returns default javascript options
125
     *
126
     * Available options:
127
     * @see https://github.com/dangrossman/bootstrap-daterangepicker#options
128
     *
129
     * @return array
130
     */
131
    protected function getDefaultJsOptions()
132
    {
133
        $carbon = new Carbon();
134
        $prev_month = Carbon::now()->startOfMonth()->subWeek();
135
        $today = Carbon::now();
136
        $res = [
137
            'format' => 'YYYY-MM-DD',
138
            'ranges' => [
139
                'previous_month' => [
140
                    'Previous month (' . $prev_month->format('F') . ')',
141
                    [
142
                        $prev_month->startOfMonth()->format('Y-m-d'),
143
                        $prev_month->endOfMonth()->format('Y-m-d'),
144
                    ]
145
                ],
146
                'current_month' => [
147
                    'Cur. month (' . date('F'). ')',
148
                    [
149
                        $carbon->startOfMonth()->format('Y-m-d'),
150
                        $carbon->endOfMonth()->format('Y-m-d')
151
                    ]
152
                ],
153
                'last_week' => [
154
                    'This Week',
155
                    [
156
                        $carbon->startOfWeek()->format('Y-m-d'),
157
                        $carbon->endOfWeek()->format('Y-m-d')
158
                    ]
159
                ],
160
                'last_14' => [
161
                    'Last 14 days',
162
                    [
163
                        Carbon::now()->subDays(13)->format('Y-m-d'),
164
                        $today->format('Y-m-d')
165
                    ]
166
                ],
167
168
            ],
169
        ];
170
        // will not set dates when '' passed but set default date when null passed
171
        if ($this->getStartValue()) {
172
            $res['startDate'] = $this->getStartValue();
173
        }
174
        if ($this->getEndValue()) {
175
            $res['endDate'] = $this->getEndValue();
176
        }
177
        return $res;
178
    }
179
180
    public function getDefaultStartValue()
181
    {
182
        return $this->getDefaultValue()[0];
183
    }
184
185
    public function getDefaultEndValue()
186
    {
187
        return $this->getDefaultValue()[1];
188
    }
189
190
    /**
191
     * Returns default filter value as [$startDate, $endDate]
192
     *
193
     * @return array
194
     */
195
    public function getDefaultValue()
196
    {
197
        return is_array($this->default_value) ? $this->default_value : [
198
            Carbon::now()->subWeek()->format('Y-m-d'),
199
            Carbon::now()->format('Y-m-d'),
200
        ];
201
    }
202
203
    public function getStartInputName()
204
    {
205
        $key = $this->grid->getInputProcessor()->getKey();
206
        return "{$key}[filters][{$this->name}_start]";
207
    }
208
209
    public function getEndInputName()
210
    {
211
        $key = $this->grid->getInputProcessor()->getKey();
212
        return "{$key}[filters][{$this->name}_end]";
213
    }
214
215
    public function getFilteringFunc()
216
    {
217
        if (!$this->filtering_func) {
218
            $this->filtering_func = $this->getDefaultFilteringFunc();
219
        }
220
        return $this->filtering_func;
221
    }
222
223
    protected function getDefaultFilteringFunc()
224
    {
225
        return function($value, DataProvider $provider) {
226
            $provider->filter($this->getName(), '>=', $value[0]);
227
            $provider->filter($this->getName(), '<=', $value[1]);
228
        };
229
    }
230
}
231