Source of file DateRangeFilter.php
Size: 1,796 Bytes - Last Modified: 2021-12-24T06:43:43+00:00
/var/www/docs.ssmods.com/process/src/code/DateRangeFilter.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 | <?php namespace DeptInternalAffairsNZ\SilverStripe; use SilverStripe\Core\Convert; use SilverStripe\ORM\DataQuery; use SilverStripe\ORM\Filters\SearchFilter; class DateRangeFilter extends SearchFilter { private $min, $max; public function findMinMax() { if (!isset($this->value)) { return false; } $value = $this->value; if(is_array($value)) { if (isset($value['From'])) { $fromDate = $value['From']; } if (isset($value['To'])) { $toDate = $value['To']; } $value = $fromDate . '-to-' . $toDate; } if (strpos($value, '-to-') !== FALSE) { $valueArray = explode('-to-', $value); if ($valueArray[0] != 0) { $this->setMin($valueArray[0]); } if ($valueArray[1] != 0) { $this->setMax($valueArray[1]); } } } public function setMin($min) { $this->min = $min; } public function setMax($max) { $this->max = $max; } public function apply(DataQuery $query) { if (!isset($this->min) || !isset($this->max)) { $this->findMinMax(); } if ($this->min) { $query->where(sprintf( "%s >= '%s'", $this->getDbName(), Convert::raw2sql($this->min) )); } if ($this->max) { $query->where(sprintf( "%s <= '%s'", $this->getDbName(), Convert::raw2sql($this->max) )); } } /** * Applies a match on the starting characters of a field value. * * @return DataQuery */ protected function applyOne(DataQuery $query) { return true; } /** * Applies a match on the starting characters of a field value. * * @return DataQuery */ protected function applyMany(DataQuery $query) { return true; } /** * Excludes a match on the starting characters of a field value. * * @return DataQuery */ protected function excludeOne(DataQuery $query) { return true; } } |