HelperController.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. /**
  24. * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
  25. * @return \Symfony\Component\HttpFoundation\Response
  26. */
  27. public function appendFormFieldElementAction()
  28. {
  29. $helper = $this->getAdminHelper();
  30. $request = $this->get('request');
  31. $code = $request->query->get('code');
  32. $elementId = $request->query->get('elementId');
  33. $objectId = $request->query->get('objectId');
  34. $uniqid = $this->get('request')->query->get('uniqid');
  35. $admin = $helper->getAdmin($code);
  36. if ($uniqid) {
  37. $admin->setUniqid($uniqid);
  38. }
  39. $subject = $admin->getModelManager()->find($admin->getClass(), $objectId);
  40. if ($objectId && !$subject) {
  41. throw new NotFoundHttpException;
  42. }
  43. if (!$subject) {
  44. $subject = $admin->getNewInstance();
  45. }
  46. $admin->setSubject($subject);
  47. $admin->setRequest($request);
  48. list($fieldDescription, $form) = $helper->appendFormFieldElement($admin, $elementId);
  49. $view = $helper->getChildFormView($form->createView(), $elementId);
  50. // render the widget
  51. // todo : fix this, the twig environment variable is not set inside the extension ...
  52. $twig = $this->get('twig');
  53. $extension = $twig->getExtension('form');
  54. $extension->initRuntime($this->get('twig'));
  55. $extension->setTheme($view, $admin->getFormTheme());
  56. return new Response($extension->renderWidget($view));
  57. }
  58. /**
  59. * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
  60. * @return \Symfony\Component\HttpFoundation\Response
  61. */
  62. public function retrieveFormFieldElementAction()
  63. {
  64. $helper = $this->getAdminHelper();
  65. $code = $this->get('request')->query->get('code');
  66. $elementId = $this->get('request')->query->get('elementId');
  67. $objectId = $this->get('request')->query->get('objectId');
  68. $admin = $helper->getAdmin($code);
  69. $uniqid = $this->get('request')->query->get('uniqid');
  70. if ($objectId) {
  71. $subject = $admin->getModelManager()->find($admin->getClass(), $objectId);
  72. if (!$subject) {
  73. throw new NotFoundHttpException(sprintf('Unable to find the object id: %s, class: %s', $objectId, $admin->getClass()));
  74. }
  75. } else {
  76. $subject = $admin->getNewInstance();
  77. }
  78. if ($uniqid) {
  79. $admin->setUniqid($uniqid);
  80. }
  81. $formBuilder = $admin->getFormBuilder($subject);
  82. $form = $formBuilder->getForm();
  83. $form->bindRequest($this->get('request'));
  84. $view = $helper->getChildFormView($form->createView(), $elementId);
  85. // render the widget
  86. // todo : fix this, the twig environment variable is not set inside the extension ...
  87. $twig = $this->get('twig');
  88. $extension = $twig->getExtension('form');
  89. $extension->initRuntime($this->get('twig'));
  90. $extension->setTheme($view, $admin->getFormTheme());
  91. return new Response($extension->renderWidget($view));
  92. }
  93. /**
  94. * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
  95. * @return \Symfony\Component\HttpFoundation\Response
  96. */
  97. public function getShortObjectDescriptionAction()
  98. {
  99. $code = $this->get('request')->query->get('code');
  100. $objectId = $this->get('request')->query->get('objectId');
  101. $uniqid = $this->get('request')->query->get('uniqid');
  102. $admin = $this->container->get('sonata.admin.pool')->getInstance($code);
  103. if ($uniqid) {
  104. $admin->setUniqid($uniqid);
  105. }
  106. $object = $admin->getObject($objectId);
  107. if (!$object) {
  108. return new Response();
  109. }
  110. $description = 'no description available';
  111. foreach (array('getAdminTitle', 'getTitle', 'getName', '__toString') as $method) {
  112. if (method_exists($object, $method)) {
  113. $description = $object->$method();
  114. break;
  115. }
  116. }
  117. $description = sprintf('<a href="%s" target="new">%s</a>', $admin->generateUrl('edit', array('id' => $objectId)), $description);
  118. return new Response($description);
  119. }
  120. }