source: trunk/Modules/NetworkConfig/NetworkConfig.php

Last change on this file was 899, checked in by chronos, 4 years ago
File size: 4.4 KB
RevLine 
[522]1<?php
2
[899]3class ModuleNetworkConfig extends Module
[522]4{
[888]5 public array $ConfigItems;
[781]6
[887]7 function __construct(System $System)
[522]8 {
9 parent::__construct($System);
10 $this->Name = 'NetworkConfig';
11 $this->Version = '1.0';
12 $this->Creator = 'Chronos';
[899]13 $this->License = 'GNU/GPLv3';
[522]14 $this->Description = 'Network device remote configuration';
[899]15 $this->Dependencies = array(ModuleNetwork::GetName());
16 $this->Models = array(NetworkConfigurationLog::GetClassName(), NetworkConfiguration::GetClassName());
17
[781]18 $this->ConfigItems = array();
[522]19 }
[738]20
[887]21 function DoStart(): void
[522]22 {
[738]23 $this->System->FormManager->RegisterClass('NetworkConfiguration', array(
24 'Title' => 'Restart síťových služeb',
25 'Table' => 'NetworkConfiguration',
26 'Items' => array(
27 'Caption' => array('Type' => 'String', 'Caption' => 'Jméno', 'Default' => ''),
[820]28 'SysName' => array('Type' => 'String', 'Caption' => 'Systémové jméno', 'Default' => ''),
[738]29 'Changed' => array('Type' => 'TNetworkConfigurationState', 'Caption' => 'Stav', 'Default' => 0),
30 'LastTime' => array('Type' => 'DateTime', 'Caption' => 'Naposledy spuštěno', 'ReadOnly' => true),
31 'ExecutionTime' => array('Type' => 'Integer', 'Caption' => 'Doba běhu', 'Default' => '0', 'Suffix' => 'sekund', 'ReadOnly' => true),
32 'Enabled' => array('Type' => 'Boolean', 'Caption' => 'Povoleno', 'Default' => '0'),
33 'Period' => array('Type' => 'Integer', 'Caption' => 'Min. perioda', 'Default' => '60', 'Suffix' => 'sekund'),
34 ),
35 'ItemActions' => array(
36 array('Caption' => 'Záznam', 'URL' => '/is/?a=view&t=NetworkConfigurationLog&i=#RowId'),
37 ),
38 ));
39 $this->System->FormManager->RegisterClass('NetworkConfigurationLog', array(
40 'Title' => 'Záznam restartu síťových služeb',
41 'Table' => 'NetworkConfiguration',
42 'Items' => array(
43 'Caption' => array('Type' => 'String', 'Caption' => 'Jméno', 'Default' => ''),
44 'Log' => array('Type' => 'Text', 'Caption' => 'Záznam', 'Default' => '', 'ReadOnly' => true),
45 ),
46 ));
[817]47 $this->System->FormManager->RegisterFormType('TNetworkConfigurationState', array(
48 'Type' => 'Enumeration',
49 'States' => array('Neplánováno', 'V plánu', 'Provádí se'),
50 ));
[887]51
52 $this->System->RegisterCommandLine('config', 'Configures network services.', array($this, 'Config'));
[817]53 $this->System->Models['NetworkDevice']->RegisterOnChange('NetworkConfig', array($this, 'DoNetworkChange'));
54 $this->System->Models['NetworkInterface']->RegisterOnChange('NetworkConfig', array($this, 'DoNetworkChange'));
[781]55 }
[887]56
57 function DoNetworkChange(): void
[817]58 {
59 $this->Database->query('UPDATE `NetworkConfiguration` SET `Changed`=1 WHERE '.
60 '(`Id`=1) OR (`Id`=7) OR (`Id`=8) OR (`Id`=9) OR (`Id`=10) OR (`Id`=11) OR (`Id`=12) OR (`Id`=13)');
61 }
[738]62
[887]63 function RegisterConfigItem(string $Name, string $ClassName): void
[781]64 {
65 $this->ConfigItems[$Name] = $ClassName;
[522]66 }
[781]67
[887]68 function UnregisterConfigItem(string $Name): void
[781]69 {
70 unset($this->ConfigItems[$Name]);
71 }
72
[887]73 function Config(array $Parameters): void
[781]74 {
75 $Output = '';
[873]76 if ($Parameters >= 3)
[781]77 {
78 $ConfigItemName = $Parameters[2];
[873]79 if (array_key_exists($ConfigItemName, $this->ConfigItems))
[781]80 {
81 $ClassName = $this->ConfigItems[$ConfigItemName];
82 $ConfigItem = new $ClassName($this->System);
83 $ConfigItem->Run();
84 } else $Output = 'Config item '.$ConfigItemName.' not found';
85 } else $Output = 'Not enough parameters';
[887]86 echo($Output);
[781]87 }
[887]88
[899]89 static function Cast(Module $Module): ModuleNetworkConfig
[887]90 {
[899]91 if ($Module instanceof ModuleNetworkConfig)
[887]92 {
[899]93 return $Module;
[887]94 }
[899]95 throw new Exception('Expected ModuleNetworkConfig type but got '.gettype($Module));
[887]96 }
[522]97}
[781]98
99class NetworkConfigItem extends Model
100{
[887]101 function Run(): void
[781]102 {
103 }
[873]104}
[894]105
106class NetworkConfiguration extends Model
107{
[899]108 static function GetModelDesc(): ModelDesc
[894]109 {
110 $Desc = new ModelDesc(self::GetClassName());
111 $Desc->AddString('Caption');
112 $Desc->AddString('SysName');
113 $Desc->AddBoolean('Changed');
114 $Desc->AddDateTime('LastTime');
115 $Desc->AddInteger('ExecutionTime');
116 $Desc->AddBoolean('Enabled');
117 $Desc->AddInteger('Period');
118 return $Desc;
119 }
120}
121
122class NetworkConfigurationLog extends Model
123{
[899]124 static function GetModelDesc(): ModelDesc
[894]125 {
126 $Desc = new ModelDesc(self::GetClassName());
127 $Desc->AddString('Caption');
128 $Desc->AddText('Log');
129 return $Desc;
130 }
131}
Note: See TracBrowser for help on using the repository browser.