Ver código fonte

Restore the deleted feature on one to many

Thomas Rabaix 14 anos atrás
pai
commit
95ad431536

+ 0 - 2
Controller/CoreController.php

@@ -15,8 +15,6 @@ use Symfony\Bundle\FrameworkBundle\Controller\Controller;
 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
 use Symfony\Component\HttpFoundation\Response;
 
-use Sonata\AdminBundle\Form\RecursiveFieldIterator;
-
 class CoreController extends Controller
 {
     public function getBaseTemplate()

+ 0 - 2
Controller/HelperController.php

@@ -15,8 +15,6 @@ use Symfony\Bundle\FrameworkBundle\Controller\Controller;
 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
 use Symfony\Component\HttpFoundation\Response;
 
-use Sonata\AdminBundle\Form\RecursiveFieldIterator;
-
 class HelperController extends Controller
 {
 

+ 0 - 127
Form/EditableCollectionField.php

@@ -1,127 +0,0 @@
-<?php
-
-namespace Sonata\AdminBundle\Form;
-
-/*
- * This file is part of the Symfony framework.
- *
- * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
- *
- * This source file is subject to the MIT license that is bundled
- * with this source code in the file LICENSE.
- */
-
-use Symfony\Component\Form\Form;
-use Symfony\Component\Form\CheckboxField;
-use Symfony\Component\Form\FieldInterface;
-use Symfony\Component\Form\Exception\UnexpectedTypeException;
-
-/**
- * @author     Thomas Rabaix <thomas.rabaix@sonata-project.com>
- * @author     Bernhard Schussek <bernhard.schussek@symfony-project.com>
- */
-class EditableCollectionField extends Form
-{
-    /**
-     * The prototype for the inner fields
-     * @var FieldInterface
-     */
-    protected $prototype;
-
-    /**
-     * Remembers which fields were removed upon binding
-     * @var array
-     */
-    protected $removedFields = array();
-
-    /**
-     * Repeats the given field twice to verify the user's input
-     *
-     * @param FieldInterface $innerField
-     */
-    public function __construct(FieldInterface $innerField, array $options = array())
-    {
-        $this->prototype = $innerField;
-
-        parent::__construct($innerField->getKey(), $options);
-    }
-
-    
-    public function submit($taintedData)
-    {
-        $this->removedFields = array();
-
-        if (null === $taintedData) {
-            $taintedData = array();
-        }
-
-        foreach ($this as $name => $field) {
-            if (!isset($taintedData[$name]) || array_key_exists('_delete', $taintedData[$name])) {
-                $this->remove($name);
-                $this->removedFields[] = $name;
-            }
-        }
-
-        // add new element (+ icon)
-        foreach ($taintedData as $name => $value) {
-            if (!isset($this[$name])) {
-                $this->addField($name, $name);
-            }
-        }
-
-        parent::submit($taintedData);
-    }
-
-    /**
-     * Add a new element to the collection
-     *
-     * @param string $key
-     * @param string $propertyPath
-     */
-    public function addField($key, $propertyPath)
-    {
-        $this->add($this->newfield($key, $propertyPath));
-    }
-
-    /**
-     *
-     * @return the FieldGroup prototype used to generate the collection
-     */
-    public function getPrototype()
-    {
-        return $this->prototype;
-    }
-
-    protected function newField($key, $propertyPath)
-    {
-        $field = clone $this->prototype;
-    
-        $field->setKey($key);
-        $field->setPropertyPath(null === $propertyPath ? null : '['.$propertyPath.']');
-        
-        return $field;
-    }
-
-    public function setData($collection)
-    {
-        if (!is_array($collection) && !$collection instanceof \Traversable) {
-            throw new UnexpectedTypeException($collection, 'array or \Traversable');
-        }
-
-        foreach ($collection as $name => $value) {
-            $this->add($this->newField($name, $name));
-        }
-
-        parent::setData($collection);
-    }
-
-    protected function writeObject(&$objectOrArray)
-    {
-
-        parent::writeObject($objectOrArray);
-
-        foreach ($this->removedFields as $name) {
-            unset($objectOrArray[$name]);
-        }
-    }
-}

+ 0 - 73
Form/EditableFieldGroup.php

@@ -1,73 +0,0 @@
-<?php
-
-namespace Sonata\AdminBundle\Form;
-
-/*
- * This file is part of the Symfony framework.
- *
- * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
- *
- * This source file is subject to the MIT license that is bundled
- * with this source code in the file LICENSE.
- */
-
-use Symfony\Component\Form\Form;
-use Symfony\Component\Form\CheckboxField;
-use Symfony\Component\Form\TextField;
-use Symfony\Component\Form\RecursiveFieldIterator;
-use Symfony\Component\Form\FieldInterface;
-
-/**
- * @author     Bernhard Schussek <bernhard.schussek@symfony-project.com>
- */
-class EditableFieldGroup extends Form
-{
-
-    /**
-     * @inheritDoc
-     */
-    public function __construct($key, array $options = array())
-    {
-
-        $this->add(new CheckboxField('_delete', array(
-            'required' => false
-        )));
-
-        parent::__construct($key, $options);
-    }
-
-    /**
-     * @inheritDoc
-     */
-    protected function readObject(&$objectOrArray)
-    {
-        $iterator = new RecursiveFieldIterator($this);
-        $iterator = new \RecursiveIteratorIterator($iterator);
-
-        foreach ($iterator as $field) {
-            if ($field->getKey() == '_delete') {
-                continue;
-            }
-
-            $field->readProperty($objectOrArray);
-        }
-    }
-
-    /**
-     * @inheritDoc
-     */
-    protected function writeObject(&$objectOrArray)
-    {
-        $iterator = new RecursiveFieldIterator($this);
-        $iterator = new \RecursiveIteratorIterator($iterator);
-
-        foreach ($iterator as $field) {
-            if ($field->getKey() == '_delete') {
-                continue;
-            }
-
-            $field->writeProperty($objectOrArray);
-        }
-    }
-
-}

+ 11 - 0
Form/EventListener/ResizeFormListener.php

@@ -42,6 +42,8 @@ class ResizeFormListener implements EventSubscriberInterface
 
     private $typeOptions;
 
+    private $removed = array();
+
     public function __construct(FormFactoryInterface $factory, $type, array $typeOptions = array(), $resizeOnBind = false)
     {
         $this->factory = $factory;
@@ -117,6 +119,10 @@ class ResizeFormListener implements EventSubscriberInterface
 
                 $form->add($this->factory->createNamed($this->type, $name, null, $options));
             }
+
+            if (isset($value['_delete'])) {
+                $this->removed[] = $name;
+            }
         }
     }
 
@@ -143,6 +149,11 @@ class ResizeFormListener implements EventSubscriberInterface
             }
         }
 
+        // remove selected elements
+        foreach($this->removed as $pos) {
+            unset($data[$pos]);
+        }
+
         $event->setData($data);
     }
 }

+ 0 - 44
Form/RecursiveFieldIterator.php

@@ -1,44 +0,0 @@
-<?php
-
-/*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
-
-namespace Sonata\AdminBundle\Form;
-
-use Symfony\Component\Form\FormInterface;
-    
-/**
- * Iterator that traverses fields of a field group
- *
- * If the iterator encounters a virtual field group, it enters the field
- * group and traverses its children as well.
- *
- * @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
- */
-class RecursiveFieldIterator extends \IteratorIterator implements \RecursiveIterator
-{
-   protected $counter  = 0;
-    
-    public function __construct(FormInterface $group)
-    {
-        parent::__construct($group);
-    }
-
-    public function getChildren()
-    {
-
-        return new self($this->current());
-    }
-
-    public function hasChildren()
-    {
-
-        return $this->current() instanceof FormInterface;
-    }
-}

+ 5 - 0
Form/Type/AdminType.php

@@ -27,6 +27,10 @@ class AdminType extends AbstractType
     public function buildForm(FormBuilder $builder, array $options)
     {
         $admin = $this->getAdmin($options);
+        if ($options['delete']) {
+            $builder->add('_delete', 'checkbox', array('required' => false, 'property_path' => false));
+        }
+
         $admin->defineFormBuilder($builder);
 
         $builder->prependClientTransformer(new ArrayToModelTransformer($admin->getModelManager(), $admin->getClass()));
@@ -40,6 +44,7 @@ class AdminType extends AbstractType
     {
         return array(
             'field_description' => null,
+            'delete'            => true,
         );
     }