HelperController.php 4.5 KB

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