-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathDocumentGridSource.php
executable file
·142 lines (122 loc) · 4.44 KB
/
DocumentGridSource.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
namespace Dtc\GridBundle\Grid\Source;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
use Doctrine\ODM\MongoDB\DocumentManager;
use Dtc\GridBundle\Grid\Column\GridColumn;
class DocumentGridSource extends AbstractGridSource
{
use ColumnExtractionTrait;
protected $documentManager;
protected $repository;
protected $findCache;
protected $documentName;
public function __construct(DocumentManager $documentManager, $documentName)
{
$this->documentManager = $documentManager;
$this->repository = $documentManager->getRepository($documentName);
$this->documentName = $documentName;
}
/**
* @return \Doctrine\ODM\MongoDB\Query\Builder
*/
protected function getQueryBuilder()
{
$qb = $this->documentManager->createQueryBuilder($this->documentName);
/** @var ClassMetadata $classMetaData */
$classMetaData = $this->getClassMetadata();
$classFields = $classMetaData->fieldMappings;
$columns = $this->getColumns();
$fieldList = [];
foreach ($columns as $column) {
if ($column instanceof GridColumn && $column->isSearchable()) {
$fieldList[$column->getField()] = true;
}
}
if ($this->filter) {
$validFilters = array_intersect_key($this->filter, $classFields);
$query = array();
foreach ($validFilters as $key => $value) {
if (isset($fieldList[$key])) {
if (is_array($value)) {
$qb->field($key)->in($value);
} else {
$qb->field($key)->equals($value);
}
}
}
if (!$query) {
$starFilter = array_intersect_key($this->filter, ['*' => null]);
if ($starFilter) {
$value = current($starFilter);
foreach ($classFields as $key => $info) {
if (isset($fieldList[$key])) {
$expr = $qb->expr()->field($key);
switch ($info['type']) {
case 'integer':
$expr = $expr->equals(intval($value));
break;
default:
$expr = $expr->equals($value);
}
$qb->addOr($expr);
}
// @TODO - maybe allow pattern searches some day: new \MongoRegex('/.*'.$value.'.*/')
}
}
}
}
if ($this->orderBy) {
foreach ($this->orderBy as $key => $direction) {
$qb->sort($key, $direction);
}
}
$qb->limit($this->limit);
$qb->skip($this->offset);
return $qb;
}
/**
* @return mixed
*/
public function getClassMetadata()
{
$metaFactory = $this->documentManager->getMetadataFactory();
$classInfo = $metaFactory->getMetadataFor($this->documentName);
return $classInfo;
}
public function getCount()
{
$result = $this->getQueryBuilder()->limit(0)->skip(0)->count()->getQuery()->execute();
return $result;
}
public function getRecords()
{
return $this->getQueryBuilder()->getQuery()->execute()->toArray(false);
}
public function find($id)
{
if (!$this->hasIdColumn()) {
throw new \Exception('No id column found for '.$this->documentName);
}
$qb = $this->documentManager->createQueryBuilder($this->documentName);
$idColumn = $this->getIdColumn();
$qb->field($idColumn)->equals($id);
$result = $qb->getQuery()->execute()->toArray(false);
if (isset($result[0])) {
return $result[0];
}
}
public function remove($id, $soft = false, $softColumn = 'deletedAt', $softColumnType = 'datetime')
{
if (!$this->hasIdColumn()) {
throw new \Exception('No id column found for '.$this->documentName);
}
$repository = $this->documentManager->getRepository($this->documentName);
$document = $repository->find($id);
if ($document) {
$this->documentManager->remove($document);
$this->documentManager->flush();
return true;
}
return false;
}
}