Completed
Pull Request — 5.0 (#2162)
by Kevin
14:33
created

NodeBundle/Controller/NodeAdminController.php (4 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\Entity\User;
12
use Kunstmaan\AdminBundle\FlashMessages\FlashTypes;
13
use Kunstmaan\AdminBundle\Helper\FormWidgets\FormWidget;
14
15
use Kunstmaan\AdminBundle\Helper\FormWidgets\Tabs\Tab;
16
use Kunstmaan\AdminBundle\Helper\FormWidgets\Tabs\TabPane;
17
18
use Kunstmaan\AdminBundle\Helper\Security\Acl\AclHelper;
19
use Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\PermissionMap;
20
use Kunstmaan\AdminListBundle\AdminList\AdminList;
21
use Kunstmaan\NodeBundle\AdminList\NodeAdminListConfigurator;
22
use Kunstmaan\NodeBundle\Entity\HasNodeInterface;
23
use Kunstmaan\NodeBundle\Entity\Node;
24
use Kunstmaan\NodeBundle\Entity\NodeTranslation;
25
use Kunstmaan\NodeBundle\Entity\NodeVersion;
26
use Kunstmaan\NodeBundle\Event\AdaptFormEvent;
27
use Kunstmaan\NodeBundle\Event\CopyPageTranslationNodeEvent;
28
use Kunstmaan\NodeBundle\Event\Events;
29
use Kunstmaan\NodeBundle\Event\NodeEvent;
30
use Kunstmaan\NodeBundle\Event\RecopyPageTranslationNodeEvent;
31
use Kunstmaan\NodeBundle\Event\RevertNodeAction;
32
use Kunstmaan\NodeBundle\Form\NodeMenuTabAdminType;
33
use Kunstmaan\NodeBundle\Form\NodeMenuTabTranslationAdminType;
34
use Kunstmaan\NodeBundle\Helper\NodeAdmin\NodeAdminPublisher;
35
use Kunstmaan\NodeBundle\Helper\NodeAdmin\NodeVersionLockHelper;
36
use Kunstmaan\NodeBundle\Repository\NodeVersionRepository;
37
use Kunstmaan\UtilitiesBundle\Helper\ClassLookup;
38
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
39
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
40
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
41
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
42
use Symfony\Component\HttpFoundation\JsonResponse;
43
use Symfony\Component\HttpFoundation\RedirectResponse;
44
use Symfony\Component\HttpFoundation\Request;
45
use Symfony\Component\Security\Acl\Domain\RoleSecurityIdentity;
46
use Symfony\Component\Security\Acl\Model\EntryInterface;
47
use Symfony\Component\Security\Acl\Model\MutableAclProviderInterface;
48
use Symfony\Component\Security\Acl\Model\ObjectIdentityRetrievalStrategyInterface;
49
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
50
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
51
use Symfony\Component\Translation\TranslatorInterface;
52
53
/**
54
 * NodeAdminController
55
 */
56
class NodeAdminController extends Controller
57
{
58
    /**
59
     * @var EntityManager $em
60
     */
61
    protected $em;
62
63
    /**
64
     * @var string $locale
65
     */
66
    protected $locale;
67
68
    /**
69
     * @var AuthorizationCheckerInterface $authorizationChecker
70
     */
71
    protected $authorizationChecker;
72
73
    /**
74
     * @var BaseUser $user
75
     */
76
    protected $user;
77
78
    /**
79
     * @var AclHelper $aclHelper
80
     */
81
    protected $aclHelper;
82
83
    /**
84
     * @var NodeAdminPublisher
85
     */
86
    protected $nodePublisher;
87
88
    /**
89
     * @var TranslatorInterface
90
     */
91
    protected $translator;
92
93
    /**
94
     * init
95
     *
96
     * @param Request $request
97
     */
98
    protected function init(Request $request)
99
    {
100
        $this->em = $this->getDoctrine()->getManager();
101
        $this->locale = $request->getLocale();
102
        $this->authorizationChecker = $this->get('security.authorization_checker');
103
        $this->user = $this->getUser();
104
        $this->aclHelper = $this->get('kunstmaan_admin.acl.helper');
105
        $this->nodePublisher = $this->get('kunstmaan_node.admin_node.publisher');
106
        $this->translator = $this->get('translator');
107
    }
108
109
    /**
110
     * @Route("/", name="KunstmaanNodeBundle_nodes")
111
     * @Template("KunstmaanNodeBundle:Admin:list.html.twig")
112
     *
113
     * @param Request $request
114
     *
115
     * @return array
116
     */
117
    public function indexAction(Request $request)
118
    {
119
        $this->init($request);
120
121
        $nodeAdminListConfigurator = new NodeAdminListConfigurator(
122
            $this->em,
123
            $this->aclHelper,
124
            $this->locale,
125
            PermissionMap::PERMISSION_VIEW,
126
            $this->authorizationChecker
127
        );
128
129
        $locale = $this->locale;
130
        $acl = $this->authorizationChecker;
131
        $itemRoute = function (EntityInterface $item) use ($locale, $acl) {
132
            if ($acl->isGranted(PermissionMap::PERMISSION_VIEW, $item->getNode())) {
133
                return array(
134
                    'path' => '_slug_preview',
135
                    'params' => ['_locale' => $locale, 'url' => $item->getUrl()]
136
                );
137
            }
138
        };
139
        $nodeAdminListConfigurator->addSimpleItemAction('action.preview', $itemRoute, 'eye');
140
        $nodeAdminListConfigurator->setDomainConfiguration($this->get('kunstmaan_admin.domain_configuration'));
141
        $nodeAdminListConfigurator->setShowAddHomepage($this->getParameter('kunstmaan_node.show_add_homepage') && $this->isGranted('ROLE_SUPER_ADMIN'));
142
143
        /** @var AdminList $adminlist */
144
        $adminlist = $this->get('kunstmaan_adminlist.factory')->createList($nodeAdminListConfigurator);
145
        $adminlist->bindRequest($request);
146
147
        return array(
148
            'adminlist' => $adminlist,
149
        );
150
    }
151
152
    /**
153
     * @Route(
154
     *      "/{id}/copyfromotherlanguage",
155
     *      requirements={"id" = "\d+"},
156
     *      name="KunstmaanNodeBundle_nodes_copyfromotherlanguage"
157
     * )
158
     * @Method("GET")
159
     * @Template()
160
     *
161
     * @param Request $request
162
     * @param int $id The node id
163
     *
164
     * @return RedirectResponse
165
     * @throws AccessDeniedException
166
     */
167
    public function copyFromOtherLanguageAction(Request $request, $id)
168
    {
169
        $this->init($request);
170
        /* @var Node $node */
171
        $node = $this->em->getRepository('KunstmaanNodeBundle:Node')->find($id);
172
173
        $this->denyAccessUnlessGranted(PermissionMap::PERMISSION_EDIT, $node);
174
175
        $originalLanguage = $request->get('originallanguage');
176
        $otherLanguageNodeTranslation = $node->getNodeTranslation($originalLanguage, true);
177
        $otherLanguageNodeNodeVersion = $otherLanguageNodeTranslation->getPublicNodeVersion();
178
        $otherLanguagePage = $otherLanguageNodeNodeVersion->getRef($this->em);
179
        $myLanguagePage = $this->get('kunstmaan_admin.clone.helper')
180
            ->deepCloneAndSave($otherLanguagePage);
181
182
        /* @var NodeTranslation $nodeTranslation */
183
        $nodeTranslation = $this->em->getRepository('KunstmaanNodeBundle:NodeTranslation')
184
            ->createNodeTranslationFor($myLanguagePage, $this->locale, $node, $this->user);
185
        $nodeVersion = $nodeTranslation->getPublicNodeVersion();
186
187
        $this->get('event_dispatcher')->dispatch(
188
            Events::COPY_PAGE_TRANSLATION,
189
            new CopyPageTranslationNodeEvent(
190
                $node,
191
                $nodeTranslation,
192
                $nodeVersion,
193
                $myLanguagePage,
194
                $otherLanguageNodeTranslation,
195
                $otherLanguageNodeNodeVersion,
196
                $otherLanguagePage,
197
                $originalLanguage
198
            )
199
        );
200
201
        return $this->redirect($this->generateUrl('KunstmaanNodeBundle_nodes_edit', array('id' => $id)));
202
    }
203
204
    /**
205
     * @Route(
206
     *      "/{id}/recopyfromotherlanguage",
207
     *      requirements={"id" = "\d+"},
208
     *      name="KunstmaanNodeBundle_nodes_recopyfromotherlanguage"
209
     * )
210
     * @Method("POST")
211
     * @Template()
212
     *
213
     * @param Request $request
214
     * @param int $id The node id
215
     *
216
     * @return RedirectResponse
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
     * @throws AccessDeniedException
269
     */
270
    public function createEmptyPageAction(Request $request, $id)
271
    {
272
        $this->init($request);
273
        /* @var Node $node */
274
        $node = $this->em->getRepository('KunstmaanNodeBundle:Node')->find($id);
275
276
        $this->denyAccessUnlessGranted(PermissionMap::PERMISSION_EDIT, $node);
277
278
        $entityName = $node->getRefEntityName();
279
        /* @var HasNodeInterface $myLanguagePage */
280
        $myLanguagePage = new $entityName();
281
        $myLanguagePage->setTitle('New page');
282
283
        $this->em->persist($myLanguagePage);
284
        $this->em->flush();
285
        /* @var NodeTranslation $nodeTranslation */
286
        $nodeTranslation = $this->em->getRepository('KunstmaanNodeBundle:NodeTranslation')
287
            ->createNodeTranslationFor($myLanguagePage, $this->locale, $node, $this->user);
288
        $nodeVersion = $nodeTranslation->getPublicNodeVersion();
289
290
        $this->get('event_dispatcher')->dispatch(
291
            Events::ADD_EMPTY_PAGE_TRANSLATION,
292
            new NodeEvent($node, $nodeTranslation, $nodeVersion, $myLanguagePage)
293
        );
294
295
        return $this->redirect($this->generateUrl('KunstmaanNodeBundle_nodes_edit', array('id' => $id)));
296
    }
297
298
    /**
299
     * @Route("/{id}/publish", requirements={"id" =
300
     *                         "\d+"},
301
     *                         name="KunstmaanNodeBundle_nodes_publish")
302
     * @Method({"GET", "POST"})
303
     *
304
     * @param Request $request
305
     * @param int $id
306
     *
307
     * @return RedirectResponse
308
     * @throws AccessDeniedException
309
     */
310 View Code Duplication
    public function publishAction(Request $request, $id)
311
    {
312
        $this->init($request);
313
        /* @var Node $node */
314
        $node = $this->em->getRepository('KunstmaanNodeBundle:Node')->find($id);
315
316
        $nodeTranslation = $node->getNodeTranslation($this->locale, true);
317
        $request = $this->get('request_stack')->getCurrentRequest();
318
        $this->nodePublisher->chooseHowToPublish($request, $nodeTranslation, $this->translator);
319
320
        return $this->redirect($this->generateUrl('KunstmaanNodeBundle_nodes_edit', array('id' => $node->getId())));
321
    }
322
323
    /**
324
     * @Route(
325
     *      "/{id}/unpublish",
326
     *      requirements={"id" = "\d+"},
327
     *      name="KunstmaanNodeBundle_nodes_unpublish"
328
     * )
329
     * @Method({"GET", "POST"})
330
     *
331
     * @param Request $request
332
     * @param int $id
333
     *
334
     * @return RedirectResponse
335
     * @throws AccessDeniedException
336
     */
337 View Code Duplication
    public function unPublishAction(Request $request, $id)
338
    {
339
        $this->init($request);
340
        /* @var Node $node */
341
        $node = $this->em->getRepository('KunstmaanNodeBundle:Node')->find($id);
342
343
        $nodeTranslation = $node->getNodeTranslation($this->locale, true);
344
        $request = $this->get('request_stack')->getCurrentRequest();
345
        $this->nodePublisher->chooseHowToUnpublish($request, $nodeTranslation, $this->translator);
346
347
        return $this->redirect($this->generateUrl('KunstmaanNodeBundle_nodes_edit', array('id' => $node->getId())));
348
    }
349
350
    /**
351
     * @Route(
352
     *      "/{id}/unschedulepublish",
353
     *      requirements={"id" = "\d+"},
354
     *      name="KunstmaanNodeBundle_nodes_unschedule_publish"
355
     * )
356
     * @Method({"GET", "POST"})
357
     *
358
     * @param Request $request
359
     * @param int $id
360
     *
361
     * @return RedirectResponse
362
     * @throws AccessDeniedException
363
     */
364 View Code Duplication
    public function unSchedulePublishAction(Request $request, $id)
365
    {
366
        $this->init($request);
367
368
        /* @var Node $node */
369
        $node = $this->em->getRepository('KunstmaanNodeBundle:Node')->find($id);
370
371
        $nodeTranslation = $node->getNodeTranslation($this->locale, true);
372
        $this->nodePublisher->unSchedulePublish($nodeTranslation);
373
374
        $this->addFlash(
375
            FlashTypes::SUCCESS,
376
            $this->get('translator')->trans('kuma_node.admin.unschedule.flash.success')
377
        );
378
379
        return $this->redirect($this->generateUrl('KunstmaanNodeBundle_nodes_edit', array('id' => $id)));
380
    }
381
382
    /**
383
     * @Route(
384
     *      "/{id}/delete",
385
     *      requirements={"id" = "\d+"},
386
     *      name="KunstmaanNodeBundle_nodes_delete"
387
     * )
388
     * @Template()
389
     * @Method("POST")
390
     *
391
     * @param Request $request
392
     * @param int $id
393
     *
394
     * @return RedirectResponse
395
     * @throws AccessDeniedException
396
     */
397
    public function deleteAction(Request $request, $id)
398
    {
399
        $this->init($request);
400
        /* @var Node $node */
401
        $node = $this->em->getRepository('KunstmaanNodeBundle:Node')->find($id);
402
403
        $this->denyAccessUnlessGranted(PermissionMap::PERMISSION_DELETE, $node);
404
405
        $nodeTranslation = $node->getNodeTranslation($this->locale, true);
406
        $nodeVersion = $nodeTranslation->getPublicNodeVersion();
407
        $page = $nodeVersion->getRef($this->em);
408
409
        $this->get('event_dispatcher')->dispatch(
410
            Events::PRE_DELETE,
411
            new NodeEvent($node, $nodeTranslation, $nodeVersion, $page)
412
        );
413
414
        $node->setDeleted(true);
415
        $this->em->persist($node);
416
417
        $children = $node->getChildren();
418
        $this->deleteNodeChildren($this->em, $this->user, $this->locale, $children);
419
        $this->em->flush();
420
421
        $event = new NodeEvent($node, $nodeTranslation, $nodeVersion, $page);
422
        $this->get('event_dispatcher')->dispatch(Events::POST_DELETE, $event);
423
        if (null === $response = $event->getResponse()) {
424
            $nodeParent = $node->getParent();
425
            // Check if we have a parent. Otherwise redirect to pages overview.
426
            if ($nodeParent) {
427
                $url = $this->get('router')->generate(
428
                    'KunstmaanNodeBundle_nodes_edit',
429
                    array('id' => $nodeParent->getId())
430
                );
431
            } else {
432
                $url = $this->get('router')->generate(
433
                    'KunstmaanNodeBundle_nodes'
434
                );
435
            }
436
            $response = new RedirectResponse($url);
437
        }
438
439
        $this->addFlash(
440
            FlashTypes::SUCCESS,
441
            $this->get('translator')->trans('kuma_node.admin.delete.flash.success')
442
        );
443
444
        return $response;
445
    }
446
447
    /**
448
     * @Route(
449
     *      "/{id}/duplicate",
450
     *      requirements={"id" = "\d+"},
451
     *      name="KunstmaanNodeBundle_nodes_duplicate"
452
     * )
453
     * @Template()
454
     * @Method("POST")
455
     *
456
     * @param Request $request
457
     * @param int $id
458
     *
459
     * @return RedirectResponse
460
     * @throws AccessDeniedException
461
     */
462
    public function duplicateAction(Request $request, $id)
463
    {
464
        $this->init($request);
465
        /* @var Node $parentNode */
466
        $originalNode = $this->em->getRepository('KunstmaanNodeBundle:Node')
467
            ->find($id);
468
469
        // Check with Acl
470
        $this->denyAccessUnlessGranted(PermissionMap::PERMISSION_EDIT, $originalNode);
471
472
        $request = $this->get('request_stack')->getCurrentRequest();
473
474
        $originalNodeTranslations = $originalNode->getNodeTranslation($this->locale, true);
475
        $originalRef = $originalNodeTranslations->getPublicNodeVersion()->getRef($this->em);
476
        $newPage = $this->get('kunstmaan_admin.clone.helper')
477
            ->deepCloneAndSave($originalRef);
478
479
        //set the title
480
        $title = $request->get('title');
481
        if (is_string($title) && !empty($title)) {
482
            $newPage->setTitle($title);
483
        } else {
484
            $newPage->setTitle('New page');
485
        }
486
487
        //set the parent
488
        $parentNodeTranslation = $originalNode->getParent()->getNodeTranslation($this->locale, true);
489
        $parent = $parentNodeTranslation->getPublicNodeVersion()->getRef($this->em);
490
        $newPage->setParent($parent);
491
        $this->em->persist($newPage);
492
        $this->em->flush();
493
494
        /* @var Node $nodeNewPage */
495
        $nodeNewPage = $this->em->getRepository('KunstmaanNodeBundle:Node')->createNodeFor(
496
            $newPage,
497
            $this->locale,
498
            $this->user
499
        );
500
501
        $nodeTranslation = $nodeNewPage->getNodeTranslation($this->locale, true);
502
        if ($newPage->isStructureNode()) {
503
            $nodeTranslation->setSlug('');
504
            $this->em->persist($nodeTranslation);
505
        }
506
        $this->em->flush();
507
508
        $this->updateAcl($originalNode, $nodeNewPage);
509
510
        $this->addFlash(
511
            FlashTypes::SUCCESS,
512
            $this->get('translator')->trans('kuma_node.admin.duplicate.flash.success')
513
        );
514
515
        return $this->redirect(
516
            $this->generateUrl('KunstmaanNodeBundle_nodes_edit', array('id' => $nodeNewPage->getId()))
517
        );
518
    }
519
520
    /**
521
     * @Route(
522
     *      "/{id}/revert",
523
     *      requirements={"id" = "\d+"},
524
     *      defaults={"subaction" = "public"},
525
     *      name="KunstmaanNodeBundle_nodes_revert"
526
     * )
527
     * @Template()
528
     * @Method("GET")
529
     *
530
     * @param Request $request
531
     * @param int $id The node id
532
     *
533
     * @return RedirectResponse
534
     * @throws AccessDeniedException
535
     * @throws InvalidArgumentException
536
     */
537
    public function revertAction(Request $request, $id)
538
    {
539
        $this->init($request);
540
        /* @var Node $node */
541
        $node = $this->em->getRepository('KunstmaanNodeBundle:Node')->find($id);
542
543
        $this->denyAccessUnlessGranted(PermissionMap::PERMISSION_EDIT, $node);
544
545
        $version = $request->get('version');
546
547
        if (empty($version) || !is_numeric($version)) {
548
            throw new InvalidArgumentException('No version was specified');
549
        }
550
551
        /* @var NodeVersionRepository $nodeVersionRepo */
552
        $nodeVersionRepo = $this->em->getRepository('KunstmaanNodeBundle:NodeVersion');
553
        /* @var NodeVersion $nodeVersion */
554
        $nodeVersion = $nodeVersionRepo->find($version);
555
556
        if (is_null($nodeVersion)) {
557
            throw new InvalidArgumentException('Version does not exist');
558
        }
559
560
        /* @var NodeTranslation $nodeTranslation */
561
        $nodeTranslation = $node->getNodeTranslation($this->locale, true);
562
        $page = $nodeVersion->getRef($this->em);
563
        /* @var HasNodeInterface $clonedPage */
564
        $clonedPage = $this->get('kunstmaan_admin.clone.helper')
565
            ->deepCloneAndSave($page);
566
        $newNodeVersion = $nodeVersionRepo->createNodeVersionFor(
567
            $clonedPage,
568
            $nodeTranslation,
569
            $this->user,
570
            $nodeVersion,
571
            'draft'
572
        );
573
574
        $nodeTranslation->setTitle($clonedPage->getTitle());
575
        $this->em->persist($nodeTranslation);
576
        $this->em->flush();
577
578
        $this->get('event_dispatcher')->dispatch(
579
            Events::REVERT,
580
            new RevertNodeAction(
581
                $node,
582
                $nodeTranslation,
583
                $newNodeVersion,
584
                $clonedPage,
585
                $nodeVersion,
586
                $page
587
            )
588
        );
589
590
        $this->addFlash(
591
            FlashTypes::SUCCESS,
592
            $this->get('translator')->trans('kuma_node.admin.revert.flash.success')
593
        );
594
595
        return $this->redirect(
596
            $this->generateUrl(
597
                'KunstmaanNodeBundle_nodes_edit',
598
                array(
599
                    'id' => $id,
600
                    'subaction' => 'draft'
601
                )
602
            )
603
        );
604
    }
605
606
    /**
607
     * @Route(
608
     *      "/{id}/add",
609
     *      requirements={"id" = "\d+"},
610
     *      name="KunstmaanNodeBundle_nodes_add"
611
     * )
612
     * @Template()
613
     * @Method("POST")
614
     *
615
     * @param Request $request
616
     * @param int $id
617
     *
618
     * @return RedirectResponse
619
     * @throws AccessDeniedException
620
     * @throws InvalidArgumentException
621
     */
622
    public function addAction(Request $request, $id)
623
    {
624
        $this->init($request);
625
        /* @var Node $parentNode */
626
        $parentNode = $this->em->getRepository('KunstmaanNodeBundle:Node')->find($id);
627
628
        // Check with Acl
629
        $this->denyAccessUnlessGranted(PermissionMap::PERMISSION_EDIT, $parentNode);
630
631
        $parentNodeTranslation = $parentNode->getNodeTranslation($this->locale, true);
632
        $parentNodeVersion = $parentNodeTranslation->getPublicNodeVersion();
633
        $parentPage = $parentNodeVersion->getRef($this->em);
634
635
        $type = $this->validatePageType($request);
636
        $newPage = $this->createNewPage($request, $type);
637
        $newPage->setParent($parentPage);
638
639
        /* @var Node $nodeNewPage */
640
        $nodeNewPage = $this->em->getRepository('KunstmaanNodeBundle:Node')
641
            ->createNodeFor($newPage, $this->locale, $this->user);
642
        $nodeTranslation = $nodeNewPage->getNodeTranslation(
643
            $this->locale,
644
            true
645
        );
646
        $weight = $this->em->getRepository('KunstmaanNodeBundle:NodeTranslation')
647
                ->getMaxChildrenWeight($parentNode, $this->locale) + 1;
648
        $nodeTranslation->setWeight($weight);
649
650
        if ($newPage->isStructureNode()) {
651
            $nodeTranslation->setSlug('');
652
        }
653
654
        $this->em->persist($nodeTranslation);
655
        $this->em->flush();
656
657
        $this->updateAcl($parentNode, $nodeNewPage);
658
659
        $nodeVersion = $nodeTranslation->getPublicNodeVersion();
660
661
        $this->get('event_dispatcher')->dispatch(
662
            Events::ADD_NODE,
663
            new NodeEvent(
664
                $nodeNewPage, $nodeTranslation, $nodeVersion, $newPage
665
            )
666
        );
667
668
        return $this->redirect(
669
            $this->generateUrl(
670
                'KunstmaanNodeBundle_nodes_edit',
671
                array('id' => $nodeNewPage->getId())
672
            )
673
        );
674
    }
675
676
    /**
677
     * @Route("/add-homepage", name="KunstmaanNodeBundle_nodes_add_homepage")
678
     * @Template()
679
     * @Method("POST")
680
     *
681
     * @return RedirectResponse
682
     * @throws AccessDeniedException
683
     * @throws InvalidArgumentException
684
     */
685
    public function addHomepageAction(Request $request)
686
    {
687
        $this->init($request);
688
689
        // Check with Acl
690
        $this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN');
691
692
        $type = $this->validatePageType($request);
693
694
        $newPage = $this->createNewPage($request, $type);
695
696
        /* @var Node $nodeNewPage */
697
        $nodeNewPage = $this->em->getRepository('KunstmaanNodeBundle:Node')
698
            ->createNodeFor($newPage, $this->locale, $this->user);
699
        $nodeTranslation = $nodeNewPage->getNodeTranslation(
700
            $this->locale,
701
            true
702
        );
703
        $this->em->flush();
704
705
        // Set default permissions
706
        $this->get('kunstmaan_node.acl_permission_creator_service')
707
            ->createPermission($nodeNewPage);
708
709
        $nodeVersion = $nodeTranslation->getPublicNodeVersion();
710
711
        $this->get('event_dispatcher')->dispatch(
712
            Events::ADD_NODE,
713
            new NodeEvent(
714
                $nodeNewPage, $nodeTranslation, $nodeVersion, $newPage
715
            )
716
        );
717
718
        return $this->redirect(
719
            $this->generateUrl(
720
                'KunstmaanNodeBundle_nodes_edit',
721
                array('id' => $nodeNewPage->getId())
722
            )
723
        );
724
    }
725
726
    /**
727
     * @Route("/reorder", name="KunstmaanNodeBundle_nodes_reorder")
728
     * @Method("POST")
729
     *
730
     * @param Request $request
731
     *
732
     * @return string
733
     * @throws AccessDeniedException
734
     */
735
    public function reorderAction(Request $request)
736
    {
737
        $this->init($request);
738
        $nodes = array();
739
        $nodeIds = $request->get('nodes');
740
        $changeParents = $request->get('parent');
741
742
        foreach ($nodeIds as $id) {
743
            /* @var Node $node */
744
            $node = $this->em->getRepository('KunstmaanNodeBundle:Node')->find($id);
745
            $this->denyAccessUnlessGranted(PermissionMap::PERMISSION_EDIT, $node);
746
            $nodes[] = $node;
747
        }
748
749
        $weight = 0;
750
        foreach ($nodes as $node) {
751
752
            $newParentId = isset($changeParents[$node->getId()]) ? $changeParents[$node->getId()] : null;
753
            if ($newParentId) {
754
                $parent = $this->em->getRepository('KunstmaanNodeBundle:Node')->find($newParentId);
755
                $this->denyAccessUnlessGranted(PermissionMap::PERMISSION_EDIT, $parent);
756
                $node->setParent($parent);
757
                $this->em->persist($node);
758
                $this->em->flush($node);
759
            }
760
761
762
            /* @var NodeTranslation $nodeTranslation */
763
            $nodeTranslation = $node->getNodeTranslation($this->locale, true);
764
765
            if ($nodeTranslation) {
766
                $nodeVersion = $nodeTranslation->getPublicNodeVersion();
767
                $page = $nodeVersion->getRef($this->em);
768
769
                $this->get('event_dispatcher')->dispatch(
770
                    Events::PRE_PERSIST,
771
                    new NodeEvent($node, $nodeTranslation, $nodeVersion, $page)
772
                );
773
774
                $nodeTranslation->setWeight($weight);
775
                $this->em->persist($nodeTranslation);
776
                $this->em->flush($nodeTranslation);
777
778
                $this->get('event_dispatcher')->dispatch(
779
                    Events::POST_PERSIST,
780
                    new NodeEvent($node, $nodeTranslation, $nodeVersion, $page)
781
                );
782
783
                $weight++;
784
            }
785
        }
786
787
        return new JsonResponse(
788
            array(
789
                'Success' => 'The node-translations for [' . $this->locale . '] have got new weight values'
790
            )
791
        );
792
    }
793
794
    /**
795
     * @Route(
796
     *      "/{id}/{subaction}",
797
     *      requirements={"id" = "\d+"},
798
     *      defaults={"subaction" = "public"},
799
     *      name="KunstmaanNodeBundle_nodes_edit"
800
     * )
801
     * @Template()
802
     * @Method({"GET", "POST"})
803
     *
804
     * @param Request $request
805
     * @param int $id The node id
806
     * @param string $subaction The subaction (draft|public)
807
     *
808
     * @return RedirectResponse|array
809
     * @throws AccessDeniedException
810
     */
811
    public function editAction(Request $request, $id, $subaction)
812
    {
813
        $this->init($request);
814
        /* @var Node $node */
815
        $node = $this->em->getRepository('KunstmaanNodeBundle:Node')->find($id);
816
817
        $this->denyAccessUnlessGranted(PermissionMap::PERMISSION_EDIT, $node);
818
819
        $tabPane = new TabPane(
820
            'todo',
821
            $request,
822
            $this->container->get('form.factory')
823
        );
824
825
        $nodeTranslation = $node->getNodeTranslation($this->locale, true);
826
        if (!$nodeTranslation) {
827
            return $this->renderNodeNotTranslatedPage($node);
828
        }
829
830
        $nodeVersion = $nodeTranslation->getPublicNodeVersion();
831
        $draftNodeVersion = $nodeTranslation->getDraftNodeVersion();
832
        $nodeVersionIsLocked = false;
833
834
        /* @var HasNodeInterface $page */
835
        $page = null;
0 ignored issues
show
$page is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
836
        $draft = ($subaction == 'draft');
837
        $saveAsDraft = $request->get('saveasdraft');
838
        if ((!$draft && !empty($saveAsDraft)) || ($draft && is_null($draftNodeVersion))) {
839
            // Create a new draft version
840
            $draft = true;
841
            $subaction = "draft";
842
            $page = $nodeVersion->getRef($this->em);
843
            $nodeVersion = $this->createDraftVersion(
844
                $page,
845
                $nodeTranslation,
846
                $nodeVersion
847
            );
848
            $draftNodeVersion = $nodeVersion;
849
        } elseif ($draft) {
850
            $nodeVersion = $draftNodeVersion;
851
            $page = $nodeVersion->getRef($this->em);
852
        } else {
853
            if ($request->getMethod() == 'POST') {
854
                $nodeVersionIsLocked = $this->isNodeVersionLocked($nodeTranslation, true);
855
856
                //Check the version timeout and make a new nodeversion if the timeout is passed
857
                $thresholdDate = date(
858
                    "Y-m-d H:i:s",
859
                    time() - $this->getParameter(
860
                        "kunstmaan_node.version_timeout"
861
                    )
862
                );
863
                $updatedDate = date(
864
                    "Y-m-d H:i:s",
865
                    strtotime($nodeVersion->getUpdated()->format("Y-m-d H:i:s"))
866
                );
867
                if ($thresholdDate >= $updatedDate || $nodeVersionIsLocked) {
868
                    $page = $nodeVersion->getRef($this->em);
869
                    if ($nodeVersion == $nodeTranslation->getPublicNodeVersion()) {
870
                        $this->nodePublisher
871
                            ->createPublicVersion(
872
                                $page,
873
                                $nodeTranslation,
874
                                $nodeVersion,
875
                                $this->user
876
                            );
877
                    } else {
878
                        $this->createDraftVersion(
879
                            $page,
880
                            $nodeTranslation,
881
                            $nodeVersion
882
                        );
883
                    }
884
                }
885
            }
886
            $page = $nodeVersion->getRef($this->em);
887
        }
888
        $isStructureNode = $page->isStructureNode();
889
890
        $menubuilder = $this->get('kunstmaan_node.actions_menu_builder');
891
        $menubuilder->setActiveNodeVersion($nodeVersion);
892
        $menubuilder->setEditableNode(!$isStructureNode);
893
894
        // Building the form
895
        $propertiesWidget = new FormWidget();
896
        $propertiesWidget->addType('main', $page->getDefaultAdminType(), $page);
897
        $propertiesWidget->addType('node', $node->getDefaultAdminType(), $node);
898
        $tabPane->addTab(new Tab('kuma_node.tab.properties.title', $propertiesWidget));
899
900
        // Menu tab
901
        $menuWidget = new FormWidget();
902
        $menuWidget->addType(
903
            'menunodetranslation',
904
            NodeMenuTabTranslationAdminType::class,
905
            $nodeTranslation,
906
            ['slugable' => !$isStructureNode]
907
        );
908
        $menuWidget->addType('menunode', NodeMenuTabAdminType::class, $node, ['available_in_nav' => !$isStructureNode]);
909
        $tabPane->addTab(new Tab('kuma_node.tab.menu.title', $menuWidget));
910
911
        $this->get('event_dispatcher')->dispatch(
912
            Events::ADAPT_FORM,
913
            new AdaptFormEvent(
914
                $request,
915
                $tabPane,
916
                $page,
917
                $node,
918
                $nodeTranslation,
919
                $nodeVersion
920
            )
921
        );
922
923
        $tabPane->buildForm();
924
925
        if ($request->getMethod() == 'POST') {
926
            $tabPane->bindRequest($request);
927
928
            // Don't redirect to listing when coming from ajax request, needed for url chooser.
929
            if ($tabPane->isValid() && !$request->isXmlHttpRequest()) {
930
                $this->get('event_dispatcher')->dispatch(
931
                    Events::PRE_PERSIST,
932
                    new NodeEvent($node, $nodeTranslation, $nodeVersion, $page)
933
                );
934
935
                $nodeTranslation->setTitle($page->getTitle());
936
                if ($isStructureNode) {
937
                    $nodeTranslation->setSlug('');
938
                }
939
                $nodeVersion->setUpdated(new DateTime());
940
                if ($nodeVersion->getType() == 'public') {
941
                    $nodeTranslation->setUpdated($nodeVersion->getUpdated());
942
                }
943
                $this->em->persist($nodeTranslation);
944
                $this->em->persist($nodeVersion);
945
                $tabPane->persist($this->em);
946
                $this->em->flush();
947
948
                $this->get('event_dispatcher')->dispatch(
949
                    Events::POST_PERSIST,
950
                    new NodeEvent($node, $nodeTranslation, $nodeVersion, $page)
951
                );
952
953
                if ($nodeVersionIsLocked) {
954
                    $this->addFlash(
955
                        FlashTypes::SUCCESS,
956
                        $this->get('translator')->trans('kuma_node.admin.edit.flash.locked_success')
957
                    );
958
                } else {
959
                    if ($request->request->has('publishing') || $request->request->has('publish_later')) {
960
                        $this->nodePublisher->chooseHowToPublish($request, $nodeTranslation, $this->translator);
961
                    } elseif ($request->request->has('unpublishing') || $request->request->has('unpublish_later')) {
962
                        $this->nodePublisher->chooseHowToUnpublish($request, $nodeTranslation, $this->translator);
963
                    } else {
964
                        $this->addFlash(
965
                            FlashTypes::SUCCESS,
966
                            $this->get('translator')->trans('kuma_node.admin.edit.flash.success')
967
                        );
968
                    }
969
                }
970
971
                $params = [
972
                    'id' => $node->getId(),
973
                    'subaction' => $subaction,
974
                    'currenttab' => $tabPane->getActiveTab(),
975
                ];
976
                $params = array_merge(
977
                    $params,
978
                    $tabPane->getExtraParams($request)
979
                );
980
981
                return $this->redirect(
982
                    $this->generateUrl(
983
                        'KunstmaanNodeBundle_nodes_edit',
984
                        $params
985
                    )
986
                );
987
            }
988
        }
989
990
        $nodeVersions = $this->em->getRepository(
991
            'KunstmaanNodeBundle:NodeVersion'
992
        )->findBy(
993
            ['nodeTranslation' => $nodeTranslation],
994
            ['updated' => 'ASC']
995
        );
996
        $queuedNodeTranslationAction = $this->em->getRepository(
997
            'KunstmaanNodeBundle:QueuedNodeTranslationAction'
998
        )->findOneBy(['nodeTranslation' => $nodeTranslation]);
999
1000
        return [
1001
            'page' => $page,
1002
            'entityname' => ClassLookup::getClass($page),
1003
            'nodeVersions' => $nodeVersions,
1004
            'node' => $node,
1005
            'nodeTranslation' => $nodeTranslation,
1006
            'draft' => $draft,
1007
            'draftNodeVersion' => $draftNodeVersion,
1008
            'nodeVersion' => $nodeVersion,
1009
            'subaction' => $subaction,
1010
            'tabPane' => $tabPane,
1011
            'editmode' => true,
1012
            'queuedNodeTranslationAction' => $queuedNodeTranslationAction,
1013
            'nodeVersionLockCheck' => $this->container->getParameter('kunstmaan_node.lock_enabled'),
1014
            'nodeVersionLockInterval' => $this->container->getParameter('kunstmaan_node.lock_check_interval'),
1015
        ];
1016
    }
1017
1018
    /**
1019
     * @Route(
1020
     *      "checkNodeVersionLock/{id}/{public}",
1021
     *      requirements={"id" = "\d+", "public" = "(0|1)"},
1022
     *      name="KunstmaanNodeBundle_nodes_versionlock_check"
1023
     * )
1024
     * @param Request $request
1025
     * @param $id
1026
     * @return JsonResponse
1027
     */
1028
    public function checkNodeVersionLockAction(Request $request, $id, $public)
1029
    {
1030
        $nodeVersionIsLocked = false;
1031
        $message = '';
1032
        $this->init($request);
1033
1034
        /* @var Node $node */
1035
        $node = $this->em->getRepository('KunstmaanNodeBundle:Node')->find($id);
1036
1037
        try {
1038
            $this->checkPermission($node, PermissionMap::PERMISSION_EDIT);
1039
1040
            /** @var NodeVersionLockHelper $nodeVersionLockHelper */
1041
            $nodeVersionLockHelper = $this->get('kunstmaan_node.admin_node.node_version_lock_helper');
1042
            $nodeTranslation = $node->getNodeTranslation($this->locale, true);
1043
1044
            if ($nodeTranslation) {
1045
                $nodeVersionIsLocked = $nodeVersionLockHelper->isNodeVersionLocked($this->getUser(), $nodeTranslation, $public);
0 ignored issues
show
$this->getUser() is of type null|object, but the function expects a object<Kunstmaan\AdminBundle\Entity\BaseUser>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
1046
1047
                if ($nodeVersionIsLocked) {
1048
                    $users = $nodeVersionLockHelper->getUsersWithNodeVersionLock($nodeTranslation, $public, $this->getUser());
1049
                    $message = $this->get('translator')->trans('kuma_node.admin.edit.flash.locked', array('%users%' => implode(', ', $users)));
1050
                }
1051
            }
1052
1053
        } catch (AccessDeniedException $ade) {}
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
1054
1055
        return new JsonResponse(['lock' => $nodeVersionIsLocked, 'message' => $message]);
1056
    }
1057
1058
    /**
1059
     * @param NodeTranslation $nodeTranslation
1060
     * @param bool $isPublic
1061
     * @return bool
1062
     */
1063
    private function isNodeVersionLocked(NodeTranslation $nodeTranslation, $isPublic)
1064
    {
1065
        if ($this->container->getParameter('kunstmaan_node.lock_enabled')) {
1066
            /** @var NodeVersionLockHelper $nodeVersionLockHelper */
1067
            $nodeVersionLockHelper = $this->get('kunstmaan_node.admin_node.node_version_lock_helper');
1068
            $nodeVersionIsLocked = $nodeVersionLockHelper->isNodeVersionLocked($this->getUser(), $nodeTranslation, $isPublic);
0 ignored issues
show
$this->getUser() is of type null|object, but the function expects a object<Kunstmaan\AdminBundle\Entity\BaseUser>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
1069
            return $nodeVersionIsLocked;
1070
        }
1071
        return false;
1072
    }
1073
1074
    /**
1075
     * @param HasNodeInterface $page The page
1076
     * @param NodeTranslation $nodeTranslation The node translation
1077
     * @param NodeVersion $nodeVersion The node version
1078
     *
1079
     * @return NodeVersion
1080
     */
1081
    private function createDraftVersion(
1082
        HasNodeInterface $page,
1083
        NodeTranslation $nodeTranslation,
1084
        NodeVersion $nodeVersion
1085
    )
1086
    {
1087
        $publicPage = $this->get('kunstmaan_admin.clone.helper')
1088
            ->deepCloneAndSave($page);
1089
        /* @var NodeVersion $publicNodeVersion */
1090
1091
        $publicNodeVersion = $this->em->getRepository(
1092
            'KunstmaanNodeBundle:NodeVersion'
1093
        )->createNodeVersionFor(
1094
            $publicPage,
1095
            $nodeTranslation,
1096
            $this->user,
1097
            $nodeVersion->getOrigin(),
1098
            'public',
1099
            $nodeVersion->getCreated()
1100
        );
1101
1102
        $nodeTranslation->setPublicNodeVersion($publicNodeVersion);
1103
        $nodeVersion->setType('draft');
1104
        $nodeVersion->setOrigin($publicNodeVersion);
1105
        $nodeVersion->setCreated(new DateTime());
1106
1107
        $this->em->persist($nodeTranslation);
1108
        $this->em->persist($nodeVersion);
1109
        $this->em->flush();
1110
1111
        $this->get('event_dispatcher')->dispatch(
1112
            Events::CREATE_DRAFT_VERSION,
1113
            new NodeEvent(
1114
                $nodeTranslation->getNode(),
1115
                $nodeTranslation,
1116
                $nodeVersion,
1117
                $page
1118
            )
1119
        );
1120
1121
        return $nodeVersion;
1122
    }
1123
1124
    /**
1125
     * @param Node $node The node
1126
     * @param string $permission The permission to check for
1127
     *
1128
     * @throws AccessDeniedException
1129
     */
1130
    private function checkPermission(Node $node, $permission)
1131
    {
1132
        if (false === $this->authorizationChecker->isGranted($permission, $node)) {
1133
            throw new AccessDeniedException();
1134
        }
1135
    }
1136
1137
    /**
1138
     * @param EntityManager   $em       The Entity Manager
1139
     * @param BaseUser        $user     The user who deletes the children
1140
     * @param string          $locale   The locale that was used
1141
     * @param ArrayCollection $children The children array
1142
     */
1143
    private function deleteNodeChildren(
1144
        EntityManager $em,
1145
        BaseUser $user,
1146
        $locale,
1147
        ArrayCollection $children
1148
    )
1149
    {
1150
        /* @var Node $childNode */
1151
        foreach ($children as $childNode) {
1152
            $childNodeTranslation = $childNode->getNodeTranslation(
1153
                $this->locale,
1154
                true
1155
            );
1156
1157
            $childNodeVersion = $childNodeTranslation->getPublicNodeVersion();
1158
            $childNodePage = $childNodeVersion->getRef($this->em);
1159
1160
            $this->get('event_dispatcher')->dispatch(
1161
                Events::PRE_DELETE,
1162
                new NodeEvent(
1163
                    $childNode,
1164
                    $childNodeTranslation,
1165
                    $childNodeVersion,
1166
                    $childNodePage
1167
                )
1168
            );
1169
1170
            $childNode->setDeleted(true);
1171
            $this->em->persist($childNode);
1172
1173
            $children2 = $childNode->getChildren();
1174
            $this->deleteNodeChildren($em, $user, $locale, $children2);
1175
1176
            $this->get('event_dispatcher')->dispatch(
1177
                Events::POST_DELETE,
1178
                new NodeEvent(
1179
                    $childNode,
1180
                    $childNodeTranslation,
1181
                    $childNodeVersion,
1182
                    $childNodePage
1183
                )
1184
            );
1185
        }
1186
    }
1187
1188
    /**
1189
     * @param $originalNode
1190
     * @param $nodeNewPage
1191
     */
1192
    private function updateAcl($originalNode, $nodeNewPage)
1193
    {
1194
        /* @var MutableAclProviderInterface $aclProvider */
1195
        $aclProvider = $this->container->get('security.acl.provider');
1196
        /* @var ObjectIdentityRetrievalStrategyInterface $strategy */
1197
        $strategy = $this->container->get(
1198
            'security.acl.object_identity_retrieval_strategy'
1199
        );
1200
        $originalIdentity = $strategy->getObjectIdentity($originalNode);
1201
        $originalAcl = $aclProvider->findAcl($originalIdentity);
1202
1203
        $newIdentity = $strategy->getObjectIdentity($nodeNewPage);
1204
        $newAcl = $aclProvider->createAcl($newIdentity);
1205
1206
        $aces = $originalAcl->getObjectAces();
1207
        /* @var EntryInterface $ace */
1208
        foreach ($aces as $ace) {
1209
            $securityIdentity = $ace->getSecurityIdentity();
1210
            if ($securityIdentity instanceof RoleSecurityIdentity) {
1211
                $newAcl->insertObjectAce($securityIdentity, $ace->getMask());
1212
            }
1213
        }
1214
        $aclProvider->updateAcl($newAcl);
1215
    }
1216
1217
    /**
1218
     * @param Request $request
1219
     * @param string $type
1220
     *
1221
     * @return HasNodeInterface
1222
     */
1223
    private function createNewPage(Request $request, $type)
1224
    {
1225
        /* @var HasNodeInterface $newPage */
1226
        $newPage = new $type();
1227
1228
        $title = $request->get('title');
1229
        if (is_string($title) && !empty($title)) {
1230
            $newPage->setTitle($title);
1231
        } else {
1232
            $newPage->setTitle($this->get('translator')->trans('kuma_node.admin.new_page.title.default'));
1233
        }
1234
        $this->em->persist($newPage);
1235
        $this->em->flush();
1236
1237
        return $newPage;
1238
    }
1239
1240
    /**
1241
     * @param Request $request
1242
     *
1243
     * @return string
1244
     * @throw InvalidArgumentException
1245
     */
1246
    private function validatePageType($request)
1247
    {
1248
        $type = $request->get('type');
1249
1250
        if (empty($type)) {
1251
            throw new InvalidArgumentException(
1252
                'Please specify a type of page you want to create'
1253
            );
1254
        }
1255
1256
        return $type;
1257
    }
1258
1259
    /**
1260
     * @param Node $node
1261
     * @return \Symfony\Component\HttpFoundation\Response
1262
     */
1263
    private function renderNodeNotTranslatedPage(Node $node)
1264
    {
1265
        //try to find a parent node with the correct translation, if there is none allow copy.
1266
        //if there is a parent but it doesn't have the language to copy to don't allow it
1267
        $parentNode = $node->getParent();
1268
        if ($parentNode) {
1269
            $parentNodeTranslation = $parentNode->getNodeTranslation(
1270
                $this->locale,
1271
                true
1272
            );
1273
            $parentsAreOk = false;
1274
1275
            if ($parentNodeTranslation) {
1276
                $parentsAreOk = $this->em->getRepository(
1277
                    'KunstmaanNodeBundle:NodeTranslation'
1278
                )->hasParentNodeTranslationsForLanguage(
1279
                    $node->getParent()->getNodeTranslation(
1280
                        $this->locale,
1281
                        true
1282
                    ),
1283
                    $this->locale
1284
                );
1285
            }
1286
        } else {
1287
            $parentsAreOk = true;
1288
        }
1289
1290
        return $this->render(
1291
            'KunstmaanNodeBundle:NodeAdmin:pagenottranslated.html.twig',
1292
            array(
1293
                'node' => $node,
1294
                'nodeTranslations' => $node->getNodeTranslations(
1295
                    true
1296
                ),
1297
                'copyfromotherlanguages' => $parentsAreOk
1298
            )
1299
        );
1300
    }
1301
}
1302