From 800b3d70b533eb3ff677668b82b203fd79276e6d Mon Sep 17 00:00:00 2001 From: filip <25711588+Filip10@users.noreply.github.com> Date: Wed, 6 Dec 2023 22:13:03 +0100 Subject: [PATCH] =?UTF-8?q?funguje=20edit=20pr=C3=ADspevku=20aj=20prid?= =?UTF-8?q?=C3=A1vanie=20nov=C3=A9ho?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- App/Controllers/AdminController.php | 73 +++++++++++++++-------------- App/Controllers/PostController.php | 2 +- App/Models/Post.php | 4 +- App/Views/Admin/add.view.php | 38 +++++++++++++++ App/Views/Admin/edit.view.php | 8 +++- App/Views/Admin/index.view.php | 2 +- 6 files changed, 85 insertions(+), 42 deletions(-) create mode 100644 App/Views/Admin/add.view.php diff --git a/App/Controllers/AdminController.php b/App/Controllers/AdminController.php index 9680dee..594af83 100644 --- a/App/Controllers/AdminController.php +++ b/App/Controllers/AdminController.php @@ -8,6 +8,7 @@ use App\Core\Responses\Response; use App\Helpers\FileStorage; use App\Models\Post; +use DateTime; /** * Class HomeController @@ -35,15 +36,6 @@ public function index(): Response return $this->html(); } - /** - * Example of an action (authorization needed) - * @return \App\Core\Responses\Response|\App\Core\Responses\ViewResponse - */ - public function pridajPrispevok(): Response - { - return $this->html(); - } - public function edit(): Response { $id = (int) $this->request()->getValue('id'); @@ -63,34 +55,43 @@ public function edit(): Response public function save() { $id = (int)$this->request()->getValue('id'); - $oldFileName = ""; - if ($id > 0) { - $post = Post::getOne($id); - $oldFileName = $post->getPicture(); - } else { - $post = new Post(); - $post->setAuthor($this->app->getAuth()->getLoggedUserName()); - } - $post->setText($this->request()->getValue('text')); - $post->setPicture($this->request()->getFiles()['picture']['name']); - $formErrors = $this->formErrors(); - if (count($formErrors) > 0) { - return $this->html( - [ - 'post' => $post, - 'errors' => $formErrors - ], 'add' - ); - } else { - if ($oldFileName != "") { - FileStorage::deleteFile($oldFileName); - } - $newFileName = FileStorage::saveFile($this->request()->getFiles()['picture']); - $post->setPicture($newFileName); - $post->save(); - return new RedirectResponse($this->url("home.index")); - } + //$post = new Post(); // toto je pre nový post + $post = Post::getOne($id); //toto je pre upravenie postu + $post->setAutor($this->app->getAuth()->getLoggedUserName()); + $post->setNazov($this->request()->getValue('nazov')); + $post->setPopis($this->request()->getValue('popis')); + $inputString = $this->request()->getValue('datumPublikovania'); + $dateTime = new DateTime($inputString); + $outputString = $dateTime->format('d.m.Y'); + $post->setDatumPublikovania($outputString); + $urlInput = (string)($this->request()->getValue('url')); + $post->setZdroj($urlInput); + + $post->save(); + return new RedirectResponse($this->url("home.ostatne")); + } + + + public function add(): Response + { + return $this->html(); + } + public function create() + { + $post = new Post(); // toto je pre nový post + $post->setAutor($this->app->getAuth()->getLoggedUserName()); + $post->setNazov($this->request()->getValue('nazov')); + $post->setPopis($this->request()->getValue('popis')); + $inputString = $this->request()->getValue('datumPublikovania'); + $dateTime = new DateTime($inputString); + $outputString = $dateTime->format('d.m.Y'); + $post->setDatumPublikovania($outputString); + $urlInput = (string)($this->request()->getValue('url')); + $post->setZdroj($urlInput); + + $post->save(); + return new RedirectResponse($this->url("home.ostatne")); } } diff --git a/App/Controllers/PostController.php b/App/Controllers/PostController.php index 3d94933..d4636ad 100644 --- a/App/Controllers/PostController.php +++ b/App/Controllers/PostController.php @@ -23,7 +23,7 @@ public function authorize(string $action) if ($id && ($action == "save" || $action == "delete")) { $post = Post::getOne($id); - return $this->app->getAuth()->getLoggedUserName() == $post->getAuthor(); + return $this->app->getAuth()->getLoggedUserName() == $post->getAutor(); } diff --git a/App/Models/Post.php b/App/Models/Post.php index fdf184c..ea94c12 100644 --- a/App/Models/Post.php +++ b/App/Models/Post.php @@ -64,8 +64,8 @@ public function getDatumPublikovania(): string public function setDatumPublikovania(string $datumPublikovania): void { - - $this->datumPublikovania = $datumPublikovania; + $formattedDate = date('Y-m-d', strtotime($datumPublikovania)); + $this->datumPublikovania = $formattedDate; } public function getZdroj(): string diff --git a/App/Views/Admin/add.view.php b/App/Views/Admin/add.view.php new file mode 100644 index 0000000..4a3aab9 --- /dev/null +++ b/App/Views/Admin/add.view.php @@ -0,0 +1,38 @@ + +
\ No newline at end of file diff --git a/App/Views/Admin/edit.view.php b/App/Views/Admin/edit.view.php index d6a173f..b9183fa 100644 --- a/App/Views/Admin/edit.view.php +++ b/App/Views/Admin/edit.view.php @@ -15,7 +15,7 @@ -