/** * @param Entity $entity * @param $type * * @return \Symfony\Component\Form\Form */ private function createActionForm($entity, $type, $entity_locale = NULL) { $ah = $this->get('itf.admin_helper'); $type_class = $entity->getFormTypeClass(); switch ($type) { default: case 'add': $method = 'POST'; $action = $this->generateUrl('admin_create', array('bundle' => $ah->getBundleNameShort(), 'entity' => $entity->getName('strtolower'))); $submit_label = 'Create'; break; case 'edit': $method = 'PUT'; $infoArray = array('bundle' => $ah->getBundleNameShort(), 'entity' => $entity->getName('strtolower'), 'id' => $entity->getEntity()->getId()); if (!empty($entity_locale)) { $infoArray['locale'] = $entity_locale; } $action = $this->generateUrl('admin_update', $infoArray); $submit_label = 'Update'; break; } // create form $form = $this->createForm(new $type_class($this->container), $entity->getEntity(), array('action' => $action, 'method' => $method, 'attr' => array('type' => $type))); // if tree add root /*if ($this->get('itf.admin.annotation_reader')->isGedmoTree($entity->getEntity())) { $repo = $ah->getEntityRepositoryReference($entity->getName()); $this->get('itf.admin.gedmo.tree.form')->handleFormNew($form, $repo, $entity->getFQClassName()); }*/ $form->add('submit_stay', 'submit', array('label' => $submit_label, 'attr' => array('class' => 'btn btn-success'))); $form->add('submit', 'submit', array('label' => $submit_label . ' & back', 'attr' => array('class' => 'btn btn-success'))); return $form; }
/** * Edits an existing entity. * * @param $bundle * @param Request $request * @param $entity * @param $id * * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response */ public function updateAction($bundle, Request $request, $entity, $id) { // set locale $entity_locale = $this->getDefaultLocale(); if (strlen($request->get('locale')) > 0) { $entity_locale = $request->get('locale'); } $ah = $this->get('itf.admin_helper'); $ah->setBundle($bundle); // setup reponse $response = ControllerResponse::create($this)->setBundle($bundle)->setEntityName($entity)->setTemplate('ITFAdminBundle:Admin:edit.html.twig'); $em = $this->getDoctrine()->getManager(); $entity = $em->getRepository($ah->getEntityRepository($entity))->find($id); // locale $is_translatable = false; if (method_exists($entity, 'setTranslatableLocale')) { $entity->setTranslatableLocale($entity_locale); $em->refresh($entity); $is_translatable = true; } $entity = new \ITF\AdminBundle\Admin\Entity($entity, $this); $response->setEntity($entity->getEntity())->setEntityAssoc($entity->getEntityAssociations())->setEntityTranslatable($is_translatable); if (!$entity) { throw $this->createNotFoundException(sprintf('Unable to find %e entity.', $entity->getName())); } $deleteForm = $this->createDeleteForm($entity->getName(), $id); $form = $this->createActionForm($entity, 'edit'); $form->handleRequest($request); if ($form->isValid()) { $this->setLogging(true); $em->persist($entity->getEntity()); $em->flush(); $this->setLogging(false); // clear $em->clear(); $em->getConnection()->getConfiguration()->setSQLLogger(null); gc_collect_cycles(); // to edit if ($form->get('submit_stay')->isClicked()) { return $this->redirect($this->generateUrl('admin_edit', array('id' => $id, 'entity' => $entity->getName('strtolower'), 'bundle' => $ah->getBundleNameShort(), 'locale' => $entity_locale))); } // to list return $this->redirect($this->generateUrl('admin_list', array('entity' => $entity->getName('strtolower'), 'bundle' => $ah->getBundleNameShort()))); } $response->setForm($form->createView())->setDeleteForm($deleteForm->createView()); return $response->createResponse(); }