Browse Source

remove update action due to the last Symfony2 changes (forwarded action parameters are now urlencoded so cannot use object anymore as paramter value)

Thomas 14 years ago
parent
commit
78b768bc8c
2 changed files with 53 additions and 78 deletions
  1. 45 77
      Controller/CRUDController.php
  2. 8 1
      Resources/views/CRUD/base_edit.html.twig

+ 45 - 77
Controller/CRUDController.php

@@ -84,6 +84,10 @@ class CRUDController extends Controller
     {
         $adminCode = $this->container->get('request')->get('_sonata_admin');
 
+        if (!$adminCode) {
+            throw new \RuntimeException(sprintf('There is no `_sonata_admin` defined for the controller `%s` and the current route `%s`', get_class($this), $this->container->get('request')->get('_route')));
+        }
+
         $this->admin = $this->container->get('sonata_admin.admin.pool')->getAdminByAdminCode($adminCode);
 
         if (!$this->admin) {
@@ -174,97 +178,43 @@ class CRUDController extends Controller
      */
     public function editAction($id)
     {
-        if ($id instanceof Form) {
-            $object = $id->getData();
-            $form   = $id;
+        $object = $this->admin->getObject($this->get('request')->get($this->admin->getIdParameter()));
 
-            // todo : refactor the Form Creation
-            $this->admin->getForm($object);
-        } else {
-            $id = $this->get('request')->get($this->admin->getIdParameter());
-            $object = $this->admin->getObject($id);
-
-            if (!$object) {
-                throw new NotFoundHttpException(sprintf('unable to find the object with id : %s', $id));
-            }
-
-            $form = $this->admin->getForm($object);
+        if (!$object) {
+            throw new NotFoundHttpException(sprintf('unable to find the object with id : %s', $id));
         }
 
         $this->admin->setSubject($object);
 
-        return $this->render($this->admin->getEditTemplate(), array(
-            'form'           => $form,
-            'object'         => $object,
-            'fields'         => $this->admin->getFormFieldDescriptions(),
-            'form_groups'    => $this->admin->getFormGroups(),
-            'admin'          => $this->admin,
-            'base_template'  => $this->getBaseTemplate(),
-            'side_menu'      => $this->getSideMenu('edit'),
-            'breadcrumbs'    => $this->getBreadcrumbs('edit'),
-        ));
-    }
-
-    /**
-     * return the Response object associated to the update action
-     *
-     * @return \Symfony\Component\HttpFoundation\Response
-     */
-    public function updateAction()
-    {
-        if ($this->get('request')->getMethod() != 'POST') {
-           throw new \RuntimeException('invalid request type, POST expected');
-        }
-
-        $id = $this->get('request')->get($this->admin->getIdParameter());
-
-        if (is_numeric($id)) {
-            $object = $this->admin->getObject($id);
-
-            if (!$object) {
-                throw new NotFoundHttpException(sprintf('unable to find the object with id : %s', $id));
-            }
-
-            $action = 'edit';
-        } else {
-            $object = $this->admin->getNewInstance();
-
-            $action = 'create';
-        }
-
-        $this->admin->setSubject($object);
-        
         $form = $this->admin->getForm($object);
 
-        $form->bind($this->get('request'));
+        if ($this->get('request')->getMethod() == 'POST') {
+            $form->bind($this->get('request'));
 
-        if ($form->isValid()) {
-
-            if ($action == 'create') {
-                $this->admin->prePersist($object);
-            } else {
+            if ($form->isValid()) {
                 $this->admin->preUpdate($object);
-            }
-
-            $this->admin->getModelManager()->persist($object);
-            $this->admin->getModelManager()->flush($object);
-
-            if ($action == 'create') {
-                $this->admin->postPersist($object);
-            } else {
+                $this->admin->getModelManager()->persist($object);
+                $this->admin->getModelManager()->flush($object);
                 $this->admin->postUpdate($object);
-            }
 
-            if ($this->isXmlHttpRequest()) {
-                return $this->renderJson(array('result' => 'ok', 'objectId' => $object->getId()));
-            }
+                if ($this->isXmlHttpRequest()) {
+                   return $this->renderJson(array('result' => 'ok', 'objectId' => $object->getId()));
+                }
 
-            // redirect to edit mode
-            return $this->redirectTo($object);
+                // redirect to edit mode
+                return $this->redirectTo($object);
+            }
         }
 
-        return $this->forward(sprintf('%s:%s', $this->admin->getBaseControllerName(), $action), array(
-            'id' => $form
+        return $this->render($this->admin->getEditTemplate(), array(
+            'form'           => $form,
+            'object'         => $object,
+            'fields'         => $this->admin->getFormFieldDescriptions(),
+            'form_groups'    => $this->admin->getFormGroups(),
+            'admin'          => $this->admin,
+            'base_template'  => $this->getBaseTemplate(),
+            'side_menu'      => $this->getSideMenu('edit'),
+            'breadcrumbs'    => $this->getBreadcrumbs('edit'),
         ));
     }
 
@@ -340,6 +290,24 @@ class CRUDController extends Controller
 
         $this->admin->setSubject($object);
 
+        if ($this->get('request')->getMethod() == 'POST') {
+            $form->bind($this->get('request'));
+
+            if ($form->isValid()) {
+                $this->admin->prePersist($object);
+                $this->admin->getModelManager()->persist($object);
+                $this->admin->getModelManager()->flush($object);
+                $this->admin->postPersist($object);
+
+                if ($this->isXmlHttpRequest()) {
+                   return $this->renderJson(array('result' => 'ok', 'objectId' => $object->getId()));
+                }
+
+                // redirect to edit mode
+                return $this->redirectTo($object);
+            }
+        }
+
         return $this->render($this->admin->getEditTemplate(), array(
             'form'          => $form,
             'object'        => $object,

+ 8 - 1
Resources/views/CRUD/base_edit.html.twig

@@ -23,7 +23,14 @@ file that was distributed with this source code.
 {% block side_menu %}{% if side_menu %}{{ side_menu.render|raw }}{% endif %}{% endblock %}
 
 {% block form %}
-    <form action="{{ admin.generateUrl('update', {'id': object.id, 'uniqid': admin.uniqid}) }}"{% if form.isMultipart %} enctype="multipart/form-data"{% endif %} method="POST">
+
+    {% if object.id > 0 %}
+        {% set url = 'edit' %}
+    {% else %}
+        {% set url = 'create' %}
+    {% endif %}
+
+    <form action="{{ admin.generateUrl(url, {'id': object.id, 'uniqid': admin.uniqid}) }}"{% if form.isMultipart %} enctype="multipart/form-data"{% endif %} method="POST">
 
         {{ form_hidden(form) }}