1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Psi\Component\Grid; |
4
|
|
|
|
5
|
|
|
use Psi\Component\Grid\Metadata\GridMetadata; |
6
|
|
|
use Psi\Component\ObjectAgent\AgentInterface; |
7
|
|
|
use Psi\Component\ObjectAgent\Query\Query; |
8
|
|
|
|
9
|
|
|
class GridViewFactory |
10
|
|
|
{ |
11
|
|
|
private $cellFactory; |
12
|
|
|
private $filterFactory; |
13
|
|
|
|
14
|
|
|
public function __construct( |
15
|
|
|
CellFactory $cellFactory, |
16
|
|
|
FilterBarFactory $filterFactory |
17
|
|
|
) { |
18
|
|
|
$this->cellFactory = $cellFactory; |
19
|
|
|
$this->filterFactory = $filterFactory; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function createView(AgentInterface $agent, GridContext $gridContext, GridMetadata $gridMetadata) |
23
|
|
|
{ |
24
|
|
|
// create the filter form based on the metadata and submit any data. |
25
|
|
|
$filterForm = $this->filterFactory->createForm($gridMetadata, $agent->getCapabilities()); |
26
|
|
|
$filterForm->submit($gridContext->getFilter()); |
27
|
|
|
|
28
|
|
|
// create the query and get the data collection from the object-agent. |
29
|
|
|
$query = Query::create( |
30
|
|
|
$gridContext->getClassFqn(), |
31
|
|
|
$this->filterFactory->createExpression($gridMetadata, $filterForm->getData()), |
32
|
|
|
$gridContext->getOrderings(), |
33
|
|
|
$gridContext->getPageOffset(), |
34
|
|
|
$gridContext->getPageSize() |
35
|
|
|
); |
36
|
|
|
$collection = new \IteratorIterator($agent->query($query)); |
37
|
|
|
|
38
|
|
|
return new View\Grid( |
39
|
|
|
$gridContext->getClassFqn(), |
40
|
|
|
$gridMetadata->getName(), |
41
|
|
|
new View\Table($this->cellFactory, $gridMetadata, $collection, $gridContext), |
42
|
|
|
new View\Paginator($gridContext, count($collection), $this->getNumberOfRecords($agent, $query)), |
43
|
|
|
new View\FilterBar($filterForm->createView(), $gridContext), |
44
|
|
|
new View\ActionBar($gridMetadata) |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
private function getNumberOfRecords(AgentInterface $agent, Query $query) |
49
|
|
|
{ |
50
|
|
|
if (false === $agent->getCapabilities()->canQueryCount()) { |
51
|
|
|
return; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $agent->queryCount($query); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|