1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sylius package. |
5
|
|
|
* |
6
|
|
|
* (c) Paweł Jędrzejewski |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sylius\Bundle\GridBundle\Doctrine\PHPCRODM; |
13
|
|
|
|
14
|
|
|
use Pagerfanta\Adapter\DoctrineORMAdapter; |
15
|
|
|
use Pagerfanta\Pagerfanta; |
16
|
|
|
use Sylius\Component\Grid\Data\DataSourceInterface; |
17
|
|
|
use Sylius\Component\Grid\Data\ExpressionBuilderInterface; |
18
|
|
|
use Sylius\Component\Grid\Parameters; |
19
|
|
|
use Doctrine\ODM\PHPCR\Query\Builder\QueryBuilder; |
20
|
|
|
use Pagerfanta\Adapter\DoctrineODMPhpcrAdapter; |
21
|
|
|
|
22
|
|
|
class DataSource implements DataSourceInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var QueryBuilder |
26
|
|
|
*/ |
27
|
|
|
private $queryBuilder; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var ExpressionBuilder |
31
|
|
|
*/ |
32
|
|
|
private $expressionBuilder; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param QueryBuilder $queryBuilder |
36
|
|
|
* @param ExpressionBuilder $expressionBuilder |
37
|
|
|
*/ |
38
|
|
|
public function __construct(QueryBuilder $queryBuilder, ExpressionBuilder $expressionBuilder = null) |
39
|
|
|
{ |
40
|
|
|
$this->queryBuilder = $queryBuilder; |
41
|
|
|
$this->expressionBuilder = $expressionBuilder ?: new ExpressionBuilder(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
|
|
public function restrict($expression, $condition = DataSourceInterface::CONDITION_AND) |
48
|
|
|
{ |
49
|
|
|
switch ($condition) { |
50
|
|
|
case DataSourceInterface::CONDITION_AND: |
51
|
|
|
$parentNode = $this->queryBuilder->andWhere(); |
52
|
|
|
break; |
53
|
|
|
case DataSourceInterface::CONDITION_OR: |
54
|
|
|
$parentNode = $this->queryBuilder->orWhere(); |
55
|
|
|
break; |
56
|
|
|
default: |
57
|
|
|
throw new \RuntimeException(sprintf( |
58
|
|
|
'Unknown restrict condition "%s"', |
59
|
|
|
$condition |
60
|
|
|
)); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$visitor = new ExpressionVisitor($this->queryBuilder); |
64
|
|
|
$visitor->dispatch($expression, $parentNode); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
|
|
public function getExpressionBuilder() |
71
|
|
|
{ |
72
|
|
|
return $this->expressionBuilder; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
*/ |
78
|
|
|
public function getData(Parameters $parameters) |
79
|
|
|
{ |
80
|
|
|
$orderBy = $this->queryBuilder->orderBy(); |
81
|
|
|
foreach ($this->expressionBuilder->getOrderBys() as $field => $direction) { |
82
|
|
|
if (is_integer($field)) { |
83
|
|
|
$field = $direction; |
84
|
|
|
$direction = 'asc'; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
// todo: validate direction? |
88
|
|
|
$direction = strtolower($direction); |
89
|
|
|
$orderBy->{$direction}()->field(sprintf('%s.%s', Driver::QB_SOURCE_ALIAS, $field)); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$paginator = new Pagerfanta(new DoctrineODMPhpcrAdapter($this->queryBuilder)); |
93
|
|
|
$paginator->setCurrentPage($parameters->get('page', 1)); |
94
|
|
|
|
95
|
|
|
return $paginator; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|