Grido@master
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Todo
  • Download

Namespaces

  • Grido
    • Components
      • Actions
      • Columns
      • Filters
    • DataSources
    • PropertyAccessors
    • Translations

Classes

  • Grido\Translations\FileTranslator
  1 <?php
  2 
  3 /**
  4  * This file is part of the Grido (https://github.com/o5/grido)
  5  *
  6  * Copyright (c) 2011 Petr Bugyík (http://petr.bugyik.cz)
  7  *
  8  * For the full copyright and license information, please view
  9  * the file LICENSE.md that was distributed with this source code.
 10  */
 11 
 12 namespace Grido\Components\Filters;
 13 
 14 use Nette\Utils\Strings;
 15 
 16 /**
 17  * Date-range input filter.
 18  *
 19  * @package     Grido
 20  * @subpackage  Components\Filters
 21  * @author      Petr Bugyík
 22  *
 23  * @property string $mask
 24  */
 25 class DateRange extends Date
 26 {
 27     /** @var string */
 28     protected $condition = 'BETWEEN ? AND ?';
 29 
 30     /** @var string */
 31     protected $mask = '/(.*)\s?-\s?(.*)/';
 32 
 33     /** @var array */
 34     protected $dateFormatOutput = array('Y-m-d', 'Y-m-d G:i:s');
 35 
 36     /**
 37      * @param string $formatFrom
 38      * @param string $formatTo
 39      * @return \Grido\Components\Filters\DateRange
 40      */
 41     public function setDateFormatOutput($formatFrom, $formatTo = NULL)
 42     {
 43         $formatTo = $formatTo === NULL
 44             ? $formatFrom
 45             : $formatTo;
 46 
 47         $this->dateFormatOutput = array($formatFrom, $formatTo);
 48         return $this;
 49     }
 50 
 51     /**
 52      * Sets mask by regular expression.
 53      * @param string $mask
 54      * @return DateRange
 55      */
 56     public function setMask($mask)
 57     {
 58         $this->mask = $mask;
 59         return $this;
 60     }
 61 
 62     /**
 63      * @return string
 64      */
 65     public function getMask()
 66     {
 67         return $this->mask;
 68     }
 69 
 70     /**
 71      * @return \Nette\Forms\Controls\TextInput
 72      */
 73     protected function getFormControl()
 74     {
 75         $control = parent::getFormControl();
 76 
 77         $prototype = $control->getControlPrototype();
 78         array_pop($prototype->class); //remove "date" class
 79         $prototype->class[] = 'daterange';
 80 
 81         return $control;
 82     }
 83 
 84     /**
 85      * @param string $value
 86      * @return Condition
 87      * @throws \Exception
 88      * @internal
 89      */
 90     public function __getCondition($value)
 91     {
 92         if ($this->where === NULL && is_string($this->condition)) {
 93 
 94             list (, $from, $to) = \Nette\Utils\Strings::match($value, $this->mask);
 95             $from = \DateTime::createFromFormat($this->dateFormatInput, trim($from));
 96             $to = \DateTime::createFromFormat($this->dateFormatInput, trim($to));
 97 
 98             if ($to && !Strings::match($this->dateFormatInput, '/G|H/i')) { //input format haven't got hour option
 99                 Strings::contains($this->dateFormatOutput[1], 'G') || Strings::contains($this->dateFormatOutput[1], 'H')
100                     ? $to->setTime(23, 59, 59)
101                     : $to->setTime(11, 59, 59);
102             }
103 
104             $values = $from && $to
105                 ? array($from->format($this->dateFormatOutput[0]), $to->format($this->dateFormatOutput[1]))
106                 : NULL;
107 
108             return $values
109                 ? Condition::setup($this->getColumn(), $this->condition, $values)
110                 : Condition::setupEmpty();
111         }
112 
113         return parent::__getCondition($value);
114     }
115 }
116 
Grido@master API documentation generated by ApiGen