/** * Creates and processes the form to add a new Entity * * @param AbstractAdminListConfigurator $configurator The adminlist configurator * @param string $type The type to add * @param null|Request $request * * @throws AccessDeniedHttpException * * @return array */ protected function doAddAction(AbstractAdminListConfigurator $configurator, $type = null, Request $request) { if (!$configurator->canAdd()) { throw new AccessDeniedHttpException('You do not have sufficient rights to access this page.'); } /* @var EntityManager $em */ $em = $this->getEntityManager(); $repo = $em->getRepository($configurator->getRepositoryName()); $entityName = null; if (isset($type)) { $entityName = $type; } else { $entityName = $configurator->getRepositoryName(); } $classMetaData = $em->getClassMetadata($entityName); // Creates a new instance of the mapped class, without invoking the constructor. $classname = $classMetaData->getName(); $helper = new $classname(); $helper = $configurator->decorateNewEntity($helper); $formType = $configurator->getAdminType($helper); if (!is_object($formType) && is_string($formType)) { $formType = $this->container->get($formType); } $formFqn = get_class($formType); $event = new AdaptSimpleFormEvent($request, $formFqn, $helper, $configurator->getAdminTypeOptions()); $event = $this->container->get('event_dispatcher')->dispatch(Events::ADAPT_SIMPLE_FORM, $event); $tabPane = $event->getTabPane(); $form = $this->createForm($formFqn, $helper, $configurator->getAdminTypeOptions()); if ($request->isMethod('POST')) { if ($tabPane) { $tabPane->bindRequest($request); $form = $tabPane->getForm(); } else { $form->submit($request); } if ($form->isValid()) { $this->container->get('event_dispatcher')->dispatch(AdminListEvents::PRE_ADD, new AdminListEvent($helper)); // Check if Sortable interface is implemented if ($configurator instanceof SortableInterface) { $sort = $configurator->getSortableField(); $weight = $this->getMaxSortableField($repo, $sort); $setter = "set" . ucfirst($sort); $helper->{$setter}($weight + 1); } $em->persist($helper); $em->flush(); $this->container->get('event_dispatcher')->dispatch(AdminListEvents::POST_ADD, new AdminListEvent($helper)); $indexUrl = $configurator->getIndexUrl(); return new RedirectResponse($this->generateUrl($indexUrl['path'], isset($indexUrl['params']) ? $indexUrl['params'] : array())); } } $params = array('form' => $form->createView(), 'adminlistconfigurator' => $configurator); if ($tabPane) { $params = array_merge($params, array('tabPane' => $tabPane)); } return new Response($this->renderView($configurator->getAddTemplate(), $params)); }
/** * Creates and processes the form to add a new Entity * * @param AbstractAdminListConfigurator $configurator The adminlist configurator * @param string $type The type to add * @param null|Request $request * * @throws AccessDeniedHttpException * * @return array */ protected function doAddAction(AbstractAdminListConfigurator $configurator, $type = null, Request $request = null) { if (!$configurator->canAdd()) { throw new AccessDeniedHttpException('You do not have sufficient rights to access this page.'); } /* @var EntityManager $em */ $em = $this->getEntityManager(); if (is_null($request)) { $request = $this->getRequest(); } $entityName = null; if (isset($type)) { $entityName = $type; } else { $entityName = $configurator->getRepositoryName(); } $classMetaData = $em->getClassMetadata($entityName); // Creates a new instance of the mapped class, without invoking the constructor. $classname = $classMetaData->getName(); $helper = new $classname(); $helper = $configurator->decorateNewEntity($helper); $form = $this->createForm($configurator->getAdminType($helper), $helper); if ($request->isMethod('POST')) { $form->submit($request); if ($form->isValid()) { $em->persist($helper); $em->flush(); $indexUrl = $configurator->getIndexUrl(); return new RedirectResponse($this->generateUrl($indexUrl['path'], isset($indexUrl['params']) ? $indexUrl['params'] : array())); } } return new Response($this->renderView($configurator->getAddTemplate(), array('form' => $form->createView(), 'adminlistconfigurator' => $configurator))); }