76 строки
1.9 KiB
PHP
76 строки
1.9 KiB
PHP
<?php
|
|
|
|
namespace MyApp\Controller;
|
|
|
|
use MyApp\Core\Controller;
|
|
use MyApp\View\ServersView as View;
|
|
use MyApp\Service\ServersService as Service;
|
|
use MyApp\Service\GroupsService;
|
|
|
|
class ServersController extends Controller
|
|
{
|
|
/**
|
|
* @RouteRegExp = "|^/servers$|"
|
|
*/
|
|
public function index()
|
|
{
|
|
$this->htmlResponse((new View())->index((new Service())->getTableData()));
|
|
}
|
|
|
|
/**
|
|
* @RouteRegExp = "|^/servers/add$|"
|
|
* @HttpMethod = "GET,POST"
|
|
*/
|
|
public function add()
|
|
{
|
|
$service = new Service();
|
|
|
|
if (!empty($_POST)) {
|
|
/** @var \MyApp\Model\ServersModel $model */
|
|
$model = $service->addServer((string) $_POST['name'],
|
|
(string) $_POST['ip'], (int) $_POST['group']);
|
|
if ($model) {
|
|
$this->redirect('/servers/edit/'.$model->getId());
|
|
}
|
|
}
|
|
|
|
$group_service = new GroupsService();
|
|
$select = $group_service->sortForSelect($group_service->getTableData());
|
|
|
|
$this->htmlResponse((new View())->add($select));
|
|
}
|
|
|
|
/**
|
|
* @RouteRegExp = "|^/servers/edit/(?<id>\d+)$|"
|
|
* @HttpMethod = "GET,POST"
|
|
*/
|
|
public function edit($id)
|
|
{
|
|
$service = new Service();
|
|
|
|
$model = !empty($_POST) ? $service->updateServer((int) $id,
|
|
(string) $_POST['name'], (string) $_POST['ip'],
|
|
(int) $_POST['group']) : $service->getServer((int) $id);
|
|
|
|
if ($model) {
|
|
$group_service = new GroupsService();
|
|
$select = $group_service->sortForSelect($group_service->getTableData());
|
|
|
|
$this->htmlResponse((new View())->edit($model, $select));
|
|
} else {
|
|
$this->redirect('/servers');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @RouteRegExp = "|^/servers/delete$|"
|
|
* @HttpMethod = "POST"
|
|
*/
|
|
public function delete()
|
|
{
|
|
if (!empty($_POST)) {
|
|
(new Service())->deleteServer((int) $_POST['id']);
|
|
}
|
|
}
|
|
}
|