Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Admin #44

Merged
merged 1 commit into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions assets/styles/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ body {
bottom: 10px;
}

td a {
text-decoration: none;
}

textarea.form-control {
height: 200px;
}
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"symfony/web-link": "7.0.*",
"symfony/yaml": "7.0.*",
"twig/extra-bundle": "^2.12|^3.0",
"twig/string-extra": "^3.8",
"twig/twig": "^2.12|^3.0"
},
"config": {
Expand Down
69 changes: 68 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

69 changes: 69 additions & 0 deletions src/Controller/Admin/AdminController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

declare(strict_types=1);

namespace App\Controller\Admin;

use App\Entity\User;
use App\Repository\CategoryRepository;
use App\Repository\CommentRepository;
use App\Repository\ImageRepository;
use App\Repository\TrickRepository;
use App\Repository\UserRepository;
use App\Repository\VideoRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

#[Route('/admin')]
class AdminController extends AbstractController
{
#[Route('/', name: 'admin', methods: ['GET'])]
public function index(
TrickRepository $trickRepository,
CategoryRepository $categoryRepository,
UserRepository $userRepository,
CommentRepository $commentRepository,
ImageRepository $imageRepository,
VideoRepository $videoRepository
): Response
{
return $this->render('admin/index.html.twig', [
'tricks' => $trickRepository->findAll(),
'categories' => $categoryRepository->findAll(),
'users' => $userRepository->findAll(),
'comments' => $commentRepository->findAll(),
'images' => $imageRepository->findAll(),
'videos' => $videoRepository->findAll(),
]);
}

#[Route('/user/{id}', name: 'app_user_delete', methods: ['POST'])]
public function delete(Request $request, User $user, EntityManagerInterface $entityManager): Response
{
/** @var User $currentUser */
$currentUser = $this->getUser();
if ($currentUser->getId() === $user->getId()) {
$this->addFlash('danger', 'You cannot delete the logged in user.');
return $this->redirectToRoute('admin', [], Response::HTTP_SEE_OTHER);
}
if ($this->isCsrfTokenValid('delete'.$user->getId(), $request->request->get('_token'))) {
foreach ($user->getTokens() as $token) {
$entityManager->remove($token);
}
foreach ($user->getTricks() as $trick) {
$entityManager->remove($trick);
}
foreach ($user->getComments() as $comment) {
$entityManager->remove($comment);
}
$entityManager->remove($user);
$entityManager->flush();
$this->addFlash('success', 'User deleted with associated tokens, tricks and comments.');
}

return $this->redirectToRoute('admin', [], Response::HTTP_SEE_OTHER);
}
}
31 changes: 9 additions & 22 deletions src/Controller/Admin/CategoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use App\Entity\Category;
use App\Form\CategoryType;
use App\Repository\CategoryRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
Expand All @@ -16,14 +15,6 @@
#[Route('/admin/category')]
class CategoryController extends AbstractController
{
#[Route('/', name: 'app_category_index', methods: ['GET'])]
public function index(CategoryRepository $categoryRepository): Response
{
return $this->render('category/index.html.twig', [
'categories' => $categoryRepository->findAll(),
]);
}

#[Route('/new', name: 'app_category_new', methods: ['GET', 'POST'])]
public function new(Request $request, EntityManagerInterface $entityManager): Response
{
Expand All @@ -35,20 +26,13 @@ public function new(Request $request, EntityManagerInterface $entityManager): Re
$entityManager->persist($category);
$entityManager->flush();

return $this->redirectToRoute('app_category_index', [], Response::HTTP_SEE_OTHER);
return $this->redirectToRoute('admin', [], Response::HTTP_SEE_OTHER);
}

return $this->render('category/new.html.twig', [
return $this->render('generic/new.html.twig', [
'category' => $category,
'form' => $form,
]);
}

#[Route('/{id}', name: 'app_category_show', methods: ['GET'])]
public function show(Category $category): Response
{
return $this->render('category/show.html.twig', [
'category' => $category,
'name' => 'category',
]);
}

Expand All @@ -61,12 +45,13 @@ public function edit(Request $request, Category $category, EntityManagerInterfac
if ($form->isSubmitted() && $form->isValid()) {
$entityManager->flush();

return $this->redirectToRoute('app_category_index', [], Response::HTTP_SEE_OTHER);
return $this->redirectToRoute('admin', [], Response::HTTP_SEE_OTHER);
}

return $this->render('category/edit.html.twig', [
return $this->render('generic/edit.html.twig', [
'category' => $category,
'form' => $form,
'name' => 'category',
]);
}

Expand All @@ -77,8 +62,10 @@ public function delete(Request $request, Category $category, EntityManagerInterf
if ($this->isCsrfTokenValid('delete'.$category->getId(), $token)) {
$entityManager->remove($category);
$entityManager->flush();

$this->addFlash('success', 'Category deleted successfully');
}

return $this->redirectToRoute('app_category_index', [], Response::HTTP_SEE_OTHER);
return $this->redirectToRoute('admin', [], Response::HTTP_SEE_OTHER);
}
}
2 changes: 2 additions & 0 deletions src/Controller/User/CommentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public function delete(Request $request, Comment $comment, EntityManagerInterfac
if ($this->isCsrfTokenValid('delete'.$comment->getId(), $request->request->get('_token'))) {
$entityManager->remove($comment);
$entityManager->flush();

$this->addFlash('success', 'Comment deleted successfully');
}

return $this->redirectToRoute('home', [], Response::HTTP_SEE_OTHER);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace App\Controller\Admin;
namespace App\Controller\User;

use App\Entity\Image;
use App\Form\ImageType;
Expand All @@ -16,7 +16,7 @@
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

#[Route('/admin/image')]
#[Route('/user/image')]
class ImageController extends AbstractController
{
#[Route('/', name: 'app_image_index', methods: ['GET'])]
Expand Down Expand Up @@ -101,6 +101,8 @@ public function delete(Request $request, Image $image, EntityManagerInterface $e
}
$entityManager->remove($image);
$entityManager->flush();

$this->addFlash('success', 'Image deleted successfully');
}

return $this->redirectToRoute('app_image_index', [], Response::HTTP_SEE_OTHER);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

declare(strict_types=1);

namespace App\Controller;
namespace App\Controller\User;

use App\Controller\BaseController;
use App\Entity\Token;
use App\Entity\User;
use App\Form\ForgetPasswordType;
Expand Down
2 changes: 2 additions & 0 deletions src/Controller/User/VideoController.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ public function delete(Request $request, Video $video, EntityManagerInterface $e
if ($this->isCsrfTokenValid('delete'.$video->getId(), $token)) {
$entityManager->remove($video);
$entityManager->flush();

$this->addFlash('success', 'Video deleted successfully');
}

return $this->redirectToRoute('app_video_index', [], Response::HTTP_SEE_OTHER);
Expand Down
46 changes: 46 additions & 0 deletions templates/_inc/_admin/_category.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<a href="{{ path('app_category_new') }}" class="btn btn-primary">New category</a>
<table class="table">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Tricks</th>
<th>actions</th>
</tr>
</thead>
<tbody>
{% for category in categories %}
<tr>
<td>{{ category.id }}</td>
<td>{{ category.name }}</td>
<td>
<span class="badge text-bg-secondary">{{ category.tricks.count }}</span>
</td>
<td>
<a href="{{ path('app_category_edit', {'id': category.id}) }}">
<i class="bi bi-pencil"></i>
</a>
<span data-bs-toggle="modal" data-bs-target="#category-{{ category.id }}">
<i class="bi bi-trash"></i>
</span>
{{ include('_inc/_modal.html.twig', {
id: category.id,
htmlName: 'category',
name: category.name,
includeForm: include('_inc/_delete_form.html.twig', {
action: path('app_category_delete', {'id': category.id}),
content: 'Delete',
value: csrf_token('delete' ~ category.id),
id: category.id,
name: 'category'
})
}) }}
</td>
</tr>
{% else %}
<tr>
<td colspan="4">no records found</td>
</tr>
{% endfor %}
</tbody>
</table>
40 changes: 40 additions & 0 deletions templates/_inc/_admin/_comment.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<table class="table">
<thead>
<tr>
<th>Id</th>
<th>Content</th>
<th>User</th>
<th>Trick</th>
<th>actions</th>
</tr>
</thead>
<tbody>
{% for comment in comments %}
<tr>
<td>{{ comment.id }}</td>
<td>{{ comment.content|u.truncate(100, '...') }}</td>
<td>{{ comment.user.username }}</td>
<td>{{ comment.trick.name }}</td>
<td>
<span data-bs-toggle="modal" data-bs-target="#comment-{{ comment.id }}">
<i class="bi bi-trash"></i>
</span>
{{ include('_inc/_modal.html.twig', {
id: comment.id,
htmlName: 'comment',
name: comment.content,
includeForm: include('_inc/_delete_form.html.twig', {
action: path('app_comment_delete', {'id': comment.id}),
content: 'Delete',
value: csrf_token('delete' ~ comment.id),
})
}) }}
</td>
</tr>
{% else %}
<tr>
<td colspan="6">no records found</td>
</tr>
{% endfor %}
</tbody>
</table>
Loading