Passed
Push — develop ( 319bd8...330c7a )
by Neill
16:41 queued 15s
created

DateRange   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Test Coverage

Coverage 23.53%

Importance

Changes 0
Metric Value
eloc 28
c 0
b 0
f 0
dl 0
loc 132
ccs 8
cts 34
cp 0.2353
rs 10
wmc 18

10 Methods

Rating   Name   Duplication   Size   Complexity  
A registerScripts() 0 3 1
A getDateUserFormat() 0 3 1
A setValueFromDb() 0 3 1
A _isDateValid() 0 3 1
A getData() 0 11 5
A processAsFilter() 0 10 5
A setDateUserFormat() 0 2 1
A getProperties() 0 3 1
A getDatePickerFormat() 0 3 1
A getComponentDetails() 0 4 1
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)
0 ignored issues
show
Unused Code introduced by
The parameter $date is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

58
	public function setDateUserFormat(/** @scrutinizer ignore-unused */ $date)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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);
0 ignored issues
show
Bug Best Practice introduced by
The property dateUserFormat does not exist on neon\core\form\fields\DateRange. Since you implemented __get, consider adding a @property annotation.
Loading history...
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
}