diff --git a/public/assets/css/style.css b/public/assets/css/style.css index ee295e7..84237ce 100644 --- a/public/assets/css/style.css +++ b/public/assets/css/style.css @@ -21,6 +21,63 @@ a:visited { border-bottom: .0625rem solid rgba(207, 0, 207, .3); } +.clear-fix { + clear: both !important; +} + +.d-inline-block { + display: inline-block !important; +} + +.f-right { + float: right !important; +} + +.f-left { + float: left !important; +} + +.m-0 { + margin: 0 !important; +} + +.mb-0 { + margin-bottom: 0 !important; +} + +.mr-0 { + margin-right: 0 !important; +} + +.ml-0 { + margin-left: 0 !important; +} + +.mt-0 { + margin-top: 0 !important; +} + +.m-1 { + margin: 1rem !important; +} + +.mb-1 { + margin-bottom: 1rem !important; +} + +.mr-1 { + margin-right: 1rem !important; +} + +.ml-1 { + margin-left: 1rem !important; +} + +.mt-1 { + margin-top: 1rem !important; +} + + body { font-family: Verdana, "Geneva CY", "DejaVu Sans", sans-serif; font-size: 1rem; @@ -56,6 +113,7 @@ body { .navigate { background: black; margin-top: 1.25rem; + margin-bottom: 2.625rem; } .navigate ul { @@ -72,6 +130,7 @@ body { .navigate ul li a { color: white; text-decoration: none; + border-bottom: none; } .navigate ul li a:hover { @@ -84,6 +143,7 @@ body { padding-top: 1rem; padding-bottom: 1rem; } + .footer::before { width: 100%; display: block; @@ -94,5 +154,66 @@ body { } table { + border-spacing: 0; border-collapse: collapse; + width: 100%; +} + +td, th { + border: .0625rem solid rgba(0, 0, 0, .3); + padding: 0.375rem; +} + +.form { + border-radius: .375rem; + border: .0625rem solid rgba(0, 0, 0, .3); + max-width: 22.5rem; + padding: .875rem; +} + +.form__element { + margin-bottom: .375rem; +} + +.form__submit { + margin-top: .5rem; +} + +.form__submit, .btn { + background-color: #fff; + border-radius: .125rem; + border: .0625rem solid rgba(0, 0, 0, .3); + padding: .25rem; + cursor: pointer; +} + +a.btn, a.btn:hover, a.btn:focus { + color: #000; + border: .0625rem solid rgba(0, 0, 0, .3); +} + + +.btn:hover, .btn:focus, +.form__submit:hover, +.form__submit:focus { + background-color: rgba(0, 0, 0, .4); +} + +.form-element { + padding: .25rem 0; +} + +.form-element__data { + min-width: 15rem; + padding-left: .1875rem; +} + +.form-element__label { + min-width: 5rem; + display: inline-block; +} + +select { + height: 1.5rem; + padding-left: 0 !important; } \ No newline at end of file diff --git a/public/assets/js/script.js b/public/assets/js/script.js new file mode 100644 index 0000000..3ae251c --- /dev/null +++ b/public/assets/js/script.js @@ -0,0 +1,23 @@ +let d = document; +let removeModel = (id, url) => { + let formData = new FormData(); + formData.append("id", id || 0); + let xhr = new XMLHttpRequest(); + xhr.open("POST", url); + xhr.send(formData); + xhr.onload = () => location.reload(); +}; +let deleteEvent = (v, message, url) => { + v.addEventListener("click", e => { + if (confirm(message)) + removeModel(e.target.dataset.id, url) + }); +}; + +d.querySelectorAll('.js-group-btn-delete').forEach(v => { + deleteEvent(v, "You're sure to delete this group?", "/groups/delete") +}); + +d.querySelectorAll(".js-server-btn-delete").forEach(v => { + deleteEvent(v, "You're sure to delete this server?", "/servers/delete") +}); \ No newline at end of file diff --git a/src/controllers/GroupsController.php b/src/controllers/GroupsController.php index 8a6e19f..c8d52f9 100644 --- a/src/controllers/GroupsController.php +++ b/src/controllers/GroupsController.php @@ -3,8 +3,9 @@ namespace MyApp\Controller; use MyApp\Core\Controller; +use MyApp\Model\GroupsModel as Model; use MyApp\View\GroupsView as View; -use MyApp\Service\GroupsService; +use MyApp\Service\GroupsService as Service; class GroupsController extends Controller { @@ -13,29 +14,62 @@ class GroupsController extends Controller */ public function index() { - $this->htmlResponse((new View())->index((new GroupsService())->getTableData())); + $this->htmlResponse((new View())->index((new Service())->getTableData())); } /** * @RouteRegExp = "|^/groups/add$|" + * @HttpMethod = "GET,POST" */ public function add() { + $service = new Service(); + if (!empty($_POST)) { + /** @var Model $model */ + $model = $service->addGroup((string) $_POST['name'], + (int) $_POST['parent']); + if ($model) { + $this->redirect('/groups/edit/'.$model->getId()); + } + } + + $select = $service->sortForSelect($service->getTableData()); + + $this->htmlResponse((new View())->add($select)); + } + + /** + * @RouteRegExp = "|^/groups/delete$|" + * @HttpMethod = "POST" + */ + public function delete() + { + if (!empty($_POST)) { + (new Service())->deleteGroup((int) $_POST['id']); + } } /** * @RouteRegExp = "|^/groups/edit/(?\d+)$|" + * @HttpMethod = "GET,POST" + * + * @param $id + * + * @throws \Exception */ public function edit($id) { - $group_service = new GroupsService(); + $service = new Service(); - $model = $group_service->getGroup($id); + $model = !empty($_POST) ? $service->updateGroup((int) $id, + (string) $_POST['name'], + (int) $_POST['parent']) : $service->getGroup((int) $id); if ($model) { - $this->htmlResponse((new View())->edit($group_service->getGroup($id), - $group_service->getTableData())); + $select = $service->sortForSelect($service->getTableData()); + + $this->htmlResponse((new View())->edit($model, $select)); } else { $this->redirect('/groups'); } diff --git a/src/controllers/ServersController.php b/src/controllers/ServersController.php index 9f9556b..328e0dc 100644 --- a/src/controllers/ServersController.php +++ b/src/controllers/ServersController.php @@ -3,6 +3,9 @@ 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 { @@ -11,20 +14,62 @@ class ServersController extends Controller */ 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/(?\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']); + } } } diff --git a/src/core/Model.php b/src/core/Model.php index 7595171..ff22ed4 100644 --- a/src/core/Model.php +++ b/src/core/Model.php @@ -332,6 +332,12 @@ class Model } $query->execute(); + + if (!$update_mode) { + (function () use ($table_map, $db) { + $this->{$table_map['id']} = $db->lastInsertId(); + })->call($model); + } } } $db->commit(); diff --git a/src/models/GroupsModel.php b/src/models/GroupsModel.php index c055482..9a529f6 100644 --- a/src/models/GroupsModel.php +++ b/src/models/GroupsModel.php @@ -1,12 +1,11 @@ name = $name; + return $this; } @@ -69,9 +69,10 @@ class GroupsModel extends Model * * @return GroupsModel */ - public function setParent(int $parent): self + public function setParent(?int $parent): self { $this->parent = $parent; + return $this; } @@ -82,4 +83,4 @@ class GroupsModel extends Model { return $this->parent; } -} \ No newline at end of file +} diff --git a/src/models/ServersModel.php b/src/models/ServersModel.php index 5062f58..1dd6d2d 100644 --- a/src/models/ServersModel.php +++ b/src/models/ServersModel.php @@ -8,8 +8,6 @@ use MyApp\Core\Model; * Class ServersModel. * * @TableName = "servers" - * - * @package MyApp\Model */ class ServersModel extends Model { @@ -94,7 +92,7 @@ class ServersModel extends Model /** * @return int */ - public function getGroup(): int + public function getGroup(): ?int { return $this->group; } @@ -104,7 +102,7 @@ class ServersModel extends Model * * @return ServersModel */ - public function setGroup(int $group): self + public function setGroup(?int $group): self { $this->group = $group; diff --git a/src/services/GroupsService.php b/src/services/GroupsService.php index fe12c0a..6726f9b 100644 --- a/src/services/GroupsService.php +++ b/src/services/GroupsService.php @@ -1,9 +1,9 @@ getParent() === $parent_id) { + $item->{'select-level'} = str_repeat('-', $level); + $out[] = $item; + $out = array_merge($out, + $this->sortForSelect($models, $item->getId(), $level + 1)); + } + } + + return $out; + } + + public function updateGroup(int $id, string $name, int $parent): ?Model + { + if ($model = $this->getGroup($id)) { + $model->setName($name) + ->setParent($parent ? $parent : null); + Model::save([$model]); + } + + return $model; + } + + public function deleteGroup(int $id): void + { + if ($model = $this->getGroup($id)) { + $arr = [$model]; + Model::delete($arr); + } + } + + public function addGroup(string $name, int $parent): ?Model + { + $model = (new GroupsModel())->setName($name) + ->setParent($parent ? $parent : null); + Model::save([$model]); + + return $model; + } +} diff --git a/src/services/ServersService.php b/src/services/ServersService.php new file mode 100644 index 0000000..eb9eda4 --- /dev/null +++ b/src/services/ServersService.php @@ -0,0 +1,85 @@ +getGroup()) { + $records[] = $group_id; + } + } + if (count($records)) { + if ($groups = GroupsModel::find($records)) { + $groups_names = []; + /** @var GroupsModel $group */ + foreach ($groups as $group) { + $groups_names[$group->getId()] = $group->getName(); + } + + foreach ($models as $model) { + if (array_key_exists($model->getGroup(), $groups_names)) { + $model->{'group_name'} = $groups_names[$model->getGroup()]; + } + } + } + } + + return $models; + } + + public function getServer($id): ?ServersModel + { + $model = ServersModel::find([$id], null, null, [], '', '1'); + if (!empty($model)) { + return $model[0]; + } + + return null; + } + + public function updateServer(int $id, string $name, string $ip, int $group) + { + if ($model = $this->getServer($id)) { + $model->setName($name) + ->setIp($ip) + ->setGroup($group ? $group : null); + Model::save([$model]); + } + + return $model; + } + + public function deleteServer(int $id): void + { + if ($model = $this->getServer($id)) { + $arr = [$model]; + Model::delete($arr); + } + } + + public function addServer(string $name, string $ip, int $group) + { + $model = (new ServersModel())->setName($name) + ->setIp($ip) + ->setGroup($group ? $group : null); + Model::save([$model]); + + return $model; + } +} diff --git a/src/templates/groups/add.tpl.php b/src/templates/groups/add.tpl.php index 4fffdd8..0a2a403 100644 --- a/src/templates/groups/add.tpl.php +++ b/src/templates/groups/add.tpl.php @@ -1,7 +1,27 @@ -
+require __DIR__ . '/../shared/head.php'; -
+?> +
+
+ + +
+
+ + +
+ +
model; - -function getTree($table, $parent_id = null, $level = 1) -{ - $out = []; - foreach ($table as $item) { - /** @var \MyApp\Model\GroupsModel $item */ - if ( - $item->getParent() === $parent_id) { - $item->{"select-level"} = str_repeat("-", $level); - $out[] = $item; - $out = array_merge($out, - getTree($table, $item->getId(), $level + 1)); - } - } - - return $out; -} - ?> -
- -
- -
- - + - table) as $item): ?> - + select as $item): ?> + getId() !== $model->getId()): ?> + +
-
- - +
+ +
- + - table): ?> -
- - - - - - - - table as $item):?> - - - - - - + Add new + group +
+table): ?> +
IdNameParentActions
getId() ?>getName() ?>getParent() ?>edit
+ + + + + + + table as $item): ?> + + + + + + -
IdNameParentActions
getId() ?>getName() ?>getParent() ?>edit + delete
- + + -
+require __DIR__ . '/../shared/head.php'; +?> + +
+ + +
+
+ + +
+
+ + +
-
+ + model; +?> +
+
+ + +
+
+ + +
+
+ + +
+ + +
+ - + Add new + server +
+table): ?> +
+ + + + + + + + table as $item): ?> + + + + + + + -
IdNameIP AddressGroupActions
getId() ?>getName() ?>getIp() ?>{"group_name"} ?? 'Root' ?>edit + delete
+ + + Belousovv.ru + diff --git a/src/views/DefaultView.php b/src/views/DefaultView.php index 731c52c..beb1c95 100644 --- a/src/views/DefaultView.php +++ b/src/views/DefaultView.php @@ -8,6 +8,6 @@ class DefaultView extends View { public function index(): string { - return $this->setTitle('Index')->render('default/index'); + return $this->setTitle('Home')->render('default/index'); } } diff --git a/src/views/GroupsView.php b/src/views/GroupsView.php index e20d7a0..370f3c7 100644 --- a/src/views/GroupsView.php +++ b/src/views/GroupsView.php @@ -1,26 +1,30 @@ {"table"} = $table; + $this->{'table'} = $table; return $this->setTitle('Edit groups')->render('groups/index'); } - public function edit($model, ?array $table) + public function add(?array $select) { - $this->{"model"} = $model; - $this->{"table"} = $table; + $this->{'select'} = $select; - return $this->setTitle('Edit groups')->render('groups/edit'); + return $this->setTitle('Add new group')->render('groups/add'); } -} \ No newline at end of file + + public function edit($model, ?array $select) + { + $this->{'model'} = $model; + $this->{'select'} = $select; + + return $this->setTitle('Edit group')->render('groups/edit'); + } +} diff --git a/src/views/ServersView.php b/src/views/ServersView.php index ae7d554..5eb1d67 100644 --- a/src/views/ServersView.php +++ b/src/views/ServersView.php @@ -1,12 +1,30 @@ {'table'} = $table; -} \ No newline at end of file + return $this->setTitle('Edit servers')->render('servers/index'); + } + + public function add(?array $select) + { + $this->{'select'} = $select; + + return $this->setTitle('Add new server')->render('servers/add'); + } + + public function edit($model, ?array $select) + { + $this->{'model'} = $model; + $this->{'select'} = $select; + + return $this->setTitle('Edit server')->render('servers/edit'); + } +}