HelperController.php 4.3 KB

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