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