AdminHelper.php 5.0 KB

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