<?php
namespace App\Controller;
use App\Entity\Apps;
use App\Entity\Categories;
use App\Entity\Tokens;
use App\Form\AppsType;
use App\Form\CategoriesType;
use App\Repository\AppsRepository;
use App\Repository\TokensRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
#[Route('/apps')]
class AppsController extends AbstractController
{
#[Route('/', name: 'app_apps_index', methods: ['GET'])]
public function index(AppsRepository $appsRepository): Response
{
$roles = $this->getUser()->getRoles();
if(in_array('ROLE_ADMIN',$roles)){
$apps = $appsRepository->findAll();
} else {
//$apps = $appsRepository->findBy(['id'=>$this->getUser()->getApp()->getId()]);
return $this->redirectToRoute('app_apps_edit',['id'=>$this->getUser()->getApp()->getId()]);
}
return $this->render('apps/index.html.twig', [
'apps' => $apps,
]);
}
#[Route('/new', name: 'app_apps_new', methods: ['GET', 'POST'])]
public function new(Request $request, AppsRepository $appsRepository,TokensRepository $tokensRepository): Response
{
$application = new Apps();
$form = $this->createForm(AppsType::class, $application);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$token = new Tokens();
$permitted_chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
// Output: video-g6swmAP8X5VG4jCi.mp4
$str = substr(str_shuffle($permitted_chars), 0, 34);
// check ob schon da?
/*if ($tokensRepository->findOneByToken($str)){
dd('true');
} else {
dd('false');
}*/
$token->setToken($str);
$application->addToken($token);
$appsRepository->save($application, true);
$this->addFlash('success',"Anwendung \"" . $application->getTitle() . "\" wurde erfolgreich angelegt!");
return $this->redirectToRoute('app_apps_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('apps/new.html.twig', [
'application' => $application,
'form' => $form,
]);
}
#[Route('/{id}', name: 'app_apps_show', methods: ['GET'])]
public function show(Apps $application): Response
{
return $this->render('apps/show.html.twig', [
'application' => $application,
]);
}
#[Route('/{id}/edit', name: 'app_apps_edit', methods: ['GET', 'POST'])]
public function edit(Request $request, Apps $application, AppsRepository $appsRepository): Response
{
$roles = $this->getUser()->getRoles();
if(! in_array('ROLE_ADMIN',$roles) and $application->getId() != $this->getUser()->getApp()->getId()){
$this->addFlash('notice','Fehler: Sicherheitsverletzung');
return $this->redirectToRoute('app_places_index');
}
$form = $this->createForm(AppsType::class, $application,['roles'=>$roles]);
$form->handleRequest($request);
$cats = $application->getCategories();
$category = new Categories();
$form2 = $this->createForm(CategoriesType::class, $category,['application' => $application]);
if ($form->isSubmitted() && $form->isValid()) {
$appsRepository->save($application, true);
$this->addFlash('success',"Anwendung \"" . $application->getTitle() . "\" wurde erfolgreich aktualisiert!");
return $this->redirectToRoute('app_apps_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('apps/edit.html.twig', [
'application' => $application,
'form' => $form,
'form2' => $form2,
'cats' => $cats,
'category' => $category
]);
}
#[Route('/{id}', name: 'app_apps_delete', methods: ['POST'])]
public function delete(Request $request, Apps $application, AppsRepository $appsRepository): Response
{
$roles = $this->getUser()->getRoles();
if(! in_array('ROLE_ADMIN',$roles) and $application->getId() != $this->getUser()->getApp()->getId()){
$this->addFlash('notice','Fehler: Sicherheitsverletzung');
return $this->redirectToRoute('app_places_index');
}
// nicht den Ast absägen, auf dem man sitzt
if ($application->getUser()->getId() == $this->getUser()->getId()){
$this->addFlash('notice','Würden Sie dies tun, sägen Sie sich den Ast ab, auf dem Sie sitzen. Jedem Benutzer muss genau eine Anwendung zugewiesen sein.');
return $this->redirectToRoute('app_apps_index');
}
if ($this->isCsrfTokenValid('delete'.$application->getId(), $request->request->get('_token'))) {
$this->addFlash('success','Anwendung ' . $application->getTitle() .' erfolgreich gelöscht.');
$appsRepository->remove($application, true);
}
return $this->redirectToRoute('app_apps_index', [], Response::HTTP_SEE_OTHER);
}
}