HelperController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 Sonata\AdminBundle\Controller;
  11. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  12. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  13. use Symfony\Component\HttpFoundation\Response;
  14. class HelperController extends Controller
  15. {
  16. /**
  17. * @return \Sonata\AdminBundle\Admin\AdminHelper
  18. */
  19. public function getAdminHelper()
  20. {
  21. return $this->container->get('sonata.admin.helper');
  22. }
  23. public function appendFormFieldElementAction()
  24. {
  25. $helper = $this->getAdminHelper();
  26. $request = $this->get('request');
  27. $code = $request->query->get('code');
  28. $elementId = $request->query->get('elementId');
  29. $objectId = $request->query->get('objectId');
  30. $uniqid = $this->get('request')->query->get('uniqid');
  31. $admin = $helper->getAdmin($code);
  32. if ($uniqid) {
  33. $admin->setUniqid($uniqid);
  34. }
  35. $subject = $admin->getModelManager()->findOne($admin->getClass(), $objectId);
  36. if (!$subject) {
  37. $subject = $admin->getNewInstance();
  38. }
  39. $admin->setSubject($subject);
  40. $admin->setRequest($request);
  41. list($fieldDescription, $formBuilder) = $helper->appendFormFieldElement($admin, $elementId);
  42. $view = $helper->getChildFormView($formBuilder->getForm()->createView(), $elementId);
  43. // render the widget
  44. // todo : fix this, the twig environment variable is not set inside the extension ...
  45. $twig = $this->get('twig');
  46. $extension = $twig->getExtension('form');
  47. $extension->initRuntime($this->get('twig'));
  48. return new Response($extension->renderWidget($view));
  49. }
  50. public function retrieveFormFieldElementAction()
  51. {
  52. $helper = $this->getAdminHelper();
  53. $code = $this->get('request')->query->get('code');
  54. $elementId = $this->get('request')->query->get('elementId');
  55. $objectId = $this->get('request')->query->get('objectId');
  56. $admin = $helper->getAdmin($code);
  57. $uniqid = $this->get('request')->query->get('uniqid');
  58. if ($objectId) {
  59. $subject = $admin->getModelManager()->findOne($admin->getClass(), $objectId);
  60. if (!$subject) {
  61. throw new NotFoundHttpException(sprintf('Unable to find the object id: %s, class: %s', $objectId, $admin->getClass()));
  62. }
  63. } else {
  64. $subject = $admin->getNewInstance();
  65. }
  66. if ($uniqid) {
  67. $admin->setUniqid($uniqid);
  68. }
  69. $formBuilder = $admin->getFormBuilder($subject);
  70. $form = $formBuilder->getForm();
  71. $form->bindRequest($this->get('request'));
  72. $view = $helper->getChildFormView($form->createView(), $elementId);
  73. // render the widget
  74. // todo : fix this, the twig environment variable is not set inside the extension ...
  75. $twig = $this->get('twig');
  76. $extension = $twig->getExtension('form');
  77. $extension->initRuntime($this->get('twig'));
  78. return new Response($extension->renderWidget($view));
  79. }
  80. public function getShortObjectDescriptionAction()
  81. {
  82. $code = $this->get('request')->query->get('code');
  83. $objectId = $this->get('request')->query->get('objectId');
  84. $uniqid = $this->get('request')->query->get('uniqid');
  85. $admin = $this->container->get('sonata.admin.pool')->getInstance($code);
  86. if ($uniqid) {
  87. $admin->setUniqid($uniqid);
  88. }
  89. $object = $admin->getObject($objectId);
  90. if (!$object) {
  91. throw new NotFoundHttpException(sprintf('unable to find the object with id : %s', $objectId));
  92. }
  93. $description = 'no description available';
  94. foreach (array('getTitle', 'getName', '__toString') as $method) {
  95. if (method_exists($object, $method)) {
  96. $description = $object->$method();
  97. break;
  98. }
  99. }
  100. $description = sprintf('<a href="%s" target="new">%s</a>', $admin->generateUrl('edit', array('id' => $objectId)), $description);
  101. return new Response($description);
  102. }
  103. }