AdminHelper.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 object $subject
  79. * @param string $elementId
  80. *
  81. * @return array
  82. */
  83. public function appendFormFieldElement(AdminInterface $admin, $subject, $elementId)
  84. {
  85. // retrieve the subject
  86. $formBuilder = $admin->getFormBuilder();
  87. $form = $formBuilder->getForm();
  88. $form->setData($subject);
  89. $form->bindRequest($admin->getRequest());
  90. // get the field element
  91. $childFormBuilder = $this->getChildFormBuilder($formBuilder, $elementId);
  92. // retrieve the FieldDescription
  93. $fieldDescription = $admin->getFormFieldDescription($childFormBuilder->getName());
  94. try {
  95. $value = $fieldDescription->getValue($form->getData());
  96. } catch (NoValueException $e) {
  97. $value = null;
  98. }
  99. // retrieve the posted data
  100. $data = $admin->getRequest()->get($formBuilder->getName());
  101. if (!isset($data[$childFormBuilder->getName()])) {
  102. $data[$childFormBuilder->getName()] = array();
  103. }
  104. $objectCount = count($value);
  105. $postCount = count($data[$childFormBuilder->getName()]);
  106. $fields = array_keys($fieldDescription->getAssociationAdmin()->getFormFieldDescriptions());
  107. // for now, not sure how to do that
  108. $value = array();
  109. foreach ($fields as $name) {
  110. $value[$name] = '';
  111. }
  112. // add new elements to the subject
  113. while ($objectCount < $postCount) {
  114. // append a new instance into the object
  115. $this->addNewInstance($form->getData(), $fieldDescription);
  116. $objectCount++;
  117. }
  118. $this->addNewInstance($form->getData(), $fieldDescription);
  119. $data[$childFormBuilder->getName()][] = $value;
  120. $finalForm = $admin->getFormBuilder()->getForm();
  121. $finalForm->setData($subject);
  122. // bind the data
  123. $finalForm->setData($form->getData());
  124. return array($fieldDescription, $finalForm);
  125. }
  126. /**
  127. * Add a new instance to the related FieldDescriptionInterface value
  128. *
  129. * @param object $object
  130. * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  131. *
  132. * @return void
  133. */
  134. public function addNewInstance($object, FieldDescriptionInterface $fieldDescription)
  135. {
  136. $instance = $fieldDescription->getAssociationAdmin()->getNewInstance();
  137. $mapping = $fieldDescription->getAssociationMapping();
  138. $method = sprintf('add%s', $this->camelize($mapping['fieldName']));
  139. if (!method_exists($object, $method)) {
  140. throw new \RuntimeException(sprintf('Please add a method %s in the %s class!', $method, get_class($object)));
  141. }
  142. $object->$method($instance);
  143. }
  144. /**
  145. * Camelize a string
  146. *
  147. * @static
  148. *
  149. * @param string $property
  150. *
  151. * @return string
  152. */
  153. public function camelize($property)
  154. {
  155. return preg_replace(array('/(^|_| )+(.)/e', '/\.(.)/e'), array("strtoupper('\\2')", "'_'.strtoupper('\\1')"), $property);
  156. }
  157. }