Completed
Push — master ( ae5e03...0447ee )
by Jeroen
10:35 queued 04:37
created

NodeBundle/Controller/NodeAdminController.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\NodeBundle\Controller;
4
5
use DateTime;
6
use Doctrine\Common\Collections\ArrayCollection;
7
use Doctrine\ORM\EntityManager;
8
use InvalidArgumentException;
9
use Kunstmaan\AdminBundle\Entity\BaseUser;
10
use Kunstmaan\AdminBundle\Entity\EntityInterface;
11
use Kunstmaan\AdminBundle\FlashMessages\FlashTypes;
12
use Kunstmaan\AdminBundle\Helper\FormWidgets\FormWidget;
13
use Kunstmaan\AdminBundle\Helper\FormWidgets\Tabs\Tab;
14
use Kunstmaan\AdminBundle\Helper\FormWidgets\Tabs\TabPane;
15
use Kunstmaan\AdminBundle\Helper\Security\Acl\AclHelper;
16
use Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\PermissionMap;
17
use Kunstmaan\AdminBundle\Service\AclManager;
18
use Kunstmaan\AdminListBundle\AdminList\AdminList;
19
use Kunstmaan\NodeBundle\AdminList\NodeAdminListConfigurator;
20
use Kunstmaan\NodeBundle\Entity\HasNodeInterface;
21
use Kunstmaan\NodeBundle\Entity\Node;
22
use Kunstmaan\NodeBundle\Entity\NodeTranslation;
23
use Kunstmaan\NodeBundle\Entity\NodeVersion;
24
use Kunstmaan\NodeBundle\Entity\QueuedNodeTranslationAction;
25
use Kunstmaan\NodeBundle\Event\AdaptFormEvent;
26
use Kunstmaan\NodeBundle\Event\CopyPageTranslationNodeEvent;
27
use Kunstmaan\NodeBundle\Event\Events;
28
use Kunstmaan\NodeBundle\Event\NodeEvent;
29
use Kunstmaan\NodeBundle\Event\RecopyPageTranslationNodeEvent;
30
use Kunstmaan\NodeBundle\Event\RevertNodeAction;
31
use Kunstmaan\NodeBundle\Form\NodeMenuTabAdminType;
32
use Kunstmaan\NodeBundle\Form\NodeMenuTabTranslationAdminType;
33
use Kunstmaan\NodeBundle\Helper\NodeAdmin\NodeAdminPublisher;
34
use Kunstmaan\NodeBundle\Helper\NodeAdmin\NodeVersionLockHelper;
35
use Kunstmaan\NodeBundle\Helper\PageCloningHelper;
36
use Kunstmaan\NodeBundle\Repository\NodeVersionRepository;
37
use Kunstmaan\UtilitiesBundle\Helper\ClassLookup;
38
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
39
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
40
use Symfony\Component\HttpFoundation\JsonResponse;
41
use Symfony\Component\HttpFoundation\RedirectResponse;
42
use Symfony\Component\HttpFoundation\Request;
43
use Symfony\Component\Routing\Annotation\Route;
44
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
45
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
46
use Symfony\Component\Translation\TranslatorInterface;
47
48
/**
49
 * NodeAdminController
50
 */
51
class NodeAdminController extends Controller
52
{
53
    /**
54
     * @var EntityManager
55
     */
56
    protected $em;
57
58
    /**
59
     * @var string
60
     */
61
    protected $locale;
62
63
    /**
64
     * @var AuthorizationCheckerInterface
65
     */
66
    protected $authorizationChecker;
67
68
    /**
69
     * @var BaseUser
70
     */
71
    protected $user;
72
73
    /**
74
     * @var AclHelper
75
     */
76
    protected $aclHelper;
77
78
    /**
79
     * @var AclManager
80
     */
81
    protected $aclManager;
82
83
    /**
84
     * @var NodeAdminPublisher
85
     */
86
    protected $nodePublisher;
87
88
    /**
89
     * @var TranslatorInterface
90
     */
91
    protected $translator;
92
93
    /** @var PageCloningHelper */
94
    private $pageCloningHelper;
95
96
    /**
97
     * init
98
     *
99
     * @param Request $request
100
     */
101
    protected function init(Request $request)
102
    {
103
        $this->em = $this->getDoctrine()->getManager();
104
        $this->locale = $request->getLocale();
105
        $this->authorizationChecker = $this->container->get('security.authorization_checker');
106
        $this->user = $this->getUser();
107
        $this->aclHelper = $this->container->get('kunstmaan_admin.acl.helper');
108
        $this->aclManager = $this->container->get('kunstmaan_admin.acl.manager');
109
        $this->nodePublisher = $this->container->get('kunstmaan_node.admin_node.publisher');
110
        $this->translator = $this->container->get('translator');
111
        $this->pageCloningHelper = $this->container->get(PageCloningHelper::class);
112
    }
113
114
    /**
115
     * @Route("/", name="KunstmaanNodeBundle_nodes")
116
     * @Template("@KunstmaanNode/Admin/list.html.twig")
117
     *
118
     * @param Request $request
119
     *
120
     * @return array
121
     */
122
    public function indexAction(Request $request)
123
    {
124
        $this->init($request);
125
126
        $nodeAdminListConfigurator = new NodeAdminListConfigurator(
127
            $this->em,
128
            $this->aclHelper,
129
            $this->locale,
130
            PermissionMap::PERMISSION_VIEW,
131
            $this->authorizationChecker
132
        );
133
134
        $locale = $this->locale;
135
        $acl = $this->authorizationChecker;
136
        $itemRoute = function (EntityInterface $item) use ($locale, $acl) {
137
            if ($acl->isGranted(PermissionMap::PERMISSION_VIEW, $item->getNode())) {
138
                return array(
139
                    'path' => '_slug_preview',
140
                    'params' => ['_locale' => $locale, 'url' => $item->getUrl()],
141
                );
142
            }
143
        };
144
        $nodeAdminListConfigurator->addSimpleItemAction('action.preview', $itemRoute, 'eye');
145
        $nodeAdminListConfigurator->setDomainConfiguration($this->get('kunstmaan_admin.domain_configuration'));
146
        $nodeAdminListConfigurator->setShowAddHomepage($this->getParameter('kunstmaan_node.show_add_homepage') && $this->isGranted('ROLE_SUPER_ADMIN'));
147
148
        /** @var AdminList $adminlist */
149
        $adminlist = $this->get('kunstmaan_adminlist.factory')->createList($nodeAdminListConfigurator);
150
        $adminlist->bindRequest($request);
151
152
        return array(
153
            'adminlist' => $adminlist,
154
        );
155
    }
156
157
    /**
158
     * @Route(
159
     *      "/{id}/copyfromotherlanguage",
160
     *      requirements={"id" = "\d+"},
161
     *      name="KunstmaanNodeBundle_nodes_copyfromotherlanguage",
162
     *      methods={"GET"}
163
     * )
164
     *
165
     * @param Request $request
166
     * @param int     $id      The node id
167
     *
168
     * @return RedirectResponse
169
     *
170
     * @throws AccessDeniedException
171
     */
172
    public function copyFromOtherLanguageAction(Request $request, $id)
173
    {
174
        $this->init($request);
175
        /* @var Node $node */
176
        $node = $this->em->getRepository(Node::class)->find($id);
177
178
        $this->denyAccessUnlessGranted(PermissionMap::PERMISSION_EDIT, $node);
179
180
        $originalLanguage = $request->get('originallanguage');
181
        $otherLanguageNodeTranslation = $node->getNodeTranslation($originalLanguage, true);
182
        $otherLanguageNodeNodeVersion = $otherLanguageNodeTranslation->getPublicNodeVersion();
183
        $otherLanguagePage = $otherLanguageNodeNodeVersion->getRef($this->em);
184
        $myLanguagePage = $this->get('kunstmaan_admin.clone.helper')
185
            ->deepCloneAndSave($otherLanguagePage);
186
187
        /* @var NodeTranslation $nodeTranslation */
188
        $nodeTranslation = $this->em->getRepository(NodeTranslation::class)
189
            ->createNodeTranslationFor($myLanguagePage, $this->locale, $node, $this->user);
190
        $nodeVersion = $nodeTranslation->getPublicNodeVersion();
191
192
        $this->get('event_dispatcher')->dispatch(
193
            Events::COPY_PAGE_TRANSLATION,
194
            new CopyPageTranslationNodeEvent(
195
                $node,
196
                $nodeTranslation,
197
                $nodeVersion,
198
                $myLanguagePage,
199
                $otherLanguageNodeTranslation,
200
                $otherLanguageNodeNodeVersion,
201
                $otherLanguagePage,
202
                $originalLanguage
203
            )
204
        );
205
206
        return $this->redirect($this->generateUrl('KunstmaanNodeBundle_nodes_edit', array('id' => $id)));
207
    }
208
209
    /**
210
     * @Route(
211
     *      "/{id}/recopyfromotherlanguage",
212
     *      requirements={"id" = "\d+"},
213
     *      name="KunstmaanNodeBundle_nodes_recopyfromotherlanguage",
214
     *      methods={"POST"}
215
     * )
216
     *
217
     * @param Request $request
218
     * @param int     $id      The node id
219
     *
220
     * @return RedirectResponse
221
     *
222
     * @throws AccessDeniedException
223
     */
224
    public function recopyFromOtherLanguageAction(Request $request, $id)
225
    {
226
        $this->init($request);
227
        /* @var Node $node */
228
        $node = $this->em->getRepository(Node::class)->find($id);
229
230
        $this->denyAccessUnlessGranted(PermissionMap::PERMISSION_EDIT, $node);
231
232
        $otherLanguageNodeTranslation = $this->em->getRepository(NodeTranslation::class)->find($request->get('source'));
233
        $otherLanguageNodeNodeVersion = $otherLanguageNodeTranslation->getPublicNodeVersion();
234
        $otherLanguagePage = $otherLanguageNodeNodeVersion->getRef($this->em);
235
        $myLanguagePage = $this->get('kunstmaan_admin.clone.helper')
236
            ->deepCloneAndSave($otherLanguagePage);
237
238
        /* @var NodeTranslation $nodeTranslation */
239
        $nodeTranslation = $this->em->getRepository(NodeTranslation::class)
240
            ->addDraftNodeVersionFor($myLanguagePage, $this->locale, $node, $this->user);
241
        $nodeVersion = $nodeTranslation->getPublicNodeVersion();
242
243
        $this->get('event_dispatcher')->dispatch(
244
            Events::RECOPY_PAGE_TRANSLATION,
245
            new RecopyPageTranslationNodeEvent(
246
                $node,
247
                $nodeTranslation,
248
                $nodeVersion,
249
                $myLanguagePage,
250
                $otherLanguageNodeTranslation,
251
                $otherLanguageNodeNodeVersion,
252
                $otherLanguagePage,
253
                $otherLanguageNodeTranslation->getLang()
254
            )
255
        );
256
257
        return $this->redirect($this->generateUrl('KunstmaanNodeBundle_nodes_edit', array('id' => $id, 'subaction' => NodeVersion::DRAFT_VERSION)));
258
    }
259
260
    /**
261
     * @Route(
262
     *      "/{id}/createemptypage",
263
     *      requirements={"id" = "\d+"},
264
     *      name="KunstmaanNodeBundle_nodes_createemptypage",
265
     *      methods={"GET"}
266
     * )
267
     *
268
     * @param Request $request
269
     * @param int     $id
270
     *
271
     * @return RedirectResponse
272
     *
273
     * @throws AccessDeniedException
274
     */
275
    public function createEmptyPageAction(Request $request, $id)
276
    {
277
        $this->init($request);
278
        /* @var Node $node */
279
        $node = $this->em->getRepository(Node::class)->find($id);
280
281
        $this->denyAccessUnlessGranted(PermissionMap::PERMISSION_EDIT, $node);
282
283
        $entityName = $node->getRefEntityName();
284
        /* @var HasNodeInterface $myLanguagePage */
285
        $myLanguagePage = new $entityName();
286
        $myLanguagePage->setTitle('New page');
287
288
        $this->em->persist($myLanguagePage);
289
        $this->em->flush();
290
        /* @var NodeTranslation $nodeTranslation */
291
        $nodeTranslation = $this->em->getRepository(NodeTranslation::class)
292
            ->createNodeTranslationFor($myLanguagePage, $this->locale, $node, $this->user);
293
        $nodeVersion = $nodeTranslation->getPublicNodeVersion();
294
295
        $this->get('event_dispatcher')->dispatch(
296
            Events::ADD_EMPTY_PAGE_TRANSLATION,
297
            new NodeEvent($node, $nodeTranslation, $nodeVersion, $myLanguagePage)
298
        );
299
300
        return $this->redirect($this->generateUrl('KunstmaanNodeBundle_nodes_edit', array('id' => $id)));
301
    }
302
303
    /**
304
     * @Route("/{id}/publish", requirements={"id" =
305
     *                         "\d+"},
306
     *                         name="KunstmaanNodeBundle_nodes_publish", methods={"GET", "POST"})
307
     *
308
     * @param Request $request
309
     * @param int     $id
310
     *
311
     * @return RedirectResponse
312
     *
313
     * @throws AccessDeniedException
314
     */
315 View Code Duplication
    public function publishAction(Request $request, $id)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
316
    {
317
        $this->init($request);
318
        /* @var Node $node */
319
        $node = $this->em->getRepository(Node::class)->find($id);
320
321
        $nodeTranslation = $node->getNodeTranslation($this->locale, true);
322
        $request = $this->get('request_stack')->getCurrentRequest();
323
        $this->nodePublisher->chooseHowToPublish($request, $nodeTranslation, $this->translator);
324
325
        return $this->redirect($this->generateUrl('KunstmaanNodeBundle_nodes_edit', array('id' => $node->getId())));
326
    }
327
328
    /**
329
     * @Route(
330
     *      "/{id}/unpublish",
331
     *      requirements={"id" = "\d+"},
332
     *      name="KunstmaanNodeBundle_nodes_unpublish",
333
     *      methods={"GET", "POST"}
334
     * )
335
     *
336
     * @param Request $request
337
     * @param int     $id
338
     *
339
     * @return RedirectResponse
340
     *
341
     * @throws AccessDeniedException
342
     */
343 View Code Duplication
    public function unPublishAction(Request $request, $id)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
344
    {
345
        $this->init($request);
346
        /* @var Node $node */
347
        $node = $this->em->getRepository(Node::class)->find($id);
348
349
        $nodeTranslation = $node->getNodeTranslation($this->locale, true);
350
        $request = $this->get('request_stack')->getCurrentRequest();
351
        $this->nodePublisher->chooseHowToUnpublish($request, $nodeTranslation, $this->translator);
352
353
        return $this->redirect($this->generateUrl('KunstmaanNodeBundle_nodes_edit', array('id' => $node->getId())));
354
    }
355
356
    /**
357
     * @Route(
358
     *      "/{id}/unschedulepublish",
359
     *      requirements={"id" = "\d+"},
360
     *      name="KunstmaanNodeBundle_nodes_unschedule_publish",
361
     *      methods={"GET", "POST"}
362
     * )
363
     *
364
     * @param Request $request
365
     * @param int     $id
366
     *
367
     * @return RedirectResponse
368
     *
369
     * @throws AccessDeniedException
370
     */
371 View Code Duplication
    public function unSchedulePublishAction(Request $request, $id)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
372
    {
373
        $this->init($request);
374
375
        /* @var Node $node */
376
        $node = $this->em->getRepository(Node::class)->find($id);
377
378
        $nodeTranslation = $node->getNodeTranslation($this->locale, true);
379
        $this->nodePublisher->unSchedulePublish($nodeTranslation);
380
381
        $this->addFlash(
382
            FlashTypes::SUCCESS,
383
            $this->get('translator')->trans('kuma_node.admin.unschedule.flash.success')
384
        );
385
386
        return $this->redirect($this->generateUrl('KunstmaanNodeBundle_nodes_edit', array('id' => $id)));
387
    }
388
389
    /**
390
     * @Route(
391
     *      "/{id}/delete",
392
     *      requirements={"id" = "\d+"},
393
     *      name="KunstmaanNodeBundle_nodes_delete",
394
     *      methods={"POST"}
395
     * )
396
     *
397
     * @param Request $request
398
     * @param int     $id
399
     *
400
     * @return RedirectResponse
401
     *
402
     * @throws AccessDeniedException
403
     */
404
    public function deleteAction(Request $request, $id)
405
    {
406
        $this->init($request);
407
        /* @var Node $node */
408
        $node = $this->em->getRepository(Node::class)->find($id);
409
410
        $this->denyAccessUnlessGranted(PermissionMap::PERMISSION_DELETE, $node);
411
412
        $nodeTranslation = $node->getNodeTranslation($this->locale, true);
413
        $nodeVersion = $nodeTranslation->getPublicNodeVersion();
414
        $page = $nodeVersion->getRef($this->em);
415
416
        $this->get('event_dispatcher')->dispatch(
417
            Events::PRE_DELETE,
418
            new NodeEvent($node, $nodeTranslation, $nodeVersion, $page)
419
        );
420
421
        $node->setDeleted(true);
422
        $this->em->persist($node);
423
424
        $children = $node->getChildren();
425
        $this->deleteNodeChildren($this->em, $this->user, $this->locale, $children);
426
        $this->em->flush();
427
428
        $event = new NodeEvent($node, $nodeTranslation, $nodeVersion, $page);
429
        $this->get('event_dispatcher')->dispatch(Events::POST_DELETE, $event);
430
        if (null === $response = $event->getResponse()) {
431
            $nodeParent = $node->getParent();
432
            // Check if we have a parent. Otherwise redirect to pages overview.
433
            if ($nodeParent) {
434
                $url = $this->get('router')->generate(
435
                    'KunstmaanNodeBundle_nodes_edit',
436
                    array('id' => $nodeParent->getId())
437
                );
438
            } else {
439
                $url = $this->get('router')->generate(
440
                    'KunstmaanNodeBundle_nodes'
441
                );
442
            }
443
            $response = new RedirectResponse($url);
444
        }
445
446
        $this->addFlash(
447
            FlashTypes::SUCCESS,
448
            $this->get('translator')->trans('kuma_node.admin.delete.flash.success')
449
        );
450
451
        return $response;
452
    }
453
454
    /**
455
     * @Route(
456
     *      "/{id}/duplicate",
457
     *      requirements={"id" = "\d+"},
458
     *      name="KunstmaanNodeBundle_nodes_duplicate",
459
     *      methods={"POST"}
460
     * )
461
     *
462
     * @param Request $request
463
     * @param int     $id
464
     *
465
     * @return RedirectResponse
466
     *
467
     * @throws AccessDeniedException
468
     */
469
    public function duplicateAction(Request $request, $id)
470
    {
471
        $this->init($request);
472
        /* @var Node $parentNode */
473
        $originalNode = $this->em->getRepository(Node::class)
474
            ->find($id);
475
476
        // Check with Acl
477
        $this->denyAccessUnlessGranted(PermissionMap::PERMISSION_EDIT, $originalNode);
478
479
        $request = $this->get('request_stack')->getCurrentRequest();
480
481
        $originalNodeTranslations = $originalNode->getNodeTranslation($this->locale, true);
482
        $originalRef = $originalNodeTranslations->getPublicNodeVersion()->getRef($this->em);
483
        $newPage = $this->get('kunstmaan_admin.clone.helper')
484
            ->deepCloneAndSave($originalRef);
485
486
        //set the title
487
        $title = $request->get('title');
488
        if (\is_string($title) && !empty($title)) {
489
            $newPage->setTitle($title);
490
        } else {
491
            $newPage->setTitle('New page');
492
        }
493
494
        //set the parent
495
        $parentNodeTranslation = $originalNode->getParent()->getNodeTranslation($this->locale, true);
496
        $parent = $parentNodeTranslation->getPublicNodeVersion()->getRef($this->em);
497
        $newPage->setParent($parent);
498
        $this->em->persist($newPage);
499
        $this->em->flush();
500
501
        /* @var Node $nodeNewPage */
502
        $nodeNewPage = $this->em->getRepository(Node::class)->createNodeFor(
503
            $newPage,
504
            $this->locale,
505
            $this->user
506
        );
507
508
        $nodeTranslation = $nodeNewPage->getNodeTranslation($this->locale, true);
509
        if ($newPage->isStructureNode()) {
510
            $nodeTranslation->setSlug('');
511
            $this->em->persist($nodeTranslation);
512
        }
513
        $this->em->flush();
514
515
        $this->aclManager->updateNodeAcl($originalNode, $nodeNewPage);
516
517
        $this->addFlash(
518
            FlashTypes::SUCCESS,
519
            $this->get('translator')->trans('kuma_node.admin.duplicate.flash.success')
520
        );
521
522
        return $this->redirect(
523
            $this->generateUrl('KunstmaanNodeBundle_nodes_edit', array('id' => $nodeNewPage->getId()))
524
        );
525
    }
526
527
    /**
528
     * @Route(
529
     *      "/{id}/duplicate-with-children",
530
     *      requirements={"id" = "\d+"},
531
     *      name="KunstmaanNodeBundle_nodes_duplicate_with_children",
532
     *      methods={"POST"}
533
     * )
534
     *
535
     * @throws AccessDeniedException
536
     */
537
    public function duplicateWithChildrenAction(Request $request, $id)
538
    {
539
        if (!$this->getParameter('kunstmaan_node.show_duplicate_with_children')) {
540
            return $this->redirectToRoute('KunstmaanNodeBundle_nodes_edit', ['id' => $id]);
541
        }
542
543
        $this->init($request);
544
        $title = $request->get('title', null);
545
546
        $nodeNewPage = $this->pageCloningHelper->duplicateWithChildren($id, $this->locale, $this->user, $title);
547
548
        $this->addFlash(FlashTypes::SUCCESS, $this->get('translator')->trans('kuma_node.admin.duplicate.flash.success'));
549
550
        return $this->redirectToRoute('KunstmaanNodeBundle_nodes_edit', ['id' => $nodeNewPage->getId()]);
551
    }
552
553
    /**
554
     * @Route(
555
     *      "/{id}/revert",
556
     *      requirements={"id" = "\d+"},
557
     *      defaults={"subaction" = "public"},
558
     *      name="KunstmaanNodeBundle_nodes_revert",
559
     *      methods={"GET"}
560
     * )
561
     *
562
     * @param Request $request
563
     * @param int     $id      The node id
564
     *
565
     * @return RedirectResponse
566
     *
567
     * @throws AccessDeniedException
568
     * @throws InvalidArgumentException
569
     */
570
    public function revertAction(Request $request, $id)
571
    {
572
        $this->init($request);
573
        /* @var Node $node */
574
        $node = $this->em->getRepository(Node::class)->find($id);
575
576
        $this->denyAccessUnlessGranted(PermissionMap::PERMISSION_EDIT, $node);
577
578
        $version = $request->get('version');
579
580
        if (empty($version) || !is_numeric($version)) {
581
            throw new InvalidArgumentException('No version was specified');
582
        }
583
584
        /* @var NodeVersionRepository $nodeVersionRepo */
585
        $nodeVersionRepo = $this->em->getRepository(NodeVersion::class);
586
        /* @var NodeVersion $nodeVersion */
587
        $nodeVersion = $nodeVersionRepo->find($version);
588
589
        if (\is_null($nodeVersion)) {
590
            throw new InvalidArgumentException('Version does not exist');
591
        }
592
593
        /* @var NodeTranslation $nodeTranslation */
594
        $nodeTranslation = $node->getNodeTranslation($this->locale, true);
595
        $page = $nodeVersion->getRef($this->em);
596
        /* @var HasNodeInterface $clonedPage */
597
        $clonedPage = $this->get('kunstmaan_admin.clone.helper')
598
            ->deepCloneAndSave($page);
599
        $newNodeVersion = $nodeVersionRepo->createNodeVersionFor(
600
            $clonedPage,
601
            $nodeTranslation,
602
            $this->user,
603
            $nodeVersion,
604
            'draft'
605
        );
606
607
        $nodeTranslation->setTitle($clonedPage->getTitle());
608
        $this->em->persist($nodeTranslation);
609
        $this->em->flush();
610
611
        $this->get('event_dispatcher')->dispatch(
612
            Events::REVERT,
613
            new RevertNodeAction(
614
                $node,
615
                $nodeTranslation,
616
                $newNodeVersion,
617
                $clonedPage,
618
                $nodeVersion,
619
                $page
620
            )
621
        );
622
623
        $this->addFlash(
624
            FlashTypes::SUCCESS,
625
            $this->get('translator')->trans('kuma_node.admin.revert.flash.success')
626
        );
627
628
        return $this->redirect(
629
            $this->generateUrl(
630
                'KunstmaanNodeBundle_nodes_edit',
631
                array(
632
                    'id' => $id,
633
                    'subaction' => 'draft',
634
                )
635
            )
636
        );
637
    }
638
639
    /**
640
     * @Route(
641
     *      "/{id}/add",
642
     *      requirements={"id" = "\d+"},
643
     *      name="KunstmaanNodeBundle_nodes_add",
644
     *      methods={"POST"}
645
     * )
646
     *
647
     * @param Request $request
648
     * @param int     $id
649
     *
650
     * @return RedirectResponse
651
     *
652
     * @throws AccessDeniedException
653
     * @throws InvalidArgumentException
654
     */
655
    public function addAction(Request $request, $id)
656
    {
657
        $this->init($request);
658
        /* @var Node $parentNode */
659
        $parentNode = $this->em->getRepository(Node::class)->find($id);
660
661
        // Check with Acl
662
        $this->denyAccessUnlessGranted(PermissionMap::PERMISSION_EDIT, $parentNode);
663
664
        $parentNodeTranslation = $parentNode->getNodeTranslation($this->locale, true);
665
        $parentNodeVersion = $parentNodeTranslation->getPublicNodeVersion();
666
        $parentPage = $parentNodeVersion->getRef($this->em);
667
668
        $type = $this->validatePageType($request);
669
        $newPage = $this->createNewPage($request, $type);
670
        $newPage->setParent($parentPage);
671
672
        /* @var Node $nodeNewPage */
673
        $nodeNewPage = $this->em->getRepository(Node::class)
674
            ->createNodeFor($newPage, $this->locale, $this->user);
675
        $nodeTranslation = $nodeNewPage->getNodeTranslation(
676
            $this->locale,
677
            true
678
        );
679
        $weight = $this->em->getRepository(NodeTranslation::class)
680
                ->getMaxChildrenWeight($parentNode, $this->locale) + 1;
681
        $nodeTranslation->setWeight($weight);
682
683
        if ($newPage->isStructureNode()) {
684
            $nodeTranslation->setSlug('');
685
        }
686
687
        $this->em->persist($nodeTranslation);
688
        $this->em->flush();
689
690
        $this->aclManager->updateNodeAcl($parentNode, $nodeNewPage);
691
692
        $nodeVersion = $nodeTranslation->getPublicNodeVersion();
693
694
        $this->get('event_dispatcher')->dispatch(
695
            Events::ADD_NODE,
696
            new NodeEvent(
697
                $nodeNewPage, $nodeTranslation, $nodeVersion, $newPage
698
            )
699
        );
700
701
        return $this->redirect(
702
            $this->generateUrl(
703
                'KunstmaanNodeBundle_nodes_edit',
704
                array('id' => $nodeNewPage->getId())
705
            )
706
        );
707
    }
708
709
    /**
710
     * @Route("/add-homepage", name="KunstmaanNodeBundle_nodes_add_homepage", methods={"POST"})
711
     *
712
     * @return RedirectResponse
713
     *
714
     * @throws AccessDeniedException
715
     * @throws InvalidArgumentException
716
     */
717
    public function addHomepageAction(Request $request)
718
    {
719
        $this->init($request);
720
721
        // Check with Acl
722
        $this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN');
723
724
        $type = $this->validatePageType($request);
725
726
        $newPage = $this->createNewPage($request, $type);
727
728
        /* @var Node $nodeNewPage */
729
        $nodeNewPage = $this->em->getRepository(Node::class)
730
            ->createNodeFor($newPage, $this->locale, $this->user);
731
        $nodeTranslation = $nodeNewPage->getNodeTranslation(
732
            $this->locale,
733
            true
734
        );
735
        $this->em->flush();
736
737
        // Set default permissions
738
        $this->container->get('kunstmaan_node.acl_permission_creator_service')
739
            ->createPermission($nodeNewPage);
740
741
        $nodeVersion = $nodeTranslation->getPublicNodeVersion();
742
743
        $this->get('event_dispatcher')->dispatch(
744
            Events::ADD_NODE,
745
            new NodeEvent(
746
                $nodeNewPage, $nodeTranslation, $nodeVersion, $newPage
747
            )
748
        );
749
750
        return $this->redirect(
751
            $this->generateUrl(
752
                'KunstmaanNodeBundle_nodes_edit',
753
                array('id' => $nodeNewPage->getId())
754
            )
755
        );
756
    }
757
758
    /**
759
     * @Route("/reorder", name="KunstmaanNodeBundle_nodes_reorder", methods={"POST"})
760
     *
761
     * @param Request $request
762
     *
763
     * @return string
764
     *
765
     * @throws AccessDeniedException
766
     */
767
    public function reorderAction(Request $request)
768
    {
769
        $this->init($request);
770
        $nodes = array();
771
        $nodeIds = $request->get('nodes');
772
        $changeParents = $request->get('parent');
773
774
        foreach ($nodeIds as $id) {
775
            /* @var Node $node */
776
            $node = $this->em->getRepository(Node::class)->find($id);
777
            $this->denyAccessUnlessGranted(PermissionMap::PERMISSION_EDIT, $node);
778
            $nodes[] = $node;
779
        }
780
781
        $weight = 1;
782
        foreach ($nodes as $node) {
783
            $newParentId = isset($changeParents[$node->getId()]) ? $changeParents[$node->getId()] : null;
784
            if ($newParentId) {
785
                $parent = $this->em->getRepository(Node::class)->find($newParentId);
786
                $this->denyAccessUnlessGranted(PermissionMap::PERMISSION_EDIT, $parent);
787
                $node->setParent($parent);
788
                $this->em->persist($node);
789
                $this->em->flush($node);
790
            }
791
792
            /* @var NodeTranslation $nodeTranslation */
793
            $nodeTranslation = $node->getNodeTranslation($this->locale, true);
794
795
            if ($nodeTranslation) {
796
                $nodeVersion = $nodeTranslation->getPublicNodeVersion();
797
                $page = $nodeVersion->getRef($this->em);
798
799
                $this->get('event_dispatcher')->dispatch(
800
                    Events::PRE_PERSIST,
801
                    new NodeEvent($node, $nodeTranslation, $nodeVersion, $page)
802
                );
803
804
                $nodeTranslation->setWeight($weight);
805
                $this->em->persist($nodeTranslation);
806
                $this->em->flush($nodeTranslation);
807
808
                $this->get('event_dispatcher')->dispatch(
809
                    Events::POST_PERSIST,
810
                    new NodeEvent($node, $nodeTranslation, $nodeVersion, $page)
811
                );
812
813
                ++$weight;
814
            }
815
        }
816
817
        return new JsonResponse(
818
            array(
819
                'Success' => 'The node-translations for [' . $this->locale . '] have got new weight values',
820
            )
821
        );
822
    }
823
824
    /**
825
     * @Route(
826
     *      "/{id}/{subaction}",
827
     *      requirements={"id" = "\d+"},
828
     *      defaults={"subaction" = "public"},
829
     *      name="KunstmaanNodeBundle_nodes_edit",
830
     *      methods={"GET", "POST"}
831
     * )
832
     * @Template("@KunstmaanNode/NodeAdmin/edit.html.twig")
833
     *
834
     * @param Request $request
835
     * @param int     $id        The node id
836
     * @param string  $subaction The subaction (draft|public)
837
     *
838
     * @return RedirectResponse|array
839
     *
840
     * @throws AccessDeniedException
841
     */
842
    public function editAction(Request $request, $id, $subaction)
843
    {
844
        $this->init($request);
845
        /* @var Node $node */
846
        $node = $this->em->getRepository(Node::class)->find($id);
847
848
        $this->denyAccessUnlessGranted(PermissionMap::PERMISSION_EDIT, $node);
849
850
        $tabPane = new TabPane(
851
            'todo',
852
            $request,
853
            $this->container->get('form.factory')
854
        );
855
856
        $nodeTranslation = $node->getNodeTranslation($this->locale, true);
857
        if (!$nodeTranslation) {
858
            return $this->renderNodeNotTranslatedPage($node);
859
        }
860
861
        $nodeVersion = $nodeTranslation->getPublicNodeVersion();
862
        $draftNodeVersion = $nodeTranslation->getDraftNodeVersion();
863
        $nodeVersionIsLocked = false;
864
865
        /* @var HasNodeInterface $page */
866
        $page = null;
867
        $draft = ($subaction == 'draft');
868
        $saveAsDraft = $request->get('saveasdraft');
869
        if ((!$draft && !empty($saveAsDraft)) || ($draft && \is_null($draftNodeVersion))) {
870
            // Create a new draft version
871
            $draft = true;
872
            $subaction = 'draft';
873
            $page = $nodeVersion->getRef($this->em);
874
            $nodeVersion = $this->createDraftVersion(
875
                $page,
876
                $nodeTranslation,
877
                $nodeVersion
878
            );
879
            $draftNodeVersion = $nodeVersion;
880
        } elseif ($draft) {
881
            $nodeVersion = $draftNodeVersion;
882
            $page = $nodeVersion->getRef($this->em);
883
        } else {
884
            if ($request->getMethod() == 'POST') {
885
                $nodeVersionIsLocked = $this->isNodeVersionLocked($nodeTranslation, true);
886
887
                //Check the version timeout and make a new nodeversion if the timeout is passed
888
                $thresholdDate = date(
889
                    'Y-m-d H:i:s',
890
                    time() - $this->getParameter(
891
                        'kunstmaan_node.version_timeout'
892
                    )
893
                );
894
                $updatedDate = date(
895
                    'Y-m-d H:i:s',
896
                    strtotime($nodeVersion->getUpdated()->format('Y-m-d H:i:s'))
897
                );
898 View Code Duplication
                if ($thresholdDate >= $updatedDate || $nodeVersionIsLocked) {
899
                    $page = $nodeVersion->getRef($this->em);
900
                    if ($nodeVersion === $nodeTranslation->getPublicNodeVersion()) {
901
                        $this->nodePublisher
902
                            ->createPublicVersion(
903
                                $page,
904
                                $nodeTranslation,
905
                                $nodeVersion,
906
                                $this->user
907
                            );
908
                    } else {
909
                        $this->createDraftVersion(
910
                            $page,
911
                            $nodeTranslation,
912
                            $nodeVersion
913
                        );
914
                    }
915
                }
916
            }
917
            $page = $nodeVersion->getRef($this->em);
918
        }
919
        $isStructureNode = $page->isStructureNode();
920
921
        $menubuilder = $this->get('kunstmaan_node.actions_menu_builder');
922
        $menubuilder->setActiveNodeVersion($nodeVersion);
923
        $menubuilder->setEditableNode(!$isStructureNode);
924
925
        // Building the form
926
        $propertiesWidget = new FormWidget();
927
        $propertiesWidget->addType('main', $page->getDefaultAdminType(), $page);
928
        $propertiesWidget->addType('node', $node->getDefaultAdminType(), $node);
929
        $tabPane->addTab(new Tab('kuma_node.tab.properties.title', $propertiesWidget));
930
931
        // Menu tab
932
        $menuWidget = new FormWidget();
933
        $menuWidget->addType(
934
            'menunodetranslation',
935
            NodeMenuTabTranslationAdminType::class,
936
            $nodeTranslation,
937
            ['slugable' => !$isStructureNode]
938
        );
939
        $menuWidget->addType('menunode', NodeMenuTabAdminType::class, $node, ['available_in_nav' => !$isStructureNode]);
940
        $tabPane->addTab(new Tab('kuma_node.tab.menu.title', $menuWidget));
941
942
        $this->get('event_dispatcher')->dispatch(
943
            Events::ADAPT_FORM,
944
            new AdaptFormEvent(
945
                $request,
946
                $tabPane,
947
                $page,
948
                $node,
949
                $nodeTranslation,
950
                $nodeVersion
951
            )
952
        );
953
954
        $tabPane->buildForm();
955
956
        if ($request->getMethod() == 'POST') {
957
            $tabPane->bindRequest($request);
958
959
            // Don't redirect to listing when coming from ajax request, needed for url chooser.
960
            if ($tabPane->isValid() && !$request->isXmlHttpRequest()) {
961
                $this->get('event_dispatcher')->dispatch(
962
                    Events::PRE_PERSIST,
963
                    new NodeEvent($node, $nodeTranslation, $nodeVersion, $page)
964
                );
965
966
                $nodeTranslation->setTitle($page->getTitle());
967
                if ($isStructureNode) {
968
                    $nodeTranslation->setSlug('');
969
                }
970
                $nodeVersion->setUpdated(new DateTime());
971
                if ($nodeVersion->getType() == 'public') {
972
                    $nodeTranslation->setUpdated($nodeVersion->getUpdated());
973
                }
974
                $this->em->persist($nodeTranslation);
975
                $this->em->persist($nodeVersion);
976
                $tabPane->persist($this->em);
977
                $this->em->flush();
978
979
                $this->get('event_dispatcher')->dispatch(
980
                    Events::POST_PERSIST,
981
                    new NodeEvent($node, $nodeTranslation, $nodeVersion, $page)
982
                );
983
984
                if ($nodeVersionIsLocked) {
985
                    $this->addFlash(
986
                        FlashTypes::SUCCESS,
987
                        $this->get('translator')->trans('kuma_node.admin.edit.flash.locked_success')
988
                    );
989
                } elseif ($request->request->has('publishing') || $request->request->has('publish_later')) {
990
                    $this->nodePublisher->chooseHowToPublish($request, $nodeTranslation, $this->translator);
991
                } elseif ($request->request->has('unpublishing') || $request->request->has('unpublish_later')) {
992
                    $this->nodePublisher->chooseHowToUnpublish($request, $nodeTranslation, $this->translator);
993
                } else {
994
                    $this->addFlash(
995
                        FlashTypes::SUCCESS,
996
                        $this->get('translator')->trans('kuma_node.admin.edit.flash.success')
997
                    );
998
                }
999
1000
                $params = [
1001
                    'id' => $node->getId(),
1002
                    'subaction' => $subaction,
1003
                    'currenttab' => $tabPane->getActiveTab(),
1004
                ];
1005
                $params = array_merge(
1006
                    $params,
1007
                    $tabPane->getExtraParams($request)
1008
                );
1009
1010
                if ($subaction === 'draft' && ($request->request->has('publishing') || $request->request->has('publish_later'))) {
1011
                    unset($params['subaction']);
1012
                }
1013
1014
                return $this->redirect(
1015
                    $this->generateUrl(
1016
                        'KunstmaanNodeBundle_nodes_edit',
1017
                        $params
1018
                    )
1019
                );
1020
            }
1021
        }
1022
1023
        $nodeVersions = $this->em->getRepository(NodeVersion::class)->findBy(
1024
            ['nodeTranslation' => $nodeTranslation],
1025
            ['updated' => 'ASC']
1026
        );
1027
        $queuedNodeTranslationAction = $this->em->getRepository(QueuedNodeTranslationAction::class)->findOneBy(['nodeTranslation' => $nodeTranslation]);
1028
1029
        return [
1030
            'page' => $page,
1031
            'entityname' => ClassLookup::getClass($page),
1032
            'nodeVersions' => $nodeVersions,
1033
            'node' => $node,
1034
            'nodeTranslation' => $nodeTranslation,
1035
            'draft' => $draft,
1036
            'draftNodeVersion' => $draftNodeVersion,
1037
            'showDuplicateWithChildren' => $this->getParameter('kunstmaan_node.show_duplicate_with_children'),
1038
            'nodeVersion' => $nodeVersion,
1039
            'subaction' => $subaction,
1040
            'tabPane' => $tabPane,
1041
            'editmode' => true,
1042
            'childCount' => $this->em->getRepository('KunstmaanNodeBundle:Node')->getChildCount($node),
1043
            'queuedNodeTranslationAction' => $queuedNodeTranslationAction,
1044
            'nodeVersionLockCheck' => $this->container->getParameter('kunstmaan_node.lock_enabled'),
1045
            'nodeVersionLockInterval' => $this->container->getParameter('kunstmaan_node.lock_check_interval'),
1046
        ];
1047
    }
1048
1049
    /**
1050
     * @Route(
1051
     *      "checkNodeVersionLock/{id}/{public}",
1052
     *      requirements={"id" = "\d+", "public" = "(0|1)"},
1053
     *      name="KunstmaanNodeBundle_nodes_versionlock_check"
1054
     * )
1055
     *
1056
     * @param Request $request
1057
     * @param $id
1058
     *
1059
     * @return JsonResponse
1060
     */
1061
    public function checkNodeVersionLockAction(Request $request, $id, $public)
1062
    {
1063
        $nodeVersionIsLocked = false;
1064
        $message = '';
1065
        $this->init($request);
1066
1067
        /* @var Node $node */
1068
        $node = $this->em->getRepository(Node::class)->find($id);
1069
1070
        try {
1071
            $this->checkPermission($node, PermissionMap::PERMISSION_EDIT);
1072
1073
            /** @var NodeVersionLockHelper $nodeVersionLockHelper */
1074
            $nodeVersionLockHelper = $this->get('kunstmaan_node.admin_node.node_version_lock_helper');
1075
            $nodeTranslation = $node->getNodeTranslation($this->locale, true);
1076
1077
            if ($nodeTranslation) {
1078
                $nodeVersionIsLocked = $nodeVersionLockHelper->isNodeVersionLocked($this->getUser(), $nodeTranslation, $public);
1079
1080
                if ($nodeVersionIsLocked) {
1081
                    $users = $nodeVersionLockHelper->getUsersWithNodeVersionLock($nodeTranslation, $public, $this->getUser());
1082
                    $message = $this->get('translator')->trans('kuma_node.admin.edit.flash.locked', array('%users%' => implode(', ', $users)));
1083
                }
1084
            }
1085
        } catch (AccessDeniedException $ade) {
1086
        }
1087
1088
        return new JsonResponse(['lock' => $nodeVersionIsLocked, 'message' => $message]);
1089
    }
1090
1091
    /**
1092
     * @param NodeTranslation $nodeTranslation
1093
     * @param bool            $isPublic
1094
     *
1095
     * @return bool
1096
     */
1097
    private function isNodeVersionLocked(NodeTranslation $nodeTranslation, $isPublic)
1098
    {
1099
        if ($this->container->getParameter('kunstmaan_node.lock_enabled')) {
1100
            /** @var NodeVersionLockHelper $nodeVersionLockHelper */
1101
            $nodeVersionLockHelper = $this->get('kunstmaan_node.admin_node.node_version_lock_helper');
1102
            $nodeVersionIsLocked = $nodeVersionLockHelper->isNodeVersionLocked($this->getUser(), $nodeTranslation, $isPublic);
1103
1104
            return $nodeVersionIsLocked;
1105
        }
1106
1107
        return false;
1108
    }
1109
1110
    /**
1111
     * @param HasNodeInterface $page            The page
1112
     * @param NodeTranslation  $nodeTranslation The node translation
1113
     * @param NodeVersion      $nodeVersion     The node version
1114
     *
1115
     * @return NodeVersion
1116
     */
1117
    private function createDraftVersion(
1118
        HasNodeInterface $page,
1119
        NodeTranslation $nodeTranslation,
1120
        NodeVersion $nodeVersion
1121
    ) {
1122
        $publicPage = $this->get('kunstmaan_admin.clone.helper')
1123
            ->deepCloneAndSave($page);
1124
        /* @var NodeVersion $publicNodeVersion */
1125
1126
        $publicNodeVersion = $this->em->getRepository(
1127
            NodeVersion::class
1128
        )->createNodeVersionFor(
1129
            $publicPage,
1130
            $nodeTranslation,
1131
            $this->user,
1132
            $nodeVersion->getOrigin(),
1133
            'public',
1134
            $nodeVersion->getCreated()
1135
        );
1136
1137
        $nodeTranslation->setPublicNodeVersion($publicNodeVersion);
1138
        $nodeVersion->setType('draft');
1139
        $nodeVersion->setOrigin($publicNodeVersion);
1140
        $nodeVersion->setCreated(new DateTime());
1141
1142
        $this->em->persist($nodeTranslation);
1143
        $this->em->persist($nodeVersion);
1144
        $this->em->flush();
1145
1146
        $this->get('event_dispatcher')->dispatch(
1147
            Events::CREATE_DRAFT_VERSION,
1148
            new NodeEvent(
1149
                $nodeTranslation->getNode(),
1150
                $nodeTranslation,
1151
                $nodeVersion,
1152
                $page
1153
            )
1154
        );
1155
1156
        return $nodeVersion;
1157
    }
1158
1159
    /**
1160
     * @param Node   $node       The node
1161
     * @param string $permission The permission to check for
1162
     *
1163
     * @throws AccessDeniedException
1164
     */
1165
    private function checkPermission(Node $node, $permission)
1166
    {
1167
        if (false === $this->authorizationChecker->isGranted($permission, $node)) {
1168
            throw new AccessDeniedException();
1169
        }
1170
    }
1171
1172
    /**
1173
     * @param EntityManager   $em       The Entity Manager
1174
     * @param BaseUser        $user     The user who deletes the children
1175
     * @param string          $locale   The locale that was used
1176
     * @param ArrayCollection $children The children array
1177
     */
1178
    private function deleteNodeChildren(
1179
        EntityManager $em,
1180
        BaseUser $user,
1181
        $locale,
1182
        ArrayCollection $children
1183
    ) {
1184
        /* @var Node $childNode */
1185
        foreach ($children as $childNode) {
1186
            $childNodeTranslation = $childNode->getNodeTranslation(
1187
                $this->locale,
1188
                true
1189
            );
1190
1191
            $childNodeVersion = $childNodeTranslation->getPublicNodeVersion();
1192
            $childNodePage = $childNodeVersion->getRef($this->em);
1193
1194
            $this->get('event_dispatcher')->dispatch(
1195
                Events::PRE_DELETE,
1196
                new NodeEvent(
1197
                    $childNode,
1198
                    $childNodeTranslation,
1199
                    $childNodeVersion,
1200
                    $childNodePage
1201
                )
1202
            );
1203
1204
            $childNode->setDeleted(true);
1205
            $this->em->persist($childNode);
1206
1207
            $children2 = $childNode->getChildren();
1208
            $this->deleteNodeChildren($em, $user, $locale, $children2);
1209
1210
            $this->get('event_dispatcher')->dispatch(
1211
                Events::POST_DELETE,
1212
                new NodeEvent(
1213
                    $childNode,
1214
                    $childNodeTranslation,
1215
                    $childNodeVersion,
1216
                    $childNodePage
1217
                )
1218
            );
1219
        }
1220
    }
1221
1222
    /**
1223
     * @param Request $request
1224
     * @param string  $type
1225
     *
1226
     * @return HasNodeInterface
1227
     */
1228
    private function createNewPage(Request $request, $type)
1229
    {
1230
        /* @var HasNodeInterface $newPage */
1231
        $newPage = new $type();
1232
1233
        $title = $request->get('title');
1234
        if (\is_string($title) && !empty($title)) {
1235
            $newPage->setTitle($title);
1236
        } else {
1237
            $newPage->setTitle($this->get('translator')->trans('kuma_node.admin.new_page.title.default'));
1238
        }
1239
        $this->em->persist($newPage);
1240
        $this->em->flush();
1241
1242
        return $newPage;
1243
    }
1244
1245
    /**
1246
     * @param Request $request
1247
     *
1248
     * @return string
1249
     * @throw InvalidArgumentException
1250
     */
1251
    private function validatePageType($request)
1252
    {
1253
        $type = $request->get('type');
1254
1255
        if (empty($type)) {
1256
            throw new InvalidArgumentException('Please specify a type of page you want to create');
1257
        }
1258
1259
        return $type;
1260
    }
1261
1262
    /**
1263
     * @param Node $node
1264
     *
1265
     * @return \Symfony\Component\HttpFoundation\Response
1266
     */
1267
    private function renderNodeNotTranslatedPage(Node $node)
1268
    {
1269
        //try to find a parent node with the correct translation, if there is none allow copy.
1270
        //if there is a parent but it doesn't have the language to copy to don't allow it
1271
        $parentNode = $node->getParent();
1272
        if ($parentNode) {
1273
            $parentNodeTranslation = $parentNode->getNodeTranslation(
1274
                $this->locale,
1275
                true
1276
            );
1277
            $parentsAreOk = false;
1278
1279
            if ($parentNodeTranslation) {
1280
                $parentsAreOk = $this->em->getRepository(
1281
                    NodeTranslation::class
1282
                )->hasParentNodeTranslationsForLanguage(
1283
                    $node->getParent()->getNodeTranslation(
1284
                        $this->locale,
1285
                        true
1286
                    ),
1287
                    $this->locale
1288
                );
1289
            }
1290
        } else {
1291
            $parentsAreOk = true;
1292
        }
1293
1294
        return $this->render(
1295
            '@KunstmaanNode/NodeAdmin/pagenottranslated.html.twig',
1296
            array(
1297
                'node' => $node,
1298
                'nodeTranslations' => $node->getNodeTranslations(
1299
                    true
1300
                ),
1301
                'copyfromotherlanguages' => $parentsAreOk,
1302
            )
1303
        );
1304
    }
1305
}
1306