DatagridBuilder.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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\DoctrineORMAdminBundle\Builder;
  11. use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
  12. use Sonata\AdminBundle\Model\ModelManagerInterface;
  13. use Sonata\AdminBundle\Admin\AdminInterface;
  14. use Sonata\AdminBundle\Datagrid\DatagridInterface;
  15. use Sonata\AdminBundle\Datagrid\Datagrid;
  16. use Sonata\AdminBundle\Builder\DatagridBuilderInterface;
  17. use Sonata\AdminBundle\Guesser\TypeGuesserInterface;
  18. use Sonata\AdminBundle\Filter\FilterFactoryInterface;
  19. use Sonata\DoctrineORMAdminBundle\Datagrid\Pager;
  20. use Symfony\Component\Form\FormFactory;
  21. use Doctrine\ORM\Mapping\ClassMetadataInfo;
  22. class DatagridBuilder implements DatagridBuilderInterface
  23. {
  24. protected $filterFactory;
  25. protected $formFactory;
  26. protected $guesser;
  27. /**
  28. * @param \Symfony\Component\Form\FormFactory $formFactory
  29. * @param \Sonata\AdminBundle\Filter\FilterFactoryInterface $filterFactory
  30. * @param \Sonata\AdminBundle\Guesser\TypeGuesserInterface $guesser
  31. */
  32. public function __construct(FormFactory $formFactory, FilterFactoryInterface $filterFactory, TypeGuesserInterface $guesser)
  33. {
  34. $this->formFactory = $formFactory;
  35. $this->filterFactory = $filterFactory;
  36. $this->guesser = $guesser;
  37. }
  38. /**
  39. * @param \Sonata\AdminBundle\Admin\AdminInterface $admin
  40. * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  41. * @return void
  42. */
  43. public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription)
  44. {
  45. // set default values
  46. $fieldDescription->setAdmin($admin);
  47. if ($admin->getModelManager()->hasMetadata($admin->getClass())) {
  48. list($metadata, $lastPropertyName, $parentAssociationMappings) = $admin->getModelManager()->getParentMetadataForProperty($admin->getClass(), $fieldDescription->getName());
  49. // set the default field mapping
  50. if (isset($metadata->fieldMappings[$lastPropertyName])) {
  51. $fieldDescription->setOption('field_mapping', $fieldDescription->getOption('field_mapping', $metadata->fieldMappings[$lastPropertyName]));
  52. }
  53. // set the default association mapping
  54. if (isset($metadata->associationMappings[$lastPropertyName])) {
  55. $fieldDescription->setOption('association_mapping', $fieldDescription->getOption('association_mapping', $metadata->associationMappings[$lastPropertyName]));
  56. }
  57. $fieldDescription->setOption('parent_association_mappings', $fieldDescription->getOption('parent_association_mappings', $parentAssociationMappings));
  58. }
  59. $fieldDescription->setOption('code', $fieldDescription->getOption('code', $fieldDescription->getName()));
  60. $fieldDescription->setOption('name', $fieldDescription->getOption('name', $fieldDescription->getName()));
  61. }
  62. /**
  63. * @param \Sonata\AdminBundle\Datagrid\DatagridInterface $datagrid
  64. * @param null $type
  65. * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  66. * @param \Sonata\AdminBundle\Admin\AdminInterface $admin
  67. * @return \Sonata\AdminBundle\Filter\FilterInterface
  68. */
  69. public function addFilter(DatagridInterface $datagrid, $type = null, FieldDescriptionInterface $fieldDescription, AdminInterface $admin)
  70. {
  71. if ($type == null) {
  72. $guessType = $this->guesser->guessType($admin->getClass(), $fieldDescription->getName(), $admin->getModelManager());
  73. $type = $guessType->getType();
  74. $fieldDescription->setType($type);
  75. $options = $guessType->getOptions();
  76. foreach($options as $name => $value) {
  77. if (is_array($value)) {
  78. $fieldDescription->setOption($name, array_merge($value, $fieldDescription->getOption($name, array())));
  79. } else {
  80. $fieldDescription->setOption($name, $fieldDescription->getOption($name, $value));
  81. }
  82. }
  83. } else {
  84. $fieldDescription->setType($type);
  85. }
  86. $this->fixFieldDescription($admin, $fieldDescription);
  87. $admin->addFilterFieldDescription($fieldDescription->getName(), $fieldDescription);
  88. $fieldDescription->mergeOption('field_options', array('required' => false));
  89. $filter = $this->filterFactory->create($fieldDescription->getName(), $type, $fieldDescription->getOptions());
  90. if (!$filter->getLabel()) {
  91. $filter->setLabel($admin->getLabelTranslatorStrategy()->getLabel($fieldDescription->getName(), 'filter', 'label'));
  92. }
  93. $datagrid->addFilter($filter);
  94. return $datagrid->addFilter($filter);
  95. }
  96. /**
  97. * @param \Sonata\AdminBundle\Admin\AdminInterface $admin
  98. * @param array $values
  99. * @return \Sonata\AdminBundle\Datagrid\DatagridInterface
  100. */
  101. public function getBaseDatagrid(AdminInterface $admin, array $values = array())
  102. {
  103. $pager = new Pager;
  104. $pager->setCountColumn($admin->getModelManager()->getIdentifierFieldNames($admin->getClass()));
  105. $formBuilder = $this->formFactory->createNamedBuilder('form', 'filter', array(), array('csrf_protection' => false));
  106. return new Datagrid($admin->createQuery(), $admin->getList(), $pager, $formBuilder, $values);
  107. }
  108. }