AdminHelper.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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\AdminBundle\Admin;
  11. use Symfony\Component\Form\FormBuilder;
  12. use Symfony\Component\Form\FormView;
  13. use Sonata\AdminBundle\Util\FormViewIterator;
  14. use Sonata\AdminBundle\Util\FormBuilderIterator;
  15. class AdminHelper
  16. {
  17. protected $pool;
  18. /**
  19. * @param Pool $pool
  20. */
  21. public function __construct(Pool $pool)
  22. {
  23. $this->pool = $pool;
  24. }
  25. /**
  26. * @throws \RuntimeException
  27. * @param \Symfony\Component\Form\FormBuilder $formBuilder
  28. * @param $elementId
  29. * @return \Symfony\Component\Form\FormBuilder
  30. */
  31. public function getChildFormBuilder(FormBuilder $formBuilder, $elementId)
  32. {
  33. foreach (new FormBuilderIterator($formBuilder) as $name => $formBuilder) {
  34. if ($name == $elementId) {
  35. return $formBuilder;
  36. }
  37. }
  38. return null;
  39. }
  40. /**
  41. * @throws \RuntimeException
  42. * @param \Symfony\Component\Form\FormView $formBuilder
  43. * @param $elementId
  44. * @return \Symfony\Component\Form\FormView
  45. */
  46. public function getChildFormView(FormView $formView, $elementId)
  47. {
  48. foreach (new \RecursiveIteratorIterator(new FormViewIterator($formView), \RecursiveIteratorIterator::SELF_FIRST) as $name => $formView) {
  49. if ($name == $elementId) {
  50. return $formView;
  51. }
  52. }
  53. return null;
  54. }
  55. /**
  56. * @param string $code
  57. * @return \Sonata\AdminBundle\Admin\AdminInterface
  58. */
  59. public function getAdmin($code)
  60. {
  61. return $this->pool->getInstance($code);
  62. }
  63. /**
  64. * Note:
  65. * This code is ugly, but there is no better way of doing it.
  66. * For now the append form element action used to add a new row works
  67. * only for direct FieldDescription (not nested one)
  68. *
  69. * @throws \RuntimeException
  70. * @param \Sonata\AdminBundle\Admin\AdminInterface $admin
  71. * @param $elementId
  72. * @return void
  73. */
  74. public function appendFormFieldElement(AdminInterface $admin, $elementId)
  75. {
  76. // retrieve the subject
  77. $formBuilder = $admin->getFormBuilder();
  78. $form = $formBuilder->getForm();
  79. $form->bindRequest($admin->getRequest());
  80. // get the field element
  81. $childFormBuilder = $this->getChildFormBuilder($formBuilder, $elementId);
  82. // retrieve the FieldDescription
  83. $fieldDescription = $admin->getFormFieldDescription($childFormBuilder->getName());
  84. try {
  85. $value = $fieldDescription->getValue($form->getData());
  86. } catch (NoValueException $e) {
  87. $value = null;
  88. }
  89. // retrieve the posted data
  90. $data = $admin->getRequest()->get($formBuilder->getName());
  91. if (!isset($data[$childFormBuilder->getName()])) {
  92. $data[$childFormBuilder->getName()] = array();
  93. }
  94. $objectCount = count($value);
  95. $postCount = count($data[$childFormBuilder->getName()]);
  96. $fields = array_keys($fieldDescription->getAssociationAdmin()->getFormFieldDescriptions());
  97. // for now, not sure how to do that
  98. $value = array();
  99. foreach ($fields as $name) {
  100. $value[$name] = '';
  101. }
  102. // add new elements to the subject
  103. while($objectCount < $postCount) {
  104. // append a new instance into the object
  105. $this->addNewInstance($form->getData(), $fieldDescription);
  106. $objectCount++;
  107. }
  108. $this->addNewInstance($form->getData(), $fieldDescription);
  109. $data[$childFormBuilder->getName()][] = $value;
  110. $form = $admin->getFormBuilder($form->getData())->getForm();
  111. // bind the data
  112. $form->bind($data);
  113. return array($fieldDescription, $form);
  114. }
  115. /**
  116. * Add a new instance to the related FieldDescriptionInterface value
  117. *
  118. * @param object $object
  119. * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  120. * @return void
  121. */
  122. public function addNewInstance($object, FieldDescriptionInterface $fieldDescription)
  123. {
  124. $instance = $fieldDescription->getAssociationAdmin()->getNewInstance();
  125. $mapping = $fieldDescription->getAssociationMapping();
  126. $method = sprintf('add%s', $this->camelize($mapping['fieldName']));
  127. $object->$method($instance);
  128. }
  129. /**
  130. * Camelize a string
  131. *
  132. * @static
  133. * @param string $property
  134. * @return string
  135. */
  136. public function camelize($property)
  137. {
  138. return preg_replace(array('/(^|_| )+(.)/e', '/\.(.)/e'), array("strtoupper('\\2')", "'_'.strtoupper('\\1')"), $property);
  139. }
  140. }