/
EntryList.php
177 lines (150 loc) · 5.42 KB
/
EntryList.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
namespace Concrete\Core\Express;
use Concrete\Core\Attribute\Category\ExpressCategory;
use Concrete\Core\Entity\Express\Association;
use Concrete\Core\Entity\Express\Entity;
use Concrete\Core\Entity\Express\Entry;
use Concrete\Core\Search\ItemList\Database\AttributedItemList as DatabaseItemList;
use Concrete\Core\Search\PermissionableListItemInterface;
use Pagerfanta\Adapter\DoctrineDbalAdapter;
use Concrete\Core\Search\Pagination\Pagination;
class EntryList extends DatabaseItemList implements PermissionableListItemInterface
{
protected $category;
protected $entity;
public function __construct(Entity $entity)
{
$this->category = $entity->getAttributeKeyCategory();
$this->entity = $entity;
$this->setItemsPerPage($entity->getItemsPerPage());
parent::__construct(null);
if ($entity->supportsCustomDisplayOrder()) {
$this->setItemsPerPage(-1);
$this->sortByDisplayOrderAscending();
}
}
protected function getAttributeKeyClassName()
{
return $this->category;
}
/**
* @return Entity
*/
public function getEntity()
{
return $this->entity;
}
/**
* The total results of the query.
*
* @return int
*/
public function getTotalResults()
{
$query = $this->deliverQueryObject();
return $query->resetQueryParts(['groupBy', 'orderBy'])->select('count(distinct e.exEntryID)')->setMaxResults(1)->execute()->fetchColumn();
}
public function sortByDisplayOrderAscending()
{
$this->query->orderBy('e.exEntryDisplayOrder', 'asc');
}
public function filterByKeywords($keywords)
{
$keys = $this->category->getSearchableIndexedList();
if (count($keys)) {
foreach ($keys as $ak) {
$cnt = $ak->getController();
$expressions[] = $cnt->searchKeywords($keywords, $this->query);
}
$expr = $this->query->expr();
$this->query->andWhere(call_user_func_array(array($expr, 'orX'), $expressions));
$this->query->setParameter('keywords', '%' . $keywords . '%');
} else {
$this->query->andWhere('1 = 0');
}
}
/**
* Gets the pagination object for the query.
*
* @return Pagination
*/
protected function createPaginationObject()
{
$adapter = new DoctrineDbalAdapter($this->deliverQueryObject(), function ($query) {
$query->resetQueryParts(['groupBy', 'orderBy'])->select('count(distinct e.exEntryID)')->setMaxResults(1);
});
$pagination = new Pagination($this, $adapter);
return $pagination;
}
public function getResult($queryRow)
{
$r = $this->category->getEntityManager()->getRepository('Concrete\Core\Entity\Express\Entry');
$entry = $r->findOneById($queryRow['exEntryID']);
if (is_object($entry)) {
if ($this->checkPermissions($entry)) {
return $entry;
}
}
}
public function checkPermissions($mixed)
{
if (isset($this->permissionsChecker)) {
if ($this->permissionsChecker === -1) {
return true;
} else {
return call_user_func_array($this->permissionsChecker, array($mixed));
}
}
$fp = new \Permissions($mixed);
return $fp->canViewExpressEntry();
}
public function setPermissionsChecker(\Closure $checker)
{
$this->permissionsChecker = $checker;
}
public function getPermissionsChecker()
{
return $this->permissionsChecker;
}
public function enablePermissions()
{
unset($this->permissionsChecker);
}
public function ignorePermissions()
{
$this->permissionsChecker = -1;
}
public function createQuery()
{
$table = $this->category->getIndexedSearchTable();
$this->query->select('e.exEntryID')
->from('ExpressEntityEntries', 'e')
->leftJoin('e', $table, 'ea', 'e.exEntryID = ea.exEntryID');
}
public function finalizeQuery(\Doctrine\DBAL\Query\QueryBuilder $query)
{
$query->andWhere('e.exEntryEntityID = :entityID');
$query->setParameter('entityID', $this->entity->getID());
return $query;
}
public function filterByAssociatedEntry(Association $association, Entry $entry)
{
// Find the inverse association to this one.
$sourceEntity = $association->getSourceEntity();
$targetEntity = $association->getTargetEntity();
foreach($targetEntity->getAssociations() as $targetAssociation) {
if ($targetAssociation->getTargetEntity() == $sourceEntity) {
// we have a match.
$entryAssociation = $entry->getEntryAssociation($targetAssociation);
if ($entryAssociation) {
$table = 'ase' . $entryAssociation->getID();
$this->query->innerJoin('e', 'ExpressEntityAssociationSelectedEntries', $table, 'e.exEntryID = ' . $table . '.exSelectedEntryID');
$this->query->andWhere($table . '.id = :entryAssociationID' . $entryAssociation->getID());
$this->query->setParameter('entryAssociationID' . $entryAssociation->getID(), $entryAssociation->getID());
} else {
$this->query->andWhere('1 = 0');
}
}
}
}
}