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; |
10
|
|
|
|
11
|
|
|
use Nette; |
12
|
|
|
use DibiFluent; |
13
|
|
|
use DibiOdbcDriver; |
14
|
|
|
use DibiMsSqlDriver; |
15
|
|
|
use Nette\Database\Table\Selection; |
16
|
|
|
use Doctrine\Common\Collections\Collection; |
17
|
|
|
use Doctrine\ORM\QueryBuilder; |
18
|
|
|
use Ublaboo\DataGrid\Column\Column; |
19
|
|
|
use Ublaboo\DataGrid\DataSource\IDataSource; |
20
|
|
|
use Ublaboo\DataGrid\Exception\DataGridWrongDataSourceException; |
21
|
|
|
use Ublaboo\DataGrid\Utils\Sorting; |
22
|
|
|
use Ublaboo\DataGrid\Utils\NetteDatabaseSelectionHelper; |
23
|
|
|
use Nette\Database\Drivers as NDBDrivers; |
24
|
|
|
use Ublaboo\DataGrid\DataSource\ApiDataSource; |
25
|
|
|
use Nextras\Orm\Collection\ICollection; |
26
|
|
|
|
27
|
|
|
class DataModel |
28
|
|
|
{ |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var IDataSource |
32
|
|
|
*/ |
33
|
|
|
protected $data_source; |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param IDataSource|array|DibiFluent|Selection|QueryBuilder|Collection $source |
38
|
|
|
* @param string $primary_key |
39
|
|
|
*/ |
40
|
|
|
public function __construct($source, $primary_key) |
41
|
|
|
{ |
42
|
|
|
if ($source instanceof IDataSource || $source instanceof ApiDataSource) { |
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Custom user datasource is ready for use |
45
|
|
|
* |
46
|
|
|
* $source = $source; |
47
|
|
|
*/ |
48
|
|
|
|
49
|
|
|
} else if (is_array($source)) { |
50
|
|
|
$source = new DataSource\ArrayDataSource($source); |
51
|
|
|
|
52
|
|
|
} else if ($source instanceof DibiFluent) { |
|
|
|
|
53
|
|
|
$driver = $source->getConnection()->getDriver(); |
54
|
|
|
|
55
|
|
|
if ($driver instanceof DibiOdbcDriver) { |
|
|
|
|
56
|
|
|
$source = new DataSource\DibiFluentMssqlDataSource($source, $primary_key); |
57
|
|
|
|
58
|
|
|
} else if ($driver instanceof DibiMsSqlDriver) { |
|
|
|
|
59
|
|
|
$source = new DataSource\DibiFluentMssqlDataSource($source, $primary_key); |
60
|
|
|
|
61
|
|
|
} else { |
62
|
|
|
$source = new DataSource\DibiFluentDataSource($source, $primary_key); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
} else if ($source instanceof Selection) { |
66
|
|
|
$driver = NetteDatabaseSelectionHelper::getDriver($source); |
67
|
|
|
|
68
|
|
|
if ($driver instanceof NDBDrivers\MsSqlDriver || $driver instanceof NDBDrivers\SqlsrvDriver) { |
69
|
|
|
$source = new DataSource\NetteDatabaseTableMssqlDataSource($source, $primary_key); |
70
|
|
|
} else { |
71
|
|
|
$source = new DataSource\NetteDatabaseTableDataSource($source, $primary_key); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
} else if ($source instanceof QueryBuilder) { |
75
|
|
|
$source = new DataSource\DoctrineDataSource($source, $primary_key); |
76
|
|
|
|
77
|
|
|
} else if ($source instanceof Collection) { |
78
|
|
|
$source = new DataSource\DoctrineCollectionDataSource($source, $primary_key); |
79
|
|
|
|
80
|
|
|
} elseif ($source instanceof ICollection) { |
81
|
|
|
$source = new DataSource\NextrasDataSource($source, $primary_key); |
82
|
|
|
|
83
|
|
|
} else { |
84
|
|
|
throw new DataGridWrongDataSourceException(sprintf( |
85
|
|
|
"DataGrid can not take [%s] as data source.", |
86
|
|
|
is_object($source) ? get_class($source) : 'NULL' |
87
|
|
|
)); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$this->data_source = $source; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Return dat asource |
96
|
|
|
* @return IDataSource |
97
|
|
|
*/ |
98
|
|
|
public function getDataSource() |
99
|
|
|
{ |
100
|
|
|
return $this->data_source; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Filter/paginate/limit/order data source and return reset of data in array |
106
|
|
|
* @param Components\DataGridPaginator\DataGridPaginator $paginator_component |
107
|
|
|
* @param Sorting $sorting |
108
|
|
|
* @param array $filters |
109
|
|
|
* @return array |
110
|
|
|
*/ |
111
|
|
|
public function filterData( |
112
|
|
|
Components\DataGridPaginator\DataGridPaginator $paginator_component = NULL, |
113
|
|
|
Sorting $sorting, |
114
|
|
|
array $filters |
115
|
|
|
) { |
116
|
|
|
$this->data_source->filter($filters); |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Paginator is optional |
120
|
|
|
*/ |
121
|
|
|
if ($paginator_component) { |
122
|
|
|
$paginator = $paginator_component->getPaginator(); |
123
|
|
|
$paginator->setItemCount($this->data_source->getCount()); |
124
|
|
|
|
125
|
|
|
$this->data_source->sort($sorting)->limit( |
126
|
|
|
$paginator->getOffset(), |
127
|
|
|
$paginator->getItemsPerPage() |
128
|
|
|
); |
129
|
|
|
|
130
|
|
|
return $this->data_source->getData(); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return $this->data_source->sort($sorting)->getData(); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Filter one row |
139
|
|
|
* @param array $condition |
140
|
|
|
* @return mixed |
141
|
|
|
*/ |
142
|
|
|
public function filterRow(array $condition) |
143
|
|
|
{ |
144
|
|
|
return $this->data_source->filterOne($condition)->getData(); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param array |
150
|
|
|
*/ |
151
|
|
|
public function aggregationRow(array $columns){ |
152
|
|
|
$hasAggregation = FALSE; |
153
|
|
|
/** @var Column $column */ |
154
|
|
|
foreach($columns as $column){ |
155
|
|
|
if($column->hasAggregationFunction()){ |
156
|
|
|
$hasAggregation = TRUE; |
157
|
|
|
$aggregationFunction = $column->getAggregationFunction(); |
158
|
|
|
$this->data_source->addAggregationColumn($aggregationFunction->getAggregationType(), $aggregationFunction->getColumn()); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
if(!$hasAggregation){ |
162
|
|
|
return FALSE; |
163
|
|
|
} |
164
|
|
|
$this->data_source->getAggregationData(); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
} |
168
|
|
|
|
This check looks for the bodies of
if
statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.These
if
bodies can be removed. If you have an empty if but statements in theelse
branch, consider inverting the condition.could be turned into
This is much more concise to read.