123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- <?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 Bundle\Sonata\BaseApplicationBundle\Controller;
- use Symfony\Bundle\FrameworkBundle\Controller\Controller;
- use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
- use Symfony\Component\Form\Form;
- use Bundle\Sonata\BaseApplicationBundle\Tool\DoctrinePager as Pager;
- class CRUDController extends Controller
- {
- protected $configuration;
- /**
- * Sets the Container associated with this Controller.
- *
- * @param ContainerInterface $container A ContainerInterface instance
- */
- public function setContainer(\Symfony\Component\DependencyInjection\ContainerInterface $container = null)
- {
- $this->container = $container;
- $this->configure();
- }
- public function configure()
- {
- $this->configuration = $this->container
- ->get('base_application.admin.pool')
- ->getAdminConfigurationByControllerName(get_class($this));
- }
- public function getBaseTemplate()
- {
- if($this->get('request')->isXmlHttpRequest()) {
- return 'Sonata\BaseApplicationBundle::ajax_layout.twig';
- }
- return 'Sonata\BaseApplicationBundle::standard_layout.twig';
- }
- public function listAction()
- {
- $datagrid = $this->configuration->getFilterDatagrid();
- $datagrid->setValues($this->get('request')->query->all());
- return $this->render($this->configuration->getListTemplate(), array(
- 'datagrid' => $datagrid,
- 'fields' => $this->configuration->getListFields(),
- 'class_meta_data' => $this->configuration->getClassMetaData(),
- 'configuration' => $this->configuration,
- 'batch_actions' => $this->configuration->getBatchActions(),
- 'base_template' => $this->getBaseTemplate(),
- ));
- }
- public function batchActionDelete($idx)
- {
- $em = $this->configuration->getEntityManager();
- $query_builder = $em->createQueryBuilder();
- $objects = $query_builder
- ->select('o')
- ->from($this->configuration->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->configuration->generateUrl('list'));
- }
- public function deleteAction($id)
- {
- // todo
- }
- public function editAction($id)
- {
- $this->get('session')->start();
- $fields = $this->configuration->getFormFields();
- if($id instanceof Form) {
- $object = $id->getData();
- $form = $id;
- } else {
- $object = $this->configuration->getObject($id);
- if(!$object) {
- throw new NotFoundHttpException(sprintf('unable to find the object with id : %s', $id));
- }
- $form = $this->configuration->getForm($object, $fields);
- }
- return $this->render($this->configuration->getEditTemplate(), array(
- 'form' => $form,
- 'object' => $object,
- 'fields' => $fields,
- 'configuration' => $this->configuration,
- '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->configuration->getObject($id);
- if(!$object) {
- throw new NotFoundHttpException(sprintf('unable to find the object with id : %s', $id));
- }
- $action = 'edit';
- } else {
- $class = $this->configuration->getClass();
- $object = new $class;
- $action = 'create';
- }
- $fields = $this->configuration->getFormFields();
- $form = $this->configuration->getForm($object, $fields);
- $form->bind($this->get('request')->get('data'));
- if($form->isValid()) {
- $this->configuration->getEntityManager()->persist($object);
- $this->configuration->getEntityManager()->flush($object);
- if($this->get('request')->isXmlHttpRequest()) {
- return $this->createResponse('ok');
- }
- // redirect to edit mode
- return $this->redirect($this->configuration->generateUrl('edit', array('id' => $object->getId())));
- }
- return $this->forward(sprintf('%s:%s', $this->configuration->getBaseControllerName(), $action), array(
- 'id' => $form
- ));
- }
- 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->configuration->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->configuration->getFormFields();
- if($id instanceof Form) {
- $object = $id->getData();
- $form = $id;
- } else {
- $class = $this->configuration->getClass();
- $object = new $class;
- $form = $this->configuration->getForm($object, $fields);
- }
- return $this->render($this->configuration->getEditTemplate(), array(
- 'form' => $form,
- 'object' => $object,
- 'fields' => $fields,
- 'configuration' => $this->configuration,
- 'base_template' => $this->getBaseTemplate(),
- ));
- }
- public function setConfiguration($configuration)
- {
- $this->configuration = $configuration;
- }
- public function getConfiguration()
- {
- return $this->configuration;
- }
- }
|