<?php

include_once(dirname(__FILE__).'/HostList.php');
include_once(dirname(__FILE__).'/Subnet.php');
include_once(dirname(__FILE__).'/Hosting.php');
include_once(dirname(__FILE__).'/UserHosts.php');

class PageFrequencyPlan extends Page
{
  var $FullTitle = 'Výpis obsazení frekvenčních kanálů';
  var $ShortTitle = 'Frekvenční plán';
  var $ParentClass = 'PageNetwork';

  function Show()
  {
    // http://en.wikipedia.org/wiki/List_of_WLAN_channels
    //$ChannelList = array(2412 => 1, 2417 => 2, 2422 => 3, 2427 => 4, 2432 => 5, 2437 => 6, 2442 => 7, 2447 => 8, 2452 => 9, 2457 => 10, 2462 => 11, 2467 => 12, 2472 => 13, 5200 => 40, 5205 => 41, 5210 => 42, 5215 => 43, 5220 => 44, 5225 => 45, 5230 => 46, 5235 => 47, 5240 => 48, 5245 => 49, 5250 => 50, 5255 => 51, 5260 => 52, 5265 => 53, 5270 => 54, 5275 => 55, 5280 => 56, 5285 => 57, 5290 => 58, 5295 => 59, 5300 => 60, 5500 => 100, 5520 => 104, 5540 => 108, 5560 => 112, 5580 => 116, 5600 => 120, 5620 => 124, 5640 => 128, 5660 => 132, 5700 => 140, 5720 => 144);
    $Output = '<div align="center">'.
    '<a href="?section=obsazeni_wifi_kanalu&range=a">Pásmo 2,4 GHz (a)</a> '.
    '<a href="?section=obsazeni_wifi_kanalu&amp;range=bc">Pásmo 5 GHz dolní (b, c)</a> '.
    '<a href="?section=obsazeni_wifi_kanalu&amp;range=d">Pásmo 5 GHz horní (d)</a> '.
    '<a href="http://www.ctu.cz/1/download/Opatreni%20obecne%20povahy/VO_R_12_08_2005_34.pdf">VO_R_12_08_2005_34</a><br/>'.
    '<strong>Seznam známých AP a obsazení kmitočtových pásem:</strong></div>'.
    '<table class="WideTable">'.
    '<tr><th/><br/>SSID<br/><br/></th>';
    $ChannelList = array();
    if(!array_key_exists('range', $_GET)) $_GET['range'] = 'a';
    if($_GET['range'] == 'a')
    {
      $Where = '(Frequency < 5000)';
      for($Freq = 2402; $Freq <= 2482; $Freq = $Freq + 5) $ChannelList[] = $Freq;
    }
    if($_GET['range'] == 'bc')
    {
      $Where = '(Frequency >= 5000) AND (Frequency <= 5350)';
      for($Freq = 5150; $Freq <= 5350; $Freq = $Freq + 5) $ChannelList[] = $Freq;
    }
    if($_GET['range'] == 'd')
    {
      $Where = '(Frequency >= 5470)';
      for($Freq = 5470; $Freq <= 5725; $Freq = $Freq + 5) $ChannelList[] = $Freq;
    }

    foreach($ChannelList as $Frequency)
    {
      $Output .= '<th><div class="RotatedHeader">'.$Frequency.'<div></th>';
    }
    $Output .= '</tr>';
    $DbResult = $this->Database->query('SELECT `Frequency` FROM `NetworkInterfaceWireless` WHERE '.$Where.' AND (`Mode`=0) GROUP BY `Frequency`');
    while($DbRow = $DbResult->fetch_assoc())
    {
      $DbResult2 = $this->Database->query('SELECT * FROM `NetworkInterfaceWireless` WHERE (`Frequency`='.$DbRow['Frequency'].') AND '.$Where);
      while($DbRow2 = $DbResult2->fetch_assoc())
      {
        $LowFrequency = $DbRow['Frequency'] - $DbRow2['ChannelWidth'] / 2 - $DbRow2['ChannelWidthLower'];
        $HighFrequency = $DbRow['Frequency'] + $DbRow2['ChannelWidth'] / 2 + $DbRow2['ChannelWidthUpper'];
        $Output .= '<tr><td>'.$DbRow2['SSID'].'</td>';
        foreach($ChannelList as $Frequency)
        {
          if(($DbRow2['Frequency'] == $Frequency)) $Color = '#000000';
            else if(($LowFrequency <= ($Frequency - 2.5)) and ($HighFrequency >= ($Frequency + 2.5))) $Color = '#808080';
            else if(($LowFrequency == $Frequency) or ($HighFrequency == $Frequency)) $Color = '#c0c0c0';
            else $Color = '#ffffff';
          $Output .= '<td style="background-color: '.$Color.';">&nbsp;</td>';
        }

        $Output .= '</tr>';
      }
    }
    $Output .= '</table>';
    return($Output);
  }
}

class PageNetwork extends Page
{
  var $FullTitle = 'Technické informace o síti';
  var $ShortTitle = 'Síť';
  var $ParentClass = 'PagePortal';

  function Show()
  {
    if(count($this->System->PathItems) > 1)
    {
      $Output = $this->PageNotFound();
    } else $Output = $this->ShowInformation();
    return($Output);
  }

  function ShowInformation()
  {
    $Output = '<a href="'.$this->System->Link('/network/frequency-plan/').'">Frekvenční plán</a><br />';
    $Output .= '<a href="'.$this->System->Link('/network/subnet/').'">Výpis registrovaných podsítí</a><br />';
    $Output .= '<a href="'.$this->System->Link('/network/hosts/').'">Registrované zařízení</a><br />';
    return($Output);
  }
}

class ModuleNetwork extends AppModule
{
  function __construct($System)
  {
    parent::__construct($System);
    $this->Name = 'Network';
    $this->Version = '1.0';
    $this->Creator = 'Chronos';
    $this->License = 'GNU/GPLv3';
    $this->Description = 'Networking related tools';
    $this->Dependencies = array();
  }

  function DoInstall()
  {
  }

  function DoUninstall()
  {
  }

  function DoStart()
  {
    $this->System->RegisterPage('network', 'PageNetwork');
    $this->System->RegisterPage(array('network', 'administration'), 'PageNetworkAdministration');
    $this->System->RegisterPage(array('network', 'subnet'), 'PageSubnet');
    $this->System->RegisterPage(array('network', 'user-hosts'), 'PageNetworkHostList');
    $this->System->RegisterPage(array('network', 'hosting'),'PageHosting');
    $this->System->RegisterPage(array('network', 'hosts'), 'PageHostList');
    $this->System->RegisterPage(array('network', 'frequency-plan'), 'PageFrequencyPlan');

    $this->System->FormManager->RegisterClass('NetworkDomainAlias', array(
      'Title' => 'Alias domény',
      'Table' => 'NetworkDomainAlias',
      'Items' => array(
        'Name' => array('Type' => 'String', 'Caption' => 'Jméno', 'Default' => ''),
        'Target' => array('Type' => 'String', 'Caption' => 'Cíl', 'Default' => ''),
        'Comment' => array('Type' => 'String', 'Caption' => 'Komentář', 'Default' => ''),
        'Domain' => array('Type' => 'TNetworkDomain', 'Caption' => 'Síťová doména', 'Default' => ''),
      ),
    ));
    $this->System->FormManager->RegisterFormType('TNetworkDomainAliasListDomain', array(
      'Type' => 'ManyToOne',
      'Table' => 'NetworkDomainAlias',
      'Id' => 'Id',
      'Ref' => 'Domain',
      'Filter' => '1',
    ));
    $this->System->FormManager->RegisterClass('NetworkDevice', array(
      'Title' => 'Síťové zařízení',
      'Table' => 'NetworkDevice',
      'Items' => array(
        'Name' => array('Type' => 'String', 'Caption' => 'Jméno', 'Default' => ''),
        'Type' => array('Type' => 'TNetworkDeviceType', 'Caption' => 'Typ', 'Default' => '0'),
        'Member' => array('Type' => 'TMember', 'Caption' => 'Zákazník', 'Default' => '0'),
        'Location' => array('Type' => 'TMember', 'Caption' => 'Umístění', 'Default' => '0'),
        'Service' => array('Type' => 'TServiceCustomerRel', 'Caption' => 'Služba', 'Default' => '', 'Null' => true),
        'Used' => array('Type' => 'Boolean', 'Caption' => 'Použito', 'Default' => '1'),
        'Online' => array('Type' => 'TOnlineState', 'Caption' => 'Běží', 'Default' => '0', 'ReadOnly' => true),
        'LastOnline' => array('Type' => 'DateTime', 'Caption' => 'Naposledy běželo', 'Default' => '', 'ReadOnly' => true),
        'PermanentOnline' => array('Type' => 'Boolean', 'Caption' => 'Běží stále', 'Default' => '0'),
        'Interfaces' => array('Type' => 'TInterfaceList', 'Caption' => 'Rozhraní', 'Default' => ''),
        'MapPosition' => array('Type' => 'TMapPosition', 'Caption' => 'Pozice na mapě', 'Default' => '0', 'Null' => true),
        'Product' => array('Type' => 'TProduct', 'Caption' => 'Produkt', 'Default' => '', 'Null' => true),
        'LoginName' => array('Type' => 'String', 'Caption' => 'Přihlašovací jméno', 'Default' => '', 'Null' => true, 'NotInList' => true),
        'LoginPassword' => array('Type' => 'String', 'Caption' => 'Přihlašovací heslo', 'Default' => '', 'Null' => true, 'NotInList' => true),
        'API' => array('Type' => 'TDeviceAPIType', 'Caption' => 'API', 'Default' => '', 'Null' => true),
      ),
    ));
    $this->System->FormManager->RegisterClass('NetworkDeviceType', array(
      'Title' => 'Typ síťového zařízení',
      'Table' => 'NetworkDeviceType',
      'Items' => array(
        'Name' => array('Type' => 'String', 'Caption' => 'Jméno', 'Default' => ''),
        'ShowOnline' => array('Type' => 'Boolean', 'Caption' => 'Ukázat online', 'Default' => '0'),
        'IconName' => array('Type' => 'String', 'Caption' => 'Jméno ikony', 'Default' => '0'),
      ),
    ));
    $this->System->FormManager->RegisterClass('NetworkInterface', array(
      'Title' => 'Síťové rozhraní',
      'Table' => 'NetworkInterface',
      'Items' => array(
        'Name' => array('Type' => 'String', 'Caption' => 'Jméno', 'Default' => ''),
        'Type' => array('Type' => 'TNetworkInterfaceType', 'Caption' => 'Typ', 'Default' => '0'),
        'MAC' => array('Type' => 'MacAddress', 'Caption' => 'Fyzická adresa (MAC)', 'Default' => ''),
        'LocalIP' => array('Type' => 'IPv4Address', 'Caption' => 'IPv4', 'Default' => '', 'Null' => true),
        'IPv6' => array('Type' => 'IPv6Address', 'Caption' => 'IPv6', 'Default' => '', 'Null' => true),
        'ExternalIP' => array('Type' => 'IPv4Address', 'Caption' => 'Veřejná IPv4', 'Default' => '', 'Null' => true),
        'Device' => array('Type' => 'TNetworkDevice', 'Caption' => 'Zařízení', 'Default' => ''),
        'Online' => array('Type' => 'TOnlineState', 'Caption' => 'Běží', 'Default' => '0', 'ReadOnly' => true),
        'LastOnline' => array('Type' => 'DateTime', 'Caption' => 'Naposledy běželo', 'Default' => '', 'ReadOnly' => true),
        'Links' => array('Type' => 'TNetworkLinkListInterface', 'Caption' => 'Propojení', 'Default' => ''),
        'UpDown' => array('Type' => 'TNetworkInterfaceUpDown', 'Caption' => 'Změny stavu', 'Default' => ''),
        'Signal' => array('Type' => 'TNetworkSignalListInterface', 'Caption' => 'Signál', 'Default' => ''),
        'Wireless' => array('Type' => 'TNetworkInterfaceWirelessListInterface', 'Caption' => 'Bezdrátové spoje', 'Default' => ''),

      ),
    ));
    $this->System->FormManager->RegisterClass('NetworkInterfaceType', array(
      'Title' => 'Typ síťového rozhraní',
      'Table' => 'NetworkInterfaceType',
      'Items' => array(
        'Name' => array('Type' => 'String', 'Caption' => 'Jméno', 'Default' => ''),
        'MaxSpeed' => array('Type' => 'Integer', 'Caption' => 'Max. rychlost', 'Default' => '0', 'Suffix' => 'Mbit/s'),
        'FullDuplex' => array('Type' => 'Boolean', 'Caption' => 'Plně duplexní', 'Default' => '0'),
        'Color' => array('Type' => 'Color', 'Caption' => 'Barva', 'Default' => '0'),
      ),
    ));
    $this->System->FormManager->RegisterClass('NetworkSubnet', array(
      'Title' => 'Podsítě',
      'DefaultSortColumn' => 'Name',
      'Table' => 'NetworkSubnet',
      'Items' => array(
        'Name' => array('Type' => 'String', 'Caption' => 'Jméno', 'Default' => ''),
        'AddressRange' => array('Type' => 'String', 'Caption' => 'Rozsah adres', 'Default' => ''),
        'Mask' => array('Type' => 'Integer', 'Caption' => 'Prefix', 'Default' => ''),
        'DHCP' => array('Type' => 'IPv4Address', 'Caption' => 'DHCP', 'Default' => ''),
        'Gateway' => array('Type' => 'IPv4Address', 'Caption' => 'Brána', 'Default' => ''),
        'WINS' => array('Type' => 'IPv4Address', 'Caption' => 'WINS', 'Default' => ''),
        'DNS' => array('Type' => 'String', 'Caption' => 'DNS', 'Default' => ''),
        'Domain' => array('Type' => 'String', 'Caption' => 'Doména', 'Default' => ''),
        'NTP' => array('Type' => 'String', 'Caption' => 'NTP', 'Default' => ''),
        'Member' => array('Type' => 'TMember', 'Caption' => 'Zákazník', 'Default' => '', 'Null' => true),
        'ExtAddressRange' => array('Type' => 'String', 'Caption' => 'Vnější rozsah adres', 'Default' => ''),
        'ExtMask' => array('Type' => 'String', 'Caption' => 'Vnější prefix', 'Default' => ''),
        'AddressRangeIPv6' => array('Type' => 'String', 'Caption' => 'Rozsah adres IPv6', 'Default' => ''),
        'Configure' => array('Type' => 'Boolean', 'Caption' => 'Nastavovat', 'Default' => ''),
      ),
    ));
    $this->System->FormManager->RegisterClass('NetworkLink', array(
      'Title' => 'Síťové propojení',
      'Table' => 'NetworkLink',
      'Items' => array(
        'Type' => array('Type' => 'TNetworkLinkType', 'Caption' => 'Typ', 'Default' => '1'),
        'Interface1' => array('Type' => 'TNetworkInterface', 'Caption' => 'Rozhraní 1', 'Default' => ''),
        'Interface2' => array('Type' => 'TNetworkInterface', 'Caption' => 'Rozhraní 2', 'Default' => ''),
      ),
    ));
    $this->System->FormManager->RegisterClass('NetworkLinkUnion', array(
      'Title' => 'Síťové propojení',
      'BaseTable' => 'NetworkLink',
      'SQL' => '(SELECT `Id`, `Type`, `Interface1` AS `Interface`, `Interface2` AS `InterfaceOther` FROM `NetworkLink`) '.
        'UNION (SELECT `Id`, `Type`, `Interface2` AS `Interface`, `Interface1` AS `InterfaceOther` FROM `NetworkLink`)',
      'Items' => array(
        'Type' => array('Type' => 'TNetworkLinkType', 'Caption' => 'Typ', 'Default' => '1', 'ReadOnly' => true),
        'Interface' => array('Type' => 'TNetworkInterface', 'Caption' => 'Rozhraní 1', 'Default' => '', 'ReadOnly' => true),
        'InterfaceOther' => array('Type' => 'TNetworkInterface', 'Caption' => 'Rozhraní 2', 'Default' => '', 'ReadOnly' => true),
      ),
    ));

    $this->System->FormManager->RegisterClass('NetworkLinkType', array(
      'Title' => 'Typ síťového propojení',
      'Table' => 'NetworkLinkType',
      'Items' => array(
        'Name' => array('Type' => 'String', 'Caption' => 'Název', 'Default' => ''),
      ),
    ));
    $this->System->FormManager->RegisterClass('NetworkSignal', array(
      'Title' => 'Signál rozhraní',
      'Table' => 'NetworkSignal',
      'DefaultSortColumn' => 'Time',
      'DefaultSortOrder' => 1,
      'Items' => array(
        'Time' => array('Type' => 'DateTime', 'Caption' => 'Čas', 'Default' => ''),
        'MAC' => array('Type' => 'MacAddress', 'Caption' => 'Fyzická adresa (MAC)', 'Default' => ''),
        'Interface' => array('Type' => 'TNetworkInterface', 'Caption' => 'Rozhraní', 'Default' => '', 'Null' => true),
        'Value' => array('Type' => 'Integer', 'Caption' => 'Signál', 'Default' => '0', 'Suffix' => 'dBm'),
        'RateRx' => array('Type' => 'Integer', 'Caption' => 'Rychlost Rx', 'Default' => '0', 'Suffix' => 'MHz'),
        'RateTx' => array('Type' => 'Integer', 'Caption' => 'Rychlost Tx', 'Default' => '0', 'Suffix' => 'MHz'),
        'Device' => array('Type' => 'TNetworkDevice', 'Caption' => 'Měřeno z', 'Default' => '0'),
      ),
    ));
    $this->System->FormManager->RegisterClass('NetworkAddressCategory', array(
      'Title' => 'Kategorie síťové adresy',
      'Table' => 'NetworkAddressCategory',
      'DefaultSortColumn' => 'Name',
      'DefaultSortOrder' => 1,
      'Items' => array(
        'Name' => array('Type' => 'String', 'Caption' => 'Jméno', 'Default' => ''),
      ),
    ));
    $this->System->FormManager->RegisterFormType('TNetworkAddressCategory', array(
      'Type' => 'Reference',
      'Table' => 'NetworkAddressCategory',
      'Id' => 'Id',
      'Name' => 'Name',
      'Filter' => '1',
    ));
    $this->System->FormManager->RegisterClass('NetworkDeviceConfig', array(
      'Title' => 'Nastavení zařízení',
      'Table' => 'NetworkDeviceConfig',
      'DefaultSortColumn' => 'Time',
      'DefaultSortOrder' => 1,
      'Items' => array(
        'Device' => array('Type' => 'TNetworkDevice', 'Caption' => 'Zařízení', 'Default' => ''),
        'Time' => array('Type' => 'Date', 'Caption' => 'Čas vytvoření', 'Default' => ''),
        'ConfigFull' => array('Type' => 'Text', 'Caption' => 'Kompletní nastavení', 'Default' => ''),
        'ConfigCompact' => array('Type' => 'Text', 'Caption' => 'Rozdílové nastavení', 'Default' => ''),
      ),
    ));
    $this->System->FormManager->RegisterClass('NetworkDomain', array(
      'Title' => 'Síťová doména',
      'Table' => 'NetworkDomain',
      'DefaultSortColumn' => 'Name',
      'DefaultSortOrder' => 1,
      'Items' => array(
        'Name' => array('Type' => 'String', 'Caption' => 'Jméno', 'Default' => ''),
        'Parent' => array('Type' => 'TNetworkDomain', 'Caption' => 'Nadřazená doména', 'Default' => '', 'Null' => true),
        'Description' => array('Type' => 'String', 'Caption' => 'Popis', 'Default' => ''),
        'Serial' => array('Type' => 'Integer', 'Caption' => 'Sériové číslo', 'Default' => '1'),
        'Minimum' => array('Type' => 'Integer', 'Caption' => 'Minimum', 'Default' => '10800'),
        'Retry' => array('Type' => 'Integer', 'Caption' => 'Opakování', 'Default' => '7200'),
        'Expire' => array('Type' => 'Integer', 'Caption' => 'Čas vypršení', 'Default' => '2419200'),
        'Refresh' => array('Type' => 'Integer', 'Caption' => 'Obnovení', 'Default' => '28800'),
        'TTL' => array('Type' => 'Integer', 'Caption' => 'TTL', 'Default' => '86400', 'Suffix' => 'sekund'),
        'Servers' => array('Type' => 'TNetworkDomainServerList', 'Caption' => 'Servery', 'Default' => ''),
        'Views' => array('Type' => 'TNetworkDomainViewListDomain', 'Caption' => 'Pohledy', 'Default' => ''),
        'ItemFilters' => array('Type' => 'TNetworkDomainItemFilterListDomain', 'Caption' => 'Filtry položek', 'Default' => ''),
        'Aliases' => array('Type' => 'TNetworkDomainAliasListDomain', 'Caption' => 'Aliasy', 'Default' => ''),
      ),
    ));
    $this->System->FormManager->RegisterFormType('TNetworkDomain', array(
      'Type' => 'Reference',
      'Table' => 'NetworkDomain',
      'Id' => 'Id',
      'Name' => 'Name',
      'Filter' => '1',
    ));
    $this->System->FormManager->RegisterClass('NetworkDomainServer', array(
      'Title' => 'Doménový server',
      'Table' => 'NetworkDomainServer',
      'DefaultSortColumn' => 'Address',
      'DefaultSortOrder' => 1,
      'Items' => array(
        'Address' => array('Type' => 'String', 'Caption' => 'Adresa', 'Default' => ''),
        'Domain' => array('Type' => 'TNetworkDomain', 'Caption' => 'Doména', 'Default' => ''),
        'Sequence' => array('Type' => 'Integer', 'Caption' => 'Pořadí', 'Default' => '1'),
      ),
    ));
    $this->System->FormManager->RegisterFormType('TNetworkDomainServerList', array(
      'Type' => 'ManyToOne',
      'Table' => 'NetworkDomainServer',
      'Id' => 'Id',
      'Ref' => 'Domain',
      'Filter' => '1',
    ));
    $this->System->FormManager->RegisterClass('NetworkDomainView', array(
      'Title' => 'Pohled síťové domény',
      'Table' => 'NetworkDomainView',
      'DefaultSortColumn' => 'Name',
      'DefaultSortOrder' => 1,
      'Items' => array(
        'Name' => array('Type' => 'String', 'Caption' => 'Jméno', 'Default' => ''),
        'SysName' => array('Type' => 'String', 'Caption' => 'Systémové jméno', 'Default' => ''),
        'Domain' => array('Type' => 'TNetworkDomain', 'Caption' => 'Doména', 'Default' => ''),
        'AddressRange' => array('Type' => 'String', 'Caption' => 'Rozsah adres', 'Default' => ''),
        'ItemFilters' => array('Type' => 'TNetworkDomainItemFilterListView', 'Caption' => 'Filtry položek', 'Default' => ''),
      ),
    ));
    $this->System->FormManager->RegisterFormType('TNetworkDomainView', array(
      'Type' => 'Reference',
      'Table' => 'NetworkDomainView',
      'Id' => 'Id',
      'Name' => 'Name',
      'Filter' => '1',
    ));
    $this->System->FormManager->RegisterFormType('TNetworkDomainViewListDomain', array(
      'Type' => 'ManyToOne',
      'Table' => 'NetworkDomainView',
      'Id' => 'Id',
      'Ref' => 'Domain',
      'Filter' => '1',
    ));
    $this->System->FormManager->RegisterClass('NetworkDomainItemFilter', array(
      'Title' => 'Filtr doménových položek',
      'Table' => 'NetworkDomainItemFilter',
      'DefaultSortColumn' => 'Name',
      'DefaultSortOrder' => 1,
      'Items' => array(
        'Name' => array('Type' => 'String', 'Caption' => 'Jméno', 'Default' => ''),
        'Domain' => array('Type' => 'TNetworkDomain', 'Caption' => 'Domén', 'Default' => ''),
        'AddressCategory' => array('Type' => 'TNetworkAddressCategory', 'Caption' => 'Kategorie adresy', 'Default' => ''),
        'Suffix' => array('Type' => 'String', 'Caption' => 'Přípona jména položek', 'Default' => ''),
        'View' => array('Type' => 'TNetworkDomainView', 'Caption' => 'Pohled', 'Default' => ''),
        'AddressRange' => array('Type' => 'String', 'Caption' => 'Rozsah adres', 'Default' => ''),
      ),
    ));
    $this->System->FormManager->RegisterFormType('TNetworkDomainItemFilterListDomain', array(
      'Type' => 'ManyToOne',
      'Table' => 'NetworkDomainItemFilter',
      'Id' => 'Id',
      'Ref' => 'Domain',
      'Filter' => '1',
    ));
    $this->System->FormManager->RegisterFormType('TNetworkDomainItemFilterListView', array(
      'Type' => 'ManyToOne',
      'Table' => 'NetworkDomainItemFilter',
      'Id' => 'Id',
      'Ref' => 'View',
      'Filter' => '1',
    ));
    $this->System->FormManager->RegisterClass('DeviceAPIType', array(
      'Title' => 'Typ API zařízení',
      'Table' => 'DeviceAPIType',
      'Items' => array(
        'Name' => array('Type' => 'String', 'Caption' => 'Jméno', 'Default' => ''),
        'Devices' => array('Type' => 'TDeviceListAPI', 'Caption' => 'Zařízení', 'Default' => ''),
      ),
    ));
    $this->System->FormManager->RegisterClass('NetworkInterfaceWireless', array(
      'Title' => 'Bezdrátová rozhraní',
      'Table' => 'NetworkInterfaceWireless',
      'Items' => array(
        'SSID' => array('Type' => 'String', 'Caption' => 'SSID', 'Default' => ''),
        'MAC' => array('Type' => 'MacAddress', 'Caption' => 'MAC', 'Default' => ''),
        'NetworkInterface' => array('Type' => 'TNetworkInterface', 'Caption' => 'Rozhraní', 'Default' => ''),
        'TxPower' => array('Type' => 'Integer', 'Caption' => 'Výstupní výkon', 'Default' => '18', 'Suffix' => 'dBm'),
        'CableAttenuation' => array('Type' => 'Integer', 'Caption' => 'Útlum vedení', 'Default' => '0', 'Suffix' => 'dB'),
        'Antenna' => array('Type' => 'TProduct', 'Caption' => 'Anténa', 'Default' => '', 'Null' => true),
        'AntenaGain' => array('Type' => 'Integer', 'Caption' => 'Zisk antény', 'Default' => '', 'Suffix' => 'dBi'),
        'AntennaPolarity' => array('Type' => 'TAntennaPolarity', 'Caption' => 'Polarizace antény', 'Default' => '0'),
        'Frequency' => array('Type' => 'Float', 'Caption' => 'Frekvence', 'Default' => '5600', 'Suffix' => 'MHz'),
        'ChannelWidthLower' => array('Type' => 'Integer', 'Caption' => 'Šírka kanálu dolního', 'Default' => '0', 'Suffix' => 'MHz'),
        'ChannelWidth' => array('Type' => 'Integer', 'Caption' => 'Šírka kanálu', 'Default' => '20', 'Suffix' => 'MHz'),
        'ChannelWidthUpper' => array('Type' => 'Integer', 'Caption' => 'Šírka kanálu horního', 'Default' => '0', 'Suffix' => 'MHz'),
        'Mode' => array('Type'
        => 'TWirelessMode', 'Caption' => 'Režim', 'Default' => '0', 'Suffix' => ''),
        'TotalPower' => array('Type' => 'Integer', 'Caption' => 'Celkový výkon', 'Default' => '20', 'Suffix' => 'dBm',
          'SQL' => '(`TxPower` - `CableAttenuation` + `AntenaGain`)', 'ReadOnly' => true),
        'LimitPower' => array('Type' => 'Integer', 'Caption' => 'Max. limit', 'Default' => '', 'Suffix' => 'dBm',
          'ReadOnly' => true, 'SQL' => '(CASE WHEN `Frequency` >= 5450 AND `Frequency` <= 5725 THEN 27 ELSE 20 END)'),
        'UnderLimit' => array('Type' => 'Boolean', 'Caption' => 'V limitu', 'Default' => '', 'Suffix' => '',
          'ReadOnly' => true, 'SQL' => '((`TxPower` - `CableAttenuation` + `AntenaGain`) <= (CASE WHEN `Frequency` >= 5450 AND `Frequency` <= 5725 THEN 27 ELSE 20 END))'),
        'Description' => array('Type' => 'String', 'Caption' => 'Popis', 'Default' => ''),
      ),
      'Actions' => array(
        array('Caption' => 'Frekvenční plán', 'URL' => '/network/frequency-plan/'),
      ),
    ));
    $this->System->FormManager->RegisterClass('NetworkInterfaceWirelessCTU', array(
      'Title' => 'Bezdrátová rozhraní pro ČTÚ',
      'Table' => 'NetworkInterfaceWireless',
      'Items' => array(
        'SSID' => array('Type' => 'String', 'Caption' => 'SSID', 'Default' => ''),
        'MAC' => array('Type' => 'MacAddress', 'Caption' => 'MAC', 'Default' => ''),
        'TxPower' => array('Type' => 'Integer', 'Caption' => 'Výstupní výkon', 'Default' => '18', 'Suffix' => 'dBm'),
        'CableAttenuation' => array('Type' => 'Integer', 'Caption' => 'Útlum vedení', 'Default' => '0', 'Suffix' => 'dB'),
        'AntenaGain' => array('Type' => 'Integer', 'Caption' => 'Zisk antény', 'Default' => '', 'Suffix' => 'dBi'),
        'AntennaPolarity' => array('Type' => 'TAntennaPolarity', 'Caption' => 'Polarizace antény', 'Default' => '0'),
        'Frequency' => array('Type' => 'Float', 'Caption' => 'Frekvence', 'Default' => '5600', 'Suffix' => 'MHz'),
        'ChannelWidthLower' => array('Type' => 'Integer', 'Caption' => 'Šírka kanálu dolního', 'Default' => '0', 'Suffix' => 'MHz'),
        'ChannelWidth' => array('Type' => 'Integer', 'Caption' => 'Šírka kanálu', 'Default' => '20', 'Suffix' => 'MHz'),
        'ChannelWidthUpper' => array('Type' => 'Integer', 'Caption' => 'Šírka kanálu horního', 'Default' => '0', 'Suffix' => 'MHz'),
        'Mode' => array('Type' => 'TWirelessMode', 'Caption' => 'Režim', 'Default' => '0', 'Suffix' => ''),
        'TotalPower' => array('Type' => 'Integer', 'Caption' => 'Celkový výkon', 'Default' => '20', 'Suffix' => 'dBm',
          'SQL' => '(`TxPower` - `CableAttenuation` + `AntenaGain`)', 'ReadOnly' => true),
        'LimitPower' => array('Type' => 'Integer', 'Caption' => 'Max. limit', 'Default' => '', 'Suffix' => 'dBm',
          'ReadOnly' => true, 'SQL' => '(CASE WHEN `Frequency` >= 5450 AND `Frequency` <= 5725 THEN 27 ELSE 20 END)'),
        'UnderLimit' => array('Type' => 'Boolean', 'Caption' => 'V limitu', 'Default' => '', 'Suffix' => '',
          'ReadOnly' => true, 'SQL' => '((`TxPower` - `CableAttenuation` + `AntenaGain`) <= (CASE WHEN `Frequency` >= 5450 AND `Frequency` <= 5725 THEN 27 ELSE 20 END))'),
        'Position' => array('Type' => 'String', 'Caption' => 'GPS poloha', 'Default' => '',
          'SQL' => '(SELECT MapPosition.Pos FROM MapPosition WHERE MapPosition.Id=#Id)'),
        'Description' => array('Type' => 'String', 'Caption' => 'Popis', 'Default' => ''),
      ),
      'Actions' => array(
        array('Caption' => 'Frekvenční plán', 'URL' => '/network/frequency-plan/'),
      ),
    ));
    $this->System->FormManager->RegisterFormType('TNetworkDevice', array(
      'Type' => 'Reference',
      'Table' => 'NetworkDevice',
      'Id' => 'Id',
      'Name' => 'Name',
      'Filter' => '1',
    ));
    $this->System->FormManager->RegisterFormType('TNetworkDeviceType', array(
      'Type' => 'Reference',
      'Table' => 'NetworkDeviceType',
      'Id' => 'Id',
      'Name' => 'Name',
      'Filter' => '1',
    ));
    $this->System->FormManager->RegisterFormType('TNetworkInterface', array(
      'Type' => 'Reference',
      'Table' => 'NetworkInterface',
      'View' => '(SELECT NetworkInterface.*, CONCAT_WS("-", NetworkDevice.Name, NULLIF(NetworkInterface.Name, "")) AS DeviceName FROM NetworkInterface '.
      'LEFT JOIN NetworkDevice ON NetworkDevice.Id = NetworkInterface.Device) AS T',
      'Id' => 'Id',
      'Name' => 'DeviceName',
      'Filter' => '1',
    ));
    $this->System->FormManager->RegisterFormType('TNetworkInterfaceType', array(
      'Type' => 'Reference',
      'Table' => 'NetworkInterfaceType',
      'Id' => 'Id',
      'Name' => 'Name',
      'Filter' => '1',
    ));
    $this->System->FormManager->RegisterFormType('TDeviceList', array(
      'Type' => 'ManyToOne',
      'Table' => 'NetworkDevice',
      'Id' => 'Id',
      'Ref' => 'Member',
      'Filter' => '1',
    ));
    $this->System->FormManager->RegisterFormType('TDeviceListAPI', array(
      'Type' => 'ManyToOne',
      'Table' => 'NetworkDevice',
      'Id' => 'Id',
      'Ref' => 'API',
      'Filter' => '1',
    ));
    $this->System->FormManager->RegisterFormType('TNetworkLinkType', array(
      'Type' => 'Reference',
      'Table' => 'NetworkLinkType',
      'Id' => 'Id',
      'Name' => 'Name',
      'Filter' => '1',
    ));
    $this->System->FormManager->RegisterFormType('TDeviceAPIType', array(
      'Type' => 'Reference',
      'Table' => 'DeviceAPIType',
      'Id' => 'Id',
      'Name' => 'Name',
      'Filter' => '1',
    ));
    $this->System->FormManager->RegisterFormType('TNetworkInterfaceWirelessListInterface', array(
      'Type' => 'ManyToOne',
      'Table' => 'NetworkInterfaceWireless',
      'Id' => 'Id',
      'Ref' => 'NetworkInterface',
      'Filter' => '1',
    ));
    $this->System->FormManager->RegisterFormType('TInterfaceList', array(
      'Type' => 'ManyToOne',
      'Table' => 'NetworkInterface',
      'Id' => 'Id',
      'Ref' => 'Device',
      'Filter' => '1',
    ));
    $this->System->FormManager->RegisterFormType('TNetworkLinkListInterface', array(
      'Type' => 'ManyToOne',
      'Table' => 'NetworkLinkUnion',
      'Id' => 'Id',
      'Ref' => 'Interface',
      'Filter' => '1',
    ));
    $this->System->FormManager->RegisterFormType('TNetworkLinkListInterface1', array(
      'Type' => 'ManyToOne',
      'Table' => 'NetworkLink',
      'Id' => 'Id',
      'Ref' => 'Interface1',
      'Filter' => '1',
    ));
    $this->System->FormManager->RegisterFormType('TNetworkLinkListInterface2', array(
      'Type' => 'ManyToOne',
      'Table' => 'NetworkLink',
      'Id' => 'Id',
      'Ref' => 'Interface2',
      'Filter' => '1',
    ));
    $this->System->FormManager->RegisterFormType('TNetworkInterfaceUpDown', array(
      'Type' => 'ManyToOne',
      'Table' => 'NetworkInterfaceUpDown',
      'Id' => 'Id',
      'Ref' => 'Interface',
      'Filter' => '1',
    ));

    $this->System->ModuleManager->Modules['IS']->RegisterDashboardItem('Network',
      array('ModuleNetwork', 'ShowDashboardItem'));
  }

  function ShowDashboardItem()
  {
    $Output = '';
    $DbResult = $this->Database->select('NetworkDevice', 'COUNT(*)', '1');
    $DbRow = $DbResult->fetch_row();
    $Output .= 'Síťových zařízení: registrovaných:'.$DbRow['0'];

    $DbResult = $this->Database->select('NetworkDevice', 'COUNT(*)', '`Used`=1');
    $DbRow = $DbResult->fetch_row();
    $Output .= ' použitých:'.$DbRow['0'].'<br>';

    $DbResult = $this->Database->select('NetworkInterface', 'COUNT(*)', '1');
    $DbRow = $DbResult->fetch_row();
    $Output .= 'Síťových rozhraní: '.$DbRow['0'].'<br>';

    $DbResult = $this->Database->select('NetworkSubnet', 'COUNT(*)', '1');
    $DbRow = $DbResult->fetch_row();
    $Output .= 'Síťových podsítí: '.$DbRow['0'].'<br/>';
    return $Output;
  }

  function DoStop()
  {
  }
}
