HelperController.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. use Symfony\Component\Form\Util\PropertyPath;
  15. class HelperController extends Controller
  16. {
  17. /**
  18. * @return \Sonata\AdminBundle\Admin\AdminHelper
  19. */
  20. public function getAdminHelper()
  21. {
  22. return $this->container->get('sonata.admin.helper');
  23. }
  24. /**
  25. * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
  26. * @return \Symfony\Component\HttpFoundation\Response
  27. */
  28. public function appendFormFieldElementAction()
  29. {
  30. $helper = $this->getAdminHelper();
  31. $request = $this->get('request');
  32. $code = $request->query->get('code');
  33. $elementId = $request->query->get('elementId');
  34. $objectId = $request->query->get('objectId');
  35. $uniqid = $this->get('request')->query->get('uniqid');
  36. $admin = $helper->getAdmin($code);
  37. if ($uniqid) {
  38. $admin->setUniqid($uniqid);
  39. }
  40. $subject = $admin->getModelManager()->find($admin->getClass(), $objectId);
  41. if ($objectId && !$subject) {
  42. throw new NotFoundHttpException;
  43. }
  44. if (!$subject) {
  45. $subject = $admin->getNewInstance();
  46. }
  47. $admin->setSubject($subject);
  48. $admin->setRequest($request);
  49. list($fieldDescription, $form) = $helper->appendFormFieldElement($admin, $elementId);
  50. $view = $helper->getChildFormView($form->createView(), $elementId);
  51. // render the widget
  52. // todo : fix this, the twig environment variable is not set inside the extension ...
  53. $twig = $this->get('twig');
  54. $extension = $twig->getExtension('form');
  55. $extension->initRuntime($this->get('twig'));
  56. $extension->setTheme($view, $admin->getFormTheme());
  57. return new Response($extension->renderWidget($view));
  58. }
  59. /**
  60. * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
  61. * @return \Symfony\Component\HttpFoundation\Response
  62. */
  63. public function retrieveFormFieldElementAction()
  64. {
  65. $helper = $this->getAdminHelper();
  66. $code = $this->get('request')->query->get('code');
  67. $elementId = $this->get('request')->query->get('elementId');
  68. $objectId = $this->get('request')->query->get('objectId');
  69. $admin = $helper->getAdmin($code);
  70. $uniqid = $this->get('request')->query->get('uniqid');
  71. if ($objectId) {
  72. $subject = $admin->getModelManager()->find($admin->getClass(), $objectId);
  73. if (!$subject) {
  74. throw new NotFoundHttpException(sprintf('Unable to find the object id: %s, class: %s', $objectId, $admin->getClass()));
  75. }
  76. } else {
  77. $subject = $admin->getNewInstance();
  78. }
  79. if ($uniqid) {
  80. $admin->setUniqid($uniqid);
  81. }
  82. $formBuilder = $admin->getFormBuilder($subject);
  83. $form = $formBuilder->getForm();
  84. $form->bindRequest($this->get('request'));
  85. $view = $helper->getChildFormView($form->createView(), $elementId);
  86. // render the widget
  87. // todo : fix this, the twig environment variable is not set inside the extension ...
  88. $twig = $this->get('twig');
  89. $extension = $twig->getExtension('form');
  90. $extension->initRuntime($this->get('twig'));
  91. $extension->setTheme($view, $admin->getFormTheme());
  92. return new Response($extension->renderWidget($view));
  93. }
  94. /**
  95. * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
  96. * @return \Symfony\Component\HttpFoundation\Response
  97. */
  98. public function getShortObjectDescriptionAction()
  99. {
  100. $code = $this->get('request')->query->get('code');
  101. $objectId = $this->get('request')->query->get('objectId');
  102. $uniqid = $this->get('request')->query->get('uniqid');
  103. $admin = $this->container->get('sonata.admin.pool')->getInstance($code);
  104. if ($uniqid) {
  105. $admin->setUniqid($uniqid);
  106. }
  107. $object = $admin->getObject($objectId);
  108. if (!$object) {
  109. return new Response();
  110. }
  111. $description = 'no description available';
  112. foreach (array('getAdminTitle', 'getTitle', 'getName', '__toString') as $method) {
  113. if (method_exists($object, $method)) {
  114. $description = $object->$method();
  115. break;
  116. }
  117. }
  118. $description = sprintf('<a href="%s" target="new">%s</a>', $admin->generateUrl('edit', array('id' => $objectId)), $description);
  119. return new Response($description);
  120. }
  121. /**
  122. * Toggle boolean value of property in list
  123. * @return \Symfony\Component\HttpFoundation\Response
  124. */
  125. public function setObjectFieldValueAction()
  126. {
  127. $field = $this->get('request')->query->get('field');
  128. $code = $this->get('request')->query->get('code');
  129. $objectId = $this->get('request')->query->get('objectId');
  130. $uniqid = $this->get('request')->query->get('uniqid');
  131. $value = $this->get('request')->query->get('value');
  132. $admin = $this->container->get('sonata.admin.pool')->getInstance($code);
  133. if (false === $admin->isGranted('EDIT')) {
  134. $response = new Response(json_encode(array('status' => 'Error')));
  135. return $response;
  136. }
  137. if ($uniqid) {
  138. $admin->setUniqid($uniqid);
  139. }
  140. $object = $admin->getObject($objectId);
  141. if (!$object) {
  142. $response = new Response(json_encode(array('status' => 'Error')));
  143. return $response;
  144. }
  145. $propertyPath = new PropertyPath($field);
  146. $propertyPath->setValue($object, $value);
  147. $admin->update($object);
  148. // render the widget
  149. // todo : fix this, the twig environment variable is not set inside the extension ...
  150. $twig = $this->get('twig');
  151. $extension = $twig->getExtension('sonata_admin');
  152. $extension->initRuntime($this->get('twig'));
  153. $response = new Response(json_encode(array('status' => 'OK', 'html' => $extension->renderListElement($object, $admin->getListFieldDescription($field)))));
  154. return $response;
  155. }
  156. }