-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathTreeController.php
More file actions
143 lines (125 loc) · 4.79 KB
/
TreeController.php
File metadata and controls
143 lines (125 loc) · 4.79 KB
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
<?php
namespace Netgen\Bundle\InformationCollectionBundle\Controller\Admin;
use eZ\Publish\Core\MVC\Symfony\Security\Authorization\Attribute;
use eZ\Bundle\EzPublishCoreBundle\Controller;
use Netgen\InformationCollection\API\Service\InformationCollection;
use Netgen\InformationCollection\API\Value\Content;
use Netgen\InformationCollection\API\Value\Filter\ContentId;
use Netgen\InformationCollection\API\Value\Filter\Query;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
class TreeController extends Controller
{
/**
* @var \Symfony\Contracts\Translation\TranslatorInterface
*/
protected $translator;
/**
* @var \Symfony\Component\Routing\RouterInterface
*/
protected $router;
/**
* @var \Netgen\InformationCollection\API\Service\InformationCollection
*/
protected $service;
/**
* TreeController constructor.
*
* @param \Netgen\InformationCollection\API\Service\InformationCollection $service
* @param \Symfony\Contracts\Translation\TranslatorInterface $translator
* @param \Symfony\Component\Routing\RouterInterface $router
*/
public function __construct(
InformationCollection $service,
TranslatorInterface $translator,
RouterInterface $router
) {
$this->translator = $translator;
$this->router = $router;
$this->service = $service;
}
/**
* Get contents with collections
*
* @param bool $isRoot
*
* @return \Symfony\Component\HttpFoundation\JsonResponse
*/
public function getChildrenAction($isRoot = false)
{
$attribute = new Attribute('infocollector', 'read');
$this->denyAccessUnlessGranted($attribute);
$result = array();
if ((bool) $isRoot) {
$result[] = $this->getRootTreeData();
} else {
$query = Query::withLimit($this->getConfigResolver()->getParameter('admin.tree_limit', 'netgen_information_collection'));
$objects = $this->service->getObjectsWithCollections($query);
foreach ($objects->getContents() as $content) {
$result[] = $this->getCollections($content, $isRoot);
}
}
return (new JsonResponse())->setData($result);
}
/**
* Generates data for root of the tree.
*
* @return array
*/
protected function getRootTreeData()
{
$count = $this->service->getObjectsWithCollectionsCount();
return array(
'id' => '0',
'parent' => '#',
'text' => $this->translator->trans('netgen_information_collection_admin_collected_information', ['%count%' => $count->getCount()], 'netgen_information_collection_admin'),
'children' => true,
'state' => array(
'opened' => true,
),
'a_attr' => array(
'href' => $this->router->generate('netgen_information_collection.route.admin.overview'),
'rel' => '0',
),
'data' => array(
),
);
}
/**
* Creates tree structure for Content
*
* @param \Netgen\InformationCollection\API\Value\Content $content
* @param bool $isRoot
*
* @return array
*/
protected function getCollections(Content $content, $isRoot = false)
{
$languages = $this->getConfigResolver()->getParameter('languages');
$query = ContentId::countWithContentId($content->getContent()->id);
$count = $this->service->getCollectionsCount($query);
return array(
'id' => $content->getContent()->id,
'parent' => $isRoot ? '#' : '0',
'text' => $content->getContent()->getName(in_array($languages[0], $content->getContent()->getVersionInfo()->languageCodes) ? $languages[0] : null) . ' (' . strval($count->getCount()) . ')',
'children' => false,
'a_attr' => array(
'href' => $this->router->generate('netgen_information_collection.route.admin.collection_list', ['contentId' => $content->getContent()->id]),
'rel' => $content->getContent()->id,
),
'state' => array(
'opened' => $isRoot,
),
'data' => array(
'context_menu' => array(
array(
'name' => 'export',
'url' => $this->router->generate('netgen_information_collection.route.admin.export', ['contentId' => $content->getContent()->id]),
'text' => $this->translator->trans('netgen_information_collection_admin_export_export', [], 'netgen_information_collection_admin'),
),
),
),
);
}
}