HelperController.php 8.5 KB

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