1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunstmaan\NodeSearchBundle\EventListener; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
6
|
|
|
use Doctrine\ORM\Event\LifecycleEventArgs; |
7
|
|
|
use Kunstmaan\NodeBundle\Entity\Node; |
8
|
|
|
use Kunstmaan\NodeBundle\Entity\NodeTranslation; |
9
|
|
|
use Kunstmaan\NodeBundle\Entity\StructureNode; |
10
|
|
|
use Kunstmaan\NodeBundle\Event\NodeEvent; |
11
|
|
|
use Kunstmaan\NodeSearchBundle\Configuration\NodePagesConfiguration; |
12
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* EventListener which will be triggered when a Node has been updated in order to update its related documents |
16
|
|
|
* in the index |
17
|
|
|
*/ |
18
|
|
|
class NodeIndexUpdateEventListener implements NodeIndexUpdateEventListenerInterface |
19
|
|
|
{ |
20
|
|
|
/** @var ContainerInterface */ |
21
|
|
|
private $container; |
22
|
|
|
|
23
|
|
|
/** @var EntityManagerInterface */ |
24
|
|
|
private $em; |
25
|
|
|
|
26
|
|
|
/** @var NodePagesConfiguration */ |
27
|
|
|
private $nodePagesConfiguration; |
28
|
|
|
|
29
|
|
|
/** @var array */ |
30
|
|
|
private $entityChangeSet; |
31
|
|
|
|
32
|
|
|
public function __construct(/* NodePagesConfiguration */ |
33
|
|
|
$nodePagesConfiguration, /* EntityManagerInterface */ |
34
|
|
|
$em = null) |
35
|
|
|
{ |
36
|
|
|
if ($nodePagesConfiguration instanceof ContainerInterface) { |
37
|
|
|
@trigger_error(sprintf('Passing the container as the first argument of "%s" is deprecated in KunstmaanNodeSearchBundle 5.2 and will be removed in KunstmaanNodeSearchBundle 6.0. Inject the "%s" service instead.', __CLASS__, 'kunstmaan_node_search.search_configuration.node'), E_USER_DEPRECATED); |
|
|
|
|
38
|
|
|
|
39
|
|
|
$this->container = $nodePagesConfiguration; |
40
|
|
|
$this->nodePagesConfiguration = $this->container->get('kunstmaan_node_search.search_configuration.node'); |
41
|
|
|
|
42
|
|
|
if (null === $em) { |
43
|
|
|
$this->em = $this->container->get('doctrine.orm.default_entity_manager'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if (!($em instanceof EntityManagerInterface)) { |
50
|
|
|
@trigger_error(sprintf('Passing null or something other than an entitymanagerinterface as the second argument of "%s" is deprecated in KunstmaanNodeSearchBundle 5.2 and will be removed in KunstmaanNodeSearchBundle 6.0. Inject the "%s" service instead.', __CLASS__, 'doctrine.orm.default_entity_manager'), E_USER_DEPRECATED); |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$this->nodePagesConfiguration = $nodePagesConfiguration; |
54
|
|
|
$this->em = $em; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param LifecycleEventArgs $args |
59
|
|
|
*/ |
60
|
|
|
public function preUpdate(LifecycleEventArgs $args) |
61
|
|
|
{ |
62
|
|
|
if ($args->getObject() instanceof NodeTranslation) { |
63
|
|
|
// unfortunately we have to keep a state to see what has changed |
64
|
|
|
$this->entityChangeSet = [ |
65
|
|
|
'nodeTranslationId' => $args->getObject()->getId(), |
66
|
|
|
'changeSet' => $args->getEntityManager()->getUnitOfWork()->getEntityChangeSet($args->getObject()), |
67
|
|
|
]; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param NodeEvent $event |
73
|
|
|
*/ |
74
|
|
|
public function onPostPublish(NodeEvent $event) |
75
|
|
|
{ |
76
|
|
|
$this->index($event); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param NodeEvent $event |
81
|
|
|
*/ |
82
|
|
|
public function onPostPersist(NodeEvent $event) |
83
|
|
|
{ |
84
|
|
|
$reIndexChildren = ( |
85
|
|
|
!is_null($this->entityChangeSet) |
86
|
|
|
&& $this->entityChangeSet['nodeTranslationId'] == $event->getNodeTranslation()->getId() |
87
|
|
|
&& isset($this->entityChangeSet['changeSet']['url']) |
88
|
|
|
); |
89
|
|
|
$this->index($event, $reIndexChildren); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param NodeEvent $event |
94
|
|
|
* @param bool $reIndexChildren |
95
|
|
|
*/ |
96
|
|
|
private function index(NodeEvent $event, $reIndexChildren = false) |
97
|
|
|
{ |
98
|
|
|
$nodeTranslation = $event->getNodeTranslation(); |
99
|
|
|
|
100
|
|
|
if ($this->hasOfflineParents($nodeTranslation)) { |
101
|
|
|
return; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$this->nodePagesConfiguration->indexNodeTranslation($nodeTranslation, true); |
105
|
|
|
|
106
|
|
|
if ($reIndexChildren) { |
107
|
|
|
$this->nodePagesConfiguration->indexChildren($event->getNode(), $nodeTranslation->getLang()); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param NodeEvent $event |
113
|
|
|
*/ |
114
|
|
|
public function onPostDelete(NodeEvent $event) |
115
|
|
|
{ |
116
|
|
|
$this->delete($event); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param NodeEvent $event |
121
|
|
|
*/ |
122
|
|
|
public function onPostUnPublish(NodeEvent $event) |
123
|
|
|
{ |
124
|
|
|
$this->delete($event); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param NodeEvent $event |
129
|
|
|
*/ |
130
|
|
|
public function delete(NodeEvent $event) |
131
|
|
|
{ |
132
|
|
|
$this->nodePagesConfiguration->deleteNodeTranslation($event->getNodeTranslation()); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param $nodeTranslation |
137
|
|
|
* |
138
|
|
|
* @return bool |
139
|
|
|
*/ |
140
|
|
|
private function hasOfflineParents(NodeTranslation $nodeTranslation) |
141
|
|
|
{ |
142
|
|
|
$lang = $nodeTranslation->getLang(); |
143
|
|
|
$node = $nodeTranslation->getNode(); |
144
|
|
|
if (null !== $this->em) { |
145
|
|
|
$em = $this->em; |
146
|
|
|
} else { |
147
|
|
|
$lang = $nodeTranslation->getLang(); |
148
|
|
|
foreach ($nodeTranslation->getNode()->getParents() as $node) { |
149
|
|
|
$nodeNT = $node->getNodeTranslation($lang, true); |
150
|
|
|
if ($nodeNT && !$nodeNT->isOnline()) { |
151
|
|
|
return true; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
return false; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
foreach ($node->getParents() as $parent) { |
158
|
|
|
$parentNodeTranslation = $parent->getNodeTranslation($lang, true); |
159
|
|
|
if (null === $parentNodeTranslation) { |
160
|
|
|
continue; |
161
|
|
|
} |
162
|
|
|
$parentRef = $parentNodeTranslation->getRef($em); |
|
|
|
|
163
|
|
|
// Continue looping unless we find an offline page that is not a StructureNode |
164
|
|
|
if ($parentRef instanceof StructureNode || $parentNodeTranslation->isOnline()) { |
165
|
|
|
continue; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return true; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return false; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: