HelperController.php 8.3 KB

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