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 Dibi; |
13
|
|
|
use Nette\Database\Table\Selection; |
14
|
|
|
use Doctrine\Common\Collections\Collection; |
15
|
|
|
use Doctrine\ORM\QueryBuilder; |
16
|
|
|
use Ublaboo\DataGrid\DataSource\IDataSource; |
17
|
|
|
use Ublaboo\DataGrid\Exception\DataGridWrongDataSourceException; |
18
|
|
|
use Ublaboo\DataGrid\Utils\Sorting; |
19
|
|
|
use Ublaboo\DataGrid\Utils\NetteDatabaseSelectionHelper; |
20
|
|
|
use Nette\Database\Drivers as NDBDrivers; |
21
|
|
|
use Ublaboo\DataGrid\DataSource\ApiDataSource; |
22
|
|
|
|
23
|
|
|
final class DataModel extends Nette\Object |
|
|
|
|
24
|
1 |
|
{ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var callable[] |
28
|
|
|
*/ |
29
|
|
|
public $onBeforeFilter = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var callable[] |
33
|
|
|
*/ |
34
|
|
|
public $onAfterFilter = []; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var callable[] |
38
|
|
|
*/ |
39
|
|
|
public $onAfterPaginated = []; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var IDataSource |
43
|
|
|
*/ |
44
|
|
|
private $data_source; |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param IDataSource|array|Dibi\Fluent|Selection|QueryBuilder|Collection $source |
49
|
|
|
* @param string $primary_key |
50
|
|
|
*/ |
51
|
|
|
public function __construct($source, $primary_key) |
52
|
|
|
{ |
53
|
|
|
if ($source instanceof IDataSource || $source instanceof ApiDataSource) { |
|
|
|
|
54
|
1 |
|
/** |
55
|
|
|
* Custom user datasource is ready for use |
56
|
|
|
* |
57
|
|
|
* $source = $source; |
58
|
|
|
*/ |
59
|
|
|
|
60
|
|
|
} else if (is_array($source)) { |
61
|
1 |
|
$source = new DataSource\ArrayDataSource($source); |
62
|
1 |
|
|
63
|
|
|
} else if ($source instanceof Dibi\Fluent) { |
64
|
|
|
$driver = $source->getConnection()->getDriver(); |
65
|
|
|
|
66
|
|
|
if ($driver instanceof Dibi\Drivers\OdbcDriver) { |
67
|
|
|
$source = new DataSource\DibiFluentMssqlDataSource($source, $primary_key); |
|
|
|
|
68
|
|
|
|
69
|
|
|
} else if ($driver instanceof Dibi\Drivers\MsSqlDriver) { |
70
|
|
|
$source = new DataSource\DibiFluentMssqlDataSource($source, $primary_key); |
|
|
|
|
71
|
|
|
|
72
|
|
|
} else if ($driver instanceof Dibi\Drivers\SqlsrvDriver) { |
73
|
|
|
$source = new DataSource\DibiFluentMssqlDataSource($source, $primary_key); |
|
|
|
|
74
|
|
|
|
75
|
|
|
} else { |
76
|
|
|
$source = new DataSource\DibiFluentDataSource($source, $primary_key); |
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
} else if ($source instanceof Selection) { |
80
|
|
|
$driver = NetteDatabaseSelectionHelper::getDriver($source); |
81
|
|
|
|
82
|
|
|
if ($driver instanceof NDBDrivers\MsSqlDriver || $driver instanceof NDBDrivers\SqlsrvDriver) { |
83
|
|
|
$source = new DataSource\NetteDatabaseTableMssqlDataSource($source, $primary_key); |
84
|
|
|
} else { |
85
|
|
|
$source = new DataSource\NetteDatabaseTableDataSource($source, $primary_key); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
} else if ($source instanceof QueryBuilder) { |
89
|
|
|
$source = new DataSource\DoctrineDataSource($source, $primary_key); |
90
|
|
|
|
91
|
|
|
} else if ($source instanceof Collection) { |
92
|
|
|
$source = new DataSource\DoctrineCollectionDataSource($source, $primary_key); |
93
|
|
|
|
94
|
|
|
} else { |
95
|
|
|
throw new DataGridWrongDataSourceException(sprintf( |
96
|
|
|
"DataGrid can not take [%s] as data source.", |
97
|
|
|
is_object($source) ? get_class($source) : 'NULL' |
98
|
|
|
)); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$this->data_source = $source; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
|
105
|
1 |
|
/** |
106
|
1 |
|
* Return dat asource |
107
|
|
|
* @return IDataSource |
108
|
|
|
*/ |
109
|
|
|
public function getDataSource() |
110
|
|
|
{ |
111
|
|
|
return $this->data_source; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Filter/paginate/limit/order data source and return reset of data in array |
117
|
|
|
* @param Components\DataGridPaginator\DataGridPaginator $paginator_component |
118
|
|
|
* @param Sorting $sorting |
119
|
|
|
* @param array $filters |
120
|
|
|
* @return array |
121
|
|
|
*/ |
122
|
|
|
public function filterData( |
123
|
|
|
Components\DataGridPaginator\DataGridPaginator $paginator_component = NULL, |
124
|
|
|
Sorting $sorting, |
125
|
|
|
array $filters |
126
|
|
|
) { |
127
|
|
|
$this->onBeforeFilter($this->data_source); |
|
|
|
|
128
|
|
|
|
129
|
|
|
$this->data_source->filter($filters); |
130
|
|
|
|
131
|
1 |
|
$this->onAfterFilter($this->data_source); |
|
|
|
|
132
|
|
|
|
133
|
1 |
|
/** |
134
|
|
|
* Paginator is optional |
135
|
1 |
|
*/ |
136
|
|
|
if ($paginator_component) { |
137
|
|
|
$paginator = $paginator_component->getPaginator(); |
138
|
|
|
$paginator->setItemCount($this->data_source->getCount()); |
139
|
|
|
|
140
|
1 |
|
$this->data_source->sort($sorting)->limit( |
141
|
|
|
$paginator->getOffset(), |
142
|
|
|
$paginator->getItemsPerPage() |
143
|
|
|
); |
144
|
|
|
|
145
|
|
|
$this->onAfterPaginated($this->data_source); |
|
|
|
|
146
|
|
|
|
147
|
|
|
return $this->data_source->getData(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return $this->data_source->sort($sorting)->getData(); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
|
154
|
1 |
|
/** |
155
|
|
|
* Filter one row |
156
|
|
|
* @param array $condition |
157
|
|
|
* @return mixed |
158
|
|
|
*/ |
159
|
|
|
public function filterRow(array $condition) |
160
|
|
|
{ |
161
|
|
|
return $this->data_source->filterOne($condition)->getData(); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
} |
165
|
|
|
|
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.