/
entry.php
150 lines (130 loc) · 5.73 KB
/
entry.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
<?php
namespace Concrete\Controller\Backend\Express;
use Concrete\Core\Controller\AbstractController;
use Concrete\Core\Entity\Express\Entity;
use Concrete\Core\Error\ErrorList\ErrorList;
use Concrete\Core\Express\EntryList;
use Concrete\Core\Express\ExpressTransformer;
use Concrete\Core\Express\Search\Field\SiteField;
use Concrete\Core\Express\Search\SearchProvider;
use Concrete\Core\Permission\Checker;
use Concrete\Core\Search\Field\Field\KeywordsField;
use Concrete\Core\Search\Pagination\Pagination;
use Concrete\Core\Search\Pagination\PaginationFactory;
use Concrete\Core\Entity\Express\Entry as EntryEntity;
use Concrete\Core\Search\Query\Modifier\AutoSortColumnRequestModifier;
use Concrete\Core\Search\Query\QueryFactory;
use Concrete\Core\Search\Query\QueryModifier;
use Concrete\Core\Search\Result\ResultFactory;
use Doctrine\ORM\EntityManager;
use League\Fractal\Manager;
use League\Fractal\Pagination\PagerfantaPaginatorAdapter;
use League\Fractal\Resource\Collection;
use League\Fractal\Serializer\DataArraySerializer;
use Symfony\Component\HttpFoundation\JsonResponse;
use Exception;
use Symfony\Component\HttpFoundation\Request;
class Entry extends AbstractController
{
protected $entityManager;
/**
* @var Manager
*/
protected $manager;
public function __construct(EntityManager $entityManager, Manager $manager)
{
parent::__construct();
$this->entityManager = $entityManager;
$this->manager = $manager->setSerializer(new DataArraySerializer());
}
public function getJSON()
{
/** @var ErrorList $errorList */
$errorList = $this->app->make(ErrorList::class);
$data = [];
if ($this->request->query->has('exEntryID')) {
$entryIDs = [];
$entryID = $this->request->query->get('exEntryID');
if (is_array($entryID)) {
$entryIDs = $entryID;
} else {
$entryIDs[] = $entryID;
}
if (count($entryIDs) > 0) {
$data = [
"entries" => []
];
$entryRepository = $this->entityManager->getRepository(EntryEntity::class);
foreach ($entryIDs as $entryID) {
/** @noinspection PhpPossiblePolymorphicInvocationInspection */
$entry = $entryRepository->findOneById($entryID);
if ($entry instanceof EntryEntity) {
$permissionChecker = new Checker($entry->getEntity());
if ($permissionChecker->canViewExpressEntries()) {
$data["entries"][] = $entry->jsonSerialize();
} else {
$errorList->add(t('Access Denied.'));
}
}
}
if (count($data["entries"]) === 0) {
$errorList->add(t('Entries not found.'));
}
}
} elseif ($this->request->query->has('exEntityID')) {
$exEntityID = $this->request->query->get('exEntityID');
$entityRepository = $this->entityManager->getRepository(Entity::class);
$entity = $entityRepository->find($exEntityID);
if ($entity instanceof Entity) {
$permissionChecker = new Checker($entity);
if ($permissionChecker->canViewExpressEntries()) {
$filters = [new SiteField()];
if ($this->request->query->has('keyword')) {
$filters[] = new KeywordsField($this->request->query->get('keyword'));
}
/** @var Entity $entity */
$entity = $entityRepository->find($exEntityID);
$provider = $this->app->make(SearchProvider::class, ['entity' => $entity]);
$resultFactory = $this->app->make(ResultFactory::class);
$queryFactory = $this->app->make(QueryFactory::class);
$query = $queryFactory->createQuery($provider, $filters);
$queryModifier = new QueryModifier();
$queryModifier->addModifier(new AutoSortColumnRequestModifier($provider, $this->request, Request::METHOD_GET));
$query = $queryModifier->process($query);
$result = $resultFactory->createFromQuery($provider, $query);
$list = $result->getItemListObject();
$data = $this->buildExpressListFractalArray($list);
}
}
}
if ($errorList->has()) {
return new JsonResponse($errorList);
} else {
return new JsonResponse($data);
}
}
/**
* @param EntryList $list
*
* @return array
*/
protected function buildExpressListFractalArray(EntryList $list)
{
$adapter = $list->getPaginationAdapter();
$paginationFactory = $this->app->make(PaginationFactory::class, ['request' => $this->request]);
$pagination = $paginationFactory->deliverPaginationObject($list, new Pagination($list, $adapter));
$collection = new Collection($pagination->getCurrentPageResults(), new ExpressTransformer());
$collection->setPaginator(new PagerfantaPaginatorAdapter($pagination, function ($page) {
return $page;
}));
$collection->setMeta([
'query_params' => [
'pagination_page' => $list->getQueryPaginationPageParameter(),
'sort_column' => $list->getQuerySortColumnParameter(),
'sort_direction' => $list->getQuerySortDirectionParameter(),
],
]);
$response = $this->manager->createData($collection);
return $response->toArray();
}
}