HelperController.php 4.2 KB

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