|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Sergii Bondarenko, <[email protected]> |
|
4
|
|
|
*/ |
|
5
|
|
|
namespace Drupal\TqExtension\Utils\DatePicker; |
|
6
|
|
|
|
|
7
|
|
|
class Native extends DatePickerBase |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var string |
|
11
|
|
|
*/ |
|
12
|
|
|
private $dateFormat = ''; |
|
13
|
|
|
/** |
|
14
|
|
|
* @var string |
|
15
|
|
|
*/ |
|
16
|
|
|
private $dateTime = ''; |
|
17
|
|
|
/** |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
private $dateFormatted = ''; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* {@inheritdoc} |
|
24
|
|
|
*/ |
|
25
|
|
|
public function initialize() |
|
26
|
|
|
{ |
|
27
|
|
|
$this->dateFormat = $this->context->executeJsOnElement($this->element, "return jQuery({{ELEMENT}}).data('drupalDateFormat')"); |
|
28
|
|
|
|
|
29
|
|
|
if (empty($this->dateFormat)) { |
|
30
|
|
|
throw new \RuntimeException('Unknown date format.'); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
$this->dateTime = strtotime($this->date); |
|
|
|
|
|
|
34
|
|
|
$this->dateFormatted = date($this->dateFormat, $this->dateTime); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* {@inheritdoc} |
|
39
|
|
|
*/ |
|
40
|
|
|
public function isDateAvailable() |
|
41
|
|
|
{ |
|
42
|
|
|
$ranges = []; |
|
43
|
|
|
|
|
44
|
|
|
foreach (['min', 'max'] as $range) { |
|
45
|
|
|
$value = $this->element->getAttribute($range); |
|
46
|
|
|
// If no range was set then use the original date as its value. |
|
47
|
|
|
$ranges[$range] = null === $value ? $this->dateTime : strtotime($value); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
if ($this->dateTime < $ranges['min'] || $this->dateTime > $ranges['max']) { |
|
51
|
|
|
throw new \Exception(sprintf('The "%s" is not available for choosing.', $this->date)); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return $this; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* {@inheritdoc} |
|
59
|
|
|
*/ |
|
60
|
|
|
public function setDate() |
|
61
|
|
|
{ |
|
62
|
|
|
$this->context->executeJsOnElement($this->element, "jQuery({{ELEMENT}}).val('$this->dateFormatted')"); |
|
63
|
|
|
|
|
64
|
|
|
return $this; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* {@inheritdoc} |
|
69
|
|
|
*/ |
|
70
|
|
|
public function isDateSelected() |
|
71
|
|
|
{ |
|
72
|
|
|
$value = $this->context->executeJsOnElement($this->element, "return jQuery({{ELEMENT}}).val()"); |
|
73
|
|
|
|
|
74
|
|
|
self::debug(['Comparing "%s" with "%s".'], [$value, $this->dateFormatted]); |
|
75
|
|
|
|
|
76
|
|
|
if ($value !== $this->dateFormatted) { |
|
77
|
|
|
throw new \Exception(sprintf('DatePicker contains the "%s" but should "%s".', $value, $this->dateFormatted)); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return $this; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.