Overview

Namespaces

  • App
    • Components
    • Interfaces
    • Lib
      • Files
      • Forms
        • Controls
      • Html
      • Repair
      • Statics
    • Model
    • Modules
      • BaseModule
        • Presenters
      • PartnersModule
        • Presenters
      • ProductModule
        • Presenters
      • SaleModule
        • Model
        • Presenters
      • SettingsModule
        • Model
        • Presenters
      • StoreModule
        • Model
        • Presenters
    • Presenters
    • Router
  • PHP

Classes

  • DateTime
  • SplFileInfo

Interfaces

  • ArrayAccess
  • Countable
  • Exception
  • Iterator
  • Traversable
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Download
  1: <?php
  2: /**
  3:  * Addons and code snippets for Nette Framework. (unofficial)
  4:  *
  5:  * @author   Jan Tvrdík
  6:  * @license  MIT
  7:  */
  8: 
  9: namespace App\Lib\Forms\Controls;
 10: 
 11: use Nette;
 12: use Nette\Forms;
 13: use DateTime;
 14: 
 15: /**
 16:  * Form control pro výběr datumu.
 17:  *
 18:  *  – compatible with jQuery UI DatePicker and HTML 5
 19:  *  – works with DateTime
 20:  *
 21:  * @author   Jan Tvrdík
 22:  * @version  2.3
 23:  * @link     http://nette.merxes.cz/date-picker/
 24:  */
 25: class DatePicker extends Forms\Controls\BaseControl
 26: {
 27:     /** @link    http://dev.w3.org/html5/spec/common-microsyntaxes.html#valid-date-string */
 28:     const W3C_DATE_FORMAT = 'Y-m-d';
 29: 
 30:     /** @var     DateTime|NULL     internal date reprezentation */
 31:     protected $value;
 32: 
 33:     /** @var     string            value entered by user (unfiltered) */
 34:     protected $rawValue;
 35: 
 36:     /** @var     string            class name */
 37:     private $className = 'date';
 38: 
 39: 
 40: 
 41:     /**
 42:      * Class constructor.
 43:      *
 44:      * @author   Jan Tvrdík
 45:      * @param    string            label
 46:      */
 47:     public function __construct($label = NULL)
 48:     {
 49:         parent::__construct($label);
 50:         $this->control->type = 'date';
 51:     }
 52: 
 53: 
 54: 
 55:     /**
 56:      * Returns class name.
 57:      *
 58:      * @author   Jan Tvrdík
 59:      * @return   string
 60:      */
 61:     public function getClassName()
 62:     {
 63:         return $this->className;
 64:     }
 65: 
 66: 
 67: 
 68:     /**
 69:      * Sets class name for input element.
 70:      *
 71:      * @author   Jan Tvrdík
 72:      * @param    string
 73:      * @return   self
 74:      */
 75:     public function setClassName($className)
 76:     {
 77:         $this->className = $className;
 78:         return $this;
 79:     }
 80: 
 81: 
 82: 
 83:     /**
 84:      * Generates control's HTML element.
 85:      *
 86:      * @author   Jan Tvrdík
 87:      * @return   Nette\Utils\Html
 88:      */
 89:     public function getControl()
 90:     {
 91:         $control = parent::getControl();
 92:         $control->addClass($this->className);
 93:         list($min, $max) = $this->extractRangeRule($this->getRules());
 94:         if ($min !== NULL) {
 95:         $control->min = $min->format(self::W3C_DATE_FORMAT);
 96:     }
 97:     if ($max !== NULL) {
 98:         $control->max = $max->format(self::W3C_DATE_FORMAT);
 99:     }
100:     if ($this->value) {
101:         $control->value = $this->value->format(self::W3C_DATE_FORMAT);
102:     }
103:     return $control;
104:     }
105: 
106: 
107: 
108:     /**
109:      * Sets DatePicker value.
110:      *
111:      * @author   Jan Tvrdík
112:      * @param    DateTime|int|string
113:      * @return   self
114:      */
115:     public function setValue($value)
116:     {
117:         if ($value instanceof DateTime) {
118: 
119:         } elseif (is_int($value)) { // timestamp
120: 
121:         } elseif (empty($value)) {
122:             $rawValue = $value;
123:             $value = NULL;
124: 
125:         } elseif (is_string($value)) {
126:             $rawValue = $value;
127: 
128:             if (preg_match('#^(?P<dd>\d{1,2})[. -] *(?P<mm>\d{1,2})([. -] *(?P<yyyy>\d{4})?)?$#', $value, $matches)) {
129:                 $dd = $matches['dd'];
130:                 $mm = $matches['mm'];
131:                 $yyyy = isset($matches['yyyy']) ? $matches['yyyy'] : date('Y');
132: 
133:                 if (checkdate($mm, $dd, $yyyy)) {
134:                     $value = "$yyyy-$mm-$dd";
135:                 } else {
136:                     $value = NULL;
137:                 }
138:             }
139: 
140:         } else {
141:             throw new \InvalidArgumentException();
142:         }
143: 
144:         if ($value !== NULL) {
145:             // DateTime constructor throws Exception when invalid input given
146:             try {
147:                 $value = Nette\DateTime::from($value); // clone DateTime when given
148:             } catch (\Exception $e) {
149:                 $value = NULL;
150:             }
151:         }
152: 
153:         if (!isset($rawValue) && isset($value)) {
154:             $rawValue = $value->format(self::W3C_DATE_FORMAT);
155:         }
156: 
157:         $this->value = $value;
158:         $this->rawValue = $rawValue;
159: 
160:         return $this;
161:     }
162: 
163: 
164: 
165:     /**
166:      * Returns unfiltered value.
167:      *
168:      * @author   Jan Tvrdík
169:      * @return   string
170:      */
171:     public function getRawValue()
172:     {
173:         return $this->rawValue;
174:     }
175: 
176: 
177: 
178:     /**
179:      * Does user enter anything? (the value doesn't have to be valid)
180:      *
181:      * @author   Jan Tvrdík
182:      * @param    DatePicker
183:      * @return   bool
184:      */
185:     public static function validateFilled(Forms\IControl $control)
186:     {
187:         if (!$control instanceof self) {
188:         throw new Nette\InvalidStateException('Unable to validate ' . get_class($control) . ' instance.');
189:     }
190:     $rawValue = $control->rawValue;
191:         return !empty($rawValue);
192:     }
193: 
194: 
195: 
196:     /**
197:      * Is entered value valid? (empty value is also valid!)
198:      *
199:      * @author   Jan Tvrdík
200:      * @param    DatePicker
201:      * @return   bool
202:      */
203:     public static function validateValid(Forms\IControl $control)
204:     {
205:         if (!$control instanceof self) {
206:         throw new Nette\InvalidStateException('Unable to validate ' . get_class($control) . ' instance.');
207:     }
208:     $value = $control->value;
209:         return (empty($control->rawValue) || $value instanceof DateTime);
210:     }
211: 
212: 
213: 
214:     /**
215:      * Is entered values within allowed range?
216:      *
217:      * @author   Jan Tvrdík, David Grudl
218:      * @param    DatePicker
219:      * @param    array             0 => minDate, 1 => maxDate
220:      * @return   bool
221:      */
222:     public static function validateRange(Forms\IControl $control, $range)
223:     {
224:         return Nette\Utils\Validators::isInRange($control->getValue(), $range);
225:     }
226: 
227: 
228: 
229:     /**
230:      * Finds minimum and maximum allowed dates.
231:      *
232:      * @author   Jan Tvrdík
233:      * @param    Forms\Rules
234:      * @return   array             0 => DateTime|NULL $minDate, 1 => DateTime|NULL $maxDate
235:      */
236:     private function extractRangeRule(Forms\Rules $rules)
237:     {
238:         $controlMin = $controlMax = NULL;
239:         foreach ($rules as $rule) {
240:             if ($rule->type === Forms\Rule::VALIDATOR) {
241:                 if ($rule->operation === Forms\Form::RANGE && !$rule->isNegative) {
242:                     $ruleMinMax = $rule->arg;
243:                 }
244: 
245:             } elseif ($rule->type === Forms\Rule::CONDITION) {
246:                 if ($rule->operation === Forms\Form::FILLED && !$rule->isNegative && $rule->control === $this) {
247:                     $ruleMinMax = $this->extractRangeRule($rule->subRules);
248:                 }
249:             }
250: 
251:             if (isset($ruleMinMax)) {
252:                 list($ruleMin, $ruleMax) = $ruleMinMax;
253:                 if ($ruleMin !== NULL && ($controlMin === NULL || $ruleMin > $controlMin)) {
254:             $controlMin = $ruleMin;
255:         }
256:         if ($ruleMax !== NULL && ($controlMax === NULL || $ruleMax < $controlMax)) {
257:             $controlMax = $ruleMax;
258:         }
259:         $ruleMinMax = NULL;
260:             }
261:         }
262:         return array($controlMin, $controlMax);
263:     }
264: 
265: }
266: 
sberp API API documentation generated by ApiGen