HelperController.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. $admin->setRequest($request);
  94. if ($uniqid) {
  95. $admin->setUniqid($uniqid);
  96. }
  97. if ($objectId) {
  98. $subject = $admin->getModelManager()->find($admin->getClass(), $objectId);
  99. if (!$subject) {
  100. throw new NotFoundHttpException(sprintf('Unable to find the object id: %s, class: %s', $objectId, $admin->getClass()));
  101. }
  102. } else {
  103. $subject = $admin->getNewInstance();
  104. }
  105. $admin->setSubject($subject);
  106. $formBuilder = $admin->getFormBuilder($subject);
  107. $form = $formBuilder->getForm();
  108. $form->bind($request);
  109. $view = $this->helper->getChildFormView($form->createView(), $elementId);
  110. // render the widget
  111. // todo : fix this, the twig environment variable is not set inside the extension ...
  112. $extension = $this->twig->getExtension('form');
  113. $extension->initRuntime($this->twig);
  114. $extension->renderer->setTheme($view, $admin->getFormTheme());
  115. return new Response($extension->renderer->searchAndRenderBlock($view, 'widget'));
  116. }
  117. /**
  118. * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
  119. *
  120. * @param \Symfony\Component\HttpFoundation\Request $request
  121. *
  122. * @return \Symfony\Component\HttpFoundation\Response
  123. */
  124. public function getShortObjectDescriptionAction(Request $request)
  125. {
  126. $code = $request->get('code');
  127. $objectId = $request->get('objectId');
  128. $uniqid = $request->get('uniqid');
  129. $admin = $this->pool->getInstance($code);
  130. $admin->setRequest($request);
  131. if (!$admin) {
  132. throw new NotFoundHttpException();
  133. }
  134. if ($uniqid) {
  135. $admin->setUniqid($uniqid);
  136. }
  137. $object = $admin->getObject($objectId);
  138. if (!$object) {
  139. return new Response();
  140. }
  141. $description = 'no description available';
  142. foreach (array('getAdminTitle', 'getTitle', 'getName', '__toString') as $method) {
  143. if (method_exists($object, $method)) {
  144. $description = call_user_func(array($object, $method));
  145. break;
  146. }
  147. }
  148. $url = $admin->generateUrl('edit', array('id' => $objectId));
  149. $htmlOutput = $this->twig->render($admin->getTemplate('short_object_description'),
  150. array(
  151. 'description' => $description,
  152. 'object' => $object,
  153. 'url' => $url
  154. )
  155. );
  156. return new Response($htmlOutput);
  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->getMethod() != 'POST') {
  173. return new Response(json_encode(array('status' => 'KO', 'message' => 'Expected a POST Request')), 200, array(
  174. 'Content-Type' => 'application/json'
  175. ));
  176. }
  177. $object = $admin->getObject($objectId);
  178. if (!$object) {
  179. return new Response(json_encode(array('status' => 'KO', 'message' => 'Object does not exist')), 200, array(
  180. 'Content-Type' => 'application/json'
  181. ));
  182. }
  183. // check user permission
  184. if (false === $admin->isGranted('EDIT', $object)) {
  185. return new Response(json_encode(array('status' => 'KO', 'message' => 'Invalid permissions')), 200, array(
  186. 'Content-Type' => 'application/json'
  187. ));
  188. }
  189. if ($context == 'list') {
  190. $fieldDescription = $admin->getListFieldDescription($field);
  191. } else {
  192. return new Response(json_encode(array('status' => 'KO', 'message' => 'Invalid context')), 200, array(
  193. 'Content-Type' => 'application/json'
  194. ));
  195. }
  196. if (!$fieldDescription) {
  197. return new Response(json_encode(array('status' => 'KO', 'message' => 'The field does not exist')), 200, array(
  198. 'Content-Type' => 'application/json'
  199. ));
  200. }
  201. if (!$fieldDescription->getOption('editable')) {
  202. return new Response(json_encode(array('status' => 'KO', 'message' => 'The field cannot be edit, editable option must be set to true')), 200, array(
  203. 'Content-Type' => 'application/json'
  204. ));
  205. }
  206. // TODO : call the validator component ...
  207. $propertyPath = new PropertyPath($field);
  208. $propertyPath->setValue($object, $value);
  209. $admin->update($object);
  210. // render the widget
  211. // todo : fix this, the twig environment variable is not set inside the extension ...
  212. $extension = $this->twig->getExtension('sonata_admin');
  213. $extension->initRuntime($this->twig);
  214. $content = $extension->renderListElement($object, $fieldDescription);
  215. return new Response(json_encode(array('status' => 'OK', 'content' => $content)), 200, array(
  216. 'Content-Type' => 'application/json'
  217. ));
  218. }
  219. }