Exemplo n.º 1
0
 /**
  * Add a menu item
  *
  * @param Entity\Menu $menu
  *
  * @Route(
  *     "/{id}/add/{type}",
  *     name     = "zym_menus_item_add",
  *     defaults = {
  *         "type" = null
  *     }
  * )
  * @ParamConverter("menu", class="ZymMenuBundle:Menu")
  * @Template()
  *
  * @SecureParam(name="menu", permissions="EDIT")
  */
 public function addMenuItemAction(Entity\Menu $menu, $type = null)
 {
     $securityContext = $this->get('security.context');
     // check for create access
     if (!$securityContext->isGranted('CREATE', new ObjectIdentity('class', 'Zym\\Bundle\\MenuBundle\\Entity\\MenuItem'))) {
         throw new \Symfony\Component\Security\Core\Exception\AccessDeniedException();
     }
     if ($type) {
         switch ($type) {
             case 'routed':
                 $menuItem = new Entity\MenuItem\RoutedMenuItem(null, $this->get('knp_menu.factory'), $menu);
                 $menuItem->setRouter($this->get('router'));
                 $form = $this->createForm(new Form\MenuItem\RoutedMenuItemType(), $menuItem);
                 break;
             case 'static':
                 $menuItem = new Entity\MenuItem\StaticMenuItem(null, $this->get('knp_menu.factory'), $menu);
                 $form = $this->createForm(new Form\MenuItem\StaticMenuItemType(), $menuItem);
                 break;
             case 'section':
                 $menuItem = new Entity\MenuItem\SectionMenuItem(null, $this->get('knp_menu.factory'), $menu);
                 $form = $this->createForm(new Form\MenuItem\SectionMenuItemType(), $menuItem);
                 break;
             default:
                 throw $this->createNotFoundException('Invalid MenuItem Type');
         }
         $request = $this->get('request');
         if ($request->getMethod() == 'POST') {
             $form->bind($request);
             if ($form->isValid()) {
                 $menuItemManager = $this->get('zym_menu.menu_item_manager');
                 $menuItemManager->createMenuItem($menuItem);
                 return $this->redirect($this->generateUrl('zym_menus_show', array('id' => $menu->getName())));
             }
         }
         return array('menu' => $menu, 'form' => $form->createView(), 'type' => $type);
     }
 }