Completed
Push — 5.0 ( 5d9832...217db4 )
by Kristof
96:00 queued 83:47
created

NodeBundle/Controller/NodeAdminController.php (1 issue)

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\Entity\User;
12
use Kunstmaan\AdminBundle\FlashMessages\FlashTypes;
13
use Kunstmaan\AdminBundle\Helper\FormWidgets\FormWidget;
14
use Kunstmaan\AdminBundle\Helper\FormWidgets\Tabs\Tab;
15
use Kunstmaan\AdminBundle\Helper\FormWidgets\Tabs\TabPane;
16
use Kunstmaan\AdminBundle\Helper\Security\Acl\AclHelper;
17
use Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\PermissionMap;
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\Event\AdaptFormEvent;
25
use Kunstmaan\NodeBundle\Event\CopyPageTranslationNodeEvent;
26
use Kunstmaan\NodeBundle\Event\Events;
27
use Kunstmaan\NodeBundle\Event\NodeEvent;
28
use Kunstmaan\NodeBundle\Event\RecopyPageTranslationNodeEvent;
29
use Kunstmaan\NodeBundle\Event\RevertNodeAction;
30
use Kunstmaan\NodeBundle\Form\NodeMenuTabAdminType;
31
use Kunstmaan\NodeBundle\Form\NodeMenuTabTranslationAdminType;
32
use Kunstmaan\NodeBundle\Helper\NodeAdmin\NodeAdminPublisher;
33
use Kunstmaan\NodeBundle\Helper\NodeAdmin\NodeVersionLockHelper;
34
use Kunstmaan\NodeBundle\Repository\NodeVersionRepository;
35
use Kunstmaan\UtilitiesBundle\Helper\ClassLookup;
36
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
37
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
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\Security\Acl\Domain\RoleSecurityIdentity;
44
use Symfony\Component\Security\Acl\Model\EntryInterface;
45
use Symfony\Component\Security\Acl\Model\MutableAclProviderInterface;
46
use Symfony\Component\Security\Acl\Model\ObjectIdentityRetrievalStrategyInterface;
47
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
48
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
49
use Symfony\Component\Translation\TranslatorInterface;
50
51
/**
52
 * NodeAdminController
53
 */
54
class NodeAdminController extends Controller
55
{
56
    /**
57
     * @var EntityManager
58
     */
59
    protected $em;
60
61
    /**
62
     * @var string
63
     */
64
    protected $locale;
65
66
    /**
67
     * @var AuthorizationCheckerInterface
68
     */
69
    protected $authorizationChecker;
70
71
    /**
72
     * @var BaseUser
73
     */
74
    protected $user;
75
76
    /**
77
     * @var AclHelper
78
     */
79
    protected $aclHelper;
80
81
    /**
82
     * @var NodeAdminPublisher
83
     */
84
    protected $nodePublisher;
85
86
    /**
87
     * @var TranslatorInterface
88
     */
89
    protected $translator;
90
91
    /**
92
     * init
93
     *
94
     * @param Request $request
95
     */
96
    protected function init(Request $request)
97
    {
98
        $this->em = $this->getDoctrine()->getManager();
99
        $this->locale = $request->getLocale();
100
        $this->authorizationChecker = $this->get('security.authorization_checker');
101
        $this->user = $this->getUser();
102
        $this->aclHelper = $this->get('kunstmaan_admin.acl.helper');
103
        $this->nodePublisher = $this->get('kunstmaan_node.admin_node.publisher');
104
        $this->translator = $this->get('translator');
105
    }
106
107
    /**
108
     * @Route("/", name="KunstmaanNodeBundle_nodes")
109
     * @Template("KunstmaanNodeBundle:Admin:list.html.twig")
110
     *
111
     * @param Request $request
112
     *
113
     * @return array
114
     */
115
    public function indexAction(Request $request)
116
    {
117
        $this->init($request);
118
119
        $nodeAdminListConfigurator = new NodeAdminListConfigurator(
120
            $this->em,
121
            $this->aclHelper,
122
            $this->locale,
123
            PermissionMap::PERMISSION_VIEW,
124
            $this->authorizationChecker
125
        );
126
127
        $locale = $this->locale;
128
        $acl = $this->authorizationChecker;
129
        $itemRoute = function (EntityInterface $item) use ($locale, $acl) {
130
            if ($acl->isGranted(PermissionMap::PERMISSION_VIEW, $item->getNode())) {
131
                return array(
132
                    'path' => '_slug_preview',
133
                    'params' => ['_locale' => $locale, 'url' => $item->getUrl()],
134
                );
135
            }
136
        };
137
        $nodeAdminListConfigurator->addSimpleItemAction('action.preview', $itemRoute, 'eye');
138
        $nodeAdminListConfigurator->setDomainConfiguration($this->get('kunstmaan_admin.domain_configuration'));
139
        $nodeAdminListConfigurator->setShowAddHomepage($this->getParameter('kunstmaan_node.show_add_homepage') && $this->isGranted('ROLE_SUPER_ADMIN'));
140
141
        /** @var AdminList $adminlist */
142
        $adminlist = $this->get('kunstmaan_adminlist.factory')->createList($nodeAdminListConfigurator);
143
        $adminlist->bindRequest($request);
144
145
        return array(
146
            'adminlist' => $adminlist,
147
        );
148
    }
149
150
    /**
151
     * @Route(
152
     *      "/{id}/copyfromotherlanguage",
153
     *      requirements={"id" = "\d+"},
154
     *      name="KunstmaanNodeBundle_nodes_copyfromotherlanguage"
155
     * )
156
     * @Method("GET")
157
     * @Template()
158
     *
159
     * @param Request $request
160
     * @param int     $id      The node id
161
     *
162
     * @return RedirectResponse
163
     *
164
     * @throws AccessDeniedException
165
     */
166
    public function copyFromOtherLanguageAction(Request $request, $id)
167
    {
168
        $this->init($request);
169
        /* @var Node $node */
170
        $node = $this->em->getRepository('KunstmaanNodeBundle:Node')->find($id);
171
172
        $this->denyAccessUnlessGranted(PermissionMap::PERMISSION_EDIT, $node);
173
174
        $originalLanguage = $request->get('originallanguage');
175
        $otherLanguageNodeTranslation = $node->getNodeTranslation($originalLanguage, true);
176
        $otherLanguageNodeNodeVersion = $otherLanguageNodeTranslation->getPublicNodeVersion();
177
        $otherLanguagePage = $otherLanguageNodeNodeVersion->getRef($this->em);
178
        $myLanguagePage = $this->get('kunstmaan_admin.clone.helper')
179
            ->deepCloneAndSave($otherLanguagePage);
180
181
        /* @var NodeTranslation $nodeTranslation */
182
        $nodeTranslation = $this->em->getRepository('KunstmaanNodeBundle:NodeTranslation')
183
            ->createNodeTranslationFor($myLanguagePage, $this->locale, $node, $this->user);
184
        $nodeVersion = $nodeTranslation->getPublicNodeVersion();
185
186
        $this->get('event_dispatcher')->dispatch(
187
            Events::COPY_PAGE_TRANSLATION,
188
            new CopyPageTranslationNodeEvent(
189
                $node,
190
                $nodeTranslation,
191
                $nodeVersion,
192
                $myLanguagePage,
193
                $otherLanguageNodeTranslation,
194
                $otherLanguageNodeNodeVersion,
195
                $otherLanguagePage,
196
                $originalLanguage
197
            )
198
        );
199
200
        return $this->redirect($this->generateUrl('KunstmaanNodeBundle_nodes_edit', array('id' => $id)));
201
    }
202
203
    /**
204
     * @Route(
205
     *      "/{id}/recopyfromotherlanguage",
206
     *      requirements={"id" = "\d+"},
207
     *      name="KunstmaanNodeBundle_nodes_recopyfromotherlanguage"
208
     * )
209
     * @Method("POST")
210
     * @Template()
211
     *
212
     * @param Request $request
213
     * @param int     $id      The node id
214
     *
215
     * @return RedirectResponse
216
     *
217
     * @throws AccessDeniedException
218
     */
219
    public function recopyFromOtherLanguageAction(Request $request, $id)
220
    {
221
        $this->init($request);
222
        /* @var Node $node */
223
        $node = $this->em->getRepository('KunstmaanNodeBundle:Node')->find($id);
224
225
        $this->denyAccessUnlessGranted(PermissionMap::PERMISSION_EDIT, $node);
226
227
        $otherLanguageNodeTranslation = $this->em->getRepository('KunstmaanNodeBundle:NodeTranslation')->find($request->get('source'));
228
        $otherLanguageNodeNodeVersion = $otherLanguageNodeTranslation->getPublicNodeVersion();
229
        $otherLanguagePage = $otherLanguageNodeNodeVersion->getRef($this->em);
230
        $myLanguagePage = $this->get('kunstmaan_admin.clone.helper')
231
            ->deepCloneAndSave($otherLanguagePage);
232
233
        /* @var NodeTranslation $nodeTranslation */
234
        $nodeTranslation = $this->em->getRepository('KunstmaanNodeBundle:NodeTranslation')
235
            ->addDraftNodeVersionFor($myLanguagePage, $this->locale, $node, $this->user);
236
        $nodeVersion = $nodeTranslation->getPublicNodeVersion();
237
238
        $this->get('event_dispatcher')->dispatch(
239
            Events::RECOPY_PAGE_TRANSLATION,
240
            new RecopyPageTranslationNodeEvent(
241
                $node,
242
                $nodeTranslation,
243
                $nodeVersion,
244
                $myLanguagePage,
245
                $otherLanguageNodeTranslation,
246
                $otherLanguageNodeNodeVersion,
247
                $otherLanguagePage,
248
                $otherLanguageNodeTranslation->getLang()
249
            )
250
        );
251
252
        return $this->redirect($this->generateUrl('KunstmaanNodeBundle_nodes_edit', array('id' => $id, 'subaction' => NodeVersion::DRAFT_VERSION)));
253
    }
254
255
    /**
256
     * @Route(
257
     *      "/{id}/createemptypage",
258
     *      requirements={"id" = "\d+"},
259
     *      name="KunstmaanNodeBundle_nodes_createemptypage"
260
     * )
261
     * @Method("GET")
262
     * @Template()
263
     *
264
     * @param Request $request
265
     * @param int     $id
266
     *
267
     * @return RedirectResponse
268
     *
269
     * @throws AccessDeniedException
270
     */
271
    public function createEmptyPageAction(Request $request, $id)
272
    {
273
        $this->init($request);
274
        /* @var Node $node */
275
        $node = $this->em->getRepository('KunstmaanNodeBundle:Node')->find($id);
276
277
        $this->denyAccessUnlessGranted(PermissionMap::PERMISSION_EDIT, $node);
278
279
        $entityName = $node->getRefEntityName();
280
        /* @var HasNodeInterface $myLanguagePage */
281
        $myLanguagePage = new $entityName();
282
        $myLanguagePage->setTitle('New page');
283
284
        $this->em->persist($myLanguagePage);
285
        $this->em->flush();
286
        /* @var NodeTranslation $nodeTranslation */
287
        $nodeTranslation = $this->em->getRepository('KunstmaanNodeBundle:NodeTranslation')
288
            ->createNodeTranslationFor($myLanguagePage, $this->locale, $node, $this->user);
289
        $nodeVersion = $nodeTranslation->getPublicNodeVersion();
290
291
        $this->get('event_dispatcher')->dispatch(
292
            Events::ADD_EMPTY_PAGE_TRANSLATION,
293
            new NodeEvent($node, $nodeTranslation, $nodeVersion, $myLanguagePage)
294
        );
295
296
        return $this->redirect($this->generateUrl('KunstmaanNodeBundle_nodes_edit', array('id' => $id)));
297
    }
298
299
    /**
300
     * @Route("/{id}/publish", requirements={"id" =
301
     *                         "\d+"},
302
     *                         name="KunstmaanNodeBundle_nodes_publish")
303
     * @Method({"GET", "POST"})
304
     *
305
     * @param Request $request
306
     * @param int     $id
307
     *
308
     * @return RedirectResponse
309
     *
310
     * @throws AccessDeniedException
311
     */
312 View Code Duplication
    public function publishAction(Request $request, $id)
313
    {
314
        $this->init($request);
315
        /* @var Node $node */
316
        $node = $this->em->getRepository('KunstmaanNodeBundle:Node')->find($id);
317
318
        $nodeTranslation = $node->getNodeTranslation($this->locale, true);
319
        $request = $this->get('request_stack')->getCurrentRequest();
320
        $this->nodePublisher->chooseHowToPublish($request, $nodeTranslation, $this->translator);
321
322
        return $this->redirect($this->generateUrl('KunstmaanNodeBundle_nodes_edit', array('id' => $node->getId())));
323
    }
324
325
    /**
326
     * @Route(
327
     *      "/{id}/unpublish",
328
     *      requirements={"id" = "\d+"},
329
     *      name="KunstmaanNodeBundle_nodes_unpublish"
330
     * )
331
     * @Method({"GET", "POST"})
332
     *
333
     * @param Request $request
334
     * @param int     $id
335
     *
336
     * @return RedirectResponse
337
     *
338
     * @throws AccessDeniedException
339
     */
340 View Code Duplication
    public function unPublishAction(Request $request, $id)
341
    {
342
        $this->init($request);
343
        /* @var Node $node */
344
        $node = $this->em->getRepository('KunstmaanNodeBundle:Node')->find($id);
345
346
        $nodeTranslation = $node->getNodeTranslation($this->locale, true);
347
        $request = $this->get('request_stack')->getCurrentRequest();
348
        $this->nodePublisher->chooseHowToUnpublish($request, $nodeTranslation, $this->translator);
349
350
        return $this->redirect($this->generateUrl('KunstmaanNodeBundle_nodes_edit', array('id' => $node->getId())));
351
    }
352
353
    /**
354
     * @Route(
355
     *      "/{id}/unschedulepublish",
356
     *      requirements={"id" = "\d+"},
357
     *      name="KunstmaanNodeBundle_nodes_unschedule_publish"
358
     * )
359
     * @Method({"GET", "POST"})
360
     *
361
     * @param Request $request
362
     * @param int     $id
363
     *
364
     * @return RedirectResponse
365
     *
366
     * @throws AccessDeniedException
367
     */
368 View Code Duplication
    public function unSchedulePublishAction(Request $request, $id)
369
    {
370
        $this->init($request);
371
372
        /* @var Node $node */
373
        $node = $this->em->getRepository('KunstmaanNodeBundle:Node')->find($id);
374
375
        $nodeTranslation = $node->getNodeTranslation($this->locale, true);
376
        $this->nodePublisher->unSchedulePublish($nodeTranslation);
377
378
        $this->addFlash(
379
            FlashTypes::SUCCESS,
380
            $this->get('translator')->trans('kuma_node.admin.unschedule.flash.success')
381
        );
382
383
        return $this->redirect($this->generateUrl('KunstmaanNodeBundle_nodes_edit', array('id' => $id)));
384
    }
385
386
    /**
387
     * @Route(
388
     *      "/{id}/delete",
389
     *      requirements={"id" = "\d+"},
390
     *      name="KunstmaanNodeBundle_nodes_delete"
391
     * )
392
     * @Template()
393
     * @Method("POST")
394
     *
395
     * @param Request $request
396
     * @param int     $id
397
     *
398
     * @return RedirectResponse
399
     *
400
     * @throws AccessDeniedException
401
     */
402
    public function deleteAction(Request $request, $id)
403
    {
404
        $this->init($request);
405
        /* @var Node $node */
406
        $node = $this->em->getRepository('KunstmaanNodeBundle:Node')->find($id);
407
408
        $this->denyAccessUnlessGranted(PermissionMap::PERMISSION_DELETE, $node);
409
410
        $nodeTranslation = $node->getNodeTranslation($this->locale, true);
411
        $nodeVersion = $nodeTranslation->getPublicNodeVersion();
412
        $page = $nodeVersion->getRef($this->em);
413
414
        $this->get('event_dispatcher')->dispatch(
415
            Events::PRE_DELETE,
416
            new NodeEvent($node, $nodeTranslation, $nodeVersion, $page)
417
        );
418
419
        $node->setDeleted(true);
420
        $this->em->persist($node);
421
422
        $children = $node->getChildren();
423
        $this->deleteNodeChildren($this->em, $this->user, $this->locale, $children);
424
        $this->em->flush();
425
426
        $event = new NodeEvent($node, $nodeTranslation, $nodeVersion, $page);
427
        $this->get('event_dispatcher')->dispatch(Events::POST_DELETE, $event);
428
        if (null === $response = $event->getResponse()) {
429
            $nodeParent = $node->getParent();
430
            // Check if we have a parent. Otherwise redirect to pages overview.
431
            if ($nodeParent) {
432
                $url = $this->get('router')->generate(
433
                    'KunstmaanNodeBundle_nodes_edit',
434
                    array('id' => $nodeParent->getId())
435
                );
436
            } else {
437
                $url = $this->get('router')->generate(
438
                    'KunstmaanNodeBundle_nodes'
439
                );
440
            }
441
            $response = new RedirectResponse($url);
442
        }
443
444
        $this->addFlash(
445
            FlashTypes::SUCCESS,
446
            $this->get('translator')->trans('kuma_node.admin.delete.flash.success')
447
        );
448
449
        return $response;
450
    }
451
452
    /**
453
     * @Route(
454
     *      "/{id}/duplicate",
455
     *      requirements={"id" = "\d+"},
456
     *      name="KunstmaanNodeBundle_nodes_duplicate"
457
     * )
458
     * @Template()
459
     * @Method("POST")
460
     *
461
     * @param Request $request
462
     * @param int     $id
463
     *
464
     * @return RedirectResponse
465
     *
466
     * @throws AccessDeniedException
467
     */
468
    public function duplicateAction(Request $request, $id)
469
    {
470
        $this->init($request);
471
        /* @var Node $parentNode */
472
        $originalNode = $this->em->getRepository('KunstmaanNodeBundle:Node')
473
            ->find($id);
474
475
        // Check with Acl
476
        $this->denyAccessUnlessGranted(PermissionMap::PERMISSION_EDIT, $originalNode);
477
478
        $request = $this->get('request_stack')->getCurrentRequest();
479
480
        $originalNodeTranslations = $originalNode->getNodeTranslation($this->locale, true);
481
        $originalRef = $originalNodeTranslations->getPublicNodeVersion()->getRef($this->em);
482
        $newPage = $this->get('kunstmaan_admin.clone.helper')
483
            ->deepCloneAndSave($originalRef);
484
485
        //set the title
486
        $title = $request->get('title');
487
        if (is_string($title) && !empty($title)) {
488
            $newPage->setTitle($title);
489
        } else {
490
            $newPage->setTitle('New page');
491
        }
492
493
        //set the parent
494
        $parentNodeTranslation = $originalNode->getParent()->getNodeTranslation($this->locale, true);
495
        $parent = $parentNodeTranslation->getPublicNodeVersion()->getRef($this->em);
496
        $newPage->setParent($parent);
497
        $this->em->persist($newPage);
498
        $this->em->flush();
499
500
        /* @var Node $nodeNewPage */
501
        $nodeNewPage = $this->em->getRepository('KunstmaanNodeBundle:Node')->createNodeFor(
502
            $newPage,
503
            $this->locale,
504
            $this->user
505
        );
506
507
        $nodeTranslation = $nodeNewPage->getNodeTranslation($this->locale, true);
508
        if ($newPage->isStructureNode()) {
509
            $nodeTranslation->setSlug('');
510
            $this->em->persist($nodeTranslation);
511
        }
512
        $this->em->flush();
513
514
        $this->updateAcl($originalNode, $nodeNewPage);
515
516
        $this->addFlash(
517
            FlashTypes::SUCCESS,
518
            $this->get('translator')->trans('kuma_node.admin.duplicate.flash.success')
519
        );
520
521
        return $this->redirect(
522
            $this->generateUrl('KunstmaanNodeBundle_nodes_edit', array('id' => $nodeNewPage->getId()))
523
        );
524
    }
525
526
    /**
527
     * @Route(
528
     *      "/{id}/revert",
529
     *      requirements={"id" = "\d+"},
530
     *      defaults={"subaction" = "public"},
531
     *      name="KunstmaanNodeBundle_nodes_revert"
532
     * )
533
     * @Template()
534
     * @Method("GET")
535
     *
536
     * @param Request $request
537
     * @param int     $id      The node id
538
     *
539
     * @return RedirectResponse
540
     *
541
     * @throws AccessDeniedException
542
     * @throws InvalidArgumentException
543
     */
544
    public function revertAction(Request $request, $id)
545
    {
546
        $this->init($request);
547
        /* @var Node $node */
548
        $node = $this->em->getRepository('KunstmaanNodeBundle:Node')->find($id);
549
550
        $this->denyAccessUnlessGranted(PermissionMap::PERMISSION_EDIT, $node);
551
552
        $version = $request->get('version');
553
554
        if (empty($version) || !is_numeric($version)) {
555
            throw new InvalidArgumentException('No version was specified');
556
        }
557
558
        /* @var NodeVersionRepository $nodeVersionRepo */
559
        $nodeVersionRepo = $this->em->getRepository('KunstmaanNodeBundle:NodeVersion');
560
        /* @var NodeVersion $nodeVersion */
561
        $nodeVersion = $nodeVersionRepo->find($version);
562
563
        if (is_null($nodeVersion)) {
564
            throw new InvalidArgumentException('Version does not exist');
565
        }
566
567
        /* @var NodeTranslation $nodeTranslation */
568
        $nodeTranslation = $node->getNodeTranslation($this->locale, true);
569
        $page = $nodeVersion->getRef($this->em);
570
        /* @var HasNodeInterface $clonedPage */
571
        $clonedPage = $this->get('kunstmaan_admin.clone.helper')
572
            ->deepCloneAndSave($page);
573
        $newNodeVersion = $nodeVersionRepo->createNodeVersionFor(
574
            $clonedPage,
575
            $nodeTranslation,
576
            $this->user,
577
            $nodeVersion,
578
            'draft'
579
        );
580
581
        $nodeTranslation->setTitle($clonedPage->getTitle());
582
        $this->em->persist($nodeTranslation);
583
        $this->em->flush();
584
585
        $this->get('event_dispatcher')->dispatch(
586
            Events::REVERT,
587
            new RevertNodeAction(
588
                $node,
589
                $nodeTranslation,
590
                $newNodeVersion,
591
                $clonedPage,
592
                $nodeVersion,
593
                $page
594
            )
595
        );
596
597
        $this->addFlash(
598
            FlashTypes::SUCCESS,
599
            $this->get('translator')->trans('kuma_node.admin.revert.flash.success')
600
        );
601
602
        return $this->redirect(
603
            $this->generateUrl(
604
                'KunstmaanNodeBundle_nodes_edit',
605
                array(
606
                    'id' => $id,
607
                    'subaction' => 'draft',
608
                )
609
            )
610
        );
611
    }
612
613
    /**
614
     * @Route(
615
     *      "/{id}/add",
616
     *      requirements={"id" = "\d+"},
617
     *      name="KunstmaanNodeBundle_nodes_add"
618
     * )
619
     * @Template()
620
     * @Method("POST")
621
     *
622
     * @param Request $request
623
     * @param int     $id
624
     *
625
     * @return RedirectResponse
626
     *
627
     * @throws AccessDeniedException
628
     * @throws InvalidArgumentException
629
     */
630
    public function addAction(Request $request, $id)
631
    {
632
        $this->init($request);
633
        /* @var Node $parentNode */
634
        $parentNode = $this->em->getRepository('KunstmaanNodeBundle:Node')->find($id);
635
636
        // Check with Acl
637
        $this->denyAccessUnlessGranted(PermissionMap::PERMISSION_EDIT, $parentNode);
638
639
        $parentNodeTranslation = $parentNode->getNodeTranslation($this->locale, true);
640
        $parentNodeVersion = $parentNodeTranslation->getPublicNodeVersion();
641
        $parentPage = $parentNodeVersion->getRef($this->em);
642
643
        $type = $this->validatePageType($request);
644
        $newPage = $this->createNewPage($request, $type);
645
        $newPage->setParent($parentPage);
646
647
        /* @var Node $nodeNewPage */
648
        $nodeNewPage = $this->em->getRepository('KunstmaanNodeBundle:Node')
649
            ->createNodeFor($newPage, $this->locale, $this->user);
650
        $nodeTranslation = $nodeNewPage->getNodeTranslation(
651
            $this->locale,
652
            true
653
        );
654
        $weight = $this->em->getRepository('KunstmaanNodeBundle:NodeTranslation')
655
                ->getMaxChildrenWeight($parentNode, $this->locale) + 1;
656
        $nodeTranslation->setWeight($weight);
657
658
        if ($newPage->isStructureNode()) {
659
            $nodeTranslation->setSlug('');
660
        }
661
662
        $this->em->persist($nodeTranslation);
0 ignored issues
show
It seems like $nodeTranslation defined by $nodeNewPage->getNodeTra...on($this->locale, true) on line 650 can be null; however, Doctrine\ORM\EntityManager::persist() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
663
        $this->em->flush();
664
665
        $this->updateAcl($parentNode, $nodeNewPage);
666
667
        $nodeVersion = $nodeTranslation->getPublicNodeVersion();
668
669
        $this->get('event_dispatcher')->dispatch(
670
            Events::ADD_NODE,
671
            new NodeEvent(
672
                $nodeNewPage, $nodeTranslation, $nodeVersion, $newPage
673
            )
674
        );
675
676
        return $this->redirect(
677
            $this->generateUrl(
678
                'KunstmaanNodeBundle_nodes_edit',
679
                array('id' => $nodeNewPage->getId())
680
            )
681
        );
682
    }
683
684
    /**
685
     * @Route("/add-homepage", name="KunstmaanNodeBundle_nodes_add_homepage")
686
     * @Template()
687
     * @Method("POST")
688
     *
689
     * @return RedirectResponse
690
     *
691
     * @throws AccessDeniedException
692
     * @throws InvalidArgumentException
693
     */
694
    public function addHomepageAction(Request $request)
695
    {
696
        $this->init($request);
697
698
        // Check with Acl
699
        $this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN');
700
701
        $type = $this->validatePageType($request);
702
703
        $newPage = $this->createNewPage($request, $type);
704
705
        /* @var Node $nodeNewPage */
706
        $nodeNewPage = $this->em->getRepository('KunstmaanNodeBundle:Node')
707
            ->createNodeFor($newPage, $this->locale, $this->user);
708
        $nodeTranslation = $nodeNewPage->getNodeTranslation(
709
            $this->locale,
710
            true
711
        );
712
        $this->em->flush();
713
714
        // Set default permissions
715
        $this->get('kunstmaan_node.acl_permission_creator_service')
716
            ->createPermission($nodeNewPage);
717
718
        $nodeVersion = $nodeTranslation->getPublicNodeVersion();
719
720
        $this->get('event_dispatcher')->dispatch(
721
            Events::ADD_NODE,
722
            new NodeEvent(
723
                $nodeNewPage, $nodeTranslation, $nodeVersion, $newPage
724
            )
725
        );
726
727
        return $this->redirect(
728
            $this->generateUrl(
729
                'KunstmaanNodeBundle_nodes_edit',
730
                array('id' => $nodeNewPage->getId())
731
            )
732
        );
733
    }
734
735
    /**
736
     * @Route("/reorder", name="KunstmaanNodeBundle_nodes_reorder")
737
     * @Method("POST")
738
     *
739
     * @param Request $request
740
     *
741
     * @return string
742
     *
743
     * @throws AccessDeniedException
744
     */
745
    public function reorderAction(Request $request)
746
    {
747
        $this->init($request);
748
        $nodes = array();
749
        $nodeIds = $request->get('nodes');
750
        $changeParents = $request->get('parent');
751
752
        foreach ($nodeIds as $id) {
753
            /* @var Node $node */
754
            $node = $this->em->getRepository('KunstmaanNodeBundle:Node')->find($id);
755
            $this->denyAccessUnlessGranted(PermissionMap::PERMISSION_EDIT, $node);
756
            $nodes[] = $node;
757
        }
758
759
        $weight = 0;
760
        foreach ($nodes as $node) {
761
            $newParentId = isset($changeParents[$node->getId()]) ? $changeParents[$node->getId()] : null;
762
            if ($newParentId) {
763
                $parent = $this->em->getRepository('KunstmaanNodeBundle:Node')->find($newParentId);
764
                $this->denyAccessUnlessGranted(PermissionMap::PERMISSION_EDIT, $parent);
765
                $node->setParent($parent);
766
                $this->em->persist($node);
767
                $this->em->flush($node);
768
            }
769
770
            /* @var NodeTranslation $nodeTranslation */
771
            $nodeTranslation = $node->getNodeTranslation($this->locale, true);
772
773
            if ($nodeTranslation) {
774
                $nodeVersion = $nodeTranslation->getPublicNodeVersion();
775
                $page = $nodeVersion->getRef($this->em);
776
777
                $this->get('event_dispatcher')->dispatch(
778
                    Events::PRE_PERSIST,
779
                    new NodeEvent($node, $nodeTranslation, $nodeVersion, $page)
780
                );
781
782
                $nodeTranslation->setWeight($weight);
783
                $this->em->persist($nodeTranslation);
784
                $this->em->flush($nodeTranslation);
785
786
                $this->get('event_dispatcher')->dispatch(
787
                    Events::POST_PERSIST,
788
                    new NodeEvent($node, $nodeTranslation, $nodeVersion, $page)
789
                );
790
791
                ++$weight;
792
            }
793
        }
794
795
        return new JsonResponse(
796
            array(
797
                'Success' => 'The node-translations for [' . $this->locale . '] have got new weight values',
798
            )
799
        );
800
    }
801
802
    /**
803
     * @Route(
804
     *      "/{id}/{subaction}",
805
     *      requirements={"id" = "\d+"},
806
     *      defaults={"subaction" = "public"},
807
     *      name="KunstmaanNodeBundle_nodes_edit"
808
     * )
809
     * @Template()
810
     * @Method({"GET", "POST"})
811
     *
812
     * @param Request $request
813
     * @param int     $id        The node id
814
     * @param string  $subaction The subaction (draft|public)
815
     *
816
     * @return RedirectResponse|array
817
     *
818
     * @throws AccessDeniedException
819
     */
820
    public function editAction(Request $request, $id, $subaction)
821
    {
822
        $this->init($request);
823
        /* @var Node $node */
824
        $node = $this->em->getRepository('KunstmaanNodeBundle:Node')->find($id);
825
826
        $this->denyAccessUnlessGranted(PermissionMap::PERMISSION_EDIT, $node);
827
828
        $tabPane = new TabPane(
829
            'todo',
830
            $request,
831
            $this->container->get('form.factory')
832
        );
833
834
        $nodeTranslation = $node->getNodeTranslation($this->locale, true);
835
        if (!$nodeTranslation) {
836
            return $this->renderNodeNotTranslatedPage($node);
837
        }
838
839
        $nodeVersion = $nodeTranslation->getPublicNodeVersion();
840
        $draftNodeVersion = $nodeTranslation->getDraftNodeVersion();
841
        $nodeVersionIsLocked = false;
842
843
        /* @var HasNodeInterface $page */
844
        $page = null;
845
        $draft = ($subaction == 'draft');
846
        $saveAsDraft = $request->get('saveasdraft');
847
        if ((!$draft && !empty($saveAsDraft)) || ($draft && is_null($draftNodeVersion))) {
848
            // Create a new draft version
849
            $draft = true;
850
            $subaction = 'draft';
851
            $page = $nodeVersion->getRef($this->em);
852
            $nodeVersion = $this->createDraftVersion(
853
                $page,
854
                $nodeTranslation,
855
                $nodeVersion
856
            );
857
            $draftNodeVersion = $nodeVersion;
858
        } elseif ($draft) {
859
            $nodeVersion = $draftNodeVersion;
860
            $page = $nodeVersion->getRef($this->em);
861
        } else {
862
            if ($request->getMethod() == 'POST') {
863
                $nodeVersionIsLocked = $this->isNodeVersionLocked($nodeTranslation, true);
864
865
                //Check the version timeout and make a new nodeversion if the timeout is passed
866
                $thresholdDate = date(
867
                    'Y-m-d H:i:s',
868
                    time() - $this->getParameter(
869
                        'kunstmaan_node.version_timeout'
870
                    )
871
                );
872
                $updatedDate = date(
873
                    'Y-m-d H:i:s',
874
                    strtotime($nodeVersion->getUpdated()->format('Y-m-d H:i:s'))
875
                );
876
                if ($thresholdDate >= $updatedDate || $nodeVersionIsLocked) {
877
                    $page = $nodeVersion->getRef($this->em);
878
                    if ($nodeVersion == $nodeTranslation->getPublicNodeVersion()) {
879
                        $this->nodePublisher
880
                            ->createPublicVersion(
881
                                $page,
882
                                $nodeTranslation,
883
                                $nodeVersion,
884
                                $this->user
885
                            );
886
                    } else {
887
                        $this->createDraftVersion(
888
                            $page,
889
                            $nodeTranslation,
890
                            $nodeVersion
891
                        );
892
                    }
893
                }
894
            }
895
            $page = $nodeVersion->getRef($this->em);
896
        }
897
        $isStructureNode = $page->isStructureNode();
898
899
        $menubuilder = $this->get('kunstmaan_node.actions_menu_builder');
900
        $menubuilder->setActiveNodeVersion($nodeVersion);
901
        $menubuilder->setEditableNode(!$isStructureNode);
902
903
        // Building the form
904
        $propertiesWidget = new FormWidget();
905
        $propertiesWidget->addType('main', $page->getDefaultAdminType(), $page);
906
        $propertiesWidget->addType('node', $node->getDefaultAdminType(), $node);
907
        $tabPane->addTab(new Tab('kuma_node.tab.properties.title', $propertiesWidget));
908
909
        // Menu tab
910
        $menuWidget = new FormWidget();
911
        $menuWidget->addType(
912
            'menunodetranslation',
913
            NodeMenuTabTranslationAdminType::class,
914
            $nodeTranslation,
915
            ['slugable' => !$isStructureNode]
916
        );
917
        $menuWidget->addType('menunode', NodeMenuTabAdminType::class, $node, ['available_in_nav' => !$isStructureNode]);
918
        $tabPane->addTab(new Tab('kuma_node.tab.menu.title', $menuWidget));
919
920
        $this->get('event_dispatcher')->dispatch(
921
            Events::ADAPT_FORM,
922
            new AdaptFormEvent(
923
                $request,
924
                $tabPane,
925
                $page,
926
                $node,
927
                $nodeTranslation,
928
                $nodeVersion
929
            )
930
        );
931
932
        $tabPane->buildForm();
933
934
        if ($request->getMethod() == 'POST') {
935
            $tabPane->bindRequest($request);
936
937
            // Don't redirect to listing when coming from ajax request, needed for url chooser.
938
            if ($tabPane->isValid() && !$request->isXmlHttpRequest()) {
939
                $this->get('event_dispatcher')->dispatch(
940
                    Events::PRE_PERSIST,
941
                    new NodeEvent($node, $nodeTranslation, $nodeVersion, $page)
942
                );
943
944
                $nodeTranslation->setTitle($page->getTitle());
945
                if ($isStructureNode) {
946
                    $nodeTranslation->setSlug('');
947
                }
948
                $nodeVersion->setUpdated(new DateTime());
949
                if ($nodeVersion->getType() == 'public') {
950
                    $nodeTranslation->setUpdated($nodeVersion->getUpdated());
951
                }
952
                $this->em->persist($nodeTranslation);
953
                $this->em->persist($nodeVersion);
954
                $tabPane->persist($this->em);
955
                $this->em->flush();
956
957
                $this->get('event_dispatcher')->dispatch(
958
                    Events::POST_PERSIST,
959
                    new NodeEvent($node, $nodeTranslation, $nodeVersion, $page)
960
                );
961
962
                if ($nodeVersionIsLocked) {
963
                    $this->addFlash(
964
                        FlashTypes::SUCCESS,
965
                        $this->get('translator')->trans('kuma_node.admin.edit.flash.locked_success')
966
                    );
967
                } else {
968
                    if ($request->request->has('publishing') || $request->request->has('publish_later')) {
969
                        $this->nodePublisher->chooseHowToPublish($request, $nodeTranslation, $this->translator);
970
                    } elseif ($request->request->has('unpublishing') || $request->request->has('unpublish_later')) {
971
                        $this->nodePublisher->chooseHowToUnpublish($request, $nodeTranslation, $this->translator);
972
                    } else {
973
                        $this->addFlash(
974
                            FlashTypes::SUCCESS,
975
                            $this->get('translator')->trans('kuma_node.admin.edit.flash.success')
976
                        );
977
                    }
978
                }
979
980
                $params = [
981
                    'id' => $node->getId(),
982
                    'subaction' => $subaction,
983
                    'currenttab' => $tabPane->getActiveTab(),
984
                ];
985
                $params = array_merge(
986
                    $params,
987
                    $tabPane->getExtraParams($request)
988
                );
989
990
                return $this->redirect(
991
                    $this->generateUrl(
992
                        'KunstmaanNodeBundle_nodes_edit',
993
                        $params
994
                    )
995
                );
996
            }
997
        }
998
999
        $nodeVersions = $this->em->getRepository(
1000
            'KunstmaanNodeBundle:NodeVersion'
1001
        )->findBy(
1002
            ['nodeTranslation' => $nodeTranslation],
1003
            ['updated' => 'ASC']
1004
        );
1005
        $queuedNodeTranslationAction = $this->em->getRepository(
1006
            'KunstmaanNodeBundle:QueuedNodeTranslationAction'
1007
        )->findOneBy(['nodeTranslation' => $nodeTranslation]);
1008
1009
        return [
1010
            'page' => $page,
1011
            'entityname' => ClassLookup::getClass($page),
1012
            'nodeVersions' => $nodeVersions,
1013
            'node' => $node,
1014
            'nodeTranslation' => $nodeTranslation,
1015
            'draft' => $draft,
1016
            'draftNodeVersion' => $draftNodeVersion,
1017
            'nodeVersion' => $nodeVersion,
1018
            'subaction' => $subaction,
1019
            'tabPane' => $tabPane,
1020
            'editmode' => true,
1021
            'queuedNodeTranslationAction' => $queuedNodeTranslationAction,
1022
            'nodeVersionLockCheck' => $this->container->getParameter('kunstmaan_node.lock_enabled'),
1023
            'nodeVersionLockInterval' => $this->container->getParameter('kunstmaan_node.lock_check_interval'),
1024
        ];
1025
    }
1026
1027
    /**
1028
     * @Route(
1029
     *      "checkNodeVersionLock/{id}/{public}",
1030
     *      requirements={"id" = "\d+", "public" = "(0|1)"},
1031
     *      name="KunstmaanNodeBundle_nodes_versionlock_check"
1032
     * )
1033
     *
1034
     * @param Request $request
1035
     * @param $id
1036
     *
1037
     * @return JsonResponse
1038
     */
1039
    public function checkNodeVersionLockAction(Request $request, $id, $public)
1040
    {
1041
        $nodeVersionIsLocked = false;
1042
        $message = '';
1043
        $this->init($request);
1044
1045
        /* @var Node $node */
1046
        $node = $this->em->getRepository('KunstmaanNodeBundle:Node')->find($id);
1047
1048
        try {
1049
            $this->checkPermission($node, PermissionMap::PERMISSION_EDIT);
1050
1051
            /** @var NodeVersionLockHelper $nodeVersionLockHelper */
1052
            $nodeVersionLockHelper = $this->get('kunstmaan_node.admin_node.node_version_lock_helper');
1053
            $nodeTranslation = $node->getNodeTranslation($this->locale, true);
1054
1055
            if ($nodeTranslation) {
1056
                $nodeVersionIsLocked = $nodeVersionLockHelper->isNodeVersionLocked($this->getUser(), $nodeTranslation, $public);
1057
1058
                if ($nodeVersionIsLocked) {
1059
                    $users = $nodeVersionLockHelper->getUsersWithNodeVersionLock($nodeTranslation, $public, $this->getUser());
1060
                    $message = $this->get('translator')->trans('kuma_node.admin.edit.flash.locked', array('%users%' => implode(', ', $users)));
1061
                }
1062
            }
1063
        } catch (AccessDeniedException $ade) {
1064
        }
1065
1066
        return new JsonResponse(['lock' => $nodeVersionIsLocked, 'message' => $message]);
1067
    }
1068
1069
    /**
1070
     * @param NodeTranslation $nodeTranslation
1071
     * @param bool            $isPublic
1072
     *
1073
     * @return bool
1074
     */
1075
    private function isNodeVersionLocked(NodeTranslation $nodeTranslation, $isPublic)
1076
    {
1077
        if ($this->container->getParameter('kunstmaan_node.lock_enabled')) {
1078
            /** @var NodeVersionLockHelper $nodeVersionLockHelper */
1079
            $nodeVersionLockHelper = $this->get('kunstmaan_node.admin_node.node_version_lock_helper');
1080
            $nodeVersionIsLocked = $nodeVersionLockHelper->isNodeVersionLocked($this->getUser(), $nodeTranslation, $isPublic);
1081
1082
            return $nodeVersionIsLocked;
1083
        }
1084
1085
        return false;
1086
    }
1087
1088
    /**
1089
     * @param HasNodeInterface $page            The page
1090
     * @param NodeTranslation  $nodeTranslation The node translation
1091
     * @param NodeVersion      $nodeVersion     The node version
1092
     *
1093
     * @return NodeVersion
1094
     */
1095
    private function createDraftVersion(
1096
        HasNodeInterface $page,
1097
        NodeTranslation $nodeTranslation,
1098
        NodeVersion $nodeVersion
1099
    ) {
1100
        $publicPage = $this->get('kunstmaan_admin.clone.helper')
1101
            ->deepCloneAndSave($page);
1102
        /* @var NodeVersion $publicNodeVersion */
1103
1104
        $publicNodeVersion = $this->em->getRepository(
1105
            'KunstmaanNodeBundle:NodeVersion'
1106
        )->createNodeVersionFor(
1107
            $publicPage,
1108
            $nodeTranslation,
1109
            $this->user,
1110
            $nodeVersion->getOrigin(),
1111
            'public',
1112
            $nodeVersion->getCreated()
1113
        );
1114
1115
        $nodeTranslation->setPublicNodeVersion($publicNodeVersion);
1116
        $nodeVersion->setType('draft');
1117
        $nodeVersion->setOrigin($publicNodeVersion);
1118
        $nodeVersion->setCreated(new DateTime());
1119
1120
        $this->em->persist($nodeTranslation);
1121
        $this->em->persist($nodeVersion);
1122
        $this->em->flush();
1123
1124
        $this->get('event_dispatcher')->dispatch(
1125
            Events::CREATE_DRAFT_VERSION,
1126
            new NodeEvent(
1127
                $nodeTranslation->getNode(),
1128
                $nodeTranslation,
1129
                $nodeVersion,
1130
                $page
1131
            )
1132
        );
1133
1134
        return $nodeVersion;
1135
    }
1136
1137
    /**
1138
     * @param Node   $node       The node
1139
     * @param string $permission The permission to check for
1140
     *
1141
     * @throws AccessDeniedException
1142
     */
1143
    private function checkPermission(Node $node, $permission)
1144
    {
1145
        if (false === $this->authorizationChecker->isGranted($permission, $node)) {
1146
            throw new AccessDeniedException();
1147
        }
1148
    }
1149
1150
    /**
1151
     * @param EntityManager   $em       The Entity Manager
1152
     * @param BaseUser        $user     The user who deletes the children
1153
     * @param string          $locale   The locale that was used
1154
     * @param ArrayCollection $children The children array
1155
     */
1156
    private function deleteNodeChildren(
1157
        EntityManager $em,
1158
        BaseUser $user,
1159
        $locale,
1160
        ArrayCollection $children
1161
    ) {
1162
        /* @var Node $childNode */
1163
        foreach ($children as $childNode) {
1164
            $childNodeTranslation = $childNode->getNodeTranslation(
1165
                $this->locale,
1166
                true
1167
            );
1168
1169
            $childNodeVersion = $childNodeTranslation->getPublicNodeVersion();
1170
            $childNodePage = $childNodeVersion->getRef($this->em);
1171
1172
            $this->get('event_dispatcher')->dispatch(
1173
                Events::PRE_DELETE,
1174
                new NodeEvent(
1175
                    $childNode,
1176
                    $childNodeTranslation,
1177
                    $childNodeVersion,
1178
                    $childNodePage
1179
                )
1180
            );
1181
1182
            $childNode->setDeleted(true);
1183
            $this->em->persist($childNode);
1184
1185
            $children2 = $childNode->getChildren();
1186
            $this->deleteNodeChildren($em, $user, $locale, $children2);
1187
1188
            $this->get('event_dispatcher')->dispatch(
1189
                Events::POST_DELETE,
1190
                new NodeEvent(
1191
                    $childNode,
1192
                    $childNodeTranslation,
1193
                    $childNodeVersion,
1194
                    $childNodePage
1195
                )
1196
            );
1197
        }
1198
    }
1199
1200
    /**
1201
     * @param $originalNode
1202
     * @param $nodeNewPage
1203
     */
1204
    private function updateAcl($originalNode, $nodeNewPage)
1205
    {
1206
        /* @var MutableAclProviderInterface $aclProvider */
1207
        $aclProvider = $this->container->get('security.acl.provider');
1208
        /* @var ObjectIdentityRetrievalStrategyInterface $strategy */
1209
        $strategy = $this->container->get(
1210
            'security.acl.object_identity_retrieval_strategy'
1211
        );
1212
        $originalIdentity = $strategy->getObjectIdentity($originalNode);
1213
        $originalAcl = $aclProvider->findAcl($originalIdentity);
1214
1215
        $newIdentity = $strategy->getObjectIdentity($nodeNewPage);
1216
        $newAcl = $aclProvider->createAcl($newIdentity);
1217
1218
        $aces = $originalAcl->getObjectAces();
1219
        /* @var EntryInterface $ace */
1220
        foreach ($aces as $ace) {
1221
            $securityIdentity = $ace->getSecurityIdentity();
1222
            if ($securityIdentity instanceof RoleSecurityIdentity) {
1223
                $newAcl->insertObjectAce($securityIdentity, $ace->getMask());
1224
            }
1225
        }
1226
        $aclProvider->updateAcl($newAcl);
1227
    }
1228
1229
    /**
1230
     * @param Request $request
1231
     * @param string  $type
1232
     *
1233
     * @return HasNodeInterface
1234
     */
1235
    private function createNewPage(Request $request, $type)
1236
    {
1237
        /* @var HasNodeInterface $newPage */
1238
        $newPage = new $type();
1239
1240
        $title = $request->get('title');
1241
        if (is_string($title) && !empty($title)) {
1242
            $newPage->setTitle($title);
1243
        } else {
1244
            $newPage->setTitle($this->get('translator')->trans('kuma_node.admin.new_page.title.default'));
1245
        }
1246
        $this->em->persist($newPage);
1247
        $this->em->flush();
1248
1249
        return $newPage;
1250
    }
1251
1252
    /**
1253
     * @param Request $request
1254
     *
1255
     * @return string
1256
     * @throw InvalidArgumentException
1257
     */
1258
    private function validatePageType($request)
1259
    {
1260
        $type = $request->get('type');
1261
1262
        if (empty($type)) {
1263
            throw new InvalidArgumentException(
1264
                'Please specify a type of page you want to create'
1265
            );
1266
        }
1267
1268
        return $type;
1269
    }
1270
1271
    /**
1272
     * @param Node $node
1273
     *
1274
     * @return \Symfony\Component\HttpFoundation\Response
1275
     */
1276
    private function renderNodeNotTranslatedPage(Node $node)
1277
    {
1278
        //try to find a parent node with the correct translation, if there is none allow copy.
1279
        //if there is a parent but it doesn't have the language to copy to don't allow it
1280
        $parentNode = $node->getParent();
1281
        if ($parentNode) {
1282
            $parentNodeTranslation = $parentNode->getNodeTranslation(
1283
                $this->locale,
1284
                true
1285
            );
1286
            $parentsAreOk = false;
1287
1288
            if ($parentNodeTranslation) {
1289
                $parentsAreOk = $this->em->getRepository(
1290
                    'KunstmaanNodeBundle:NodeTranslation'
1291
                )->hasParentNodeTranslationsForLanguage(
1292
                    $node->getParent()->getNodeTranslation(
1293
                        $this->locale,
1294
                        true
1295
                    ),
1296
                    $this->locale
1297
                );
1298
            }
1299
        } else {
1300
            $parentsAreOk = true;
1301
        }
1302
1303
        return $this->render(
1304
            'KunstmaanNodeBundle:NodeAdmin:pagenottranslated.html.twig',
1305
            array(
1306
                'node' => $node,
1307
                'nodeTranslations' => $node->getNodeTranslations(
1308
                    true
1309
                ),
1310
                'copyfromotherlanguages' => $parentsAreOk,
1311
            )
1312
        );
1313
    }
1314
}
1315