CoreController.php 6.7 KB

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