class = [ 'start' => uniqid('column-filter-start-'), 'end' => uniqid('column-filter-end-'), ]; } /** * Convert the datetime into unix timestamp. * * @return $this */ public function toTimestamp() { $this->timestamp = true; return $this; } /** * Date filter. * * @return $this */ public function date() { return $this->setDateFormat('YYYY-MM-DD'); } /** * Time filter. * * @return $this */ public function time() { return $this->setDateFormat('HH:mm:ss'); } /** * Datetime filter. * * @return $this */ public function datetime() { return $this->setDateFormat('YYYY-MM-DD HH:mm:ss'); } /** * @param string $format * @return $this */ protected function setDateFormat($format) { $this->dateFormat = $format; $this->requireAssets(); return $this; } /** * Add a binding to the query. * * @param mixed $value * @param Model $model */ public function addBinding($value, Model $model) { $value = array_filter((array) $value); if (empty($value)) { return; } if ($this->timestamp) { $value = array_map(function ($v) { if ($v) { return strtotime($v); } }, $value); } if (! isset($value['start'])) { $this->withQuery($model, 'where', ['<=', $value['end']]); return; } if (! isset($value['end'])) { $this->withQuery($model, 'where', ['>=', $value['start']]); return; } $this->withQuery($model, 'whereBetween', [array_values($value)]); } protected function addScript() { if (! $this->dateFormat) { return; } $options = [ 'locale' => config('app.locale'), 'allowInputToggle' => true, 'format' => $this->dateFormat, 'extraFormats' => ['DD-MM-YYYY', 'DD/MM/YYYY', 'DD.MM.YYYY', 'DD,MM,YYYY'], ]; $options = json_encode($options); Admin::script(<<class['start']}').datetimepicker($options); $('.{$this->class['end']}').datetimepicker($options); JS ); // $('.{$this->class['start']}').on("dp.change", function (e) { // $('.{$this->class['end']}').data("DateTimePicker").minDate(e.date); // }); // $('.{$this->class['end']}').on("dp.change", function (e) { // $('.{$this->class['start']}').data("DateTimePicker").maxDate(e.date); // }); } /** * Render this filter. * * @return string */ public function render() { if (! $this->shouldDisplay()) { return; } $script = <<<'JS' $('.dropdown-menu input').on('click', function(e) { e.stopPropagation(); }); JS; Admin::script($script); $this->addScript(); $value = $this->value(['start' => '', 'end' => '']); $active = empty(array_filter($value)) ? '' : 'active'; return <<
EOT; } protected function requireAssets() { Admin::requireAssets(['moment', 'bootstrap-datetimepicker']); } }