CoreController.php 5.9 KB

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