CoreController.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 Sonata\AdminBundle\Form\RecursiveFieldIterator;
  15. class CoreController extends Controller
  16. {
  17. public function getBaseTemplate()
  18. {
  19. if ($this->get('request')->isXmlHttpRequest()) {
  20. return $this->container->getParameter('sonata_admin.templates.ajax');
  21. }
  22. return $this->container->getParameter('sonata_admin.templates.layout');
  23. }
  24. public function retrieveFormFieldElementAction()
  25. {
  26. $code = $this->get('request')->get('code');
  27. $elementId = $this->get('request')->get('elementId');
  28. $admin = $this->getAdmin($code);
  29. $form = $this->getForm($admin, $code);
  30. $form->bind($this->get('request'));
  31. $field_element = $this->getFieldElement($form, $elementId);
  32. // render the widget
  33. // todo : fix this, the twig environment variable is not set inside the extension ...
  34. $twig = $this->get('twig');
  35. $extension = $twig->getExtension('form');
  36. $extension->initRuntime($this->get('twig'));
  37. return new Response($extension->renderField($field_element));
  38. }
  39. public function getAdmin($code)
  40. {
  41. // todo : refactor the code into inside the admin
  42. $admin = $this->container
  43. ->get('sonata_admin.admin.pool')
  44. ->getInstance($code);
  45. if ($this->container->get('request')->get('uniqid')) {
  46. $admin->setUniqid($this->container->get('request')->get('uniqid'));
  47. }
  48. return $admin;
  49. }
  50. public function getForm($admin, $code)
  51. {
  52. if (is_numeric($this->get('request')->get('object_id'))) {
  53. $object = $admin->getObject($this->get('request')->get('object_id'));
  54. } else {
  55. $class = $admin->getClass();
  56. $object = new $class;
  57. }
  58. if (!$object) {
  59. throw new NotFoundHttpException(sprintf('unable to find the object with id : `%s`', $this->get('request')->get('object_id')));
  60. }
  61. return $admin->getForm($object);
  62. }
  63. public function getFieldElement($form, $element_id)
  64. {
  65. $iterator = new RecursiveFieldIterator($form);
  66. $iterator = new \RecursiveIteratorIterator($iterator, \RecursiveIteratorIterator::CHILD_FIRST);
  67. $field_element = false;
  68. foreach ($iterator as $field) {
  69. if ($field->getId() == $element_id) {
  70. // find the targeted element
  71. return $field;
  72. }
  73. }
  74. if (!$field_element) {
  75. throw new NotFoundHttpException(sprintf('unable to retrieve the form field element with id : `%s`', $element_id));
  76. }
  77. }
  78. public function appendFormFieldElementAction()
  79. {
  80. $code = $this->get('request')->get('code');
  81. $elementId = $this->get('request')->get('elementId');
  82. // Note : This code is ugly, I guess there is a better way of doing it.
  83. // For now the append form element action used to add a new row works
  84. // only for direct FieldDescription (not nested one)
  85. // retrieve the admin
  86. $admin = $this->getAdmin($code);
  87. // retrieve the subject
  88. $form = $this->getForm($admin, $code);
  89. // get the field element
  90. $field_element = $this->getFieldElement($form, $elementId);
  91. // retrieve the FieldDescription
  92. $fieldDescription = $admin->getFormFieldDescription($field_element->getKey());
  93. $subject = $form->getData();
  94. $value = $fieldDescription->getValue($subject);
  95. // retrieve the posted data
  96. $data = $this->get('request')->get($form->getName());
  97. if (!isset($data[$field_element->getKey()])) {
  98. $data[$field_element->getKey()] = array();
  99. }
  100. $object_count = count($value);
  101. $post_count = count($data[$field_element->getKey()]);
  102. // for now, not sure how to do that
  103. $value = array();
  104. foreach ($field_element->getPrototype()->getFields() as $name => $t) {
  105. $value[$name] = '';
  106. }
  107. // add new elements to the subject
  108. while($object_count < $post_count) {
  109. // append a new instance into the object
  110. $admin->getFormBuilder()->addNewInstance($subject, $fieldDescription);
  111. $object_count++;
  112. }
  113. $admin->getFormBuilder()->addNewInstance($subject, $fieldDescription);
  114. $data[$field_element->getKey()][] = $value;
  115. $form = $admin->getForm($subject);
  116. // bind the data
  117. $form->submit($data);
  118. $admin->setSubject($subject);
  119. // render the widget
  120. // todo : fix this, the twig environment variable is not set inside the extension ...
  121. $twig = $this->get('twig');
  122. $extension = $twig->getExtension('sonata_admin');
  123. $extension->initRuntime($this->get('twig'));
  124. return new Response($extension->renderFormElement($fieldDescription, $form, $form->getData()));
  125. }
  126. public function getShortObjectDescriptionAction($code = null, $objectId = null, $uniqid = null)
  127. {
  128. $code = $code ?: $this->get('request')->query->get('code');
  129. $objectId = $objectId ?: $this->get('request')->query->get('objectId');
  130. $uniqid = $uniqid ?: $this->get('request')->get('uniqid');
  131. $admin = $this->container->get('sonata_admin.admin.pool')->getInstance($code);
  132. if ($uniqid) {
  133. $admin->setUniqid($uniqid);
  134. }
  135. $object = $admin->getObject($objectId);
  136. if (!$object) {
  137. throw new NotFoundHttpException(sprintf('unable to find the object with id : %s', $objectId));
  138. }
  139. $description = 'no description available';
  140. foreach (array('getTitle', 'getName', '__toString') as $method) {
  141. if (method_exists($object, $method)) {
  142. $description = $object->$method();
  143. break;
  144. }
  145. }
  146. $description = sprintf('<a href="%s" target="new">%s</a>', $admin->generateUrl('edit', array('id' => $objectId)), $description);
  147. return new Response($description);
  148. }
  149. public function dashboardAction()
  150. {
  151. return $this->render('SonataAdmin:Core:dashboard.html.twig', array(
  152. 'groups' => $this->get('sonata_admin.admin.pool')->getDashboardGroups(),
  153. 'base_template' => $this->getBaseTemplate(),
  154. ));
  155. }
  156. }