1: <?php
2: 3: 4: 5: 6: 7:
8:
9: namespace App\Lib\Forms\Controls;
10:
11: use Nette;
12: use Nette\Forms;
13: use DateTime;
14:
15: 16: 17: 18: 19: 20: 21: 22: 23: 24:
25: class DatePicker extends Forms\Controls\BaseControl
26: {
27:
28: const W3C_DATE_FORMAT = 'Y-m-d';
29:
30:
31: protected $value;
32:
33:
34: protected $rawValue;
35:
36:
37: private $className = 'date';
38:
39:
40:
41: 42: 43: 44: 45: 46:
47: public function __construct($label = NULL)
48: {
49: parent::__construct($label);
50: $this->control->type = 'date';
51: }
52:
53:
54:
55: 56: 57: 58: 59: 60:
61: public function getClassName()
62: {
63: return $this->className;
64: }
65:
66:
67:
68: 69: 70: 71: 72: 73: 74:
75: public function setClassName($className)
76: {
77: $this->className = $className;
78: return $this;
79: }
80:
81:
82:
83: 84: 85: 86: 87: 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: 110: 111: 112: 113: 114:
115: public function setValue($value)
116: {
117: if ($value instanceof DateTime) {
118:
119: } elseif (is_int($value)) {
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:
146: try {
147: $value = Nette\DateTime::from($value);
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: 167: 168: 169: 170:
171: public function getRawValue()
172: {
173: return $this->rawValue;
174: }
175:
176:
177:
178: 179: 180: 181: 182: 183: 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: 198: 199: 200: 201: 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: 216: 217: 218: 219: 220: 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: 231: 232: 233: 234: 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: