src/Controller/AppsController.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Apps;
  4. use App\Entity\Categories;
  5. use App\Entity\Tokens;
  6. use App\Form\AppsType;
  7. use App\Form\CategoriesType;
  8. use App\Repository\AppsRepository;
  9. use App\Repository\TokensRepository;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. #[Route('/apps')]
  15. class AppsController extends AbstractController
  16. {
  17.     #[Route('/'name'app_apps_index'methods: ['GET'])]
  18.     public function index(AppsRepository $appsRepository): Response
  19.     {
  20.         $roles $this->getUser()->getRoles();
  21.         if(in_array('ROLE_ADMIN',$roles)){
  22.             $apps $appsRepository->findAll();
  23.         } else {
  24.             //$apps = $appsRepository->findBy(['id'=>$this->getUser()->getApp()->getId()]);
  25.             return $this->redirectToRoute('app_apps_edit',['id'=>$this->getUser()->getApp()->getId()]);
  26.         }
  27.         return $this->render('apps/index.html.twig', [
  28.             'apps' => $apps,
  29.         ]);
  30.     }
  31.     #[Route('/new'name'app_apps_new'methods: ['GET''POST'])]
  32.     public function new(Request $requestAppsRepository $appsRepository,TokensRepository $tokensRepository): Response
  33.     {
  34.         $application = new Apps();
  35.         $form $this->createForm(AppsType::class, $application);
  36.         $form->handleRequest($request);
  37.         if ($form->isSubmitted() && $form->isValid()) {
  38.             $token = new Tokens();
  39.             $permitted_chars '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  40.             // Output: video-g6swmAP8X5VG4jCi.mp4
  41.             $str substr(str_shuffle($permitted_chars), 034);
  42.             // check ob schon da?
  43.             /*if ($tokensRepository->findOneByToken($str)){
  44.                 dd('true');
  45.             } else {
  46.                 dd('false');
  47.             }*/
  48.             $token->setToken($str);
  49.             $application->addToken($token);
  50.             $appsRepository->save($applicationtrue);
  51.             $this->addFlash('success',"Anwendung \"" $application->getTitle() . "\" wurde erfolgreich angelegt!");
  52.             return $this->redirectToRoute('app_apps_index', [], Response::HTTP_SEE_OTHER);
  53.         }
  54.         return $this->renderForm('apps/new.html.twig', [
  55.             'application' => $application,
  56.             'form' => $form,
  57.         ]);
  58.     }
  59.     #[Route('/{id}'name'app_apps_show'methods: ['GET'])]
  60.     public function show(Apps $application): Response
  61.     {
  62.         return $this->render('apps/show.html.twig', [
  63.             'application' => $application,
  64.         ]);
  65.     }
  66.     #[Route('/{id}/edit'name'app_apps_edit'methods: ['GET''POST'])]
  67.     public function edit(Request $requestApps $applicationAppsRepository $appsRepository): Response
  68.     {
  69.         $roles $this->getUser()->getRoles();
  70.         if(! in_array('ROLE_ADMIN',$roles) and $application->getId() != $this->getUser()->getApp()->getId()){
  71.             $this->addFlash('notice','Fehler: Sicherheitsverletzung');
  72.             return $this->redirectToRoute('app_places_index');
  73.         }
  74.         $form $this->createForm(AppsType::class, $application,['roles'=>$roles]);
  75.         $form->handleRequest($request);
  76.         $cats $application->getCategories();
  77.         $category = new Categories();
  78.         $form2 $this->createForm(CategoriesType::class, $category,['application' => $application]);
  79.         if ($form->isSubmitted() && $form->isValid()) {
  80.             $appsRepository->save($applicationtrue);
  81.             $this->addFlash('success',"Anwendung \"" $application->getTitle() . "\" wurde erfolgreich aktualisiert!");
  82.             return $this->redirectToRoute('app_apps_index', [], Response::HTTP_SEE_OTHER);
  83.         }
  84.         return $this->renderForm('apps/edit.html.twig', [
  85.             'application' => $application,
  86.             'form' => $form,
  87.             'form2' => $form2,
  88.             'cats' => $cats,
  89.             'category' => $category
  90.         ]);
  91.     }
  92.     #[Route('/{id}'name'app_apps_delete'methods: ['POST'])]
  93.     public function delete(Request $requestApps $applicationAppsRepository $appsRepository): Response
  94.     {
  95.         $roles $this->getUser()->getRoles();
  96.         if(! in_array('ROLE_ADMIN',$roles) and $application->getId() != $this->getUser()->getApp()->getId()){
  97.             $this->addFlash('notice','Fehler: Sicherheitsverletzung');
  98.             return $this->redirectToRoute('app_places_index');
  99.         }
  100.         // nicht den Ast absägen, auf dem man sitzt
  101.         if ($application->getUser()->getId() == $this->getUser()->getId()){
  102.             $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.');
  103.             return $this->redirectToRoute('app_apps_index');
  104.         }
  105.         if ($this->isCsrfTokenValid('delete'.$application->getId(), $request->request->get('_token'))) {
  106.             $this->addFlash('success','Anwendung ' $application->getTitle() .' erfolgreich gelöscht.');
  107.             $appsRepository->remove($applicationtrue);
  108.         }
  109.         return $this->redirectToRoute('app_apps_index', [], Response::HTTP_SEE_OTHER);
  110.     }
  111. }