HelperController.php 8.6 KB

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