浏览代码

FD3-576 se agrega asociación de host con otros host. Se acomoda filtro de host y listado

Espinoza Guillermo 7 年之前
父节点
当前提交
2cbd1c2a1c

+ 43 - 2
src/HostBundle/Admin/HostAdmin.php

@@ -3,11 +3,14 @@
 namespace HostBundle\Admin;
 
 use Base\AdminBundle\Admin\BaseAdmin;
+use Doctrine\ORM\EntityRepository;
+use HostBundle\Entity\Host  ;
 use HostBundle\Utils\HostStatus;
 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;
 use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
 
 class HostAdmin extends BaseAdmin
@@ -19,8 +22,24 @@ class HostAdmin extends BaseAdmin
     {
         $datagridMapper
             ->add('mac')
-            ->add('state')
-        ;
+            ->add('hostType')
+            ->add('state', 'doctrine_orm_choice', [], 'choice', [
+                'choices' => HostStatus::getChoices(),
+            ])
+            ->add('options', 'doctrine_orm_callback', [
+                'callback' => function($queryBuilder, $alias, $field, $value) {
+                    if (!$value['value']) {
+                        return;
+                    }
+
+                    $queryBuilder
+                        ->andWhere("{$alias}.options LIKE :fixed_address")
+                        ->setParameter('fixed_address', "%\"fixed_address\":\"{$value['value']}%");
+
+                    return true;
+                },
+                'label' => 'filter.label_options_fixed_address',
+            ]);
     }
 
     /**
@@ -32,6 +51,8 @@ class HostAdmin extends BaseAdmin
             ->add('mac')
             ->add('hostType')
             ->add('state')
+            ->add('options.fixed_address')
+            ->add('host')
             ->add('_action', null, array(
                 'actions' => array(
                     'show' => array(),
@@ -47,6 +68,8 @@ class HostAdmin extends BaseAdmin
      */
     protected function configureFormFields(FormMapper $formMapper)
     {
+        $subject = $this->getSubject();
+        
         $formMapper
             ->tab('Host')
             ->with('Host')
@@ -58,6 +81,23 @@ class HostAdmin extends BaseAdmin
                 'choices' => HostStatus::getChoices(),
                 'translation_domain' => 'HostBundle',
             ])
+            ->add('host', EntityType::class, [
+                'class' => Host::class,
+                'query_builder' => function (EntityRepository $er) use ($subject) {
+                    $qb = $er->createQueryBuilder('Host')
+                    ->join('Host.hostType', 'HostType')
+                    ->andWhere('HostType.name = \'Cablemodem\'')
+                    ->orderBy('Host.mac', 'ASC');
+                    
+                    $mac = $subject->getMac();
+                    if ($mac) {
+                        $qb->andWhere('Host.mac <> :mac')->setParameter('mac', $mac);
+                    }
+                    
+                    return $qb;
+                },
+                'required' => false,
+            ])
         ->end()
         ->end()
         ;
@@ -74,6 +114,7 @@ class HostAdmin extends BaseAdmin
             ->add('mac')
             ->add('hostType')
             ->add('state')
+            ->add('host')
         ->end()
         ->end()
         ;

+ 29 - 1
src/HostBundle/Entity/Host.php

@@ -34,7 +34,7 @@ class Host implements WorkflowInterface
     /**
      * @var string $name
      *
-     * @ORM\Column(type="string", length=100, unique=true)
+     * @ORM\Column(type="string", length=100, unique=true, nullable=true)
      */
     protected $mac;
 
@@ -83,6 +83,14 @@ class Host implements WorkflowInterface
      * @ORM\Column(type="boolean", nullable=true)
      */
     protected $fixedIP = false;
+    
+    /**
+     * @ORM\ManyToOne(targetEntity="Host", fetch="EXTRA_LAZY")
+     * @ORM\JoinColumn(name="host_id", referencedColumnName="id", onDelete="SET NULL")
+     *
+     * @JMS\MaxDepth(1)
+     */
+    protected $host;
 
 
     /**
@@ -200,5 +208,25 @@ class Host implements WorkflowInterface
         
         return $this;
     }
+        
+    /**
+     * @return boolean
+     */
+    public function getHost()
+    {
+        return $this->host;
+    }
+    
+    /**
+     * @param Host $host
+     *
+     * @return Host
+     */
+    public function setHost($host = null)
+    {
+        $this->host = $host;
+        
+        return $this;
+    }
 
 }

+ 1 - 1
src/HostBundle/EventListener/AdminDHCPOption.php

@@ -52,8 +52,8 @@ class AdminDHCPOption
                     $fixedAddress = $subject->getFixedAddress();
                     if ($fixedAddress) {
                         $freeIP[] = $fixedAddress;
-                        sort($freeIP);
                     }
+                    sort($freeIP);
                     $fieldOptions['choices'] = array_combine($freeIP, $freeIP);
                     $mapper->add($opt, ChoiceType::class, $fieldOptions);
                     unset($fieldOptions['choices']);

+ 3 - 1
src/HostBundle/Form/HostType.php

@@ -16,7 +16,9 @@ class HostType extends AbstractType
     public function buildForm(FormBuilderInterface $builder, array $options)
     {
         $builder
-            ->add('mac')
+            ->add('mac', TextType::class, [
+                'required' => false,
+            ])
             ->add('hostType')
             ->add('state')
             ->add('fixedIP', TextType::class, [

+ 7 - 1
src/HostBundle/Resources/translations/HostBundle.es.yml

@@ -43,6 +43,7 @@ form:
     label_time_servers: Time Servers
     label_status: Estado
     label_fixed_address: Fixed Address
+    label_host: Host
 
 list:
     label_mac: Mac
@@ -63,7 +64,9 @@ list:
     label_state: Estado
     label_extra_data: Extra Data
     label_status: Estado
-
+    label_host: Host
+    label_options_fixed_address: IP
+    
 show:
     label_mac: Mac
     label_options: Opciones
@@ -101,6 +104,7 @@ show:
     label_time_servers: Time Servers
     label_status: Estado
     label_fixed_address: Fixed Address
+    label_host: Host
 
 filter:
     label_mac: Mac
@@ -116,6 +120,8 @@ filter:
     label_extra_data: Extra Data
     label_allowed_host_type: Tipo de Host permitido
     label_status: Estado
+    label_host_type: Tipo de Host
+    label_options_fixed_address: IP
 
 breadcrumb:
     link_host_list: Listado Host

+ 1 - 1
src/HostBundle/Utils/DHCPOptions.php

@@ -9,6 +9,7 @@ class DHCPOptions
 
     use ChoiceTrait;
 
+    const DHCP_FIXED_ADDRESS = 'fixed_address';
     const DHCP_FILENAME = 'filename';
     const DHCP_SUBNET_MASK = 'subnet_mask';
     const DHCP_TIME_OFFSET = 'time_offset';
@@ -27,6 +28,5 @@ class DHCPOptions
     const DHCP_OPT122_DHCP_SERVER_SECONDARY = 'option122_dhcp_server_secondary';
     const DHCP_OPT122_PROVISIONING_SERVER = 'option122_provisioning_server';
     const DHCP_OPT122_PROVISIONING_TYPE = 'option122_provisioning_type';
-    const DHCP_FIXED_ADDRESS = 'fixed_address';
 
 }