Skip to content

Commit

Permalink
funguje edit príspevku aj pridávanie nového
Browse files Browse the repository at this point in the history
  • Loading branch information
Filip10 committed Dec 6, 2023
1 parent 8fec9af commit 800b3d7
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 42 deletions.
73 changes: 37 additions & 36 deletions App/Controllers/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Core\Responses\Response;
use App\Helpers\FileStorage;
use App\Models\Post;
use DateTime;

/**
* Class HomeController
Expand Down Expand Up @@ -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');
Expand All @@ -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"));
}
}
2 changes: 1 addition & 1 deletion App/Controllers/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();


}
Expand Down
4 changes: 2 additions & 2 deletions App/Models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 38 additions & 0 deletions App/Views/Admin/add.view.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/** @var \App\Core\LinkGenerator $link */
/** @var Array $data */

use App\Models\Post;

?>
<form method="post" action="<?= $link->url('admin.create') ?>" enctype="multipart/form-data">

<input type="hidden" name="id" value="">

<!-- text (Nazov) -->
<label for="post-nazov" class="form-label">Názov príspevku</label>
<div class="input-group has-validation mb-3 ">
<input type="text" class="form-control" name="nazov" id="post-nazov" value="">
</div>

<!-- text (Popis) -->
<label for="post-popis" class="form-label">Text príspevku</label>
<div class="input-group has-validation mb-3 ">
<input type="text" class="form-control" name="popis" id="post-popis" value="">
</div>

<!-- date (Datum Publikovania) -->
<label for="post-date" class="form-label">Dátum publikovania</label>
<div class="input-group has-validation mb-3 ">
<input type="date" class="form-control" name="date" id="post-date" value="">
</div>

<!-- url (Zdroj) -->
<label for="post-url" class="form-label">Adresa</label>
<div class="input-group has-validation mb-3 ">
<input type="url" class="form-control" name="url" id="post-url" value="">
</div>

<button type="submit" class="btn btn-primary">Uložiť</button>
</form>
8 changes: 6 additions & 2 deletions App/Views/Admin/edit.view.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
</div>
<?php endforeach; ?>
<?php endif; ?>
<form method="post" action="<?= $link->url('post.save') ?>" enctype="multipart/form-data">
<form method="post" action="<?= $link->url('admin.save') ?>" enctype="multipart/form-data">

<input type="hidden" name="id" value="<?= @$data['post']?->getId() ?>">

Expand All @@ -32,10 +32,14 @@
</div>

<!-- date (Datum Publikovania) -->
<?php
$originalDateString = @$data['post']?->getDatumPublikovania();
$formattedDate = date('Y-m-d', strtotime($originalDateString));
?>
<label for="post-date" class="form-label">Dátum publikovania</label>
<div class="input-group has-validation mb-3 ">
<input type="date" class="form-control" name="date" id="post-date"
value="<?= @$data['post']?->getDatumPublikovania() ?>">
value="<?= $formattedDate ?>">
</div>

<!-- url (Zdroj) -->
Expand Down
2 changes: 1 addition & 1 deletion App/Views/Admin/index.view.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<div>
Vitaj, <strong><?= $auth->getLoggedUserName() ?></strong>!<br><br>
Táto časť aplikácie je prístupná len po prihlásení.<br>
<a class="nav-link" href="<?= $link->url("admin.pridajPrispevok") ?>">Pridaj príspevok tu</a>
<a class="nav-link" href="<?= $link->url("admin.add") ?>">Pridaj príspevok tu</a>
</div>
</div>
</div>
Expand Down

0 comments on commit 800b3d7

Please sign in to comment.