HelperController.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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\HttpFoundation\JsonResponse;
  15. use Symfony\Component\PropertyAccess\PropertyAccess;
  16. use Symfony\Component\PropertyAccess\PropertyPath;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Sonata\AdminBundle\Admin\Pool;
  19. use Sonata\AdminBundle\Admin\AdminHelper;
  20. class HelperController
  21. {
  22. /**
  23. * @var \Twig_Environment
  24. */
  25. protected $twig;
  26. /**
  27. * @var \Sonata\AdminBundle\Admin\AdminHelper
  28. */
  29. protected $helper;
  30. /**
  31. * @var \Sonata\AdminBundle\Admin\Pool
  32. */
  33. protected $pool;
  34. /**
  35. * @param \Twig_Environment $twig
  36. * @param \Sonata\AdminBundle\Admin\Pool $pool
  37. * @param \Sonata\AdminBundle\Admin\AdminHelper $helper
  38. */
  39. public function __construct(\Twig_Environment $twig, Pool $pool, AdminHelper $helper)
  40. {
  41. $this->twig = $twig;
  42. $this->pool = $pool;
  43. $this->helper = $helper;
  44. }
  45. /**
  46. * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
  47. *
  48. * @param \Symfony\Component\HttpFoundation\Request $request
  49. *
  50. * @return \Symfony\Component\HttpFoundation\Response
  51. */
  52. public function appendFormFieldElementAction(Request $request)
  53. {
  54. $code = $request->get('code');
  55. $elementId = $request->get('elementId');
  56. $objectId = $request->get('objectId');
  57. $uniqid = $request->get('uniqid');
  58. $admin = $this->pool->getInstance($code);
  59. $admin->setRequest($request);
  60. if ($uniqid) {
  61. $admin->setUniqid($uniqid);
  62. }
  63. $subject = $admin->getModelManager()->find($admin->getClass(), $objectId);
  64. if ($objectId && !$subject) {
  65. throw new NotFoundHttpException;
  66. }
  67. if (!$subject) {
  68. $subject = $admin->getNewInstance();
  69. }
  70. $admin->setSubject($subject);
  71. list($fieldDescription, $form) = $this->helper->appendFormFieldElement($admin, $subject, $elementId);
  72. /** @var $form \Symfony\Component\Form\Form */
  73. $view = $this->helper->getChildFormView($form->createView(), $elementId);
  74. // render the widget
  75. // todo : fix this, the twig environment variable is not set inside the extension ...
  76. $extension = $this->twig->getExtension('form');
  77. $extension->initRuntime($this->twig);
  78. $extension->renderer->setTheme($view, $admin->getFormTheme());
  79. return new Response($extension->renderer->searchAndRenderBlock($view, 'widget'));
  80. }
  81. /**
  82. * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
  83. *
  84. * @param \Symfony\Component\HttpFoundation\Request $request
  85. *
  86. * @return \Symfony\Component\HttpFoundation\Response
  87. */
  88. public function retrieveFormFieldElementAction(Request $request)
  89. {
  90. $code = $request->get('code');
  91. $elementId = $request->get('elementId');
  92. $objectId = $request->get('objectId');
  93. $uniqid = $request->get('uniqid');
  94. $admin = $this->pool->getInstance($code);
  95. $admin->setRequest($request);
  96. if ($uniqid) {
  97. $admin->setUniqid($uniqid);
  98. }
  99. if ($objectId) {
  100. $subject = $admin->getModelManager()->find($admin->getClass(), $objectId);
  101. if (!$subject) {
  102. throw new NotFoundHttpException(sprintf('Unable to find the object id: %s, class: %s', $objectId, $admin->getClass()));
  103. }
  104. } else {
  105. $subject = $admin->getNewInstance();
  106. }
  107. $admin->setSubject($subject);
  108. $formBuilder = $admin->getFormBuilder($subject);
  109. $form = $formBuilder->getForm();
  110. $form->bind($request);
  111. $view = $this->helper->getChildFormView($form->createView(), $elementId);
  112. // render the widget
  113. // todo : fix this, the twig environment variable is not set inside the extension ...
  114. $extension = $this->twig->getExtension('form');
  115. $extension->initRuntime($this->twig);
  116. $extension->renderer->setTheme($view, $admin->getFormTheme());
  117. return new Response($extension->renderer->searchAndRenderBlock($view, 'widget'));
  118. }
  119. /**
  120. * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException|\RuntimeException
  121. *
  122. * @param \Symfony\Component\HttpFoundation\Request $request
  123. *
  124. * @return \Symfony\Component\HttpFoundation\Response
  125. */
  126. public function getShortObjectDescriptionAction(Request $request)
  127. {
  128. $code = $request->get('code');
  129. $objectId = $request->get('objectId');
  130. $uniqid = $request->get('uniqid');
  131. $admin = $this->pool->getInstance($code);
  132. if (!$admin) {
  133. throw new NotFoundHttpException();
  134. }
  135. $admin->setRequest($request);
  136. if ($uniqid) {
  137. $admin->setUniqid($uniqid);
  138. }
  139. $object = $admin->getObject($objectId);
  140. if (!$object) {
  141. throw new NotFoundHttpException();
  142. }
  143. if ('json' == $request->get('_format')) {
  144. return new JsonResponse(array('result' => array(
  145. 'id' => $admin->id($object),
  146. 'label' => $admin->toString($object)
  147. )));
  148. } elseif ('html' == $request->get('_format')) {
  149. return new Response($this->twig->render($admin->getTemplate('short_object_description'), array(
  150. 'admin' => $admin,
  151. 'description' => $admin->toString($object),
  152. 'object' => $object,
  153. )));
  154. } else {
  155. throw new \RuntimeException('Invalid format');
  156. }
  157. }
  158. /**
  159. * @param \Symfony\Component\HttpFoundation\Request $request
  160. * @return \Symfony\Component\HttpFoundation\Response
  161. */
  162. public function setObjectFieldValueAction(Request $request)
  163. {
  164. $field = $request->get('field');
  165. $code = $request->get('code');
  166. $objectId = $request->get('objectId');
  167. $value = $request->get('value');
  168. $context = $request->get('context');
  169. $admin = $this->pool->getInstance($code);
  170. $admin->setRequest($request);
  171. // alter should be done by using a post method
  172. if (!$request->isXmlHttpRequest()) {
  173. return new JsonResponse(array('status' => 'KO', 'message' => 'Expected a XmlHttpRequest request header'));
  174. }
  175. if ($request->getMethod() != 'POST') {
  176. return new JsonResponse(array('status' => 'KO', 'message' => 'Expected a POST Request'));
  177. }
  178. $object = $admin->getObject($objectId);
  179. if (!$object) {
  180. return new JsonResponse(array('status' => 'KO', 'message' => 'Object does not exist'));
  181. }
  182. // check user permission
  183. if (false === $admin->isGranted('EDIT', $object)) {
  184. return new JsonResponse(array('status' => 'KO', 'message' => 'Invalid permissions'));
  185. }
  186. if ($context == 'list') {
  187. $fieldDescription = $admin->getListFieldDescription($field);
  188. } else {
  189. return new JsonResponse(array('status' => 'KO', 'message' => 'Invalid context'));
  190. }
  191. if (!$fieldDescription) {
  192. return new JsonResponse(array('status' => 'KO', 'message' => 'The field does not exist'));
  193. }
  194. if (!$fieldDescription->getOption('editable')) {
  195. return new JsonResponse(array('status' => 'KO', 'message' => 'The field cannot be edit, editable option must be set to true'));
  196. }
  197. // TODO : call the validator component ...
  198. $propertyAccessor = PropertyAccess::getPropertyAccessor();
  199. $propertyPath = new PropertyPath($field);
  200. $propertyAccessor->setValue($object, $propertyPath, $value);
  201. $admin->update($object);
  202. // render the widget
  203. // todo : fix this, the twig environment variable is not set inside the extension ...
  204. $extension = $this->twig->getExtension('sonata_admin');
  205. $extension->initRuntime($this->twig);
  206. $content = $extension->renderListElement($object, $fieldDescription);
  207. return new JsonResponse(array('status' => 'OK', 'content' => $content));
  208. }
  209. }