CoreController.php 5.6 KB

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