Skip to content

Commit 50bc09c

Browse files
author
Daniel Stříbrný
committed
Initial commit
0 parents  commit 50bc09c

5 files changed

Lines changed: 252 additions & 0 deletions

File tree

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.idea/
2+
3+
/vendor/
4+
5+
composer.lock

README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Ublaboo Datagrid data source bindings for Kdyby Doctrine query objects
2+
3+
## Installation
4+
5+
via composer:
6+
7+
```sh
8+
composer require adt/query-object-data-source
9+
```
10+
11+
and in config.neon:
12+
13+
```neon
14+
extensions:
15+
- ADT\QueryObjectDataSource\DI\QueryObjectDataSourceExtension
16+
```
17+
18+
## Usage
19+
20+
Inject or autowire QueryObjectDataSourceFactory:
21+
```php
22+
/** @var \ADT\QueryObjectDataSource\IQueryObjectDataSourceFactory @autowire */
23+
protected $queryObjectDataSourceFactory;
24+
```
25+
26+
Create query object and wrap it as data source:
27+
```php
28+
$qo = /* create query object */;
29+
30+
$dataSource = $this->queryObjectDataSourceFactory->create($qo, "id")
31+
->setSortCallback(function($queryObject, \Ublaboo\DataGrid\Utils\Sorting $sorting) {
32+
$sort = $sorting->getSort();
33+
34+
if (!empty($sort)) {
35+
foreach ($sort as $order => $by) {
36+
$queryObject->order("e.$order", $by);
37+
}
38+
}
39+
})
40+
->setFilterCallback(function ($queryObject, array $filter) {
41+
foreach ($filter as $field => $value) {
42+
$queryObject->{'by' . $field}($value);
43+
}
44+
});
45+
46+
$grid->setDataSource($queryObjectDataSource);
47+
```

composer.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "adt/query-object-data-source",
3+
"description": "Ublaboo Datagrid data source bindings for Kdyby Doctrine query objects.",
4+
"type": "library",
5+
"license": [
6+
"MIT",
7+
"BSD-3-Clause",
8+
"GPL-2.0",
9+
"GPL-3.0"
10+
],
11+
"require-dev": {
12+
"kdyby/doctrine": "^3.1",
13+
"ublaboo/datagrid": "^5.0",
14+
"nette/utils": "^2.4",
15+
"nette/di": "^2.4"
16+
},
17+
"autoload": {
18+
"psr-4": {
19+
"ADT\\QueryObjectDataSource\\": "src/"
20+
}
21+
}
22+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace ADT\QueryObjectDataSource\DI;
4+
5+
class QueryObjectDataSourceExtension extends \Nette\DI\CompilerExtension {
6+
7+
public function loadConfiguration() {
8+
9+
$this->getContainerBuilder()
10+
->addDefinition($this->prefix('factory'))
11+
->setImplement(\ADT\QueryObjectDataSource\IQueryObjectDataSourceFactory::class);
12+
}
13+
14+
}

src/QueryObjectDataSource.php

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
<?php
2+
3+
namespace ADT\QueryObjectDataSource;
4+
5+
interface IQueryObjectDataSourceFactory {
6+
7+
/** @return QueryObjectDataSource */
8+
function create(\Kdyby\Doctrine\QueryObject $queryObject, $primaryKey);
9+
10+
}
11+
12+
13+
class QueryObjectDataSource extends \Nette\Object implements \Ublaboo\DataGrid\DataSource\IDataSource {
14+
15+
/** @var \Kdyby\Doctrine\EntityManager */
16+
protected $em;
17+
18+
/** @var \Kdyby\Doctrine\ResultSet */
19+
protected $resultSet;
20+
21+
/** @var \Kdyby\Doctrine\QueryObject */
22+
protected $queryObject;
23+
24+
/** @var string */
25+
protected $primaryKey;
26+
27+
/** @var callable */
28+
public $filterCallback;
29+
30+
/** @var callable */
31+
public $filterOneCallback;
32+
33+
/** @var callable */
34+
public $sortCallback;
35+
36+
/**
37+
* @param \Kdyby\Doctrine\QueryObject $queryObject
38+
* @param string $primaryKey
39+
* @param \Kdyby\Doctrine\EntityManager $em
40+
*/
41+
public function __construct(\Kdyby\Doctrine\QueryObject $queryObject, $primaryKey, \Kdyby\Doctrine\EntityManager $em) {
42+
$this->queryObject = $queryObject;
43+
$this->primaryKey = $primaryKey;
44+
$this->em = $em;
45+
}
46+
47+
/**
48+
* @param callable $callback
49+
* @return self
50+
*/
51+
public function setFilterCallback($callback) {
52+
$this->filterCallback = $callback;
53+
return $this;
54+
}
55+
56+
/**
57+
* @param callable $callback
58+
* @return self
59+
*/
60+
public function setFilterOneCallback($callback) {
61+
$this->filterOneCallback = $callback;
62+
return $this;
63+
}
64+
65+
/**
66+
* @param callable $callback
67+
* @return self
68+
*/
69+
public function setSortCallback($callback) {
70+
$this->sortCallback = $callback;
71+
return $this;
72+
}
73+
74+
protected function getResultSet() {
75+
if (!$this->resultSet) {
76+
$this->resultSet = $this->em->getRepository($this->queryObject->getEntity())
77+
->fetch($this->queryObject);
78+
}
79+
80+
return $this->resultSet;
81+
}
82+
83+
/**
84+
* Get count of data
85+
* @return int
86+
*/
87+
public function getCount() {
88+
return $this->em->getRepository($this->queryObject->getEntity())
89+
->fetch($this->queryObject)
90+
->getTotalCount();
91+
}
92+
93+
/**
94+
* Get the data
95+
* @return array
96+
*/
97+
public function getData() {
98+
return $this->getResultSet()->toArray();
99+
}
100+
101+
/**
102+
* Filter data
103+
* @param array $filters
104+
* @return static
105+
*/
106+
public function filter(array $filters) {
107+
foreach ($filters as $filter) {
108+
if ($filter->isValueSet()) {
109+
if ($filter->hasConditionCallback()) {
110+
\Nette\Utils\Callback::invokeArgs(
111+
$filter->getConditionCallback(), [ $this->queryObject, $filter->getValue() ]
112+
);
113+
}
114+
}
115+
}
116+
117+
if (is_callable($this->filterCallback)) {
118+
$this->filterCallback($this->queryObject, $filters);
119+
}
120+
121+
return $this;
122+
}
123+
124+
/**
125+
* Filter data - get one row
126+
* @param array $filter
127+
* @return static
128+
*/
129+
public function filterOne(array $filter) {
130+
131+
if (is_callable($this->filterOneCallback)) {
132+
$this->filterOneCallback($this->queryObject, $filter);
133+
}
134+
135+
return $this;
136+
}
137+
138+
/**
139+
* Apply limit and offset on data
140+
* @param int $offset
141+
* @param int $limit
142+
* @return static
143+
*/
144+
public function limit($offset, $limit) {
145+
$this->getResultSet()->applyPaging($offset, $limit);
146+
147+
return $this;
148+
}
149+
150+
/**
151+
* Sort data
152+
* @param \Ublaboo\DataGrid\Utils\Sorting $sorting
153+
* @return static
154+
*/
155+
public function sort(\Ublaboo\DataGrid\Utils\Sorting $sorting) {
156+
157+
if (is_callable($this->sortCallback)) {
158+
$this->sortCallback($this->queryObject, $sorting);
159+
}
160+
161+
return $this;
162+
}
163+
164+
}

0 commit comments

Comments
 (0)