CoreController.php 6.0 KB

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