123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- <?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\AdminBundle\Form;
- use Sonata\AdminBundle\Builder\FormContractorInterface;
- use Sonata\AdminBundle\Admin\AdminInterface;
- use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
- use Symfony\Component\Form\FormBuilder;
- /**
- * This class is use to simulate the Form API
- *
- */
- class FormMapper
- {
- protected $formBuilder;
- protected $formContractor;
- protected $admin;
- protected $currentGroup;
- /**
- * @param \Sonata\AdminBundle\Builder\FormContractorInterface $formContractor
- * @param \Symfony\Component\Form\FormBuilder $formBuilder
- * @param \Sonata\AdminBundle\Admin\AdminInterface $admin
- */
- public function __construct(FormContractorInterface $formContractor, FormBuilder $formBuilder, AdminInterface $admin)
- {
- $this->formBuilder = $formBuilder;
- $this->formContractor = $formContractor;
- $this->admin = $admin;
- }
- /**
- * @param string $name
- * @param array $options
- *
- * @return \Sonata\AdminBundle\Form\FormMapper
- */
- public function with($name, array $options = array())
- {
- $formGroups = $this->admin->getFormGroups();
- if (!isset($formGroups[$name])) {
- $formGroups[$name] = array();
- }
- $formGroups[$name] = array_merge(array(
- 'collapsed' => false,
- 'fields' => array()
- ), $formGroups[$name], $options);
- $this->admin->setFormGroups($formGroups);
- $this->currentGroup = $name;
- return $this;
- }
- /**
- * @return \Sonata\AdminBundle\Form\FormMapper
- */
- public function end()
- {
- $this->currentGroup = null;
- return $this;
- }
- /**
- * @param array $keys field names
- *
- * @return \Sonata\AdminBundle\Form\FormMapper
- */
- public function reorder(array $keys)
- {
- if (!$this->currentGroup) {
- $this->with($this->admin->getLabel());
- }
- $this->admin->reorderFormGroup($this->currentGroup, $keys);
- return $this;
- }
- /**
- * @param string $name
- * @param string $type
- * @param array $options
- * @param array $fieldDescriptionOptions
- *
- * @return \Sonata\AdminBundle\Form\FormMapper
- */
- public function add($name, $type = null, array $options = array(), array $fieldDescriptionOptions = array())
- {
- if (!$this->currentGroup) {
- $this->with($this->admin->getLabel());
- }
- $label = $name instanceof FormBuilder ? $name->getName() : $name;
- $formGroups = $this->admin->getFormGroups();
- $formGroups[$this->currentGroup]['fields'][$label] = $label;
- $this->admin->setFormGroups($formGroups);
- if (!isset($fieldDescriptionOptions['type']) && is_string($type)) {
- $fieldDescriptionOptions['type'] = $type;
- }
- $fieldDescription = $this->admin->getModelManager()->getNewFieldDescriptionInstance(
- $this->admin->getClass(),
- $name instanceof FormBuilder ? $name->getName() : $name,
- $fieldDescriptionOptions
- );
- $this->formContractor->fixFieldDescription($this->admin, $fieldDescription, $fieldDescriptionOptions);
- $this->admin->addFormFieldDescription($name instanceof FormBuilder ? $name->getName() : $name, $fieldDescription);
- if ($name instanceof FormBuilder) {
- $this->formBuilder->add($name);
- } else {
- $options = array_replace_recursive($this->formContractor->getDefaultOptions($type, $fieldDescription), $options);
- if (!isset($options['label'])) {
- $options['label'] = $this->admin->getLabelTranslatorStrategy()->getLabel($fieldDescription->getName(), 'form', 'label');
- }
- $help = null;
- if (isset($options['help'])) {
- $help = $options['help'];
- unset($options['help']);
- }
- $this->formBuilder->add($name, $type, $options);
- if (null !== $help) {
- $this->admin->getFormFieldDescription($name)->setHelp($help);
- }
- }
- return $this;
- }
- /**
- * @param string $name
- *
- * @return \Symfony\Component\Form\FormInterface
- */
- public function get($name)
- {
- return $this->formBuilder->get($name);
- }
- /**
- * @param string $key
- *
- * @return boolean
- */
- public function has($key)
- {
- return $this->formBuilder->has($key);
- }
- /**
- * @param string $key
- *
- * @return \Sonata\AdminBundle\Form\FormMapper
- */
- public function remove($key)
- {
- $this->admin->removeFormFieldDescription($key);
- $this->formBuilder->remove($key);
- return $this;
- }
- /**
- * @return \Symfony\Component\Form\FormBuilder
- */
- public function getFormBuilder()
- {
- return $this->formBuilder;
- }
- /**
- * @return \Sonata\AdminBundle\Admin\AdminInterface
- */
- public function getAdmin()
- {
- return $this->admin;
- }
- /**
- * @param string $name
- * @param mixed $type
- * @param array $options
- *
- * @return \Symfony\Component\Form\FormBuilder
- */
- public function create($name, $type = null, array $options = array())
- {
- return $this->formBuilder->create($name, $type, $options);
- }
- /**
- * @param array $helps
- *
- * @return FormMapper
- */
- public function setHelps(array $helps = array())
- {
- foreach ($helps as $name => $help) {
- if ($this->admin->hasFormFieldDescription($name)) {
- $this->admin->getFormFieldDescription($name)->setHelp($help);
- }
- }
- return $this;
- }
- }
|