Skip to content

Commit

Permalink
úprava názvu triedy aby to fungovalo
Browse files Browse the repository at this point in the history
  • Loading branch information
Filip10 committed Dec 6, 2023
1 parent 475d1ee commit 8fec9af
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 24 deletions.
63 changes: 60 additions & 3 deletions App/Controllers/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
namespace App\Controllers;

use App\Core\AControllerBase;
use App\Core\HTTPException;
use App\Core\Responses\RedirectResponse;
use App\Core\Responses\Response;
use App\Helpers\FileStorage;
use App\Models\Post;

class PostController extends AControllerBase
Expand All @@ -12,7 +15,36 @@ public function index(): Response
{
return $this->html();
}
public function add() : Response

public function authorize(string $action)
{
$id = $this->request()->getValue('id');

if ($id && ($action == "save" || $action == "delete")) {

$post = Post::getOne($id);
return $this->app->getAuth()->getLoggedUserName() == $post->getAuthor();


}

return $this->app->getAuth()->isLogged();
}

public function save()
{
$id = (int)$this->request()->getValue('id');


$post = new Post();
$post->setAutor($this->app->getAuth()->getLoggedUserName());
$post->setNazov($this->request()->getValue('nazov'));
$post->setPopis($this->request()->getValue('popis'));
$post->setDatumPublikovania($this->request()->getValue('datumPublikovania'));
$post->setZdroj($this->request()->getValue('zdroj'));
}

public function add(): Response
{
if ($this->request()->getValue('text')) {

Expand All @@ -29,8 +61,33 @@ public function add() : Response
return $this->html();
}

public function edit() : response {
public function edit(): Response
{
$id = (int)$this->request()->getValue('id');
$post = Post::getOne($id);

return $this->html();
if (is_null($post)) {
throw new HTTPException(404);
}

return $this->html(
[
'post' => $post
]
);
}

public function delete()
{
$id = (int)$this->request()->getValue('id');
$post = Post::getOne($id);

// if (is_null($post)) {
// throw new HTTPException(404);
// } else {
// FileStorage::deleteFile($post->getPicture());
// $post->delete();
// return new RedirectResponse($this->url("home.index"));
// }
}
}
11 changes: 11 additions & 0 deletions App/Models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ class Post extends Model
protected string $popis;
protected string $datumPublikovania;
protected string $zdroj;
protected string $autor;

public function getAutor(): string
{
return $this->autor;
}

public function setAutor(string $autor): void
{
$this->autor = $autor;
}

public function getId(): int
{
Expand Down
48 changes: 48 additions & 0 deletions App/Views/Admin/edit.view.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

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

use App\Models\Post;

?>
<?php

if (!is_null(@$data['errors'])): ?>
<?php foreach ($data['errors'] as $error): ?>
<div class="alert alert-danger" role="alert">
<?= $error ?>
</div>
<?php endforeach; ?>
<?php endif; ?>
<form method="post" action="<?= $link->url('post.save') ?>" enctype="multipart/form-data">

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

<!-- 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="<?= @$data['post']?->getNazov() ?>">
</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="<?= @$data['post']?->getPopis() ?>">
</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="<?= @$data['post']?->getDatumPublikovania() ?>">
</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="<?= @$data['post']?->getZdroj() ?>">
</div>

<button type="submit" class="btn btn-primary">Uložiť</button>
</form>
20 changes: 0 additions & 20 deletions App/Views/Admin/pridajPrispevok.view.php

This file was deleted.

6 changes: 5 additions & 1 deletion App/Views/Home/ostatne.view.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php

/** @var Post[] $data */

/** @var \App\Models\Post $post */
/** @var \App\Core\LinkGenerator $link */
/** @var \App\Core\IAuthenticator $auth */

use App\Models\Post;

Expand Down Expand Up @@ -54,6 +55,9 @@ class="icon-link gap-1 icon-link-hover stretched-link" style="margin-bottom: 0.8
</svg>
</div>
</div>
<?php if ($auth->isLogged() && $auth->getLoggedUserName()) { ?>
<a href="<?= $link->url('admin.edit', ['id' => $post->getId()]) ?>" class="btn btn-primary">Upraviť</a>
<?php } ?>
</div>
<?php
}
Expand Down
18 changes: 18 additions & 0 deletions public/js/negative-number.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function validateNumber() {
var numericInput = document.getElementById("numericInput").value;
var errorMessage = "";

// Check if the entered value is not negative
if (parseFloat(numericInput) < 0) {
errorMessage = "Please enter a non-negative number.";
}

// Display error message or submit the form
if (errorMessage !== "") {
document.getElementById("error-message").innerHTML = errorMessage;
return false;
} else {
document.getElementById("error-message").innerHTML = "";
return true;
}
}

0 comments on commit 8fec9af

Please sign in to comment.