1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) 2015 ublaboo <[email protected]> |
5
|
|
|
* @author Pavel Janda <[email protected]> |
6
|
|
|
* @package Ublaboo |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Ublaboo\DataGrid\DataSource; |
10
|
|
|
|
11
|
|
|
use Ublaboo\DataGrid\Filter; |
12
|
|
|
use Ublaboo\DataGrid\Utils\NetteDatabaseSelectionHelper; |
13
|
|
|
|
14
|
|
|
class NetteDatabaseTableMssqlDataSource extends NetteDatabaseTableDataSource implements IDataSource |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Filter by date |
19
|
|
|
* @param Filter\FilterDate $filter |
20
|
|
|
* @return void |
21
|
|
|
*/ |
22
|
|
|
public function applyFilterDate(Filter\FilterDate $filter) |
23
|
|
|
{ |
24
|
|
|
$conditions = $filter->getCondition(); |
25
|
|
|
|
26
|
|
|
$date = \DateTime::createFromFormat($filter->getPhpFormat(), $conditions[$filter->getColumn()]); |
27
|
|
|
|
28
|
|
|
$this->data_source->where( |
29
|
|
|
"CONVERT(varchar(10), {$filter->getColumn()}, 112) = ?", |
30
|
|
|
$date->format('Y-m-d') |
31
|
|
|
); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Filter by date range |
37
|
|
|
* @param Filter\FilterDateRange $filter |
38
|
|
|
* @return void |
39
|
|
|
*/ |
40
|
|
View Code Duplication |
public function applyFilterDateRange(Filter\FilterDateRange $filter) |
|
|
|
|
41
|
|
|
{ |
42
|
|
|
$conditions = $filter->getCondition(); |
43
|
|
|
|
44
|
|
|
$value_from = $conditions[$filter->getColumn()]['from']; |
45
|
|
|
$value_to = $conditions[$filter->getColumn()]['to']; |
46
|
|
|
|
47
|
|
|
if ($value_from) { |
48
|
|
|
$date_from = \DateTime::createFromFormat($filter->getPhpFormat(), $value_from); |
49
|
|
|
$date_from->setTime(0, 0, 0); |
50
|
|
|
|
51
|
|
|
$this->data_source->where( |
52
|
|
|
"CONVERT(varchar(10), {$filter->getColumn()}, 112) >= ?", |
53
|
|
|
$date_from->format('Y-m-d') |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if ($value_to) { |
58
|
|
|
$date_to = \DateTime::createFromFormat($filter->getPhpFormat(), $value_to); |
59
|
|
|
$date_to->setTime(23, 59, 59); |
60
|
|
|
|
61
|
|
|
$this->data_source->where( |
62
|
|
|
"CONVERT(varchar(10), {$filter->getColumn()}, 112) <= ?", |
63
|
|
|
$date_to->format('Y-m-d') |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.