/vendor/sonata-project/doctrine-orm-admin-bundle/Builder/ListBuilder.php
PHP | 193 lines | 120 code | 31 blank | 42 comment | 21 complexity | f5bcede842dc460ceac052d308334362 MD5 | raw file
- <?php
- /*
- * This file is part of the Sonata Project 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 Doctrine\ORM\Mapping\ClassMetadataInfo;
- use Sonata\AdminBundle\Admin\AdminInterface;
- use Sonata\AdminBundle\Admin\FieldDescriptionCollection;
- use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
- use Sonata\AdminBundle\Builder\ListBuilderInterface;
- use Sonata\AdminBundle\Guesser\TypeGuesserInterface;
- class ListBuilder implements ListBuilderInterface
- {
- /**
- * @var TypeGuesserInterface
- */
- protected $guesser;
- /**
- * @var string[]
- */
- protected $templates = array();
- /**
- * @param TypeGuesserInterface $guesser
- * @param string[] $templates
- */
- public function __construct(TypeGuesserInterface $guesser, array $templates = array())
- {
- $this->guesser = $guesser;
- $this->templates = $templates;
- }
- /**
- * {@inheritdoc}
- */
- public function getBaseList(array $options = array())
- {
- return new FieldDescriptionCollection();
- }
- /**
- * {@inheritdoc}
- */
- public function buildField($type, 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);
- }
- /**
- * {@inheritdoc}
- */
- public function addField(FieldDescriptionCollection $list, $type, FieldDescriptionInterface $fieldDescription, AdminInterface $admin)
- {
- $this->buildField($type, $fieldDescription, $admin);
- $admin->addListFieldDescription($fieldDescription->getName(), $fieldDescription);
- $list->add($fieldDescription);
- }
- /**
- * @param string $type
- *
- * @return string
- */
- private function getTemplate($type)
- {
- if (!isset($this->templates[$type])) {
- return;
- }
- return $this->templates[$type];
- }
- /**
- * {@inheritdoc}
- */
- public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription)
- {
- if ($fieldDescription->getName() == '_action') {
- $this->buildActionFieldDescription($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]);
- if ($fieldDescription->getOption('sortable') !== false) {
- $fieldDescription->setOption('sortable', $fieldDescription->getOption('sortable', true));
- $fieldDescription->setOption('sort_parent_association_mappings', $fieldDescription->getOption('sort_parent_association_mappings', $fieldDescription->getParentAssociationMappings()));
- $fieldDescription->setOption('sort_field_mapping', $fieldDescription->getOption('sort_field_mapping', $fieldDescription->getFieldMapping()));
- }
- }
- // set the default association mapping
- if (isset($metadata->associationMappings[$lastPropertyName])) {
- $fieldDescription->setAssociationMapping($metadata->associationMappings[$lastPropertyName]);
- }
- $fieldDescription->setOption('_sort_order', $fieldDescription->getOption('_sort_order', 'ASC'));
- }
- 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->getTemplate()) {
- switch ($fieldDescription->getMappingType()) {
- case ClassMetadataInfo::MANY_TO_ONE:
- $fieldDescription->setTemplate('SonataDoctrineORMAdminBundle:CRUD:list_orm_many_to_one.html.twig');
- break;
- case ClassMetadataInfo::ONE_TO_ONE:
- $fieldDescription->setTemplate('SonataDoctrineORMAdminBundle:CRUD:list_orm_one_to_one.html.twig');
- break;
- case ClassMetadataInfo::ONE_TO_MANY:
- $fieldDescription->setTemplate('SonataDoctrineORMAdminBundle:CRUD:list_orm_one_to_many.html.twig');
- break;
- case ClassMetadataInfo::MANY_TO_MANY:
- $fieldDescription->setTemplate('SonataDoctrineORMAdminBundle:CRUD:list_orm_many_to_many.html.twig');
- break;
- }
- }
- }
- if (in_array($fieldDescription->getMappingType(), array(ClassMetadataInfo::MANY_TO_ONE, ClassMetadataInfo::ONE_TO_ONE, ClassMetadataInfo::ONE_TO_MANY, ClassMetadataInfo::MANY_TO_MANY))) {
- $admin->attachAdminClass($fieldDescription);
- }
- }
- /**
- * @param FieldDescriptionInterface $fieldDescription
- *
- * @return FieldDescriptionInterface
- */
- public function buildActionFieldDescription(FieldDescriptionInterface $fieldDescription)
- {
- if (null === $fieldDescription->getTemplate()) {
- $fieldDescription->setTemplate('SonataAdminBundle:CRUD:list__action.html.twig');
- }
- if (null === $fieldDescription->getType()) {
- $fieldDescription->setType('action');
- }
- if (null === $fieldDescription->getOption('name')) {
- $fieldDescription->setOption('name', 'Action');
- }
- if (null === $fieldDescription->getOption('code')) {
- $fieldDescription->setOption('code', 'Action');
- }
- if (null !== $fieldDescription->getOption('actions')) {
- $actions = $fieldDescription->getOption('actions');
- foreach ($actions as $k => $action) {
- if (!isset($action['template'])) {
- $actions[$k]['template'] = sprintf('SonataAdminBundle:CRUD:list__action_%s.html.twig', $k);
- }
- }
- $fieldDescription->setOption('actions', $actions);
- }
- return $fieldDescription;
- }
- }