AdminHelper.php 5.3 KB

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