AdminHelper.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project 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 Doctrine\Common\Inflector\Inflector;
  12. use Doctrine\Common\Util\ClassUtils;
  13. use Sonata\AdminBundle\Exception\NoValueException;
  14. use Sonata\AdminBundle\Util\FormBuilderIterator;
  15. use Sonata\AdminBundle\Util\FormViewIterator;
  16. use Symfony\Component\Form\FormBuilderInterface;
  17. use Symfony\Component\Form\FormView;
  18. /**
  19. * Class AdminHelper.
  20. *
  21. * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
  22. */
  23. class AdminHelper
  24. {
  25. protected $pool;
  26. /**
  27. * @param Pool $pool
  28. */
  29. public function __construct(Pool $pool)
  30. {
  31. $this->pool = $pool;
  32. }
  33. /**
  34. * @throws \RuntimeException
  35. *
  36. * @param \Symfony\Component\Form\FormBuilderInterface $formBuilder
  37. * @param string $elementId
  38. *
  39. * @return \Symfony\Component\Form\FormBuilderInterface
  40. */
  41. public function getChildFormBuilder(FormBuilderInterface $formBuilder, $elementId)
  42. {
  43. foreach (new FormBuilderIterator($formBuilder) as $name => $formBuilder) {
  44. if ($name == $elementId) {
  45. return $formBuilder;
  46. }
  47. }
  48. return;
  49. }
  50. /**
  51. * @param \Symfony\Component\Form\FormView $formView
  52. * @param string $elementId
  53. *
  54. * @return null|\Symfony\Component\Form\FormView
  55. */
  56. public function getChildFormView(FormView $formView, $elementId)
  57. {
  58. foreach (new \RecursiveIteratorIterator(new FormViewIterator($formView), \RecursiveIteratorIterator::SELF_FIRST) as $name => $formView) {
  59. if ($name === $elementId) {
  60. return $formView;
  61. }
  62. }
  63. return;
  64. }
  65. /**
  66. * @deprecated
  67. *
  68. * @param string $code
  69. *
  70. * @return \Sonata\AdminBundle\Admin\AdminInterface
  71. */
  72. public function getAdmin($code)
  73. {
  74. return $this->pool->getInstance($code);
  75. }
  76. /**
  77. * Note:
  78. * This code is ugly, but there is no better way of doing it.
  79. * For now the append form element action used to add a new row works
  80. * only for direct FieldDescription (not nested one).
  81. *
  82. * @throws \RuntimeException
  83. *
  84. * @param \Sonata\AdminBundle\Admin\AdminInterface $admin
  85. * @param object $subject
  86. * @param string $elementId
  87. *
  88. * @return array
  89. */
  90. public function appendFormFieldElement(AdminInterface $admin, $subject, $elementId)
  91. {
  92. // retrieve the subject
  93. $formBuilder = $admin->getFormBuilder();
  94. $form = $formBuilder->getForm();
  95. $form->setData($subject);
  96. $form->handleRequest($admin->getRequest());
  97. // get the field element
  98. $childFormBuilder = $this->getChildFormBuilder($formBuilder, $elementId);
  99. // retrieve the FieldDescription
  100. $fieldDescription = $admin->getFormFieldDescription($childFormBuilder->getName());
  101. try {
  102. $value = $fieldDescription->getValue($form->getData());
  103. } catch (NoValueException $e) {
  104. $value = null;
  105. }
  106. // retrieve the posted data
  107. $data = $admin->getRequest()->get($formBuilder->getName());
  108. if (!isset($data[$childFormBuilder->getName()])) {
  109. $data[$childFormBuilder->getName()] = array();
  110. }
  111. $objectCount = count($value);
  112. $postCount = count($data[$childFormBuilder->getName()]);
  113. $fields = array_keys($fieldDescription->getAssociationAdmin()->getFormFieldDescriptions());
  114. // for now, not sure how to do that
  115. $value = array();
  116. foreach ($fields as $name) {
  117. $value[$name] = '';
  118. }
  119. // add new elements to the subject
  120. while ($objectCount < $postCount) {
  121. // append a new instance into the object
  122. $this->addNewInstance($form->getData(), $fieldDescription);
  123. ++$objectCount;
  124. }
  125. $this->addNewInstance($form->getData(), $fieldDescription);
  126. $finalForm = $admin->getFormBuilder()->getForm();
  127. $finalForm->setData($subject);
  128. // bind the data
  129. $finalForm->setData($form->getData());
  130. return array($fieldDescription, $finalForm);
  131. }
  132. /**
  133. * Add a new instance to the related FieldDescriptionInterface value.
  134. *
  135. * @param object $object
  136. * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  137. *
  138. * @throws \RuntimeException
  139. */
  140. public function addNewInstance($object, FieldDescriptionInterface $fieldDescription)
  141. {
  142. $instance = $fieldDescription->getAssociationAdmin()->getNewInstance();
  143. $mapping = $fieldDescription->getAssociationMapping();
  144. $method = sprintf('add%s', $this->camelize($mapping['fieldName']));
  145. if (!method_exists($object, $method)) {
  146. $method = rtrim($method, 's');
  147. if (!method_exists($object, $method)) {
  148. $method = sprintf('add%s', $this->camelize(Inflector::singularize($mapping['fieldName'])));
  149. if (!method_exists($object, $method)) {
  150. throw new \RuntimeException(sprintf('Please add a method %s in the %s class!', $method, ClassUtils::getClass($object)));
  151. }
  152. }
  153. }
  154. $object->$method($instance);
  155. }
  156. /**
  157. * Camelize a string.
  158. *
  159. * @static
  160. *
  161. * @param string $property
  162. *
  163. * @return string
  164. */
  165. public function camelize($property)
  166. {
  167. return BaseFieldDescription::camelize($property);
  168. }
  169. }