123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- <?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\Show;
- use Sonata\AdminBundle\Admin\AdminInterface;
- use Sonata\AdminBundle\Admin\FieldDescriptionCollection;
- use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
- use Sonata\AdminBundle\Builder\ShowBuilderInterface;
- use Sonata\AdminBundle\Mapper\BaseGroupedMapper;
- /**
- * This class is used to simulate the Form API.
- *
- * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
- */
- class ShowMapper extends BaseGroupedMapper
- {
- protected $list;
- /**
- * @param ShowBuilderInterface $showBuilder
- * @param FieldDescriptionCollection $list
- * @param AdminInterface $admin
- */
- public function __construct(ShowBuilderInterface $showBuilder, FieldDescriptionCollection $list, AdminInterface $admin)
- {
- parent::__construct($showBuilder, $admin);
- $this->list = $list;
- }
- /**
- * @throws \RuntimeException
- *
- * @param mixed $name
- * @param mixed $type
- * @param array $fieldDescriptionOptions
- *
- * @return $this
- */
- public function add($name, $type = null, array $fieldDescriptionOptions = array())
- {
- if ($this->apply !== null && !$this->apply) {
- return $this;
- }
- $fieldKey = ($name instanceof FieldDescriptionInterface) ? $name->getName() : $name;
- $this->addFieldToCurrentGroup($fieldKey);
- if ($name instanceof FieldDescriptionInterface) {
- $fieldDescription = $name;
- $fieldDescription->mergeOptions($fieldDescriptionOptions);
- } elseif (is_string($name)) {
- if (!$this->admin->hasShowFieldDescription($name)) {
- $fieldDescription = $this->admin->getModelManager()->getNewFieldDescriptionInstance(
- $this->admin->getClass(),
- $name,
- $fieldDescriptionOptions
- );
- } else {
- throw new \RuntimeException(sprintf('Duplicate field name "%s" in show mapper. Names should be unique.', $name));
- }
- } else {
- throw new \RuntimeException('invalid state');
- }
- if (!$fieldDescription->getLabel() && false !== $fieldDescription->getOption('label')) {
- $fieldDescription->setOption('label', $this->admin->getLabelTranslatorStrategy()->getLabel($fieldDescription->getName(), 'show', 'label'));
- }
- $fieldDescription->setOption('safe', $fieldDescription->getOption('safe', false));
- // 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->removeShowFieldDescription($key);
- $this->list->remove($key);
- return $this;
- }
- /**
- * Removes a group.
- *
- * @param string $group The group to delete
- * @param string $tab The tab the group belongs to, defaults to 'default'
- * @param bool $deleteEmptyTab Whether or not the parent Tab should be deleted too,
- * when the deleted group leaves the tab empty after deletion
- *
- * @return $this
- */
- public function removeGroup($group, $tab = 'default', $deleteEmptyTab = false)
- {
- $groups = $this->getGroups();
- // When the default tab is used, the tabname is not prepended to the index in the group array
- if ($tab !== 'default') {
- $group = $tab.'.'.$group;
- }
- if (isset($groups[$group])) {
- foreach ($groups[$group]['fields'] as $field) {
- $this->remove($field);
- }
- }
- unset($groups[$group]);
- $tabs = $this->getTabs();
- $key = array_search($group, $tabs[$tab]['groups']);
- if (false !== $key) {
- unset($tabs[$tab]['groups'][$key]);
- }
- if ($deleteEmptyTab && count($tabs[$tab]['groups']) == 0) {
- unset($tabs[$tab]);
- }
- $this->setTabs($tabs);
- $this->setGroups($groups);
- return $this;
- }
- /**
- * {@inheritdoc}
- */
- final public function keys()
- {
- return array_keys($this->list->getElements());
- }
- /**
- * {@inheritdoc}
- */
- public function reorder(array $keys)
- {
- $this->admin->reorderShowGroup($this->getCurrentGroupName(), $keys);
- return $this;
- }
- /**
- * {@inheritdoc}
- */
- protected function getGroups()
- {
- return $this->admin->getShowGroups();
- }
- /**
- * {@inheritdoc}
- */
- protected function setGroups(array $groups)
- {
- $this->admin->setShowGroups($groups);
- }
- /**
- * {@inheritdoc}
- */
- protected function getTabs()
- {
- return $this->admin->getShowTabs();
- }
- /**
- * {@inheritdoc}
- */
- protected function setTabs(array $tabs)
- {
- $this->admin->setShowTabs($tabs);
- }
- /**
- * {@inheritdoc}
- */
- protected function getName()
- {
- return 'show';
- }
- }
|