CoreController.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /*
  3. * This file is part of the Sonata package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Bundle\Sonata\BaseApplicationBundle\Controller;
  11. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  12. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  13. use Symfony\Component\Form\RecursiveFieldIterator;
  14. class CoreController extends Controller
  15. {
  16. public function retrieveFormFieldElementAction($code, $element_id)
  17. {
  18. // todo : refactor the code into inside the admin
  19. $admin = $this->container
  20. ->get('base_application.admin.pool')
  21. ->getInstance($code);
  22. if(is_numeric($this->get('request')->get('object_id'))) {
  23. $object = $this->configuration->getObject($this->get('request')->get('object_id'));
  24. } else {
  25. $class = $admin->getClass();
  26. $object = new $class;
  27. }
  28. if(!$object) {
  29. throw new NotFoundHttpException(sprintf('unable to find the object with id : `%s`', $this->get('request')->get('object_id')));
  30. }
  31. $fields = $admin->getFormFields();
  32. $form = $admin->getForm($object, $fields);
  33. // bind the form so the form element will be populated with the lastest elements
  34. $form->bind($this->get('request')->get('data'));
  35. $iterator = new RecursiveFieldIterator($form);
  36. $iterator = new \RecursiveIteratorIterator($iterator);
  37. $field_element = false;
  38. foreach ($iterator as $field) {
  39. if($field->getId() == $element_id) {
  40. // find the targeted element
  41. $field_element = $field;
  42. }
  43. }
  44. if(!$field_element) {
  45. throw new NotFoundHttpException(sprintf('unable to retrieve the form field element with id : `%s`', $element_id));
  46. }
  47. // $key = $field_element->getKey();
  48. //
  49. // if(!isset($fields[$key])) {
  50. // throw new NotFoundHttpException(sprintf('unable to retrieve the form field description with key : %s', $key));
  51. // }
  52. //
  53. // $field_description = $fields[$key];
  54. // render the widget
  55. // todo : fix this, the twig environment variable is not set inside the extension ...
  56. $twig = $this->get('twig');
  57. $extension = $twig->getExtension('form');
  58. $extension->initRuntime($this->get('twig'));
  59. return $this->createResponse($extension->renderField($field_element));
  60. // return $this->render($field_description['template'], array(
  61. // 'configuration' => $admin,
  62. // 'field_description' => $field_description,
  63. // 'object' => $object,
  64. // 'field_element' => $field_element,
  65. // ));
  66. }
  67. public function dashboardAction()
  68. {
  69. return $this->render('Sonata/BaseApplicationBundle:Core:dashboard.twig', array(
  70. 'groups' => $this->get('base_application.admin.pool')->getGroups()
  71. ));
  72. }
  73. }