/**
  * @param AdaptFormEvent $event
  */
 public function adaptForm(AdaptFormEvent $event)
 {
     if ($event->getPage() instanceof HasNodeInterface) {
         /* @var Seo $seo */
         $seo = $this->em->getRepository('KunstmaanSeoBundle:Seo')->findOrCreateFor($event->getPage());
         $seoWidget = new FormWidget();
         $seoWidget->addType('seo', new SeoType(), $seo);
         $event->getTabPane()->addTab(new Tab('SEO', $seoWidget));
         $socialWidget = new FormWidget();
         $socialWidget->addType('social', new SocialType(), $seo);
         $event->getTabPane()->addTab(new Tab('Social', $socialWidget));
     }
 }
 /**
  * @param AdaptFormEvent $event
  */
 public function adaptForm(AdaptFormEvent $event)
 {
     if ($event->getPage() instanceof HasNodeInterface && !$event->getPage()->isStructureNode()) {
         /* @var Seo $seo */
         $seo = $this->em->getRepository('KunstmaanSeoBundle:Seo')->findOrCreateFor($event->getPage());
         $seoWidget = new FormWidget();
         $seoWidget->addType('seo', new SeoType(), $seo);
         $event->getTabPane()->addTab(new Tab('seo.tab.seo.title', $seoWidget));
         $socialWidget = new FormWidget();
         $socialWidget->addType('social', new SocialType(), $seo);
         $socialWidget->setTemplate('KunstmaanSeoBundle:Admin\\Social:social.html.twig');
         $event->getTabPane()->addTab(new Tab('seo.tab.social.title', $socialWidget));
     }
 }
 /**
  * @param int    $id        The node id
  * @param string $subaction The subaction (draft|public)
  *
  * @throws AccessDeniedException
  * @Route("/{id}/{subaction}", requirements={"_method" = "GET|POST", "id" = "\d+"}, defaults={"subaction" = "public"}, name="KunstmaanNodeBundle_nodes_edit")
  * @Template()
  *
  * @return RedirectResponse|array
  */
 public function editAction($id, $subaction)
 {
     $this->init();
     /* @var Node $node */
     $node = $this->em->getRepository('KunstmaanNodeBundle:Node')->find($id);
     $this->checkPermission($node, PermissionMap::PERMISSION_EDIT);
     $request = $this->getRequest();
     $tabPane = new TabPane('todo', $request, $this->container->get('form.factory'));
     $nodeTranslation = $node->getNodeTranslation($this->locale, true);
     if (!$nodeTranslation) {
         //try to find a parent node with the correct translation, if there is none allow copy.
         //if there is a parent but it doesn't have the language to copy to don't allow it
         $parentNode = $node->getParent();
         if ($parentNode) {
             $parentNodeTranslation = $parentNode->getNodeTranslation($this->locale, true);
             $parentsAreOk = false;
             if ($parentNodeTranslation) {
                 $parentsAreOk = $this->em->getRepository('KunstmaanNodeBundle:NodeTranslation')->hasParentNodeTranslationsForLanguage($node->getParent()->getNodeTranslation($this->locale, true), $this->locale);
             }
         } else {
             $parentsAreOk = true;
         }
         return $this->render('KunstmaanNodeBundle:NodeAdmin:pagenottranslated.html.twig', array('node' => $node, 'nodeTranslations' => $node->getNodeTranslations(true), 'copyfromotherlanguages' => $parentsAreOk));
     }
     $nodeVersion = $nodeTranslation->getPublicNodeVersion();
     $draftNodeVersion = $nodeTranslation->getNodeVersion('draft');
     /* @var HasNodeInterface $page */
     $page = null;
     $draft = $subaction == 'draft';
     $saveAsDraft = $request->get('saveasdraft');
     if (!$draft && !empty($saveAsDraft) || $draft && is_null($draftNodeVersion)) {
         // Create a new draft version
         $draft = true;
         $subaction = "draft";
         $page = $nodeVersion->getRef($this->em);
         $nodeVersion = $this->createDraftVersion($page, $nodeTranslation, $nodeVersion);
         $draftNodeVersion = $nodeVersion;
     } elseif ($draft) {
         $nodeVersion = $draftNodeVersion;
         $page = $nodeVersion->getRef($this->em);
     } else {
         if ($request->getMethod() == 'POST') {
             //Check the version timeout and make a new nodeversion if the timeout is passed
             $thresholdDate = date("Y-m-d H:i:s", time() - $this->container->getParameter("kunstmaan_node.version_timeout"));
             $updatedDate = date("Y-m-d H:i:s", strtotime($nodeVersion->getUpdated()->format("Y-m-d H:i:s")));
             if ($thresholdDate >= $updatedDate) {
                 $page = $nodeVersion->getRef($this->em);
                 if ($nodeVersion == $nodeTranslation->getPublicNodeVersion()) {
                     $this->get('kunstmaan_node.admin_node.publisher')->createPublicVersion($page, $nodeTranslation, $nodeVersion, $this->user);
                 } else {
                     $this->createDraftVersion($page, $nodeTranslation, $nodeVersion);
                 }
             }
         }
         $page = $nodeVersion->getRef($this->em);
     }
     $isStructureNode = $page->isStructureNode();
     $menubuilder = $this->get('kunstmaan_node.actions_menu_builder');
     $menubuilder->setActiveNodeVersion($nodeVersion);
     $menubuilder->setEditableNode(!$isStructureNode);
     // Building the form
     $propertiesWidget = new FormWidget();
     $pageAdminType = $page->getDefaultAdminType();
     if (!is_object($pageAdminType) && is_string($pageAdminType)) {
         $pageAdminType = $this->container->get($pageAdminType);
     }
     $propertiesWidget->addType('main', $pageAdminType, $page);
     $nodeAdminType = $node->getDefaultAdminType();
     if (!is_object($nodeAdminType) && is_string($nodeAdminType)) {
         $nodeAdminType = $this->container->get($nodeAdminType);
     }
     $propertiesWidget->addType('node', $nodeAdminType, $node);
     $tabPane->addTab(new Tab('Properties', $propertiesWidget));
     // Menu tab
     if (!$isStructureNode) {
         $menuWidget = new FormWidget();
         $menuWidget->addType('menunodetranslation', new NodeMenuTabTranslationAdminType(), $nodeTranslation);
         $menuWidget->addType('menunode', new NodeMenuTabAdminType(), $node);
         $tabPane->addTab(new Tab('Menu', $menuWidget));
         $this->get('event_dispatcher')->dispatch(Events::ADAPT_FORM, new AdaptFormEvent($request, $tabPane, $page, $node, $nodeTranslation, $nodeVersion));
     }
     $tabPane->buildForm();
     if ($request->getMethod() == 'POST') {
         $tabPane->bindRequest($request);
         if ($tabPane->isValid()) {
             $this->get('event_dispatcher')->dispatch(Events::PRE_PERSIST, new NodeEvent($node, $nodeTranslation, $nodeVersion, $page));
             $nodeTranslation->setTitle($page->getTitle());
             if ($isStructureNode) {
                 $nodeTranslation->setSlug('');
             }
             $nodeVersion->setUpdated(new DateTime());
             if ($nodeVersion->getType() == 'public') {
                 $nodeTranslation->setUpdated($nodeVersion->getUpdated());
             }
             $this->em->persist($nodeTranslation);
             $this->em->persist($nodeVersion);
             $tabPane->persist($this->em);
             $this->em->flush();
             $this->get('event_dispatcher')->dispatch(Events::POST_PERSIST, new NodeEvent($node, $nodeTranslation, $nodeVersion, $page));
             $this->get('session')->getFlashBag()->add('success', 'The page has been edited');
             $params = array('id' => $node->getId(), 'subaction' => $subaction, 'currenttab' => $tabPane->getActiveTab());
             $params = array_merge($params, $tabPane->getExtraParams($request));
             return $this->redirect($this->generateUrl('KunstmaanNodeBundle_nodes_edit', $params));
         }
     }
     $nodeVersions = $this->em->getRepository('KunstmaanNodeBundle:NodeVersion')->findBy(array('nodeTranslation' => $nodeTranslation), array('updated' => 'ASC'));
     $queuedNodeTranslationAction = $this->em->getRepository('KunstmaanNodeBundle:QueuedNodeTranslationAction')->findOneBy(array('nodeTranslation' => $nodeTranslation));
     return array('page' => $page, 'entityname' => ClassLookup::getClass($page), 'nodeVersions' => $nodeVersions, 'node' => $node, 'nodeTranslation' => $nodeTranslation, 'draft' => $draft, 'draftNodeVersion' => $draftNodeVersion, 'subaction' => $subaction, 'tabPane' => $tabPane, 'editmode' => true, 'queuedNodeTranslationAction' => $queuedNodeTranslationAction);
 }