123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- <?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\AdminBundle\Datagrid;
- use Sonata\AdminBundle\Admin\AdminInterface;
- use Sonata\AdminBundle\Admin\FieldDescriptionCollection;
- use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
- use Sonata\AdminBundle\Builder\ListBuilderInterface;
- use Sonata\AdminBundle\Mapper\BaseMapper;
- /**
- * This class is used to simulate the Form API.
- *
- * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
- */
- class ListMapper extends BaseMapper
- {
- /**
- * @var FieldDescriptionCollection
- */
- protected $list;
- /**
- * @param ListBuilderInterface $listBuilder
- * @param FieldDescriptionCollection $list
- * @param AdminInterface $admin
- */
- public function __construct(ListBuilderInterface $listBuilder, FieldDescriptionCollection $list, AdminInterface $admin)
- {
- parent::__construct($listBuilder, $admin);
- $this->list = $list;
- }
- /**
- * @param string $name
- * @param null $type
- * @param array $fieldDescriptionOptions
- *
- * @return ListMapper
- */
- public function addIdentifier($name, $type = null, array $fieldDescriptionOptions = array())
- {
- $fieldDescriptionOptions['identifier'] = true;
- if (!isset($fieldDescriptionOptions['route']['name'])) {
- $routeName = ($this->admin->hasAccess('edit') && $this->admin->hasRoute('edit')) ? 'edit' : 'show';
- $fieldDescriptionOptions['route']['name'] = $routeName;
- }
- if (!isset($fieldDescriptionOptions['route']['parameters'])) {
- $fieldDescriptionOptions['route']['parameters'] = array();
- }
- return $this->add($name, $type, $fieldDescriptionOptions);
- }
- /**
- * @throws \RuntimeException
- *
- * @param mixed $name
- * @param mixed $type
- * @param array $fieldDescriptionOptions
- *
- * @return ListMapper
- */
- public function add($name, $type = null, array $fieldDescriptionOptions = array())
- {
- // Change deprecated inline action "view" to "show"
- if ($name == '_action' && $type == 'actions') {
- if (isset($fieldDescriptionOptions['actions']['view'])) {
- @trigger_error(
- 'Inline action "view" is deprecated since version 2.2.4 and will be removed in 4.0. '
- .'Use inline action "show" instead.',
- E_USER_DEPRECATED
- );
- $fieldDescriptionOptions['actions']['show'] = $fieldDescriptionOptions['actions']['view'];
- unset($fieldDescriptionOptions['actions']['view']);
- }
- }
- // Ensure batch and action pseudo-fields are tagged as virtual
- if (in_array($type, array('actions', 'batch', 'select'))) {
- $fieldDescriptionOptions['virtual_field'] = true;
- }
- if ($name instanceof FieldDescriptionInterface) {
- $fieldDescription = $name;
- $fieldDescription->mergeOptions($fieldDescriptionOptions);
- } elseif (is_string($name)) {
- if ($this->admin->hasListFieldDescription($name)) {
- throw new \RuntimeException(sprintf(
- 'Duplicate field name "%s" in list mapper. Names should be unique.',
- $name
- ));
- }
- $fieldDescription = $this->admin->getModelManager()->getNewFieldDescriptionInstance(
- $this->admin->getClass(),
- $name,
- $fieldDescriptionOptions
- );
- } else {
- throw new \RuntimeException(
- 'Unknown field name in list mapper. '
- .'Field name should be either of FieldDescriptionInterface interface or string.'
- );
- }
- if ($fieldDescription->getLabel() === null) {
- $fieldDescription->setOption(
- 'label',
- $this->admin->getLabelTranslatorStrategy()->getLabel($fieldDescription->getName(), 'list', 'label')
- );
- }
- // add the field with the FormBuilder
- $this->builder->addField($this->list, $type, $fieldDescription, $this->admin);
- return $this;
- }
- /**
- * {@inheritdoc}
- */
- public function get($name)
- {
- return $this->list->get($name);
- }
- /**
- * {@inheritdoc}
- */
- public function has($key)
- {
- return $this->list->has($key);
- }
- /**
- * {@inheritdoc}
- */
- public function remove($key)
- {
- $this->admin->removeListFieldDescription($key);
- $this->list->remove($key);
- return $this;
- }
- /**
- * {@inheritdoc}
- */
- final public function keys()
- {
- return array_keys($this->list->getElements());
- }
- /**
- * {@inheritdoc}
- */
- public function reorder(array $keys)
- {
- $this->list->reorder($keys);
- return $this;
- }
- }
|