Browse Source

Refactory Host y HostAdmin, se muestran host asociados en el listado y show de host

Espinoza Guillermo 6 năm trước cách đây
mục cha
commit
c015374f1c

+ 6 - 1
src/HostBundle/Admin/HostAdmin.php

@@ -52,7 +52,7 @@ class HostAdmin extends BaseAdmin
             ->add('hostType')
             ->add('state')
             ->add('options.fixed_address')
-            ->add('host')
+            ->add('associatedHosts')
             ->add('_action', null, array(
                 'actions' => array(
                     'show' => array(),
@@ -108,16 +108,21 @@ class HostAdmin extends BaseAdmin
      */
     protected function configureShowFields(ShowMapper $showMapper)
     {
+        $subject = $this->getSubject();
         $showMapper
             ->tab('Host')
             ->with('Host')
             ->add('mac')
             ->add('hostType')
             ->add('state')
+            ->add('associatedHosts')
             ->add('host')
         ->end()
         ->end()
         ;
+        $subject->getAssociatedHosts()->count() ?: $showMapper->remove('associatedHosts');
+        $subject->getHost() ?: $showMapper->remove('host');
+        
 
         $this->addCablemodemTab($showMapper);
     }

+ 23 - 3
src/HostBundle/Entity/Host.php

@@ -89,20 +89,32 @@ class Host implements WorkflowInterface
     protected $fixedIP = false;
     
     /**
-     * @ORM\ManyToOne(targetEntity="Host", fetch="EXTRA_LAZY")
+     * @ORM\ManyToOne(targetEntity="Host", inversedBy="associatedHosts", fetch="EXTRA_LAZY")
      * @ORM\JoinColumn(name="host_id", referencedColumnName="id", onDelete="SET NULL")
      *
      * @JMS\MaxDepth(1)
      */
     protected $host;
+    
+    /**
+     * @ORM\OneToMany(targetEntity="Host", mappedBy="host", fetch="EXTRA_LAZY")
+     *
+     * @JMS\MaxDepth(1)
+     */
+    protected $associatedHosts;
 
 
+    public function __construct() 
+    {
+        $this->associatedHosts = new \Doctrine\Common\Collections\ArrayCollection();
+    }
+
     /**
      * @return string
      */
     public function __toString()
     {
-        return strval($this->mac);
+        return $this->mac ? strval($this->mac) : sprintf('%s - %s', $this->id, $this->hostType->__toString());
     }
 
     /**
@@ -130,7 +142,7 @@ class Host implements WorkflowInterface
      */
     public function getMac()
     {
-        return $this->mac;
+        return $this->mac ?: ($this->host ? $this->host->getMac() : '');
     }
 
     /**
@@ -233,6 +245,14 @@ class Host implements WorkflowInterface
         return $this;
     }
     
+    /**
+     * @return array<Host>
+     */
+    public function getAssociatedHosts()
+    {
+        return $this->associatedHosts;
+    }
+    
     /**
      * @param ExecutionContextInterface $context
      */

+ 2 - 0
src/HostBundle/Resources/translations/HostBundle.es.yml

@@ -65,6 +65,7 @@ list:
     label_extra_data: Extra Data
     label_status: Estado
     label_host: Host
+    label_associated_hosts: Hosts asociados ( ID - Tipo de host )
     label_options_fixed_address: IP
     
 show:
@@ -105,6 +106,7 @@ show:
     label_status: Estado
     label_fixed_address: Fixed Address
     label_host: Host
+    label_associated_hosts: Hosts asociados ( ID - Tipo de host )
 
 filter:
     label_mac: Mac

+ 8 - 0
src/HostBundle/Traits/DHCPOptionTrait.php

@@ -18,6 +18,14 @@ trait DHCPOptionTrait
     {
         $this->setOptions(json_encode($this->getDHCPOption()));
     }
+    
+    /**
+     *  @ORM\PostLoad
+     */
+    public function PostLoad()
+    {
+        $this->setDHCPOption(json_decode($this->getOptions(), true));
+    }
 
     function getDHCPOption()
     {