AdminHelper.php 5.8 KB

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