Completed
Push — master ( 91fdab...75a7b9 )
by
unknown
13:37
created

NodeBundle/Controller/NodeAdminController.php (3 issues)

Upgrade to new PHP Analysis Engine

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

1
<?php
2
3
namespace Kunstmaan\NodeBundle\Controller;
4
5
use DateTime;
6
use Doctrine\Common\Collections\ArrayCollection;
7
use Doctrine\ORM\EntityManager;
8
use InvalidArgumentException;
9
use Kunstmaan\AdminBundle\Entity\BaseUser;
10
use Kunstmaan\AdminBundle\Entity\EntityInterface;
11
use Kunstmaan\AdminBundle\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\AdminBundle\Service\AclManager;
21
use Kunstmaan\AdminListBundle\AdminList\AdminList;
22
use Kunstmaan\NodeBundle\AdminList\NodeAdminListConfigurator;
23
use Kunstmaan\NodeBundle\Entity\HasNodeInterface;
24
use Kunstmaan\NodeBundle\Entity\Node;
25
use Kunstmaan\NodeBundle\Entity\NodeTranslation;
26
use Kunstmaan\NodeBundle\Entity\NodeVersion;
27
use Kunstmaan\NodeBundle\Event\AdaptFormEvent;
28
use Kunstmaan\NodeBundle\Event\CopyPageTranslationNodeEvent;
29
use Kunstmaan\NodeBundle\Event\Events;
30
use Kunstmaan\NodeBundle\Event\NodeEvent;
31
use Kunstmaan\NodeBundle\Event\RecopyPageTranslationNodeEvent;
32
use Kunstmaan\NodeBundle\Event\RevertNodeAction;
33
use Kunstmaan\NodeBundle\Form\NodeMenuTabAdminType;
34
use Kunstmaan\NodeBundle\Form\NodeMenuTabTranslationAdminType;
35
use Kunstmaan\NodeBundle\Helper\NodeAdmin\NodeAdminPublisher;
36
use Kunstmaan\NodeBundle\Helper\NodeAdmin\NodeVersionLockHelper;
37
use Kunstmaan\NodeBundle\Repository\NodeVersionRepository;
38
use Kunstmaan\UtilitiesBundle\Helper\ClassLookup;
39
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
40
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
41
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
42
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
43
use Symfony\Component\HttpFoundation\JsonResponse;
44
use Symfony\Component\HttpFoundation\RedirectResponse;
45
use Symfony\Component\HttpFoundation\Request;
46
use Symfony\Component\Security\Acl\Model\EntryInterface;
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 $em
58
     */
59
    protected $em;
60
61
    /**
62
     * @var string $locale
63
     */
64
    protected $locale;
65
66
    /**
67
     * @var AuthorizationCheckerInterface $authorizationChecker
68
     */
69
    protected $authorizationChecker;
70
71
    /**
72
     * @var BaseUser $user
73
     */
74
    protected $user;
75
76
    /**
77
     * @var AclHelper $aclHelper
78
     */
79
    protected $aclHelper;
80
81
    /**
82
     * @var AclManager
83
     */
84
    protected $aclManager;
85
86
    /**
87
     * @var NodeAdminPublisher
88
     */
89
    protected $nodePublisher;
90
91
    /**
92
     * @var TranslatorInterface
93
     */
94
    protected $translator;
95
96
    /**
97
     * init
98
     *
99
     * @param Request $request
100
     */
101
    protected function init(Request $request)
102
    {
103
        $this->em = $this->getDoctrine()->getManager();
104
        $this->locale = $request->getLocale();
105
        $this->authorizationChecker = $this->container->get('security.authorization_checker');
106
        $this->user = $this->getUser();
107
        $this->aclHelper = $this->container->get('kunstmaan_admin.acl.helper');
108
        $this->aclManager = $this->container->get('kunstmaan_admin.acl.manager');
109
        $this->nodePublisher = $this->container->get('kunstmaan_node.admin_node.publisher');
110
        $this->translator = $this->container->get('translator');
111
    }
112
113
    /**
114
     * @Route("/", name="KunstmaanNodeBundle_nodes")
115
     * @Template("KunstmaanNodeBundle:Admin:list.html.twig")
116
     *
117
     * @param Request $request
118
     *
119
     * @return array
120
     */
121
    public function indexAction(Request $request)
122
    {
123
        $this->init($request);
124
125
        $nodeAdminListConfigurator = new NodeAdminListConfigurator(
126
            $this->em,
127
            $this->aclHelper,
128
            $this->locale,
129
            PermissionMap::PERMISSION_VIEW,
130
            $this->authorizationChecker
131
        );
132
133
        $locale = $this->locale;
134
        $acl = $this->authorizationChecker;
135
        $itemRoute = function (EntityInterface $item) use ($locale, $acl) {
136
            if ($acl->isGranted(PermissionMap::PERMISSION_VIEW, $item->getNode())) {
137
                return array(
138
                    'path' => '_slug_preview',
139
                    'params' => ['_locale' => $locale, 'url' => $item->getUrl()]
140
                );
141
            }
142
        };
143
        $nodeAdminListConfigurator->addSimpleItemAction('action.preview', $itemRoute, 'eye');
144
        $nodeAdminListConfigurator->setDomainConfiguration($this->get('kunstmaan_admin.domain_configuration'));
145
        $nodeAdminListConfigurator->setShowAddHomepage($this->getParameter('kunstmaan_node.show_add_homepage') && $this->isGranted('ROLE_SUPER_ADMIN'));
146
147
        /** @var AdminList $adminlist */
148
        $adminlist = $this->get('kunstmaan_adminlist.factory')->createList($nodeAdminListConfigurator);
149
        $adminlist->bindRequest($request);
150
151
        return array(
152
            'adminlist' => $adminlist,
153
        );
154
    }
155
156
    /**
157
     * @Route(
158
     *      "/{id}/copyfromotherlanguage",
159
     *      requirements={"id" = "\d+"},
160
     *      name="KunstmaanNodeBundle_nodes_copyfromotherlanguage"
161
     * )
162
     * @Method("GET")
163
     *
164
     * @param Request $request
165
     * @param int $id The node id
166
     *
167
     * @return RedirectResponse
168
     * @throws AccessDeniedException
169
     */
170
    public function copyFromOtherLanguageAction(Request $request, $id)
171
    {
172
        $this->init($request);
173
        /* @var Node $node */
174
        $node = $this->em->getRepository('KunstmaanNodeBundle:Node')->find($id);
175
176
        $this->denyAccessUnlessGranted(PermissionMap::PERMISSION_EDIT, $node);
177
178
        $originalLanguage = $request->get('originallanguage');
179
        $otherLanguageNodeTranslation = $node->getNodeTranslation($originalLanguage, true);
180
        $otherLanguageNodeNodeVersion = $otherLanguageNodeTranslation->getPublicNodeVersion();
181
        $otherLanguagePage = $otherLanguageNodeNodeVersion->getRef($this->em);
182
        $myLanguagePage = $this->get('kunstmaan_admin.clone.helper')
183
            ->deepCloneAndSave($otherLanguagePage);
184
185
        /* @var NodeTranslation $nodeTranslation */
186
        $nodeTranslation = $this->em->getRepository('KunstmaanNodeBundle:NodeTranslation')
187
            ->createNodeTranslationFor($myLanguagePage, $this->locale, $node, $this->user);
188
        $nodeVersion = $nodeTranslation->getPublicNodeVersion();
189
190
        $this->get('event_dispatcher')->dispatch(
191
            Events::COPY_PAGE_TRANSLATION,
192
            new CopyPageTranslationNodeEvent(
193
                $node,
194
                $nodeTranslation,
195
                $nodeVersion,
196
                $myLanguagePage,
197
                $otherLanguageNodeTranslation,
198
                $otherLanguageNodeNodeVersion,
199
                $otherLanguagePage,
200
                $originalLanguage
201
            )
202
        );
203
204
        return $this->redirect($this->generateUrl('KunstmaanNodeBundle_nodes_edit', array('id' => $id)));
205
    }
206
207
    /**
208
     * @Route(
209
     *      "/{id}/recopyfromotherlanguage",
210
     *      requirements={"id" = "\d+"},
211
     *      name="KunstmaanNodeBundle_nodes_recopyfromotherlanguage"
212
     * )
213
     * @Method("POST")
214
     *
215
     * @param Request $request
216
     * @param int $id The node id
217
     *
218
     * @return RedirectResponse
219
     * @throws AccessDeniedException
220
     */
221
    public function recopyFromOtherLanguageAction(Request $request, $id)
222
    {
223
        $this->init($request);
224
        /* @var Node $node */
225
        $node = $this->em->getRepository('KunstmaanNodeBundle:Node')->find($id);
226
227
        $this->denyAccessUnlessGranted(PermissionMap::PERMISSION_EDIT, $node);
228
229
        $otherLanguageNodeTranslation = $this->em->getRepository('KunstmaanNodeBundle:NodeTranslation')->find($request->get('source'));
230
        $otherLanguageNodeNodeVersion = $otherLanguageNodeTranslation->getPublicNodeVersion();
231
        $otherLanguagePage = $otherLanguageNodeNodeVersion->getRef($this->em);
232
        $myLanguagePage = $this->get('kunstmaan_admin.clone.helper')
233
            ->deepCloneAndSave($otherLanguagePage);
234
235
        /* @var NodeTranslation $nodeTranslation */
236
        $nodeTranslation = $this->em->getRepository('KunstmaanNodeBundle:NodeTranslation')
237
            ->addDraftNodeVersionFor($myLanguagePage, $this->locale, $node, $this->user);
238
        $nodeVersion = $nodeTranslation->getPublicNodeVersion();
239
240
        $this->get('event_dispatcher')->dispatch(
241
            Events::RECOPY_PAGE_TRANSLATION,
242
            new RecopyPageTranslationNodeEvent(
243
                $node,
244
                $nodeTranslation,
245
                $nodeVersion,
246
                $myLanguagePage,
247
                $otherLanguageNodeTranslation,
248
                $otherLanguageNodeNodeVersion,
249
                $otherLanguagePage,
250
                $otherLanguageNodeTranslation->getLang()
251
            )
252
        );
253
254
        return $this->redirect($this->generateUrl('KunstmaanNodeBundle_nodes_edit', array('id' => $id, 'subaction' => NodeVersion::DRAFT_VERSION)));
255
    }
256
257
    /**
258
     * @Route(
259
     *      "/{id}/createemptypage",
260
     *      requirements={"id" = "\d+"},
261
     *      name="KunstmaanNodeBundle_nodes_createemptypage"
262
     * )
263
     * @Method("GET")
264
     *
265
     * @param Request $request
266
     * @param int $id
267
     *
268
     * @return RedirectResponse
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
     * @throws AccessDeniedException
310
     */
311 View Code Duplication
    public function publishAction(Request $request, $id)
312
    {
313
        $this->init($request);
314
        /* @var Node $node */
315
        $node = $this->em->getRepository('KunstmaanNodeBundle:Node')->find($id);
316
317
        $nodeTranslation = $node->getNodeTranslation($this->locale, true);
318
        $request = $this->get('request_stack')->getCurrentRequest();
319
        $this->nodePublisher->chooseHowToPublish($request, $nodeTranslation, $this->translator);
320
321
        return $this->redirect($this->generateUrl('KunstmaanNodeBundle_nodes_edit', array('id' => $node->getId())));
322
    }
323
324
    /**
325
     * @Route(
326
     *      "/{id}/unpublish",
327
     *      requirements={"id" = "\d+"},
328
     *      name="KunstmaanNodeBundle_nodes_unpublish"
329
     * )
330
     * @Method({"GET", "POST"})
331
     *
332
     * @param Request $request
333
     * @param int $id
334
     *
335
     * @return RedirectResponse
336
     * @throws AccessDeniedException
337
     */
338 View Code Duplication
    public function unPublishAction(Request $request, $id)
339
    {
340
        $this->init($request);
341
        /* @var Node $node */
342
        $node = $this->em->getRepository('KunstmaanNodeBundle:Node')->find($id);
343
344
        $nodeTranslation = $node->getNodeTranslation($this->locale, true);
345
        $request = $this->get('request_stack')->getCurrentRequest();
346
        $this->nodePublisher->chooseHowToUnpublish($request, $nodeTranslation, $this->translator);
347
348
        return $this->redirect($this->generateUrl('KunstmaanNodeBundle_nodes_edit', array('id' => $node->getId())));
349
    }
350
351
    /**
352
     * @Route(
353
     *      "/{id}/unschedulepublish",
354
     *      requirements={"id" = "\d+"},
355
     *      name="KunstmaanNodeBundle_nodes_unschedule_publish"
356
     * )
357
     * @Method({"GET", "POST"})
358
     *
359
     * @param Request $request
360
     * @param int $id
361
     *
362
     * @return RedirectResponse
363
     * @throws AccessDeniedException
364
     */
365 View Code Duplication
    public function unSchedulePublishAction(Request $request, $id)
366
    {
367
        $this->init($request);
368
369
        /* @var Node $node */
370
        $node = $this->em->getRepository('KunstmaanNodeBundle:Node')->find($id);
371
372
        $nodeTranslation = $node->getNodeTranslation($this->locale, true);
373
        $this->nodePublisher->unSchedulePublish($nodeTranslation);
374
375
        $this->addFlash(
376
            FlashTypes::SUCCESS,
377
            $this->get('translator')->trans('kuma_node.admin.unschedule.flash.success')
378
        );
379
380
        return $this->redirect($this->generateUrl('KunstmaanNodeBundle_nodes_edit', array('id' => $id)));
381
    }
382
383
    /**
384
     * @Route(
385
     *      "/{id}/delete",
386
     *      requirements={"id" = "\d+"},
387
     *      name="KunstmaanNodeBundle_nodes_delete"
388
     * )
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
     * @Method("POST")
454
     *
455
     * @param Request $request
456
     * @param int $id
457
     *
458
     * @return RedirectResponse
459
     * @throws AccessDeniedException
460
     */
461
    public function duplicateAction(Request $request, $id)
462
    {
463
        $this->init($request);
464
        /* @var Node $parentNode */
465
        $originalNode = $this->em->getRepository('KunstmaanNodeBundle:Node')
466
            ->find($id);
467
468
        // Check with Acl
469
        $this->denyAccessUnlessGranted(PermissionMap::PERMISSION_EDIT, $originalNode);
470
471
        $request = $this->get('request_stack')->getCurrentRequest();
472
473
        $originalNodeTranslations = $originalNode->getNodeTranslation($this->locale, true);
474
        $originalRef = $originalNodeTranslations->getPublicNodeVersion()->getRef($this->em);
475
        $newPage = $this->get('kunstmaan_admin.clone.helper')
476
            ->deepCloneAndSave($originalRef);
477
478
        //set the title
479
        $title = $request->get('title');
480
        if (is_string($title) && !empty($title)) {
481
            $newPage->setTitle($title);
482
        } else {
483
            $newPage->setTitle('New page');
484
        }
485
486
        //set the parent
487
        $parentNodeTranslation = $originalNode->getParent()->getNodeTranslation($this->locale, true);
488
        $parent = $parentNodeTranslation->getPublicNodeVersion()->getRef($this->em);
489
        $newPage->setParent($parent);
490
        $this->em->persist($newPage);
491
        $this->em->flush();
492
493
        /* @var Node $nodeNewPage */
494
        $nodeNewPage = $this->em->getRepository('KunstmaanNodeBundle:Node')->createNodeFor(
495
            $newPage,
496
            $this->locale,
497
            $this->user
498
        );
499
500
        $nodeTranslation = $nodeNewPage->getNodeTranslation($this->locale, true);
501
        if ($newPage->isStructureNode()) {
502
            $nodeTranslation->setSlug('');
503
            $this->em->persist($nodeTranslation);
504
        }
505
        $this->em->flush();
506
507
        $this->aclManager->updateNodeAcl($originalNode, $nodeNewPage);
508
509
        $this->addFlash(
510
            FlashTypes::SUCCESS,
511
            $this->get('translator')->trans('kuma_node.admin.duplicate.flash.success')
512
        );
513
514
        return $this->redirect(
515
            $this->generateUrl('KunstmaanNodeBundle_nodes_edit', array('id' => $nodeNewPage->getId()))
516
        );
517
    }
518
519
    /**
520
     * @Route(
521
     *      "/{id}/revert",
522
     *      requirements={"id" = "\d+"},
523
     *      defaults={"subaction" = "public"},
524
     *      name="KunstmaanNodeBundle_nodes_revert"
525
     * )
526
     * @Method("GET")
527
     *
528
     * @param Request $request
529
     * @param int $id The node id
530
     *
531
     * @return RedirectResponse
532
     * @throws AccessDeniedException
533
     * @throws InvalidArgumentException
534
     */
535
    public function revertAction(Request $request, $id)
536
    {
537
        $this->init($request);
538
        /* @var Node $node */
539
        $node = $this->em->getRepository('KunstmaanNodeBundle:Node')->find($id);
540
541
        $this->denyAccessUnlessGranted(PermissionMap::PERMISSION_EDIT, $node);
542
543
        $version = $request->get('version');
544
545
        if (empty($version) || !is_numeric($version)) {
546
            throw new InvalidArgumentException('No version was specified');
547
        }
548
549
        /* @var NodeVersionRepository $nodeVersionRepo */
550
        $nodeVersionRepo = $this->em->getRepository('KunstmaanNodeBundle:NodeVersion');
551
        /* @var NodeVersion $nodeVersion */
552
        $nodeVersion = $nodeVersionRepo->find($version);
553
554
        if (is_null($nodeVersion)) {
555
            throw new InvalidArgumentException('Version does not exist');
556
        }
557
558
        /* @var NodeTranslation $nodeTranslation */
559
        $nodeTranslation = $node->getNodeTranslation($this->locale, true);
560
        $page = $nodeVersion->getRef($this->em);
561
        /* @var HasNodeInterface $clonedPage */
562
        $clonedPage = $this->get('kunstmaan_admin.clone.helper')
563
            ->deepCloneAndSave($page);
564
        $newNodeVersion = $nodeVersionRepo->createNodeVersionFor(
565
            $clonedPage,
566
            $nodeTranslation,
567
            $this->user,
568
            $nodeVersion,
569
            'draft'
570
        );
571
572
        $nodeTranslation->setTitle($clonedPage->getTitle());
573
        $this->em->persist($nodeTranslation);
574
        $this->em->flush();
575
576
        $this->get('event_dispatcher')->dispatch(
577
            Events::REVERT,
578
            new RevertNodeAction(
579
                $node,
580
                $nodeTranslation,
581
                $newNodeVersion,
582
                $clonedPage,
583
                $nodeVersion,
584
                $page
585
            )
586
        );
587
588
        $this->addFlash(
589
            FlashTypes::SUCCESS,
590
            $this->get('translator')->trans('kuma_node.admin.revert.flash.success')
591
        );
592
593
        return $this->redirect(
594
            $this->generateUrl(
595
                'KunstmaanNodeBundle_nodes_edit',
596
                array(
597
                    'id' => $id,
598
                    'subaction' => 'draft'
599
                )
600
            )
601
        );
602
    }
603
604
    /**
605
     * @Route(
606
     *      "/{id}/add",
607
     *      requirements={"id" = "\d+"},
608
     *      name="KunstmaanNodeBundle_nodes_add"
609
     * )
610
     * @Method("POST")
611
     *
612
     * @param Request $request
613
     * @param int $id
614
     *
615
     * @return RedirectResponse
616
     * @throws AccessDeniedException
617
     * @throws InvalidArgumentException
618
     */
619
    public function addAction(Request $request, $id)
620
    {
621
        $this->init($request);
622
        /* @var Node $parentNode */
623
        $parentNode = $this->em->getRepository('KunstmaanNodeBundle:Node')->find($id);
624
625
        // Check with Acl
626
        $this->denyAccessUnlessGranted(PermissionMap::PERMISSION_EDIT, $parentNode);
627
628
        $parentNodeTranslation = $parentNode->getNodeTranslation($this->locale, true);
629
        $parentNodeVersion = $parentNodeTranslation->getPublicNodeVersion();
630
        $parentPage = $parentNodeVersion->getRef($this->em);
631
632
        $type = $this->validatePageType($request);
633
        $newPage = $this->createNewPage($request, $type);
634
        $newPage->setParent($parentPage);
635
636
        /* @var Node $nodeNewPage */
637
        $nodeNewPage = $this->em->getRepository('KunstmaanNodeBundle:Node')
638
            ->createNodeFor($newPage, $this->locale, $this->user);
639
        $nodeTranslation = $nodeNewPage->getNodeTranslation(
640
            $this->locale,
641
            true
642
        );
643
        $weight = $this->em->getRepository('KunstmaanNodeBundle:NodeTranslation')
644
                ->getMaxChildrenWeight($parentNode, $this->locale) + 1;
645
        $nodeTranslation->setWeight($weight);
646
647
        if ($newPage->isStructureNode()) {
648
            $nodeTranslation->setSlug('');
649
        }
650
651
        $this->em->persist($nodeTranslation);
0 ignored issues
show
It seems like $nodeTranslation defined by $nodeNewPage->getNodeTra...on($this->locale, true) on line 639 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...
652
        $this->em->flush();
653
654
        $this->aclManager->updateNodeAcl($parentNode, $nodeNewPage);
655
656
        $nodeVersion = $nodeTranslation->getPublicNodeVersion();
657
658
        $this->get('event_dispatcher')->dispatch(
659
            Events::ADD_NODE,
660
            new NodeEvent(
661
                $nodeNewPage, $nodeTranslation, $nodeVersion, $newPage
662
            )
663
        );
664
665
        return $this->redirect(
666
            $this->generateUrl(
667
                'KunstmaanNodeBundle_nodes_edit',
668
                array('id' => $nodeNewPage->getId())
669
            )
670
        );
671
    }
672
673
    /**
674
     * @Route("/add-homepage", name="KunstmaanNodeBundle_nodes_add_homepage")
675
     * @Method("POST")
676
     *
677
     * @return RedirectResponse
678
     * @throws AccessDeniedException
679
     * @throws InvalidArgumentException
680
     */
681
    public function addHomepageAction(Request $request)
682
    {
683
        $this->init($request);
684
685
        // Check with Acl
686
        $this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN');
687
688
        $type = $this->validatePageType($request);
689
690
        $newPage = $this->createNewPage($request, $type);
691
692
        /* @var Node $nodeNewPage */
693
        $nodeNewPage = $this->em->getRepository('KunstmaanNodeBundle:Node')
694
            ->createNodeFor($newPage, $this->locale, $this->user);
695
        $nodeTranslation = $nodeNewPage->getNodeTranslation(
696
            $this->locale,
697
            true
698
        );
699
        $this->em->flush();
700
701
        // Set default permissions
702
        $this->container->get('kunstmaan_node.acl_permission_creator_service')
703
            ->createPermission($nodeNewPage);
704
705
        $nodeVersion = $nodeTranslation->getPublicNodeVersion();
706
707
        $this->get('event_dispatcher')->dispatch(
708
            Events::ADD_NODE,
709
            new NodeEvent(
710
                $nodeNewPage, $nodeTranslation, $nodeVersion, $newPage
711
            )
712
        );
713
714
        return $this->redirect(
715
            $this->generateUrl(
716
                'KunstmaanNodeBundle_nodes_edit',
717
                array('id' => $nodeNewPage->getId())
718
            )
719
        );
720
    }
721
722
    /**
723
     * @Route("/reorder", name="KunstmaanNodeBundle_nodes_reorder")
724
     * @Method("POST")
725
     *
726
     * @param Request $request
727
     *
728
     * @return string
729
     * @throws AccessDeniedException
730
     */
731
    public function reorderAction(Request $request)
732
    {
733
        $this->init($request);
734
        $nodes = array();
735
        $nodeIds = $request->get('nodes');
736
        $changeParents = $request->get('parent');
737
738
        foreach ($nodeIds as $id) {
739
            /* @var Node $node */
740
            $node = $this->em->getRepository('KunstmaanNodeBundle:Node')->find($id);
741
            $this->denyAccessUnlessGranted(PermissionMap::PERMISSION_EDIT, $node);
742
            $nodes[] = $node;
743
        }
744
745
        $weight = 0;
746
        foreach ($nodes as $node) {
747
748
            $newParentId = isset($changeParents[$node->getId()]) ? $changeParents[$node->getId()] : null;
749
            if ($newParentId) {
750
                $parent = $this->em->getRepository('KunstmaanNodeBundle:Node')->find($newParentId);
751
                $this->denyAccessUnlessGranted(PermissionMap::PERMISSION_EDIT, $parent);
752
                $node->setParent($parent);
753
                $this->em->persist($node);
754
                $this->em->flush($node);
755
            }
756
757
758
            /* @var NodeTranslation $nodeTranslation */
759
            $nodeTranslation = $node->getNodeTranslation($this->locale, true);
760
761
            if ($nodeTranslation) {
762
                $nodeVersion = $nodeTranslation->getPublicNodeVersion();
763
                $page = $nodeVersion->getRef($this->em);
764
765
                $this->get('event_dispatcher')->dispatch(
766
                    Events::PRE_PERSIST,
767
                    new NodeEvent($node, $nodeTranslation, $nodeVersion, $page)
0 ignored issues
show
It seems like $page defined by $nodeVersion->getRef($this->em) on line 763 can be null; however, Kunstmaan\NodeBundle\Eve...odeEvent::__construct() 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...
768
                );
769
770
                $nodeTranslation->setWeight($weight);
771
                $this->em->persist($nodeTranslation);
772
                $this->em->flush($nodeTranslation);
773
774
                $this->get('event_dispatcher')->dispatch(
775
                    Events::POST_PERSIST,
776
                    new NodeEvent($node, $nodeTranslation, $nodeVersion, $page)
0 ignored issues
show
It seems like $page defined by $nodeVersion->getRef($this->em) on line 763 can be null; however, Kunstmaan\NodeBundle\Eve...odeEvent::__construct() 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...
777
                );
778
779
                $weight++;
780
            }
781
        }
782
783
        return new JsonResponse(
784
            array(
785
                'Success' => 'The node-translations for [' . $this->locale . '] have got new weight values'
786
            )
787
        );
788
    }
789
790
    /**
791
     * @Route(
792
     *      "/{id}/{subaction}",
793
     *      requirements={"id" = "\d+"},
794
     *      defaults={"subaction" = "public"},
795
     *      name="KunstmaanNodeBundle_nodes_edit"
796
     * )
797
     * @Template("@KunstmaanNode/NodeAdmin/edit.html.twig")
798
     * @Method({"GET", "POST"})
799
     *
800
     * @param Request $request
801
     * @param int $id The node id
802
     * @param string $subaction The subaction (draft|public)
803
     *
804
     * @return RedirectResponse|array
805
     * @throws AccessDeniedException
806
     */
807
    public function editAction(Request $request, $id, $subaction)
808
    {
809
        $this->init($request);
810
        /* @var Node $node */
811
        $node = $this->em->getRepository('KunstmaanNodeBundle:Node')->find($id);
812
813
        $this->denyAccessUnlessGranted(PermissionMap::PERMISSION_EDIT, $node);
814
815
        $tabPane = new TabPane(
816
            'todo',
817
            $request,
818
            $this->container->get('form.factory')
819
        );
820
821
        $nodeTranslation = $node->getNodeTranslation($this->locale, true);
822
        if (!$nodeTranslation) {
823
            return $this->renderNodeNotTranslatedPage($node);
824
        }
825
826
        $nodeVersion = $nodeTranslation->getPublicNodeVersion();
827
        $draftNodeVersion = $nodeTranslation->getDraftNodeVersion();
828
        $nodeVersionIsLocked = false;
829
830
        /* @var HasNodeInterface $page */
831
        $page = null;
832
        $draft = ($subaction == 'draft');
833
        $saveAsDraft = $request->get('saveasdraft');
834
        if ((!$draft && !empty($saveAsDraft)) || ($draft && is_null($draftNodeVersion))) {
835
            // Create a new draft version
836
            $draft = true;
837
            $subaction = "draft";
838
            $page = $nodeVersion->getRef($this->em);
839
            $nodeVersion = $this->createDraftVersion(
840
                $page,
841
                $nodeTranslation,
842
                $nodeVersion
843
            );
844
            $draftNodeVersion = $nodeVersion;
845
        } elseif ($draft) {
846
            $nodeVersion = $draftNodeVersion;
847
            $page = $nodeVersion->getRef($this->em);
848
        } else {
849
            if ($request->getMethod() == 'POST') {
850
                $nodeVersionIsLocked = $this->isNodeVersionLocked($nodeTranslation, true);
851
852
                //Check the version timeout and make a new nodeversion if the timeout is passed
853
                $thresholdDate = date(
854
                    "Y-m-d H:i:s",
855
                    time() - $this->getParameter(
856
                        "kunstmaan_node.version_timeout"
857
                    )
858
                );
859
                $updatedDate = date(
860
                    "Y-m-d H:i:s",
861
                    strtotime($nodeVersion->getUpdated()->format("Y-m-d H:i:s"))
862
                );
863 View Code Duplication
                if ($thresholdDate >= $updatedDate || $nodeVersionIsLocked) {
864
                    $page = $nodeVersion->getRef($this->em);
865
                    if ($nodeVersion == $nodeTranslation->getPublicNodeVersion()) {
866
                        $this->nodePublisher
867
                            ->createPublicVersion(
868
                                $page,
869
                                $nodeTranslation,
870
                                $nodeVersion,
871
                                $this->user
872
                            );
873
                    } else {
874
                        $this->createDraftVersion(
875
                            $page,
876
                            $nodeTranslation,
877
                            $nodeVersion
878
                        );
879
                    }
880
                }
881
            }
882
            $page = $nodeVersion->getRef($this->em);
883
        }
884
        $isStructureNode = $page->isStructureNode();
885
886
        $menubuilder = $this->get('kunstmaan_node.actions_menu_builder');
887
        $menubuilder->setActiveNodeVersion($nodeVersion);
888
        $menubuilder->setEditableNode(!$isStructureNode);
889
890
        // Building the form
891
        $propertiesWidget = new FormWidget();
892
        $propertiesWidget->addType('main', $page->getDefaultAdminType(), $page);
893
        $propertiesWidget->addType('node', $node->getDefaultAdminType(), $node);
894
        $tabPane->addTab(new Tab('kuma_node.tab.properties.title', $propertiesWidget));
895
896
        // Menu tab
897
        $menuWidget = new FormWidget();
898
        $menuWidget->addType(
899
            'menunodetranslation',
900
            NodeMenuTabTranslationAdminType::class,
901
            $nodeTranslation,
902
            ['slugable' => !$isStructureNode]
903
        );
904
        $menuWidget->addType('menunode', NodeMenuTabAdminType::class, $node, ['available_in_nav' => !$isStructureNode]);
905
        $tabPane->addTab(new Tab('kuma_node.tab.menu.title', $menuWidget));
906
907
        $this->get('event_dispatcher')->dispatch(
908
            Events::ADAPT_FORM,
909
            new AdaptFormEvent(
910
                $request,
911
                $tabPane,
912
                $page,
913
                $node,
914
                $nodeTranslation,
915
                $nodeVersion
916
            )
917
        );
918
919
        $tabPane->buildForm();
920
921
        if ($request->getMethod() == 'POST') {
922
            $tabPane->bindRequest($request);
923
924
            // Don't redirect to listing when coming from ajax request, needed for url chooser.
925
            if ($tabPane->isValid() && !$request->isXmlHttpRequest()) {
926
                $this->get('event_dispatcher')->dispatch(
927
                    Events::PRE_PERSIST,
928
                    new NodeEvent($node, $nodeTranslation, $nodeVersion, $page)
929
                );
930
931
                $nodeTranslation->setTitle($page->getTitle());
932
                if ($isStructureNode) {
933
                    $nodeTranslation->setSlug('');
934
                }
935
                $nodeVersion->setUpdated(new DateTime());
936
                if ($nodeVersion->getType() == 'public') {
937
                    $nodeTranslation->setUpdated($nodeVersion->getUpdated());
938
                }
939
                $this->em->persist($nodeTranslation);
940
                $this->em->persist($nodeVersion);
941
                $tabPane->persist($this->em);
942
                $this->em->flush();
943
944
                $this->get('event_dispatcher')->dispatch(
945
                    Events::POST_PERSIST,
946
                    new NodeEvent($node, $nodeTranslation, $nodeVersion, $page)
947
                );
948
949
                if ($nodeVersionIsLocked) {
950
                    $this->addFlash(
951
                        FlashTypes::SUCCESS,
952
                        $this->get('translator')->trans('kuma_node.admin.edit.flash.locked_success')
953
                    );
954
                } else {
955
                    if ($request->request->has('publishing') || $request->request->has('publish_later')) {
956
                        $this->nodePublisher->chooseHowToPublish($request, $nodeTranslation, $this->translator);
957
                    } elseif ($request->request->has('unpublishing') || $request->request->has('unpublish_later')) {
958
                        $this->nodePublisher->chooseHowToUnpublish($request, $nodeTranslation, $this->translator);
959
                    } else {
960
                        $this->addFlash(
961
                            FlashTypes::SUCCESS,
962
                            $this->get('translator')->trans('kuma_node.admin.edit.flash.success')
963
                        );
964
                    }
965
                }
966
967
                $params = [
968
                    'id' => $node->getId(),
969
                    'subaction' => $subaction,
970
                    'currenttab' => $tabPane->getActiveTab(),
971
                ];
972
                $params = array_merge(
973
                    $params,
974
                    $tabPane->getExtraParams($request)
975
                );
976
977
                return $this->redirect(
978
                    $this->generateUrl(
979
                        'KunstmaanNodeBundle_nodes_edit',
980
                        $params
981
                    )
982
                );
983
            }
984
        }
985
986
        $nodeVersions = $this->em->getRepository(
987
            'KunstmaanNodeBundle:NodeVersion'
988
        )->findBy(
989
            ['nodeTranslation' => $nodeTranslation],
990
            ['updated' => 'ASC']
991
        );
992
        $queuedNodeTranslationAction = $this->em->getRepository(
993
            'KunstmaanNodeBundle:QueuedNodeTranslationAction'
994
        )->findOneBy(['nodeTranslation' => $nodeTranslation]);
995
996
        return [
997
            'page' => $page,
998
            'entityname' => ClassLookup::getClass($page),
999
            'nodeVersions' => $nodeVersions,
1000
            'node' => $node,
1001
            'nodeTranslation' => $nodeTranslation,
1002
            'draft' => $draft,
1003
            'draftNodeVersion' => $draftNodeVersion,
1004
            'nodeVersion' => $nodeVersion,
1005
            'subaction' => $subaction,
1006
            'tabPane' => $tabPane,
1007
            'editmode' => true,
1008
            'queuedNodeTranslationAction' => $queuedNodeTranslationAction,
1009
            'nodeVersionLockCheck' => $this->container->getParameter('kunstmaan_node.lock_enabled'),
1010
            'nodeVersionLockInterval' => $this->container->getParameter('kunstmaan_node.lock_check_interval'),
1011
        ];
1012
    }
1013
1014
    /**
1015
     * @Route(
1016
     *      "checkNodeVersionLock/{id}/{public}",
1017
     *      requirements={"id" = "\d+", "public" = "(0|1)"},
1018
     *      name="KunstmaanNodeBundle_nodes_versionlock_check"
1019
     * )
1020
     * @param Request $request
1021
     * @param $id
1022
     * @return JsonResponse
1023
     */
1024
    public function checkNodeVersionLockAction(Request $request, $id, $public)
1025
    {
1026
        $nodeVersionIsLocked = false;
1027
        $message = '';
1028
        $this->init($request);
1029
1030
        /* @var Node $node */
1031
        $node = $this->em->getRepository('KunstmaanNodeBundle:Node')->find($id);
1032
1033
        try {
1034
            $this->checkPermission($node, PermissionMap::PERMISSION_EDIT);
1035
1036
            /** @var NodeVersionLockHelper $nodeVersionLockHelper */
1037
            $nodeVersionLockHelper = $this->get('kunstmaan_node.admin_node.node_version_lock_helper');
1038
            $nodeTranslation = $node->getNodeTranslation($this->locale, true);
1039
1040
            if ($nodeTranslation) {
1041
                $nodeVersionIsLocked = $nodeVersionLockHelper->isNodeVersionLocked($this->getUser(), $nodeTranslation, $public);
1042
1043
                if ($nodeVersionIsLocked) {
1044
                    $users = $nodeVersionLockHelper->getUsersWithNodeVersionLock($nodeTranslation, $public, $this->getUser());
1045
                    $message = $this->get('translator')->trans('kuma_node.admin.edit.flash.locked', array('%users%' => implode(', ', $users)));
1046
                }
1047
            }
1048
1049
        } catch (AccessDeniedException $ade) {}
1050
1051
        return new JsonResponse(['lock' => $nodeVersionIsLocked, 'message' => $message]);
1052
    }
1053
1054
    /**
1055
     * @param NodeTranslation $nodeTranslation
1056
     * @param bool $isPublic
1057
     * @return bool
1058
     */
1059
    private function isNodeVersionLocked(NodeTranslation $nodeTranslation, $isPublic)
1060
    {
1061
        if ($this->container->getParameter('kunstmaan_node.lock_enabled')) {
1062
            /** @var NodeVersionLockHelper $nodeVersionLockHelper */
1063
            $nodeVersionLockHelper = $this->get('kunstmaan_node.admin_node.node_version_lock_helper');
1064
            $nodeVersionIsLocked = $nodeVersionLockHelper->isNodeVersionLocked($this->getUser(), $nodeTranslation, $isPublic);
1065
            return $nodeVersionIsLocked;
1066
        }
1067
        return false;
1068
    }
1069
1070
    /**
1071
     * @param HasNodeInterface $page The page
1072
     * @param NodeTranslation $nodeTranslation The node translation
1073
     * @param NodeVersion $nodeVersion The node version
1074
     *
1075
     * @return NodeVersion
1076
     */
1077
    private function createDraftVersion(
1078
        HasNodeInterface $page,
1079
        NodeTranslation $nodeTranslation,
1080
        NodeVersion $nodeVersion
1081
    )
1082
    {
1083
        $publicPage = $this->get('kunstmaan_admin.clone.helper')
1084
            ->deepCloneAndSave($page);
1085
        /* @var NodeVersion $publicNodeVersion */
1086
1087
        $publicNodeVersion = $this->em->getRepository(
1088
            'KunstmaanNodeBundle:NodeVersion'
1089
        )->createNodeVersionFor(
1090
            $publicPage,
1091
            $nodeTranslation,
1092
            $this->user,
1093
            $nodeVersion->getOrigin(),
1094
            'public',
1095
            $nodeVersion->getCreated()
1096
        );
1097
1098
        $nodeTranslation->setPublicNodeVersion($publicNodeVersion);
1099
        $nodeVersion->setType('draft');
1100
        $nodeVersion->setOrigin($publicNodeVersion);
1101
        $nodeVersion->setCreated(new DateTime());
1102
1103
        $this->em->persist($nodeTranslation);
1104
        $this->em->persist($nodeVersion);
1105
        $this->em->flush();
1106
1107
        $this->get('event_dispatcher')->dispatch(
1108
            Events::CREATE_DRAFT_VERSION,
1109
            new NodeEvent(
1110
                $nodeTranslation->getNode(),
1111
                $nodeTranslation,
1112
                $nodeVersion,
1113
                $page
1114
            )
1115
        );
1116
1117
        return $nodeVersion;
1118
    }
1119
1120
    /**
1121
     * @param Node $node The node
1122
     * @param string $permission The permission to check for
1123
     *
1124
     * @throws AccessDeniedException
1125
     */
1126
    private function checkPermission(Node $node, $permission)
1127
    {
1128
        if (false === $this->authorizationChecker->isGranted($permission, $node)) {
1129
            throw new AccessDeniedException();
1130
        }
1131
    }
1132
1133
    /**
1134
     * @param EntityManager   $em       The Entity Manager
1135
     * @param BaseUser        $user     The user who deletes the children
1136
     * @param string          $locale   The locale that was used
1137
     * @param ArrayCollection $children The children array
1138
     */
1139
    private function deleteNodeChildren(
1140
        EntityManager $em,
1141
        BaseUser $user,
1142
        $locale,
1143
        ArrayCollection $children
1144
    )
1145
    {
1146
        /* @var Node $childNode */
1147
        foreach ($children as $childNode) {
1148
            $childNodeTranslation = $childNode->getNodeTranslation(
1149
                $this->locale,
1150
                true
1151
            );
1152
1153
            $childNodeVersion = $childNodeTranslation->getPublicNodeVersion();
1154
            $childNodePage = $childNodeVersion->getRef($this->em);
1155
1156
            $this->get('event_dispatcher')->dispatch(
1157
                Events::PRE_DELETE,
1158
                new NodeEvent(
1159
                    $childNode,
1160
                    $childNodeTranslation,
1161
                    $childNodeVersion,
1162
                    $childNodePage
1163
                )
1164
            );
1165
1166
            $childNode->setDeleted(true);
1167
            $this->em->persist($childNode);
1168
1169
            $children2 = $childNode->getChildren();
1170
            $this->deleteNodeChildren($em, $user, $locale, $children2);
1171
1172
            $this->get('event_dispatcher')->dispatch(
1173
                Events::POST_DELETE,
1174
                new NodeEvent(
1175
                    $childNode,
1176
                    $childNodeTranslation,
1177
                    $childNodeVersion,
1178
                    $childNodePage
1179
                )
1180
            );
1181
        }
1182
    }
1183
1184
    /**
1185
     * @param Request $request
1186
     * @param string $type
1187
     *
1188
     * @return HasNodeInterface
1189
     */
1190
    private function createNewPage(Request $request, $type)
1191
    {
1192
        /* @var HasNodeInterface $newPage */
1193
        $newPage = new $type();
1194
1195
        $title = $request->get('title');
1196
        if (is_string($title) && !empty($title)) {
1197
            $newPage->setTitle($title);
1198
        } else {
1199
            $newPage->setTitle($this->get('translator')->trans('kuma_node.admin.new_page.title.default'));
1200
        }
1201
        $this->em->persist($newPage);
1202
        $this->em->flush();
1203
1204
        return $newPage;
1205
    }
1206
1207
    /**
1208
     * @param Request $request
1209
     *
1210
     * @return string
1211
     * @throw InvalidArgumentException
1212
     */
1213
    private function validatePageType($request)
1214
    {
1215
        $type = $request->get('type');
1216
1217
        if (empty($type)) {
1218
            throw new InvalidArgumentException(
1219
                'Please specify a type of page you want to create'
1220
            );
1221
        }
1222
1223
        return $type;
1224
    }
1225
1226
    /**
1227
     * @param Node $node
1228
     * @return \Symfony\Component\HttpFoundation\Response
1229
     */
1230
    private function renderNodeNotTranslatedPage(Node $node)
1231
    {
1232
        //try to find a parent node with the correct translation, if there is none allow copy.
1233
        //if there is a parent but it doesn't have the language to copy to don't allow it
1234
        $parentNode = $node->getParent();
1235
        if ($parentNode) {
1236
            $parentNodeTranslation = $parentNode->getNodeTranslation(
1237
                $this->locale,
1238
                true
1239
            );
1240
            $parentsAreOk = false;
1241
1242
            if ($parentNodeTranslation) {
1243
                $parentsAreOk = $this->em->getRepository(
1244
                    'KunstmaanNodeBundle:NodeTranslation'
1245
                )->hasParentNodeTranslationsForLanguage(
1246
                    $node->getParent()->getNodeTranslation(
1247
                        $this->locale,
1248
                        true
1249
                    ),
1250
                    $this->locale
1251
                );
1252
            }
1253
        } else {
1254
            $parentsAreOk = true;
1255
        }
1256
1257
        return $this->render(
1258
            'KunstmaanNodeBundle:NodeAdmin:pagenottranslated.html.twig',
1259
            array(
1260
                'node' => $node,
1261
                'nodeTranslations' => $node->getNodeTranslations(
1262
                    true
1263
                ),
1264
                'copyfromotherlanguages' => $parentsAreOk
1265
            )
1266
        );
1267
    }
1268
}
1269