|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of Sulu. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) MASSIVE ART WebServices GmbH |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the MIT license that is bundled |
|
9
|
|
|
* with this source code in the file LICENSE. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Sulu\Bundle\ArticleBundle\Document\Subscriber; |
|
13
|
|
|
|
|
14
|
|
|
use Sulu\Bundle\ArticleBundle\Document\Behavior\PageBehavior; |
|
15
|
|
|
use Sulu\Component\DocumentManager\Behavior\Mapping\ChildrenBehavior; |
|
16
|
|
|
use Sulu\Component\DocumentManager\DocumentInspector; |
|
17
|
|
|
use Sulu\Component\DocumentManager\DocumentManagerInterface; |
|
18
|
|
|
use Sulu\Component\DocumentManager\Event\HydrateEvent; |
|
19
|
|
|
use Sulu\Component\DocumentManager\Event\PersistEvent; |
|
20
|
|
|
use Sulu\Component\DocumentManager\Event\PublishEvent; |
|
21
|
|
|
use Sulu\Component\DocumentManager\Event\RemoveEvent; |
|
22
|
|
|
use Sulu\Component\DocumentManager\Event\ReorderEvent; |
|
23
|
|
|
use Sulu\Component\DocumentManager\Event\RestoreEvent; |
|
24
|
|
|
use Sulu\Component\DocumentManager\Events; |
|
25
|
|
|
use Sulu\Component\DocumentManager\PropertyEncoder; |
|
26
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Handles document-manager events to set and update page-numbers. |
|
30
|
|
|
*/ |
|
31
|
|
|
class PageSubscriber implements EventSubscriberInterface |
|
32
|
|
|
{ |
|
33
|
|
|
const FIELD = 'pageNumber'; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var DocumentInspector |
|
37
|
|
|
*/ |
|
38
|
|
|
private $documentInspector; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var PropertyEncoder |
|
42
|
|
|
*/ |
|
43
|
|
|
private $propertyEncoder; |
|
44
|
|
|
/** |
|
45
|
|
|
* @var DocumentManagerInterface |
|
46
|
|
|
*/ |
|
47
|
54 |
|
private $documentManager; |
|
48
|
|
|
|
|
49
|
54 |
|
/** |
|
50
|
54 |
|
* @param DocumentInspector $documentInspector |
|
51
|
54 |
|
* @param PropertyEncoder $propertyEncoder |
|
52
|
|
|
* @param DocumentManagerInterface $documentManager |
|
53
|
|
|
*/ |
|
54
|
|
|
public function __construct( |
|
55
|
|
|
DocumentInspector $documentInspector, |
|
56
|
52 |
|
PropertyEncoder $propertyEncoder, |
|
57
|
|
|
DocumentManagerInterface $documentManager |
|
58
|
|
|
) { |
|
59
|
52 |
|
$this->documentInspector = $documentInspector; |
|
60
|
52 |
|
$this->propertyEncoder = $propertyEncoder; |
|
61
|
52 |
|
$this->documentManager = $documentManager; |
|
62
|
52 |
|
} |
|
63
|
52 |
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* {@inheritdoc} |
|
66
|
|
|
*/ |
|
67
|
|
|
public static function getSubscribedEvents() |
|
68
|
|
|
{ |
|
69
|
|
|
return [ |
|
70
|
|
|
Events::HYDRATE => ['handleHydrate'], |
|
71
|
|
|
Events::PERSIST => [['handlePersist', -1024]], |
|
72
|
50 |
|
Events::REMOVE => [['handleRemove', 5]], |
|
73
|
|
|
Events::PUBLISH => [['handlePublishPageNumber', -1024]], |
|
74
|
50 |
|
Events::REORDER => [['handleReorder', 0]], |
|
75
|
50 |
|
Events::RESTORE => [['handleRestore', -1024]], |
|
76
|
50 |
|
]; |
|
77
|
50 |
|
} |
|
78
|
50 |
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Set the page-number to existing pages. |
|
81
|
7 |
|
* |
|
82
|
7 |
|
* @param HydrateEvent $event |
|
83
|
7 |
|
*/ |
|
84
|
|
|
public function handleHydrate(HydrateEvent $event) |
|
85
|
|
|
{ |
|
86
|
|
|
$document = $event->getDocument(); |
|
87
|
|
|
$node = $event->getNode(); |
|
88
|
|
|
$propertyName = $this->propertyEncoder->systemName(static::FIELD); |
|
89
|
|
|
if (!$document instanceof PageBehavior || !$node->hasProperty($propertyName)) { |
|
90
|
51 |
|
return; |
|
91
|
|
|
} |
|
92
|
51 |
|
|
|
93
|
51 |
|
$node = $event->getNode(); |
|
94
|
51 |
|
$document->setPageNumber($node->getPropertyValue($this->propertyEncoder->systemName(static::FIELD))); |
|
95
|
51 |
|
} |
|
96
|
50 |
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Set the page-number to new pages. |
|
99
|
8 |
|
* |
|
100
|
|
|
* @param PersistEvent $event |
|
101
|
8 |
|
*/ |
|
102
|
8 |
|
public function handlePersist(PersistEvent $event) |
|
103
|
8 |
|
{ |
|
104
|
1 |
|
$document = $event->getDocument(); |
|
105
|
|
|
$node = $event->getNode(); |
|
106
|
|
|
$propertyName = $this->propertyEncoder->systemName(static::FIELD); |
|
107
|
8 |
|
if (!$document instanceof PageBehavior) { |
|
108
|
|
|
return; |
|
109
|
8 |
|
} |
|
110
|
8 |
|
|
|
111
|
|
|
$parentDocument = $document->getParent(); |
|
112
|
|
|
|
|
113
|
|
|
$page = 1; |
|
114
|
8 |
|
foreach ($parentDocument->getChildren() as $child) { |
|
115
|
8 |
|
if (!$child instanceof PageBehavior) { |
|
116
|
8 |
|
continue; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
++$page; |
|
120
|
|
|
|
|
121
|
|
|
if ($child === $document) { |
|
122
|
|
|
break; |
|
123
|
23 |
|
} |
|
124
|
|
|
} |
|
125
|
23 |
|
|
|
126
|
23 |
|
$node->setProperty($propertyName, $page); |
|
127
|
23 |
|
$document->setPageNumber($page); |
|
128
|
23 |
|
} |
|
129
|
22 |
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Adjust the page-numbers of siblings when reordering a page. |
|
132
|
1 |
|
* |
|
133
|
1 |
|
* @param ReorderEvent $event |
|
134
|
|
|
*/ |
|
135
|
|
|
public function handleReorder(ReorderEvent $event) |
|
136
|
|
|
{ |
|
137
|
|
|
$document = $event->getDocument(); |
|
138
|
|
|
if (!$document instanceof PageBehavior) { |
|
139
|
|
|
return; |
|
140
|
5 |
|
} |
|
141
|
|
|
|
|
142
|
5 |
|
$propertyName = $this->propertyEncoder->systemName(static::FIELD); |
|
143
|
5 |
|
$parentNode = $this->documentInspector->getNode($document->getParent()); |
|
144
|
3 |
|
|
|
145
|
|
|
$page = 1; |
|
146
|
|
|
foreach ($parentNode->getNodes() as $childNode) { |
|
147
|
2 |
|
$child = $this->documentManager->find($childNode->getIdentifier(), $event->getLocale()); |
|
148
|
2 |
|
if (!$child instanceof PageBehavior) { |
|
149
|
1 |
|
continue; |
|
150
|
1 |
|
} |
|
151
|
|
|
|
|
152
|
|
|
$childNode->setProperty($propertyName, ++$page); |
|
153
|
1 |
|
$child->setPageNumber($page); |
|
154
|
1 |
|
} |
|
155
|
|
|
} |
|
156
|
2 |
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Copy page-number to live workspace. |
|
159
|
|
|
* |
|
160
|
|
|
* @param PublishEvent $event |
|
161
|
|
|
*/ |
|
162
|
|
|
public function handlePublishPageNumber(PublishEvent $event) |
|
163
|
1 |
|
{ |
|
164
|
|
|
$document = $event->getDocument(); |
|
165
|
1 |
|
$node = $event->getNode(); |
|
166
|
1 |
|
$propertyName = $this->propertyEncoder->systemName(static::FIELD); |
|
167
|
|
|
if (!$document instanceof PageBehavior) { |
|
168
|
|
|
return; |
|
169
|
|
|
} |
|
170
|
1 |
|
|
|
171
|
1 |
|
$node->setProperty($propertyName, $document->getPageNumber()); |
|
172
|
1 |
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Adjust the page-numbers of siblings when removing a page. |
|
176
|
1 |
|
* |
|
177
|
1 |
|
* @param RemoveEvent $event |
|
178
|
|
|
*/ |
|
179
|
1 |
|
public function handleRemove(RemoveEvent $event) |
|
180
|
|
|
{ |
|
181
|
|
|
$document = $event->getDocument(); |
|
182
|
|
|
if (!$document instanceof PageBehavior) { |
|
183
|
|
|
return; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
$page = 1; |
|
187
|
|
|
foreach ($document->getParent()->getChildren() as $child) { |
|
188
|
|
|
if (!$child instanceof PageBehavior || $child->getUuid() === $document->getUuid()) { |
|
189
|
|
|
continue; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
$childNode = $this->documentInspector->getNode($child); |
|
193
|
|
|
$childNode->setProperty($this->propertyEncoder->systemName(static::FIELD), ++$page); |
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Adjust the page-numbers of siblings when restoring a page. |
|
199
|
|
|
* |
|
200
|
|
|
* @param RestoreEvent $event |
|
201
|
|
|
*/ |
|
202
|
|
|
public function handleRestore(RestoreEvent $event) |
|
203
|
|
|
{ |
|
204
|
|
|
$document = $event->getDocument(); |
|
205
|
|
|
if (!$document instanceof ChildrenBehavior) { |
|
206
|
|
|
return; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
$page = 1; |
|
210
|
|
|
foreach ($document->getChildren() as $child) { |
|
211
|
|
|
if (!$child instanceof PageBehavior) { |
|
212
|
|
|
continue; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
$childNode = $this->documentInspector->getNode($child); |
|
216
|
|
|
$childNode->setProperty($this->propertyEncoder->systemName(static::FIELD), ++$page); |
|
217
|
|
|
} |
|
218
|
|
|
} |
|
219
|
|
|
} |
|
220
|
|
|
|