AdminHelper.php 4.3 KB

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