1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link http://www.newicon.net/neon |
4
|
|
|
* @copyright Copyright (c) 21/09/2016 Newicon Ltd |
5
|
|
|
* @license http://www.newicon.net/neon/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace neon\core\form\fields; |
9
|
|
|
|
10
|
|
|
use \neon\core\grid\query\IQuery; |
11
|
|
|
use \neon\core\form\fields\Date; |
12
|
|
|
use \neon\core\helpers\Arr; |
13
|
|
|
use \neon\core\form\Form; |
14
|
|
|
use neon\core\helpers\Html; |
15
|
|
|
use neon\core\helpers\Converter; |
16
|
|
|
use \neon\core\assets\JQueryUiAsset; |
17
|
|
|
use neon\core\validators\DateValidator; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class DateRange |
21
|
|
|
* @package neon\core\form\fields |
22
|
|
|
*/ |
23
|
|
|
class DateRange extends Field |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* The DDS data type to store the value of the field |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
public $ddsDataType = 'json'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The format the date should appear to the user - this also sets the date picker format |
33
|
|
|
* http://php.net/manual/en/function.date.php |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
public $_dateUserFormat = 'php:d/m/Y'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The placeholder to show to the user when filtering |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
public $placeholder = 'DD/MM/YYYY'; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* The range of years to offer as a selection |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
public $yearRange = 'c-10:c+10'; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
protected $dateDatabaseFormat = 'php:Y-m-d'; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param $date |
57
|
|
|
*/ |
58
|
|
|
public function setDateUserFormat($date) |
|
|
|
|
59
|
|
|
{ |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Get the date format that should be displayed to the user |
64
|
|
|
* @return string - the date format |
65
|
|
|
* @see \neon\core\services\Formatter::$dateFormat |
66
|
|
|
*/ |
67
|
|
|
public function getDateUserFormat() |
68
|
|
|
{ |
69
|
|
|
return neon()->formatter->dateFormat; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @inheritdoc |
74
|
|
|
*/ |
75
|
|
|
public function getProperties() |
76
|
|
|
{ |
77
|
|
|
return array_merge(parent::getProperties(), ['datePickerFormat', 'yearRange']); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Returns a Jquery ui date format string by converting the dateUserFormat property which is a PHP date format string |
82
|
|
|
* http://api.jqueryui.com/datepicker/#utility-formatDate |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
|
|
public function getDatePickerFormat() |
86
|
|
|
{ |
87
|
|
|
return Converter::convertDateFormatToJui($this->dateUserFormat); |
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @inheritdoc |
92
|
|
|
*/ |
93
|
|
|
public function processAsFilter(IQuery $query, $searchData=null) |
94
|
|
|
{ |
95
|
|
|
$searchData = ($searchData === null) ? $this->getData() : $searchData; |
96
|
|
|
if ($searchData !== '') { |
97
|
|
|
$dateFrom = Arr::get($searchData, 'from'); |
98
|
|
|
$dateTo = Arr::get($searchData, 'to'); |
99
|
|
|
if ($dateFrom) |
100
|
|
|
$query->where($this->getDataKey(), '>=', $dateFrom); |
101
|
|
|
if ($dateTo) |
102
|
|
|
$query->where($this->getDataKey(), '<=', $dateTo); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @inheritdoc |
108
|
|
|
*/ |
109
|
|
|
public function registerScripts($view) |
110
|
|
|
{ |
111
|
|
|
JQueryUiAsset::register($view); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @inheritDoc |
116
|
|
|
*/ |
117
|
|
|
public function setValueFromDb($value) |
118
|
|
|
{ |
119
|
|
|
$this->_value = json_decode($value); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @inheritdoc |
124
|
|
|
*/ |
125
|
20 |
|
public function getData() |
126
|
|
|
{ |
127
|
20 |
|
$value = parent::getValue(); |
128
|
|
|
// the result must be an array with 'from'=> and 'to'=> |
129
|
20 |
|
if ( is_array($value) && isset($value['from'], $value['to']) ) { |
130
|
|
|
return [ |
131
|
16 |
|
'from' => $this->_isDateValid($value['from']) ? $value['from'] : '', |
132
|
16 |
|
'to' => $this->_isDateValid($value['to']) ? $value['to'] : '' |
133
|
|
|
]; |
134
|
|
|
} |
135
|
4 |
|
return ['from' => '', 'to' => '']; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Determine if a given date is in correct format and looks valid |
140
|
|
|
* @param string $date |
141
|
|
|
* @return bool |
142
|
|
|
*/ |
143
|
16 |
|
private function _isDateValid($date) |
144
|
|
|
{ |
145
|
16 |
|
return (new DateValidator(['format'=>'php:Y-m-d']))->validate($date); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @inheritdoc |
150
|
|
|
*/ |
151
|
|
|
public function getComponentDetails() |
152
|
|
|
{ |
153
|
|
|
return [ |
154
|
|
|
'label' => 'Date Range', 'group' => 'Date & Time', 'icon' => 'fa fa-calendar-o', 'order' => 325, |
155
|
|
|
]; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
} |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.