HelperController.php 8.2 KB

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