HelperController.php 8.3 KB

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