|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Netgen\Bundle\InformationCollectionBundle\Controller\Admin; |
|
4
|
|
|
|
|
5
|
|
|
use eZ\Publish\Core\MVC\Symfony\Security\Authorization\Attribute; |
|
6
|
|
|
use eZ\Bundle\EzPublishCoreBundle\Controller; |
|
|
|
|
|
|
7
|
|
|
use Netgen\InformationCollection\API\Service\InformationCollection; |
|
8
|
|
|
use Netgen\InformationCollection\API\Value\Content; |
|
9
|
|
|
use Netgen\InformationCollection\API\Value\Filter\ContentId; |
|
10
|
|
|
use Netgen\InformationCollection\API\Value\Filter\Query; |
|
11
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
12
|
|
|
use Symfony\Component\Routing\RouterInterface; |
|
13
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
|
14
|
|
|
|
|
15
|
|
|
class TreeController extends Controller |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var \Symfony\Contracts\Translation\TranslatorInterface |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $translator; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var \Symfony\Component\Routing\RouterInterface |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $router; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var \Netgen\InformationCollection\API\Service\InformationCollection |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $service; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* TreeController constructor. |
|
34
|
|
|
* |
|
35
|
|
|
* @param \Netgen\InformationCollection\API\Service\InformationCollection $service |
|
36
|
|
|
* @param \Symfony\Contracts\Translation\TranslatorInterface $translator |
|
37
|
|
|
* @param \Symfony\Component\Routing\RouterInterface $router |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct( |
|
40
|
|
|
InformationCollection $service, |
|
41
|
|
|
TranslatorInterface $translator, |
|
42
|
|
|
RouterInterface $router |
|
43
|
|
|
) { |
|
44
|
|
|
$this->translator = $translator; |
|
45
|
|
|
$this->router = $router; |
|
46
|
|
|
$this->service = $service; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Get contents with collections |
|
51
|
|
|
* |
|
52
|
|
|
* @param bool $isRoot |
|
53
|
|
|
* |
|
54
|
|
|
* @return \Symfony\Component\HttpFoundation\JsonResponse |
|
55
|
|
|
*/ |
|
56
|
|
|
public function getChildrenAction($isRoot = false) |
|
57
|
|
|
{ |
|
58
|
|
|
$attribute = new Attribute('infocollector', 'read'); |
|
59
|
|
|
$this->denyAccessUnlessGranted($attribute); |
|
60
|
|
|
|
|
61
|
|
|
$result = array(); |
|
62
|
|
|
|
|
63
|
|
|
if ((bool) $isRoot) { |
|
64
|
|
|
$result[] = $this->getRootTreeData(); |
|
65
|
|
|
} else { |
|
66
|
|
|
|
|
67
|
|
|
$query = Query::withLimit($this->getConfigResolver()->getParameter('admin.tree_limit', 'netgen_information_collection')); |
|
68
|
|
|
|
|
69
|
|
|
$objects = $this->service->getObjectsWithCollections($query); |
|
70
|
|
|
foreach ($objects->getContents() as $content) { |
|
71
|
|
|
$result[] = $this->getCollections($content, $isRoot); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return (new JsonResponse())->setData($result); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Generates data for root of the tree. |
|
80
|
|
|
* |
|
81
|
|
|
* @return array |
|
82
|
|
|
*/ |
|
83
|
|
|
protected function getRootTreeData() |
|
84
|
|
|
{ |
|
85
|
|
|
$count = $this->service->getObjectsWithCollectionsCount(); |
|
86
|
|
|
|
|
87
|
|
|
return array( |
|
88
|
|
|
'id' => '0', |
|
89
|
|
|
'parent' => '#', |
|
90
|
|
|
'text' => $this->translator->trans('netgen_information_collection_admin_collected_information', ['%count%' => $count->getCount()], 'netgen_information_collection_admin'), |
|
91
|
|
|
'children' => true, |
|
92
|
|
|
'state' => array( |
|
93
|
|
|
'opened' => true, |
|
94
|
|
|
), |
|
95
|
|
|
'a_attr' => array( |
|
96
|
|
|
'href' => $this->router->generate('netgen_information_collection.route.admin.overview'), |
|
97
|
|
|
'rel' => '0', |
|
98
|
|
|
), |
|
99
|
|
|
'data' => array( |
|
100
|
|
|
), |
|
101
|
|
|
); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Creates tree structure for Content |
|
106
|
|
|
* |
|
107
|
|
|
* @param \Netgen\InformationCollection\API\Value\Content $content |
|
108
|
|
|
* @param bool $isRoot |
|
109
|
|
|
* |
|
110
|
|
|
* @return array |
|
111
|
|
|
*/ |
|
112
|
|
|
protected function getCollections(Content $content, $isRoot = false) |
|
113
|
|
|
{ |
|
114
|
|
|
$languages = $this->getConfigResolver()->getParameter('languages'); |
|
115
|
|
|
|
|
116
|
|
|
$query = ContentId::countWithContentId($content->getContent()->id); |
|
117
|
|
|
|
|
118
|
|
|
$count = $this->service->getCollectionsCount($query); |
|
119
|
|
|
|
|
120
|
|
|
return array( |
|
121
|
|
|
'id' => $content->getContent()->id, |
|
122
|
|
|
'parent' => $isRoot ? '#' : '0', |
|
123
|
|
|
'text' => $content->getContent()->getName($languages[0]) . ' (' . strval($count->getCount()) . ')', |
|
124
|
|
|
'children' => false, |
|
125
|
|
|
'a_attr' => array( |
|
126
|
|
|
'href' => $this->router->generate('netgen_information_collection.route.admin.collection_list', ['contentId' => $content->getContent()->id]), |
|
127
|
|
|
'rel' => $content->getContent()->id, |
|
128
|
|
|
), |
|
129
|
|
|
'state' => array( |
|
130
|
|
|
'opened' => $isRoot, |
|
131
|
|
|
), |
|
132
|
|
|
'data' => array( |
|
133
|
|
|
'context_menu' => array( |
|
134
|
|
|
array( |
|
135
|
|
|
'name' => 'export', |
|
136
|
|
|
'url' => $this->router->generate('netgen_information_collection.route.admin.export', ['contentId' => $content->getContent()->id]), |
|
137
|
|
|
'text' => $this->translator->trans('netgen_information_collection_admin_export_export', [], 'netgen_information_collection_admin'), |
|
138
|
|
|
), |
|
139
|
|
|
), |
|
140
|
|
|
), |
|
141
|
|
|
); |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: