AdminHelper.php 5.5 KB

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