123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 |
- <?php
- /*
- * This file is part of the Sonata package.
- *
- * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Sonata\BaseApplicationBundle\Controller;
- use Symfony\Bundle\FrameworkBundle\Controller\Controller;
- use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
- use Symfony\Component\DependencyInjection\ContainerInterface;
- use Symfony\Component\Form\Form;
- use Sonata\BaseApplicationBundle\Tool\DoctrinePager as Pager;
- class CRUDController extends Controller
- {
- protected $admin;
- public function renderJson($data)
- {
- $response = new \Symfony\Component\HttpFoundation\Response;
- $response->setContent(json_encode($data));
- $response->headers->set('Content-Type', 'application/json');
- // $response->headers->set('Content-Type', 'text/plain');
- return $response;
- }
- /**
- * Sets the Container associated with this Controller.
- *
- * @param ContainerInterface $container A ContainerInterface instance
- */
- public function setContainer(ContainerInterface $container = null)
- {
- $this->container = $container;
- $this->configure();
- }
- public function configure()
- {
- $this->admin = $this->container
- ->get('base_application.admin.pool')
- ->getAdminByControllerName(get_class($this));
- }
- public function getBaseTemplate()
- {
- if ($this->get('request')->isXmlHttpRequest()) {
- return $this->container->getParameter('base_application.templates.ajax');
- }
- return $this->container->getParameter('base_application.templates.layout');
- }
- public function listAction()
- {
- $datagrid = $this->admin->getFilterDatagrid();
- $datagrid->setValues($this->get('request')->query->all());
- return $this->render($this->admin->getListTemplate(), array(
- 'datagrid' => $datagrid,
- 'fields' => $this->admin->getListFields(),
- 'class_meta_data' => $this->admin->getClassMetaData(),
- 'admin' => $this->admin,
- 'batch_actions' => $this->admin->getBatchActions(),
- 'base_template' => $this->getBaseTemplate(),
- ));
- }
- public function batchActionDelete($idx)
- {
- $em = $this->admin->getEntityManager();
- $query_builder = $em->createQueryBuilder();
- $objects = $query_builder
- ->select('o')
- ->from($this->admin->getClass(), 'o')
- ->add('where', $query_builder->expr()->in('o.id', $idx))
- ->getQuery()
- ->execute();
-
- foreach ($objects as $object) {
- $em->remove($object);
- }
- $em->flush();
- // todo : add confirmation flash var
- return $this->redirect($this->admin->generateUrl('list'));
- }
- public function deleteAction($id)
- {
- // todo
- }
- public function editAction($id)
- {
- $this->get('session')->start();
- $fields = $this->admin->getFormFields();
- if ($id instanceof Form) {
- $object = $id->getData();
- $form = $id;
- } else {
- $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, $fields);
- }
- $this->admin->setSubject($object);
- return $this->render($this->admin->getEditTemplate(), array(
- 'form' => $form,
- 'object' => $object,
- 'fields' => $fields,
- 'form_groups' => $this->admin->getFormGroups(),
- 'admin' => $this->admin,
- 'base_template' => $this->getBaseTemplate(),
- ));
- }
- public function updateAction()
- {
- $this->get('session')->start();
- if ($this->get('request')->getMethod() != 'POST') {
- throw new \RuntimeException('invalid request type, POST expected');
- }
- $id = $this->get('request')->get('id');
- 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';
- }
- $fields = $this->admin->getFormFields();
- $form = $this->admin->getForm($object, $fields);
- $form->bind($this->get('request')->get('data'));
- if ($form->isValid()) {
- if ($action == 'create') {
- $this->admin->preInsert($object);
- } else {
- $this->admin->preUpdate($object);
- }
-
- $this->admin->getEntityManager()->persist($object);
- $this->admin->getEntityManager()->flush($object);
- if ($action == 'create') {
- $this->admin->postInsert($object);
- } else {
- $this->admin->postUpdate($object);
- }
- if ($this->get('request')->isXmlHttpRequest()) {
- return $this->createResponse(json_encode(array('result' => 'ok', 'object_id' => $object->getId())));
- }
- // redirect to edit mode
- return $this->redirectTo($object);
- }
- return $this->forward(sprintf('%s:%s', $this->admin->getBaseControllerName(), $action), array(
- 'id' => $form
- ));
- }
- /**
- * redirect the user depend on this choice
- *
- * @param $object
- * @return Response
- */
- public function redirectTo($object) {
- $url = false;
- if ($this->get('request')->get('btn_update_and_list')) {
- $url = $this->admin->generateUrl('list');
- }
- if ($this->get('request')->get('btn_create_and_create')) {
- $url = $this->admin->generateUrl('create');
- }
-
- if (!$url) {
- $url = $this->admin->generateUrl('edit', array('id' => $object->getId()));
- }
- return $this->redirect($url);
- }
- public function batchAction()
- {
- if ($this->get('request')->getMethod() != 'POST') {
- throw new \RuntimeException('invalid request type, POST expected');
- }
- $action = $this->get('request')->get('action');
- $idx = $this->get('request')->get('idx');
- if (count($idx) == 0) { // no item selected
- // todo : add flash information
- return $this->redirect($this->admin->generateUrl('list'));
- }
- // execute the action, batchActionXxxxx
- $final_action = sprintf('batchAction%s', ucfirst($action));
- if (!method_exists($this, $final_action)) {
- throw new \RuntimeException(sprintf('A `%s::%s` method must be created', get_class($this), $final_action));
- }
- return call_user_func(array($this, $final_action), $idx);
- }
- public function createAction($id = null)
- {
- $this->get('session')->start();
- $fields = $this->admin->getFormFields();
- if ($id instanceof Form) {
- $object = $id->getData();
- $form = $id;
- } else {
- $object = $this->admin->getNewInstance();
- $form = $this->admin->getForm($object, $fields);
- }
- $this->admin->setSubject($object);
- return $this->render($this->admin->getEditTemplate(), array(
- 'form' => $form,
- 'object' => $object,
- 'fields' => $fields,
- 'form_groups' => $this->admin->getFormGroups(),
- 'admin' => $this->admin,
- 'base_template' => $this->getBaseTemplate(),
- ));
- }
- public function setConfiguration($configuration)
- {
- $this->admin = $configuration;
- }
- public function getConfiguration()
- {
- return $this->admin;
- }
- }
|