RU
games/xencenter-xenadmin-xcp-jumphp / src / Form / NetworkPropertiesForm.php · commit fb1b7c9
<?phpnamespace App\Form;use App\Model\Connection;use JumPHP\Gui\Button;use JumPHP\Gui\Form;use JumPHP\Gui\Label;use JumPHP\Gui\TextBox;/** * Properties of a network: network.set_name_label, set_name_description, set_MTU (what changed; Feature\Storage\Actions). * Closes with ['networkProperties' => [network, name, description, mtu]]. * * @property Label $heading * @property TextBox $name * @property TextBox $description * @property TextBox $mtu * @property Label $note * @property Label $error * @property Button $ok * @property Button $cancel */final class NetworkPropertiesForm extends Form{    public function __construct(private \App\Model\Session $session)    {        parent::__construct();    }    /** XAPI's answers, events and the console go on while this dialog is open. */    protected function tick(float $dt): void    {        $this->session->tick($dt);    }    private array $params = [];    /** @return array<string, mixed> */    public static function paramsFor(Connection $c, string $network): array    {        $r = $c->cache->get($network) ?? [];        return ['network' => $network, 'name' => (string)($r['name_label'] ?? ''), 'description' => (string)($r['name_description'] ?? ''), 'mtu' => (int)($r['MTU'] ?? 1500),            'vifs' => count((array)($r['VIFs'] ?? []))];    }    protected function loaded(array $params = []): void    {        $this->params = $params;        $this->heading->text = "Properties of '" . ($params['name'] ?? '') . "'";        $this->name->text = (string)($params['name'] ?? '');        $this->description->text = (string)($params['description'] ?? '');        $this->mtu->text = (string)($params['mtu'] ?? 1500);        $this->note->text = !empty($params['vifs']) ? 'A new MTU reaches the running VMs on this network when their interfaces are plugged again.' : '';        $this->error->text = '';        $this->setFocus($this->name);    }    public function onOkClick(mixed $sender = null): void    {        $mtu = trim($this->mtu->text);        if (trim($this->name->text) === '') {            $this->error->text = 'Enter a name.';            return;        }        if (!ctype_digit($mtu) || (int)$mtu < 68 || (int)$mtu > 9216) {            $this->error->text = 'The MTU is a number from 68 to 9216.';            return;        }        $this->close(['networkProperties' => ['network' => $this->params['network'] ?? '', 'name' => trim($this->name->text), 'description' => $this->description->text,            'mtu' => (int)$mtu]]);    }    public function onCancelClick(Button $sender): void    {        $this->close();    }}