ModelChoiceLoader.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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\Form\ChoiceList;
  11. use Doctrine\Common\Util\ClassUtils;
  12. use Sonata\AdminBundle\Model\ModelManagerInterface;
  13. use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
  14. use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
  15. use Symfony\Component\Form\Exception\RuntimeException;
  16. use Symfony\Component\PropertyAccess\PropertyAccess;
  17. use Symfony\Component\PropertyAccess\PropertyPath;
  18. /**
  19. * Class ModelChoiceLoader.
  20. *
  21. * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
  22. */
  23. class ModelChoiceLoader implements ChoiceLoaderInterface
  24. {
  25. /**
  26. * @var \Sonata\AdminBundle\Model\ModelManagerInterface
  27. */
  28. private $modelManager;
  29. /**
  30. * @var string
  31. */
  32. private $class;
  33. private $property;
  34. private $query;
  35. private $choices;
  36. private $propertyPath;
  37. private $choiceList;
  38. /**
  39. * @param ModelManagerInterface $modelManager
  40. * @param string $class
  41. * @param null $property
  42. * @param null $query
  43. * @param array $choices
  44. */
  45. public function __construct(ModelManagerInterface $modelManager, $class, $property = null, $query = null, $choices = array())
  46. {
  47. $this->modelManager = $modelManager;
  48. $this->class = $class;
  49. $this->property = $property;
  50. $this->query = $query;
  51. $this->choices = $choices;
  52. $this->identifier = $this->modelManager->getIdentifierFieldNames($this->class);
  53. // The property option defines, which property (path) is used for
  54. // displaying entities as strings
  55. if ($property) {
  56. $this->propertyPath = new PropertyPath($property);
  57. $this->propertyAccessor = PropertyAccess::createPropertyAccessor();
  58. }
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. public function loadChoiceList($value = null)
  64. {
  65. if (!$this->choiceList) {
  66. if ($this->query) {
  67. $entities = $this->modelManager->executeQuery($this->query);
  68. } elseif (is_array($this->choices) && count($this->choices) > 0) {
  69. $entities = $this->choices;
  70. } else {
  71. $entities = $this->modelManager->findBy($this->class);
  72. }
  73. $choices = array();
  74. foreach ($entities as $key => $entity) {
  75. if ($this->propertyPath) {
  76. // If the property option was given, use it
  77. $propertyAccessor = PropertyAccess::createPropertyAccessor();
  78. $valueObject = $propertyAccessor->getValue($entity, $this->propertyPath);
  79. } else {
  80. // Otherwise expect a __toString() method in the entity
  81. try {
  82. $valueObject = (string) $entity;
  83. } catch (\Exception $e) {
  84. throw new RuntimeException(sprintf("Unable to convert the entity %s to String, entity must have a '__toString()' method defined", ClassUtils::getClass($entity)), 0, $e);
  85. }
  86. }
  87. $id = implode('~', $this->getIdentifierValues($entity));
  88. if (!array_keys($choices, $valueObject)) {
  89. $choices[$valueObject] = array();
  90. }
  91. $choices[$valueObject][] = $id;
  92. }
  93. $finalChoices = array();
  94. foreach ($choices as $valueObject => $idx) {
  95. if (count($idx) > 1) { // avoid issue with identical values ...
  96. foreach ($idx as $id) {
  97. $finalChoices[sprintf('%s (id: %s)', $valueObject, $id)] = $id;
  98. }
  99. } else {
  100. $finalChoices[$valueObject] = current($idx);
  101. }
  102. }
  103. $this->choiceList = new ArrayChoiceList($finalChoices, $value);
  104. }
  105. return $this->choiceList;
  106. }
  107. /**
  108. * {@inheritdoc}
  109. */
  110. public function loadChoicesForValues(array $values, $value = null)
  111. {
  112. return $this->loadChoiceList($value)->getChoicesForValues($values);
  113. }
  114. /**
  115. * {@inheritdoc}
  116. */
  117. public function loadValuesForChoices(array $choices, $value = null)
  118. {
  119. return $this->loadChoiceList($value)->getValuesForChoices($choices);
  120. }
  121. /**
  122. * @param object $entity
  123. *
  124. * @return array
  125. */
  126. private function getIdentifierValues($entity)
  127. {
  128. try {
  129. return $this->modelManager->getIdentifierValues($entity);
  130. } catch (\Exception $e) {
  131. throw new \InvalidArgumentException(sprintf('Unable to retrieve the identifier values for entity %s', ClassUtils::getClass($entity)), 0, $e);
  132. }
  133. }
  134. }