瀏覽代碼

FD3-450 eliminación de nodo con hijos

Guillermo Espinoza 7 年之前
父節點
當前提交
39eefca440

+ 54 - 0
app/Resources/SonataAdminBundle/views/CRUD/delete.html.twig

@@ -0,0 +1,54 @@
+{#
+
+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.
+
+#}
+
+{% extends base_template %}
+
+{%- block actions -%}
+    {% include 'SonataAdminBundle:CRUD:action_buttons.html.twig' %}
+{%- endblock -%}
+
+{% block tab_menu %}{{ knp_menu_render(admin.sidemenu(action), {'currentClass' : 'active', 'template': sonata_admin.adminPool.getTemplate('tab_menu_template')}, 'twig') }}{% endblock %}
+
+{% block content %}
+    <div class="sonata-ba-delete">
+
+        <div class="box box-danger">
+            <div class="box-header">
+                <h3 class="box-title">{{ 'title_delete'|trans({}, 'SonataAdminBundle') }}</h3>
+            </div>
+            <div class="box-body">
+                {{ 'message_delete_confirmation'|trans({'%object%': admin.toString(object)}, 'SonataAdminBundle') }}
+
+                {# Muestro mensaje con las entidades relacionadas a eliminar #}
+                {% set pre_remove_entities_message = pre_remove_entities_message(object) %}
+                {% if pre_remove_entities_message != '' %}
+                <br /><br />{{ pre_remove_entities_message|raw }}
+                {% endif %}
+
+            </div>
+            <div class="box-footer clearfix">
+                <form method="POST" action="{{ admin.generateObjectUrl('delete', object) }}">
+                    <input type="hidden" name="_method" value="DELETE">
+                    <input type="hidden" name="_sonata_csrf_token" value="{{ csrf_token }}">
+
+                    <button type="submit" class="btn btn-danger"><i class="fa fa-trash" aria-hidden="true"></i> {{ 'btn_delete'|trans({}, 'SonataAdminBundle') }}</button>
+
+                    {% if admin.hasRoute('edit') and admin.hasAccess('edit', object) %}
+                        {{ 'delete_or'|trans({}, 'SonataAdminBundle') }}
+                        <a class="btn btn-success" href="{{ admin.generateObjectUrl('edit', object) }}">
+                            <i class="fa fa-pencil" aria-hidden="true"></i>
+                            {{ 'link_action_edit'|trans({}, 'SonataAdminBundle') }}</a>
+                    {% endif %}
+                </form>
+            </div>
+        </div>
+    </div>
+{% endblock %}

+ 19 - 1
src/CablemodemBundle/Admin/NodeAdmin.php

@@ -3,10 +3,13 @@
 namespace CablemodemBundle\Admin;
 
 use Base\AdminBundle\Admin\BaseAdmin;
+use CablemodemBundle\Entity\Node;
+use Doctrine\ORM\EntityRepository;
 use Sonata\AdminBundle\Datagrid\DatagridMapper;
 use Sonata\AdminBundle\Datagrid\ListMapper;
 use Sonata\AdminBundle\Form\FormMapper;
 use Sonata\AdminBundle\Show\ShowMapper;
+use Symfony\Bridge\Doctrine\Form\Type\EntityType;
 
 class NodeAdmin extends BaseAdmin
 {
@@ -43,9 +46,24 @@ class NodeAdmin extends BaseAdmin
      */
     protected function configureFormFields(FormMapper $formMapper)
     {
+        $id = $this->getSubject()->getId();
         $formMapper
             ->add('name')
-            ->add('parent')
+            ->add('parent', EntityType::class, [
+                'required' => false,
+                'class' => Node::class,
+                'query_builder' => function (EntityRepository $er) use ($id) {
+                    $qb = $er->createQueryBuilder('n')
+                            ->orderBy('n.name', 'ASC');
+
+                    if ($id) {
+                        $qb->where('n.id <> :id')
+                        ->setParameter('id', $id);
+                    }
+
+                    return $qb;
+                },
+            ])
         ;
     }
 

+ 29 - 14
src/CablemodemBundle/Entity/Node.php

@@ -2,6 +2,7 @@
 
 namespace CablemodemBundle\Entity;
 
+use Base\AdminBundle\Interfaces\PreRemoveInterface;
 use Doctrine\Common\Collections\ArrayCollection;
 use Doctrine\ORM\Mapping as ORM;
 use JMS\Serializer\Annotation as JMS;
@@ -13,10 +14,10 @@ use Base\AdminBundle\Traits\TenancyIdTraitInterface;
 /**
  * @ORM\Entity
  * @ORM\Table(uniqueConstraints={@ORM\UniqueConstraint(name="unique_idx", columns={"name","tenancy_id"})})
- * 
+ *
  * @UniqueEntity(fields={"tenancyId","name"})
  */
-class Node implements TenancyIdTraitInterface
+class Node implements TenancyIdTraitInterface, PreRemoveInterface
 {
 
     use TenancyIdTrait;
@@ -30,21 +31,22 @@ class Node implements TenancyIdTraitInterface
 
     /**
      * @ORM\Column(type="string", length=100)
-     * 
+     *
      * @Assert\NotNull
      */
     protected $name;
 
     /**
-     * @ORM\ManyToOne(targetEntity="Node")
-     * 
+     * @ORM\ManyToOne(targetEntity="Node", inversedBy="childs")
+     * @ORM\JoinColumn(onDelete="CASCADE")
+     *
      * @JMS\MaxDepth(1)
      */
     protected $parent;
 
     /**
      * @ORM\OneToMany(targetEntity="Node", mappedBy="parent")
-     * 
+     *
      * @JMS\MaxDepth(1)
      */
     protected $childs;
@@ -59,7 +61,7 @@ class Node implements TenancyIdTraitInterface
     }
 
     /**
-     * 
+     *
      * @return string
      */
     public function __toString()
@@ -68,7 +70,7 @@ class Node implements TenancyIdTraitInterface
     }
 
     /**
-     * @return bigint 
+     * @return bigint
      */
     public function getId()
     {
@@ -77,7 +79,7 @@ class Node implements TenancyIdTraitInterface
 
     /**
      * @param string $name
-     * 
+     *
      * @return Node
      */
     public function setName($name)
@@ -88,7 +90,7 @@ class Node implements TenancyIdTraitInterface
     }
 
     /**
-     * @return string 
+     * @return string
      */
     public function getName()
     {
@@ -97,7 +99,7 @@ class Node implements TenancyIdTraitInterface
 
     /**
      * @param Node $parent
-     * 
+     *
      * @return Node
      */
     public function setParent($parent = null)
@@ -108,7 +110,7 @@ class Node implements TenancyIdTraitInterface
     }
 
     /**
-     * @return Node 
+     * @return Node
      */
     public function getParent()
     {
@@ -117,7 +119,7 @@ class Node implements TenancyIdTraitInterface
 
     /**
      * @param Node $childs
-     * 
+     *
      * @return Node
      */
     public function addChild($childs)
@@ -136,11 +138,24 @@ class Node implements TenancyIdTraitInterface
     }
 
     /**
-     * @return Doctrine\Common\Collections\Collection 
+     * @return Doctrine\Common\Collections\Collection
      */
     public function getChilds()
     {
         return $this->childs;
     }
 
+    /**
+     * @return array
+     */
+    public function getEntitiesForRemove()
+    {
+        $entities = [];
+        if ($this->childs->count() != 0) {
+            $entities['childs'] = $this->childs;
+        }
+
+        return $entities;
+    }
+
 }