Browse Source

Moving all CRUD functions in the BaseControllers, now the RoleController is much lighter.

Julio Montoya 11 years ago
parent
commit
3d722a04b9

+ 13 - 7
main/template/default/default_actions/settings.tpl

@@ -2,15 +2,21 @@
     <a href="{{ url(links.create_link) }}">
         Add
     </a>
-    <hr />
+    <table class="table">
     {% for item in items %}
-        <a href="{{ url(links.read_link, { id: item.id }) }}">
-         {{ item.name }}
-        </a>
-        <a class="btn" href="{{ url(links.update_link, { id: item.id }) }}"> Edit</a>
-        <a class="btn" href="{{ url(links.delete_link, { id: item.id }) }}"> Delete</a>
-        <br />
+        <tr>
+            <td>
+                <a href="{{ url(links.read_link, { id: item.id }) }}">
+                {{ item.name }}
+                </a>
+            </td>
+            <td>
+                <a class="btn" href="{{ url(links.update_link, { id: item.id }) }}"> Edit</a>
+                <a class="btn" href="{{ url(links.delete_link, { id: item.id }) }}"> Delete</a>
+            </td>
+        </tr>
     {% endfor %}
+    </table>
 {% endmacro %}
 
 {% macro add(form, links) %}

+ 27 - 89
src/ChamiloLMS/Controller/Admin/Administrator/QuestionScore.php

@@ -20,112 +20,36 @@ use ChamiloLMS\Form\QuestionScoreType;
  */
 class QuestionScore extends CommonController
 {
-    /**
-     *
-     * @param Application $app
-     * @Route("/")
-     * @Method({"GET"})
-     */
     public function indexAction()
     {
-        $items = parent::listAction('array');
-        $template = $this->get('template');
-        $template->assign('items', $items);
-        $template->assign('links', $this->generateLinks());
-        $response = $template->render_template('admin/administrator/question_score/list.tpl');
-        return new Response($response, 200, array());
+        return parent::indexAction();
     }
 
-    /**
-     *
-     * @Route("/{id}", requirements={"id" = "\d+"}, defaults={"foo" = "bar"})
-     * @Method({"GET"})
-     */
     public function readAction($id)
     {
-        $template = $this->get('template');
-        $template->assign('links', $this->generateLinks());
-        $item = parent::getEntity($id);
-        $subItems = $item->getItems();
-
-        $template->assign('item', $item);
-        $template->assign('subitems', $subItems);
-        $response = $template->render_template('admin/administrator/question_score/read.tpl');
-        return new Response($response, 200, array());
+        return parent::readAction($id);
     }
 
-    public function editAction($id)
+    public function addAction()
     {
-        $roleRepo = $this->getRepository();
-        $request = $this->getRequest();
-
-        $role = $roleRepo->findOneById($id);
-
-        if ($role) {
-            $form = $this->get('form.factory')->create(new QuestionScoreType(), $role);
-
-            if ($request->getMethod() == 'POST') {
-                $form->bind($this->getRequest());
-
-                if ($form->isValid()) {
-                    $role = $form->getData();
-                    parent::updateAction($role);
-                    $this->get('session')->getFlashBag()->add('success', "Updated");
-                    $url = $this->get('url_generator')->generate('admin_administrator_question_scores');
-                    return $this->redirect($url);
-                }
-            }
-
-            $template = $this->get('template');
-            $template->assign('item', $role);
-            $template->assign('form', $form->createView());
-            $template->assign('links', $this->generateLinks());
-            $response = $template->render_template('admin/administrator/question_score/edit.tpl');
-            return new Response($response, 200, array());
-        } else {
-            return $this->createNotFoundException();
-        }
+        return parent::addAction();
     }
 
-    public function addAction()
+    public function editAction($id)
     {
-        $request = $this->getRequest();
-        $form = $this->get('form.factory')->create(new QuestionScoreType());
-
-        if ($request->getMethod() == 'POST') {
-            $form->bind($request);
-            if ($form->isValid()) {
-                $role = $form->getData();
-                parent::createAction($role);
-                $this->get('session')->getFlashBag()->add('success', "Added");
-                $url = $this->get('url_generator')->generate('admin_administrator_question_scores');
-                return $this->redirect($url);
-            }
-        }
-
-        $template = $this->get('template');
-        $template->assign('links', $this->generateLinks());
-        $template->assign('form', $form->createView());
-        $response = $template->render_template('admin/administrator/question_score/add.tpl');
-        return new Response($response, 200, array());
+        return parent::editAction($id);
     }
 
     public function deleteAction($id)
     {
-        $result = parent::deleteAction($id);
-        if ($result) {
-            $url = $this->get('url_generator')->generate('admin_administrator_question_scores');
-            $this->get('session')->getFlashBag()->add('success', "Deleted");
-
-            return $this->redirect($url);
-        }
+        return parent::deleteAction($id);
     }
 
     /**
      * Return an array with the string that are going to be generating by twig.
      * @return array
      */
-    private function generateLinks()
+    protected function generateLinks()
     {
         return array(
             'create_link' => 'admin_administrator_question_score_add',
@@ -137,9 +61,8 @@ class QuestionScore extends CommonController
         );
     }
 
-    /**
-     * @see BaseController::getRepository()
-     * @return EntityRepository
+   /**
+     * {@inheritdoc}
      */
     protected function getRepository()
     {
@@ -147,11 +70,26 @@ class QuestionScore extends CommonController
     }
 
     /**
-     * @see BaseController::getNewEntity()
-     * @return Object
+     * {@inheritdoc}
      */
     protected function getNewEntity()
     {
         return new Entity\QuestionScore();
     }
+
+    /**
+     * {@inheritdoc}
+     */
+    protected function getFormType()
+    {
+        return new QuestionScoreType();
+    }
+
+    /**
+    * {@inheritdoc}
+    */
+    protected function getTemplatePath()
+    {
+        return 'admin/administrator/question_score/';
+    }
 }

+ 27 - 87
src/ChamiloLMS/Controller/Admin/Administrator/QuestionScoreName.php

@@ -20,110 +20,36 @@ use ChamiloLMS\Form\QuestionScoreNameType;
  */
 class QuestionScoreName extends CommonController
 {
-    /**
-     *
-     * @param Application $app
-     * @Route("/")
-     * @Method({"GET"})
-     */
     public function indexAction()
     {
-        $items = parent::listAction('array');
-        $template = $this->get('template');
-        $template->assign('items', $items);
-        $template->assign('links', $this->generateLinks());
-        $response = $template->render_template('admin/administrator/question_score_name/list.tpl');
-        return new Response($response, 200, array());
+        return parent::indexAction();
     }
 
-    /**
-     *
-     * @Route("/{id}", requirements={"id" = "\d+"}, defaults={"foo" = "bar"})
-     * @Method({"GET"})
-     */
     public function readAction($id)
     {
-        $template = $this->get('template');
-        $template->assign('links', $this->generateLinks());
-        $item = parent::getEntity($id);
-        $template->assign('item', $item);
-        $response = $template->render_template('admin/administrator/question_score_name/read.tpl');
-        return new Response($response, 200, array());
+        return parent::readAction($id);
     }
 
-    public function editAction($id)
+    public function addAction()
     {
-        $repo = $this->getRepository();
-        $request = $this->getRequest();
-
-        $item = $repo->findOneById($id);
-
-        if ($item) {
-            $form = $this->get('form.factory')->create(new QuestionScoreNameType(), $item);
-
-            if ($request->getMethod() == 'POST') {
-                $form->bind($this->getRequest());
-
-                if ($form->isValid()) {
-                    $item = $form->getData();
-                    parent::updateAction($item);
-                    $this->get('session')->getFlashBag()->add('success', "Updated");
-                    $url = $this->get('url_generator')->generate('admin_administrator_question_score_names');
-                    return $this->redirect($url);
-                }
-            }
-
-
-            $template = $this->get('template');
-            $template->assign('item', $item);
-            $template->assign('form', $form->createView());
-            $template->assign('links', $this->generateLinks());
-            $response = $template->render_template('admin/administrator/question_score_name/edit.tpl');
-            return new Response($response, 200, array());
-        } else {
-            return $this->createNotFoundException();
-        }
+        return parent::addAction();
     }
 
-    public function addAction()
+    public function editAction($id)
     {
-        $request = $this->getRequest();
-        $form = $this->get('form.factory')->create(new QuestionScoreNameType());
-
-        if ($request->getMethod() == 'POST') {
-            $form->bind($request);
-            if ($form->isValid()) {
-                $item = $form->getData();
-                parent::createAction($item);
-                $this->get('session')->getFlashBag()->add('success', "Added");
-                $url = $this->get('url_generator')->generate('admin_administrator_question_score_names');
-                return $this->redirect($url);
-            }
-        }
-
-        $template = $this->get('template');
-        $template->assign('links', $this->generateLinks());
-        $template->assign('form', $form->createView());
-        $response = $template->render_template('admin/administrator/question_score_name/add.tpl');
-        return new Response($response, 200, array());
+        return parent::editAction($id);
     }
 
     public function deleteAction($id)
     {
-        $result = parent::deleteAction($id);
-        if ($result) {
-            $url = $this->get('url_generator')->generate('admin_administrator_question_score_names');
-            $this->get('session')->getFlashBag()->add('success', "Deleted");
-
-            return $this->redirect($url);
-        }
+        return parent::deleteAction($id);
     }
 
     /**
      * Return an array with the string that are going to be generating by twig.
      * @return array
      */
-    private function generateLinks()
+    protected function generateLinks()
     {
         return array(
             'create_link' => 'admin_administrator_question_score_names_add',
@@ -135,20 +61,34 @@ class QuestionScoreName extends CommonController
     }
 
     /**
-     * @see BaseController::getRepository()
-     * @return EntityRepository
+     * {@inheritdoc}
      */
     protected function getRepository()
     {
         return $this->get('orm.em')->getRepository('Entity\QuestionScoreName');
     }
 
-    /**
-     * @see BaseController::getNewEntity()
-     * @return Object
+     /**
+     * {@inheritdoc}
      */
     protected function getNewEntity()
     {
         return new Entity\QuestionScoreName();
     }
+
+    /**
+     * {@inheritdoc}
+     */
+    protected function getFormType()
+    {
+        return new QuestionScoreNameType();
+    }
+
+    /**
+    * {@inheritdoc}
+    */
+    protected function getTemplatePath()
+    {
+        return 'admin/administrator/question_score_name/';
+    }
 }

+ 25 - 82
src/ChamiloLMS/Controller/Admin/Administrator/RoleController.php

@@ -18,96 +18,36 @@ use ChamiloLMS\Form\RoleType;
  */
 class RoleController extends CommonController
 {
-    /**
-     *
-     * @param Application $app
-     * @Route("/")
-     * @Method({"GET"})
-     */
     public function indexAction()
     {
-        $items = parent::listAction('array');
-        $template = $this->get('template');
-        $template->assign('items', $items);
-        $template->assign('links', $this->generateLinks());
-        $response = $template->render_template('admin/administrator/role/list.tpl');
-        return new Response($response, 200, array());
+        return parent::indexAction();
     }
 
-    /**
-     *
-     * @Route("/{id}", requirements={"id" = "\d+"}, defaults={"foo" = "bar"})
-     * @Method({"GET"})
-     */
     public function readAction($id)
     {
-        $template = $this->get('template');
-        $template->assign('links', $this->generateLinks());
         return parent::readAction($id);
     }
 
-    public function editAction($id)
+    public function addAction()
     {
-        $roleRepo = $this->getRepository();
-        $request = $this->getRequest();
-
-        $role = $roleRepo->findOneById($id);
-
-        if ($role) {
-            $form = $this->get('form.factory')->create(new RoleType(), $role);
-
-            if ($request->getMethod() == 'POST') {
-                $form->bind($this->getRequest());
-
-                if ($form->isValid()) {
-                    $role = $form->getData();
-                    parent::updateAction($role);
-                    $this->get('session')->getFlashBag()->add('success', "Updated");
-                    $url = $this->get('url_generator')->generate('admin_administrator_roles');
-                    return $this->redirect($url);
-                }
-            }
-
-
-            $template = $this->get('template');
-            $template->assign('item', $role);
-            $template->assign('form', $form->createView());
-            $template->assign('links', $this->generateLinks());
-            $response = $template->render_template('admin/administrator/role/edit.tpl');
-            return new Response($response, 200, array());
-        } else {
-            return $this->createNotFoundException();
-        }
+        return parent::addAction();
     }
 
-    public function addAction()
+    public function editAction($id)
     {
-        $request = $this->getRequest();
-        $form = $this->get('form.factory')->create(new RoleType());
-
-        if ($request->getMethod() == 'POST') {
-            $form->bind($request);
-            if ($form->isValid()) {
-                $role = $form->getData();
-                parent::createAction($role);
-                $this->get('session')->getFlashBag()->add('success', "Added");
-                $url = $this->get('url_generator')->generate('admin_administrator_roles');
-                return $this->redirect($url);
-            }
-        }
+        return parent::editAction($id);
+    }
 
-        $template = $this->get('template');
-        $template->assign('links', $this->generateLinks());
-        $template->assign('form', $form->createView());
-        $response = $template->render_template('admin/administrator/role/add.tpl');
-        return new Response($response, 200, array());
+    public function deleteAction($id)
+    {
+        return parent::deleteAction($id);
     }
 
     /**
      * Return an array with the string that are going to be generating by twig.
      * @return array
      */
-    private function generateLinks()
+    protected function generateLinks()
     {
         return array(
             'create_link' => 'admin_administrator_roles_add',
@@ -118,20 +58,16 @@ class RoleController extends CommonController
         );
     }
 
-    public function deleteAction($id)
+    /**
+    * {@inheritdoc}
+    */
+    protected function getTemplatePath()
     {
-        $result = parent::deleteAction($id);
-        if ($result) {
-            $url = $this->get('url_generator')->generate('admin_administrator_roles');
-            $this->get('session')->getFlashBag()->add('success', "Deleted");
-
-            return $this->redirect($url);
-        }
+        return 'admin/administrator/role/';
     }
 
     /**
-     * @see BaseController::getRepository()
-     * @return EntityRepository
+     * {@inheritdoc}
      */
     protected function getRepository()
     {
@@ -139,11 +75,18 @@ class RoleController extends CommonController
     }
 
     /**
-     * @see BaseController::getNewEntity()
-     * @return Object
+     * {@inheritdoc}
      */
     protected function getNewEntity()
     {
         return new Entity\Role();
     }
+
+    /**
+     * {@inheritdoc}
+     */
+    protected function getFormType()
+    {
+        return new RoleType();
+    }
 }

+ 135 - 17
src/ChamiloLMS/Controller/BaseController.php

@@ -6,6 +6,7 @@ namespace ChamiloLMS\Controller;
 use Symfony\Bundle\FrameworkBundle\Controller\Controller;
 
 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
+use Symfony\Component\HttpFoundation\Response;
 use Symfony\Component\HttpFoundation\JsonResponse;
 use Symfony\Component\HttpFoundation\Request;
 use Doctrine\ORM\Query;
@@ -45,6 +46,17 @@ abstract class BaseController
      */
     abstract protected function getNewEntity();
 
+    /**
+     * Returns a new Form Type
+     * @return AbstractType
+     */
+    abstract protected function getFormType();
+
+    /**
+     * Returns the template path
+     * */
+    abstract protected function getTemplatePath();
+
     /**
      *
      * @return Request
@@ -79,6 +91,129 @@ abstract class BaseController
         return $this->app['orm.em'];
     }
 
+    public function createUrl($label)
+    {
+        $links = $this->generateLinks();
+        if (in_array($label, $links)) {
+            $url = $this->get('url_generator')->generate($links[$label]);
+            return $url;
+        }
+        return $url = $this->get('url_generator')->generate($links['list_link']);
+    }
+
+    abstract protected function generateLinks();
+
+
+    // CRUD
+
+    /**
+     * @Route("/{id}", requirements={"id" = "\d+"}, defaults={"foo" = "bar"})
+     * @Method({"GET"})
+     */
+    public function indexAction()
+    {
+        $items = $this->listAction('array');
+        $template = $this->get('template');
+        $template->assign('items', $items);
+        $template->assign('links', $this->generateLinks());
+        $response = $template->render_template($this->getTemplatePath().'list.tpl');
+        return new Response($response, 200, array());
+    }
+
+    /**
+     *
+     * @Route("/{id}", requirements={"id" = "\d+"}, defaults={"foo" = "bar"})
+     * @Method({"GET"})
+     */
+    public function readAction($id)
+    {
+        $template = $this->get('template');
+        $template->assign('links', $this->generateLinks());
+        return parent::readAction($id);
+    }
+
+    public function editAction($id)
+    {
+        $repo = $this->getRepository();
+        $request = $this->getRequest();
+        $item = $repo->findOneById($id);
+
+        if ($item) {
+            $form = $this->get('form.factory')->create($this->getFormType(), $item);
+
+            if ($request->getMethod() == 'POST') {
+                $form->bind($this->getRequest());
+
+                if ($form->isValid()) {
+                    $data = $form->getData();
+                    $this->updateAction($data);
+                    $this->get('session')->getFlashBag()->add('success', "Updated");
+                    $url = $this->createUrl('list_link');
+                    return $this->redirect($url);
+                }
+            }
+
+            $template = $this->get('template');
+            $template->assign('item', $item);
+            $template->assign('form', $form->createView());
+            $template->assign('links', $this->generateLinks());
+            $response = $template->render_template($this->getTemplatePath().'edit.tpl');
+            return new Response($response, 200, array());
+        } else {
+            return $this->createNotFoundException();
+        }
+    }
+
+    public function addAction()
+    {
+        $request = $this->getRequest();
+        $form = $this->get('form.factory')->create($this->getFormType());
+
+        if ($request->getMethod() == 'POST') {
+            $form->bind($request);
+            if ($form->isValid()) {
+                $item = $form->getData();
+                $this->createAction($item);
+                $this->get('session')->getFlashBag()->add('success', "Added");
+                $url = $this->createUrl('list_link');
+                return $this->redirect($url);
+            }
+        }
+
+        $template = $this->get('template');
+        $template->assign('links', $this->generateLinks());
+        $template->assign('form', $form->createView());
+        $response = $template->render_template($this->getTemplatePath().'add.tpl');
+        return new Response($response, 200, array());
+    }
+
+    public function deleteAction($id)
+    {
+        $result = $this->removeAction($id);
+        if ($result) {
+            $url = $this->createUrl('list_link');
+            $this->get('session')->getFlashBag()->add('success', "Deleted");
+
+            return $this->redirect($url);
+        }
+    }
+
+    /**
+     * Base "delete" action.
+     *
+     * @return JsonResponse|NotFoundHttpException
+     */
+    protected function removeAction($id)
+    {
+        $object = $this->getEntity($id);
+        if (false === $object) {
+            return $this->createNotFoundException();
+        }
+        $em = $this->getManager();
+        $em->remove($object);
+        $em->flush();
+        return new JsonResponse(array());
+    }
 
     /**
      * Base "list" action.
@@ -213,23 +348,6 @@ abstract class BaseController
         return new JsonResponse($this->getEntityForJson($object->getId()));
     }
 
-    /**
-     * Base "delete" action.
-     *
-     * @return JsonResponse|NotFoundHttpException
-     */
-    protected function deleteAction($id)
-    {
-        $object = $this->getEntity($id);
-        if (false === $object) {
-            return $this->createNotFoundException();
-        }
-        $em = $this->getManager();
-        $em->remove($object);
-        $em->flush();
-
-        return new JsonResponse(array());
-    }
 
     /**
      * Returns an entity from its ID, or FALSE in case of error.

+ 26 - 7
src/ChamiloLMS/Controller/CommonController.php

@@ -17,23 +17,42 @@ class CommonController extends BaseController
     public function __construct(Application $app)
     {
         parent::__construct($app);
-        /* $this->app['language_files'] = $this->languageFiles;*/
     }
 
-    protected function getRepository() {
+    /**
+     * {@inheritdoc}
+     */
+    protected function getRepository()
+    {
+    }
 
+    /**
+     * {@inheritdoc}
+     */
+    protected function getNewEntity()
+    {
     }
 
     /**
-     * This method should return a new entity instance to be used for the "create" action.
-     *
-     * @abstract
-     * @return Object
+     * {@inheritdoc}
      */
-    protected function getNewEntity() {
+    protected function getFormType()
+    {
+    }
 
+    /**
+     * {@inheritdoc}
+     */
+    protected function generateLinks()
+    {
     }
 
+    /**
+     * {@inheritdoc}
+     */
+    protected function getTemplatePath()
+    {
+    }
 
     /**
      * @param Application $app

+ 6 - 33
src/ChamiloLMS/Controller/LegacyController.php

@@ -20,39 +20,12 @@ class LegacyController extends CommonController
     public $language_files = array('courses', 'index', 'admin');
 
     /**
-     * Handles default Chamilo scripts handled by Display::display_header() and display_footer()
-     *
-     * @param \Silex\Application $app
-     *
-     * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response|void
-     */
-    /*public function classicAction(Application $app)
-    {
-        // User is not allowed.
-        if ($app['allowed'] == false) {
-            return $app->abort(403);
-        }
-
-        // Rendering page.
-        $response = $app['twig']->render($app['default_layout']);
-
-        // Classic style.
-        if ($app['classic_layout'] == true) {
-            //assign('content', already done in display::display_header() and display_footer()
-        } else {
-            return $app->redirect('index');
-        }
-
-        return new Response($response, 200, array());
-    }*/
-
-     /**
-     * Handles default Chamilo scripts handled by Display::display_header() and display_footer()
-     *
-     * @param \Silex\Application $app
-     *
-     * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response|void
-     */
+    * Handles default Chamilo scripts handled by Display::display_header() and display_footer()
+    *
+    * @param \Silex\Application $app
+    *
+    * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response|void
+    */
     public function classicAction(Application $app, $file)
     {
         /** @var Request $request */