CoreController.php 6.7 KB

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