HelperController.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. $admin->setSubject($subject);
  104. $formBuilder = $admin->getFormBuilder($subject);
  105. $form = $formBuilder->getForm();
  106. $form->bindRequest($this->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. * @return \Symfony\Component\HttpFoundation\Response
  118. */
  119. public function getShortObjectDescriptionAction()
  120. {
  121. $code = $this->request->get('code');
  122. $objectId = $this->request->get('objectId');
  123. $uniqid = $this->request->get('uniqid');
  124. $admin = $this->pool->getInstance($code);
  125. if (!$admin) {
  126. throw new NotFoundHttpException();
  127. }
  128. if ($uniqid) {
  129. $admin->setUniqid($uniqid);
  130. }
  131. $object = $admin->getObject($objectId);
  132. if (!$object) {
  133. return new Response();
  134. }
  135. $description = 'no description available';
  136. foreach (array('getAdminTitle', 'getTitle', 'getName', '__toString') as $method) {
  137. if (method_exists($object, $method)) {
  138. $description = call_user_func(array($object, $method));
  139. break;
  140. }
  141. }
  142. $description = sprintf('<a href="%s" target="new">%s</a>', $admin->generateUrl('edit', array('id' => $objectId)), $description);
  143. return new Response($description);
  144. }
  145. /**
  146. * Toggle boolean value of property in list
  147. * @return \Symfony\Component\HttpFoundation\Response
  148. */
  149. public function setObjectFieldValueAction()
  150. {
  151. $field = $this->request->get('field');
  152. $code = $this->request->get('code');
  153. $objectId = $this->request->get('objectId');
  154. $value = $this->request->get('value');
  155. $context = $this->request->get('context');
  156. $admin = $this->pool->getInstance($code);
  157. // alter should be done by using a post method
  158. if ($this->request->getMethod() != 'POST') {
  159. return new Response(json_encode(array('status' => 'KO', 'message' => 'Expected a POST Request')), 200, array(
  160. 'Content-Type' => 'application/json'
  161. ));
  162. }
  163. $object = $admin->getObject($objectId);
  164. if (!$object) {
  165. return new Response(json_encode(array('status' => 'KO', 'message' => 'Object does not exist')), 200, array(
  166. 'Content-Type' => 'application/json'
  167. ));
  168. }
  169. // check user permission
  170. if (false === $admin->isGranted('EDIT', $object)) {
  171. return new Response(json_encode(array('status' => 'KO', 'message' => 'Invalid permissions')), 200, array(
  172. 'Content-Type' => 'application/json'
  173. ));
  174. }
  175. if ($context == 'list') {
  176. $fieldDescription = $admin->getListFieldDescription($field);
  177. } else {
  178. return new Response(json_encode(array('status' => 'KO', 'message' => 'Invalid context')), 200, array(
  179. 'Content-Type' => 'application/json'
  180. ));
  181. }
  182. if (!$fieldDescription) {
  183. return new Response(json_encode(array('status' => 'KO', 'message' => 'The field does not exist')), 200, array(
  184. 'Content-Type' => 'application/json'
  185. ));
  186. }
  187. if (!$fieldDescription->getOption('editable')) {
  188. return new Response(json_encode(array('status' => 'KO', 'message' => 'The field cannot be edit, editable option must be set to true')), 200, array(
  189. 'Content-Type' => 'application/json'
  190. ));
  191. }
  192. // TODO : call the validator component ...
  193. $propertyPath = new PropertyPath($field);
  194. $propertyPath->setValue($object, $value);
  195. $admin->update($object);
  196. // render the widget
  197. // todo : fix this, the twig environment variable is not set inside the extension ...
  198. $extension = $this->twig->getExtension('sonata_admin');
  199. $extension->initRuntime($this->twig);
  200. $content = $extension->renderListElement($object, $fieldDescription);
  201. return new Response(json_encode(array('status' => 'OK', 'content' => $content)), 200, array(
  202. 'Content-Type' => 'application/json'
  203. ));
  204. }
  205. }