CoreController.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 = $admin->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. // render the widget
  48. // todo : fix this, the twig environment variable is not set inside the extension ...
  49. $twig = $this->get('twig');
  50. $extension = $twig->getExtension('form');
  51. $extension->initRuntime($this->get('twig'));
  52. return $this->createResponse($extension->renderField($field_element));
  53. }
  54. public function dashboardAction()
  55. {
  56. return $this->render('Sonata/BaseApplicationBundle:Core:dashboard.twig', array(
  57. 'groups' => $this->get('base_application.admin.pool')->getGroups()
  58. ));
  59. }
  60. }