123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- <?php
- namespace Dcat\Admin\Grid\Column\Filter;
- use Dcat\Admin\Admin;
- use Dcat\Admin\Grid\Column\Filter;
- use Dcat\Admin\Grid\Model;
- class Between extends Filter
- {
- protected $dateFormat = null;
- /**
- * @var bool
- */
- protected $timestamp = false;
- public function __construct()
- {
- $this->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(<<<JS
- $('.{$this->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
- <span class="dropdown">
- <form action="{$this->formAction()}" pjax-container style="display: inline-block;">
- <a href="javascript:void(0);" class="{$active}" data-toggle="dropdown">
- <i class="feather icon-filter"></i>
- </a>
- <ul class="dropdown-menu" role="menu" style="min-width: 180px;padding: 10px;left: -70px;border-radius: 0;font-weight:normal;background:#fff">
- <li class="dropdown-item">
- <input type="text"
- class="form-control input-sm {$this->class['start']}"
- name="{$this->getQueryName()}[start]"
- placeholder="{$this->trans('between_start')}"
- value="{$value['start']}"
- autocomplete="off" />
- </li>
- <li style="margin: 5px;"></li>
- <li class="dropdown-item">
- <input type="text"
- class="form-control input-sm {$this->class['end']}"
- name="{$this->getQueryName()}[end]"
- placeholder="{$this->trans('between_end')}"
- value="{$value['end']}"
- autocomplete="off"/>
- </li>
- {$this->renderFormButtons()}
- </ul>
- </form>
- </span>
- EOT;
- }
- protected function requireAssets()
- {
- Admin::requireAssets(['moment', 'bootstrap-datetimepicker']);
- }
- }
|