123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- <?php
- /*
- * This file is part of the Sonata package.
- *
- * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Sonata\DoctrineORMAdminBundle\Builder;
- use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
- use Sonata\AdminBundle\Admin\AdminInterface;
- use Sonata\AdminBundle\Admin\FieldDescriptionCollection;
- use Sonata\AdminBundle\Builder\ShowBuilderInterface;
- use Sonata\AdminBundle\Guesser\TypeGuesserInterface;
- use Doctrine\ORM\Mapping\ClassMetadataInfo;
- class ShowBuilder implements ShowBuilderInterface
- {
- protected $guesser;
- protected $templates;
- /**
- * @param \Sonata\AdminBundle\Guesser\TypeGuesserInterface $guesser
- * @param array $templates
- */
- public function __construct(TypeGuesserInterface $guesser, array $templates)
- {
- $this->guesser = $guesser;
- $this->templates = $templates;
- }
- /**
- * @param array $options
- *
- * @return \Sonata\AdminBundle\Admin\FieldDescriptionCollection
- */
- public function getBaseList(array $options = array())
- {
- return new FieldDescriptionCollection;
- }
- /**
- * @param \Sonata\AdminBundle\Admin\FieldDescriptionCollection $list
- * @param null $type
- * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
- * @param \Sonata\AdminBundle\Admin\AdminInterface $admin
- *
- * @return mixed
- */
- public function addField(FieldDescriptionCollection $list, $type = null, FieldDescriptionInterface $fieldDescription, AdminInterface $admin)
- {
- if ($type == null) {
- $guessType = $this->guesser->guessType($admin->getClass(), $fieldDescription->getName(), $admin->getModelManager());
- $fieldDescription->setType($guessType->getType());
- } else {
- $fieldDescription->setType($type);
- }
- $this->fixFieldDescription($admin, $fieldDescription);
- $admin->addShowFieldDescription($fieldDescription->getName(), $fieldDescription);
- $list->add($fieldDescription);
- }
- /**
- * @param string $type
- *
- * @return string
- */
- private function getTemplate($type)
- {
- if (!isset($this->templates[$type])) {
- return null;
- }
- return $this->templates[$type];
- }
- /**
- * The method defines the correct default settings for the provided FieldDescription
- *
- * @param \Sonata\AdminBundle\Admin\AdminInterface $admin
- * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
- *
- * @return void
- */
- public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription)
- {
- $fieldDescription->setAdmin($admin);
- if ($admin->getModelManager()->hasMetadata($admin->getClass())) {
- list($metadata, $lastPropertyName, $parentAssociationMappings) = $admin->getModelManager()->getParentMetadataForProperty($admin->getClass(), $fieldDescription->getName());
- $fieldDescription->setParentAssociationMappings($parentAssociationMappings);
- // set the default field mapping
- if (isset($metadata->fieldMappings[$lastPropertyName])) {
- $fieldDescription->setFieldMapping($metadata->fieldMappings[$lastPropertyName]);
- }
- // set the default association mapping
- if (isset($metadata->associationMappings[$lastPropertyName])) {
- $fieldDescription->setAssociationMapping($metadata->associationMappings[$lastPropertyName]);
- }
- }
- if (!$fieldDescription->getType()) {
- throw new \RuntimeException(sprintf('Please define a type for field `%s` in `%s`', $fieldDescription->getName(), get_class($admin)));
- }
- $fieldDescription->setOption('code', $fieldDescription->getOption('code', $fieldDescription->getName()));
- $fieldDescription->setOption('label', $fieldDescription->getOption('label', $fieldDescription->getName()));
- if (!$fieldDescription->getTemplate()) {
- $fieldDescription->setTemplate($this->getTemplate($fieldDescription->getType()));
- if ($fieldDescription->getMappingType() == ClassMetadataInfo::MANY_TO_ONE) {
- $fieldDescription->setTemplate('SonataDoctrineORMAdminBundle:CRUD:show_orm_many_to_one.html.twig');
- }
- if ($fieldDescription->getMappingType() == ClassMetadataInfo::ONE_TO_ONE) {
- $fieldDescription->setTemplate('SonataDoctrineORMAdminBundle:CRUD:show_orm_one_to_one.html.twig');
- }
- if ($fieldDescription->getMappingType() == ClassMetadataInfo::ONE_TO_MANY) {
- $fieldDescription->setTemplate('SonataDoctrineORMAdminBundle:CRUD:show_orm_one_to_many.html.twig');
- }
- if ($fieldDescription->getMappingType() == ClassMetadataInfo::MANY_TO_MANY) {
- $fieldDescription->setTemplate('SonataDoctrineORMAdminBundle:CRUD:show_orm_many_to_many.html.twig');
- }
- }
- if ($fieldDescription->getMappingType() == ClassMetadataInfo::MANY_TO_ONE) {
- $admin->attachAdminClass($fieldDescription);
- }
- if ($fieldDescription->getMappingType() == ClassMetadataInfo::ONE_TO_ONE) {
- $admin->attachAdminClass($fieldDescription);
- }
- if ($fieldDescription->getMappingType() == ClassMetadataInfo::ONE_TO_MANY) {
- $admin->attachAdminClass($fieldDescription);
- }
- if ($fieldDescription->getMappingType() == ClassMetadataInfo::MANY_TO_MANY) {
- $admin->attachAdminClass($fieldDescription);
- }
- }
- }
|