<?php
namespace App\Controller;
use App\AppBundle\Service\RouterRequest;
use App\Entity\ActivityBlueprint;
use App\Entity\Routeur;
use App\Entity\User;
use App\Form\ActivityBlueprintType;
use App\Form\RegistrationFormType;
use App\Form\RouteurType;
use App\Security\UserAuthenticator;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;
class JavaAppliController extends AbstractController
{
#[Route('/', name: 'app_java_appli')]
public function index(): Response
{
return $this->render('java_appli/index.html.twig');
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// USER!
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#[Route('/list_user', name: 'app_list_user')]
public function list_user(EntityManagerInterface $entityManager): Response
{
if (!$this->getUser()) {
return $this->redirectToRoute('index');
}
$users = $entityManager->getRepository(User::class)->findAll();
return $this->render('java_appli/user/list_user.html.twig', [
'users' => $users,
'title' => 'List of users!'
]);
}
#[Route('/user/{id}', name: 'app_one_user')]
public function one_user(int $id, EntityManagerInterface $entityManager): Response
{
if (!$this->getUser()) {
return $this->redirectToRoute('index');
}
$users = $entityManager->getRepository(User::class)->findBy(["id" => $id]);
if (count($users)<1){
throw $this->createNotFoundException(
'No users found'
);
}
return $this->render('java_appli/user/list_user.html.twig', [
'users' => $users,
'title' => 'Show one user'
]);
}
#[Route('/user/{id}/delete', name: 'app_delete_user')]
public function delete_user(int $id, EntityManagerInterface $entityManager): Response
{
if (!$this->getUser()) {
return $this->redirectToRoute('index');
}
$user = $entityManager->getRepository(User::class)->find($id);
if (!$user) {
throw $this->createNotFoundException(
'No user found for id '.$id
);
}
$entityManager->remove($user);
$entityManager->flush();
return $this->redirectToRoute('list_user');
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// API
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#[Route('/api/list_user', name: 'api_list_user')]
public function api_list_user(EntityManagerInterface $entityManager)
{
$response = $entityManager->getRepository(User::class)->findAll();
$user_data = [];
$counting = 1;
foreach ($response as $user){
$user_name = $user->getUserIdentifier();
$user_token = $user->getAuthToken();
$user_data[] = ["nr"=> $counting,"user name" => $user_name, "user token" => $user_token];
$counting++;
}
return new JsonResponse($user_data);
}
#[Route('api/list_router', name: 'api_list_router')]
public function api_list_router(EntityManagerInterface $entityManager): Response
{
$routers = $entityManager->getRepository(Routeur::class)->findAll();
$router_data = [];
foreach ($routers as $router){
$router_name = $router->getSignature();
$auth_token = $router->getAuthToken();
$router_data[] = ["router signature" => $router_name, "auth token" => $auth_token];
}
return new JsonResponse($router_data);
}
#[Route('api/say_my_name', name: 'api_say_my_name')]
public function api_say_my_name(EntityManagerInterface $entityManager): Response
{
$user = $this->getUser();
$router_data[] = ["my_name" => $user->getUserIdentifier()];
return new JsonResponse($router_data);
}
#[Route('api/activity_blueprint', name: 'api_activity_blueprint')]
public function api_activity_blueprint(EntityManagerInterface $entityManager): Response
{
$activities = $entityManager->getRepository(ActivityBlueprint::class)->findAll();
$activity_data = [];
foreach ($activities as $activity){
$id = $activity->getId();
$id_on_list = $activity->getIdOnList();
$title = $activity->getTitle();
$description = $activity->getDescription();
$nb_list = $activity->getNbList();
$entry_position = $activity->getEntryPosition();
$last_update = $activity->getUpdateDate();
// $deleted = $activity->getDeleted();
$activity_data[] = ["id"=>$id,
"id_on_list" => $id_on_list,
"title" => $title,
"description" => $description,
"nb_list" => $nb_list,
"entry_position" => $entry_position,
"last_update" => $last_update];
}
return new JsonResponse($activity_data);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Check List!
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#[Route('/task/list', name: 'list_task')]
public function list_task(EntityManagerInterface $entityManager): Response
{
if (!$this->getUser()) {
return $this->redirectToRoute('index');
}
$tasks = $entityManager->getRepository(ActivityBlueprint::class)->findAll();
return $this->render('java_appli/check_list/check_list.html.twig', [
'tasks' => $tasks,
'title' => 'List of all tasks!'
]);
}
#[Route('/task/create', name: 'app_create_task')]
public function create_task(Request $request, EntityManagerInterface $entityManager): Response
{
if (!$this->getUser()) {
return $this->redirectToRoute('index');
}
$task = new ActivityBlueprint();
$form = $this->createForm(ActivityBlueprintType::class, $task);
$last_on_list = $entityManager->getRepository(ActivityBlueprint::class)->findLast();
$last_nr_on_list = $last_on_list[0]->getIdOnList() ;
$task->setUpdateDate(new \DateTime("now"));
$task->setIdOnList($last_nr_on_list+1);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
try {
$entityManager->persist($task);
$entityManager->flush();
return $this->render('java_appli/check_list/new_task.html.twig', [
'mode' => 'created'
]);
}catch (\Doctrine\DBAL\Exception\UniqueConstraintViolationException $e){
return $this->render('java_appli/check_list/create_task.html.twig', [
'taskForm' => $form->createView(),
'mode' => 'Create',
'error' => 'This combination of list number and entry position already exists.'
]);
}
}
return $this->render('java_appli/check_list/create_task.html.twig', [
'taskForm' => $form->createView(),
'mode' => 'Create'
]);
}
#[Route('/task/{id}', name: 'app_one_task')]
public function one_task(string $id, EntityManagerInterface $entityManager): Response
{
if (!$this->getUser()) {
return $this->redirectToRoute('index');
}
$task = $entityManager->getRepository(ActivityBlueprint::class)->findBy(["id" => $id]);
if (!$task) {
throw $this->createNotFoundException(
'No task found with this id: '. $id
);
}
return $this->render('java_appli/check_list/check_list.html.twig', [
'tasks' => $task,
'title' => 'One of the tasks!'
]);
}
#[Route('/task/{id}/edit', name: 'app_edit_task')]
public function edit_task(int $id, Request $request, EntityManagerInterface $entityManager): Response
{
if (!$this->getUser()) {
return $this->redirectToRoute('index');
}
$task = $entityManager->getRepository(ActivityBlueprint::class)->findOneBy(["id" => $id]);
if (!$task) {
throw $this->createNotFoundException(
'No task found with this id: '. $id
);
}
$form = $this->createForm(ActivityBlueprintType::class, $task);
$task->setUpdateDate(new \DateTime("now"));
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
try {
$entityManager->persist($task);
$entityManager->flush();
return $this->render('java_appli/check_list/new_task.html.twig', [
'mode' => 'edit'
]);
}catch (\Doctrine\DBAL\Exception\UniqueConstraintViolationException $e){
return $this->render('java_appli/check_list/create_task.html.twig', [
'taskForm' => $form->createView(),
'mode' => 'Modify',
'error' => 'This combination of list number and entry position already exists.'
]);
}
}
return $this->render('java_appli/check_list/create_task.html.twig', [
'taskForm' => $form->createView(),
'mode' => 'Modify'
]);
}
#[Route('/task/{id}/delete', name: 'app_delete_task')]
public function delete_task(int $id, EntityManagerInterface $entityManager): Response
{
if (!$this->getUser()) {
return $this->redirectToRoute('index');
}
$task = $entityManager->getRepository(ActivityBlueprint::class)->findOneBy(["id" => $id]);
if (!$task) {
throw $this->createNotFoundException(
'No task found with this id: '. $id
);
}
$entityManager->remove($task);
$entityManager->flush();
return $this->redirectToRoute('list_task');
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// ROUTER!
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#[Route('/router/list', name: 'app_list_router')]
public function list_router(EntityManagerInterface $entityManager): Response
{
if (!$this->getUser()) {
return $this->redirectToRoute('index');
}
$routers = $entityManager->getRepository(Routeur::class)->findAll();
return $this->render('java_appli/router/list_router.html.twig', [
'routers' => $routers,
'title' => 'List of all routers!'
]);
}
#[Route('/router/create', name: 'app_create_router')]
public function create_router(Request $request, EntityManagerInterface $entityManager): Response
{
if (!$this->getUser()) {
return $this->redirectToRoute('index');
}
$router = new Routeur();
$form = $this->createForm(RouteurType::class, $router);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager->persist($router);
$entityManager->flush();
return $this->render('java_appli/router/new_router.html.twig', [
'mode' => 'created'
]);
}
return $this->render('java_appli/router/create_router.html.twig', [
'registrationForm' => $form->createView(),
'mode' => 'Create'
]);
}
#[Route('/router/{signature}', name: 'app_one_router')]
public function one_router(string $signature, EntityManagerInterface $entityManager): Response
{
if (!$this->getUser()) {
return $this->redirectToRoute('index');
}
$router = $entityManager->getRepository(Routeur::class)->findBy(["signature" => $signature]);
if (!$router) {
throw $this->createNotFoundException(
'No router found with signature: '.$signature
);
}
return $this->render('java_appli/router/list_router.html.twig', [
'routers' => $router,
'title' => 'One of the routers!'
]);
}
#[Route('/router/{signature}/edit', name: 'app_edit_router')]
public function edit_router(string $signature, Request $request, EntityManagerInterface $entityManager): Response
{
if (!$this->getUser()) {
return $this->redirectToRoute('index');
}
$router = $entityManager->getRepository(Routeur::class)->findOneBy(["signature" => $signature]);
if (!$router) {
throw $this->createNotFoundException(
'No router found with signature: '.$signature
);
}
$form = $this->createForm(RouteurType::class, $router);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager->persist($router);
$entityManager->flush();
// do anything else you need here, like send an email
return $this->render('java_appli/router/new_router.html.twig', [
'mode' => 'edit'
]);
}
return $this->render('java_appli/router/create_router.html.twig', [
'registrationForm' => $form->createView(),
'mode' => 'Modify'
]);
}
#[Route('/router/{signature}/delete', name: 'app_delete_router')]
public function delete_router(string $signature, EntityManagerInterface $entityManager): Response
{
if (!$this->getUser()) {
return $this->redirectToRoute('index');
}
$router = $entityManager->getRepository(Routeur::class)->findOneBy(["signature" => $signature]);
if (!$router) {
throw $this->createNotFoundException(
'No router found with signature: '.$signature
);
}
$entityManager->remove($router);
$entityManager->flush();
return $this->redirectToRoute('list_router');
}
//////////////////////////////////////////////////////////////
/// Request API for VRM
//////////////////////////////////////////////////////////////
#[Route('/api/vrm/{signature}', name: 'app_VRM_get_info')]
public function info_from_VRM(string $signature, RouterRequest $router_request, EntityManagerInterface $entityManager): Response
{
$gps = $router_request->actualisationVRM($signature, "/widgets/GPS");
if($gps["success"]){
$lat = $gps["records"]["data"]["attributes"][4]["value"];
$lng = $gps["records"]["data"]["attributes"][5]["value"];
$gps = $gps["records"]["data"]["attributes"];
}else{
$lat = [];
$lng = [];
$gps = [];
}
dd($gps); ////////////////////////////////////////////
/// ------------------------------------ add more things from page VRM
///
///
///
///
$diagnostics = $router_request->actualisationVRM($signature, "/widgets/Graph?attributeIds%5B%5D=51&start=1659020015");
$overview = $router_request->actualisationVRM($signature, "/system-overview"); //get info from VRM with $signature
if($overview["success"]){
$devices = $overview["records"]["devices"];
}else{
$devices = [];
}
return $this->render('java_appli/router/router.html.twig', [
'lat' => $lat,
'lng' => $lng,
'gps' => $gps ,
'devices' => $devices,
]);
}
// #[Route('/api/router/{signature}/update', name: 'app_java_update_router', methods: ["POST"])]
// public function update_router(string $signature, Request $request, EntityManagerInterface $entityManager): Response
// {
// $routerAuth = $entityManager->getRepository(Routeur::class)->findOneBy(["signature" =>$signature]);
// $headers = $request->headers;
// $authToken = $headers->get("AuthToken");
// if($routerAuth->getAuthToken() == $authToken){
// //update info about (add new line with info to table with date etc)
// return new Response("Auth ok, with this u can edit info in DB router!", 200, [
// "Content-Type" => "application/json"
// ]);
// }
// $response = $request->getContent();
// return new Response("Authorization denied.", 401, [
// "Content-Type" => "application/json"
// ]);
// }
}