src/Controller/HomeController.php line 73

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Comp;
  4. use App\Entity\CompFramework;
  5. use App\Entity\CompScale;
  6. use App\Entity\CompScaleValueDescription;
  7. use App\Entity\Position;
  8. use App\Entity\Skill;
  9. use App\Entity\SkillScale;
  10. use App\Enum\CsrfToken;
  11. use App\Enum\FileArea;
  12. use App\Exception\ValidationException;
  13. use App\Model\Assessment\CompetencyFrameworkModel;
  14. use App\Model\Assessment\CompetencyModel;
  15. use App\Model\Assessment\CompetencyScaleModel;
  16. use App\Model\Assessment\PositionModel;
  17. use App\Model\Assessment\SkillModel;
  18. use App\Model\Assessment\SkillScaleModel;
  19. use App\Model\Common\ResponseModel;
  20. use App\Response\ApiResponse;
  21. use App\Service\Config\ConfigLoader;
  22. use App\Service\File\Exceptions\FileNotFoundException;
  23. use App\Service\File\FileService;
  24. use App\Validator\AppValidator;
  25. use Doctrine\ORM\EntityManagerInterface;
  26. use Psr\Log\LoggerInterface;
  27. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  28. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  29. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  30. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  31. use Symfony\Component\HttpFoundation\JsonResponse;
  32. use Symfony\Component\HttpFoundation\Request;
  33. use Symfony\Component\HttpFoundation\Response;
  34. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  35. use Symfony\Component\Routing\Annotation\Route;
  36. use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
  37. use Throwable;
  38. use App\Entity\File;
  39. /**
  40.  * Class HomeController
  41.  * @package App\Controller
  42.  */
  43. class HomeController extends AbstractController
  44. {
  45.     private $em;
  46.     private $logger;
  47.     private $configLoader;
  48.     private $fileService;
  49.     private $csrfTokenManager;
  50.     /**
  51.      * HomeController constructor.
  52.      *
  53.      * @param EntityManagerInterface $em
  54.      * @param LoggerInterface        $logger
  55.      */
  56.     public function __construct(EntityManagerInterface $emLoggerInterface $loggerConfigLoader $configLoaderFileService $fileServiceCsrfTokenManagerInterface $csrfTokenManager)
  57.     {
  58.         $this->em $em;
  59.         $this->logger $logger;
  60.         $this->configLoader $configLoader;
  61.         $this->fileService $fileService;
  62.         $this->csrfTokenManager $csrfTokenManager;
  63.     }
  64.     /**
  65.      * @Route("/", name="app_homepage")
  66.      */
  67.     public function index(): Response
  68.     {
  69.         return $this->render('base.html.twig');
  70.     }
  71.     /**
  72.      * @Route("/api/init", name="init", methods={"GET"})
  73.      * @IsGranted("IS_AUTHENTICATED_FULLY")
  74.      * @return Response
  75.      */
  76.     public function init(): Response
  77.     {
  78.         $response = new ResponseModel();
  79.         $response->csrfToken $this->csrfTokenManager->getToken(CsrfToken::API_TOKEN)->getValue();
  80.         $response->config $this->configLoader->getPublicConfigValues();
  81.         $positions $this->em->getRepository(Position::class)->findAll();
  82.         $response->roles = [];
  83.         foreach ($positions as $position) {
  84.             $response->roles[$position->getId()] = PositionModel::createFromPosition($position);
  85.         }
  86.         $competencies $this->em->getRepository(Comp::class)->findAll();
  87.         $response->competencies = [];
  88.         $compStructure = [];
  89.         foreach ($competencies as $competency) {
  90.             $frameworkId $competency->getFramework()->getId();
  91.             $response->competencies[$competency->getId()] = CompetencyModel::createFromCompetency($competency);
  92.             if (empty($compStructure[$frameworkId])) {
  93.                 $compStructure[$frameworkId] = [];
  94.             }
  95.             if ($parentComp $competency->getParent()) {
  96.                 $parentCompId $parentComp->getId();
  97.                 if (empty($compStructure[$frameworkId][$parentCompId])) {
  98.                     $compStructure[$frameworkId][$parentCompId] = [];
  99.                 }
  100.                 $compStructure[$frameworkId][$parentCompId][] = $competency->getId();
  101.             } else {
  102.                 $compStructure[$frameworkId][0][] = $competency->getId();
  103.             }
  104.         }
  105.         $response->competencyStructure $compStructure;
  106.         $competencyFrameworks $this->em->getRepository(CompFramework::class)->findAll();
  107.         $response->competencyFrameworks = [];
  108.         foreach ($competencyFrameworks as $competencyFramework) {
  109.             $response->competencyFrameworks[$competencyFramework->getId()] = CompetencyFrameworkModel::createFromFramework($competencyFramework);
  110.         }
  111.         $scales $this->em->getRepository(CompScale::class)->findAll();
  112.         $response->scales = [];
  113.         foreach ($scales as $scale) {
  114.             $response->scales[$scale->getId()] = CompetencyScaleModel::createFromCompetencyScale($scaletrue);
  115.         }
  116.         $csvDescriptions $this->em->getRepository(CompScaleValueDescription::class)->findBy(['competency' => $competencies,]);
  117.         $response->customScaleValueDescriptions = [];
  118.         foreach ($csvDescriptions as $customDescription) {
  119.             $compId $customDescription->getCompetency()->getId();
  120.             $scaleValueId $customDescription->getScaleValue()->getId();
  121.             if (!array_key_exists($compId$response->customScaleValueDescriptions)) {
  122.                 $response->customScaleValueDescriptions[$compId] = [];
  123.             }
  124.             $response->customScaleValueDescriptions[$compId][$scaleValueId] = $customDescription->getDescription();
  125.         }
  126.         $skills $this->em->getRepository(Skill::class)->findAll();
  127.         $response->skills = [];
  128.         foreach ($skills as $skill) {
  129.             $response->skills[$skill->getId()] = SkillModel::createFromSkill($skill);
  130.         }
  131.         $skillScaleRecords $this->em->getRepository(SkillScale::class)->findBy(
  132.             ['sortorder' => "0"],
  133.             ['numericscore' => "DESC"]
  134.         );
  135.         $response->skillScales = [];
  136.         foreach ($skillScaleRecords as $skillScaleRecord) {
  137.             $response->skillScales[$skillScaleRecord->getId()] = SkillScaleModel::createFromSkillScale($skillScaleRecord);
  138.         }
  139.         $files =  $this->fileService->getFilesByComponent(FileArea::ADDITIONAL_FILES);
  140.         $links = [];
  141.         foreach ($files as $file) {
  142.             $links[] = [
  143.                 'name' => $file->getName(),
  144.                 'id' => $file->getId()
  145.             ];
  146.         }
  147.         $response->additionalFiles $links;
  148.         return new JsonResponse($response);
  149.     }
  150.     /**
  151.      * @Route("/photo/{filename}", name="user_photo")
  152.      * @IsGranted("IS_AUTHENTICATED_FULLY")
  153.      */
  154.     public function photo($filename): Response
  155.     {
  156.         $publicResourcesFolderPath $this->getParameter('profile_photo_dir');
  157.         return new BinaryFileResponse("{$publicResourcesFolderPath}/{$filename}");
  158.     }
  159.     /**
  160.      * @Route("/file/{id}", name="get_file", methods={"GET"})
  161.      * @IsGranted("IS_AUTHENTICATED_FULLY")
  162.      * @ParamConverter("id", class="App:File")
  163.      * @param File $file
  164.      *
  165.      * @return Response
  166.      */
  167.     public function downloadFile(File $file): Response
  168.     {
  169.         try {
  170.             if ($file->getComponent() !== FileArea::ADDITIONAL_FILES &&
  171.                 $file->getComponent() !== FileArea::LMS_INTEGRATION) {
  172.                 /** @todo: Check permission **/
  173.                 // Only allow additional_files and course images
  174.                 // to be returned until permissions handled
  175.                 throw new NotFoundHttpException();
  176.             }
  177.             $filePath $this->fileService->getFileFullPath($file);
  178.             return new BinaryFileResponse($filePath);
  179.         } catch (FileNotFoundException $exception) {
  180.             throw new NotFoundHttpException();
  181.         } catch (Throwable $e) {
  182.             $this->logger->error("Error downloading file", [
  183.                 "e" => $e,
  184.                 "file" => $file
  185.             ]);
  186.             return ApiResponse::error();
  187.         }
  188.     }
  189.     /**
  190.      * @Route("/error/log", name="log_error", methods={"POST"})
  191.      * @IsGranted("IS_AUTHENTICATED_FULLY")
  192.      */
  193.     public function logError(Request $requestAppValidator $validator): Response
  194.     {
  195.         try {
  196.             $data $validator->validateLogError($request);
  197.             $this->logger->error("React Application Error: {$data->info}");
  198.             return ApiResponse::success(true);
  199.         } catch (ValidationException $e) {
  200.             return ApiResponse::error($e->getMessage());
  201.         } catch (Throwable $e) {
  202.             $this->logger->error("Error logging React Application error", [
  203.                 "e" => $e,
  204.             ]);
  205.             return ApiResponse::error();
  206.         }
  207.     }
  208.     /**
  209.      * @Route("/get_site_logo", name="get_site_logo")
  210.      * @IsGranted("IS_AUTHENTICATED_ANONYMOUSLY")
  211.      * @param Request $request
  212.      *
  213.      */
  214.     public function getSiteLogo(Request $requestFileService $fileService)
  215.     {
  216.         $logoFilePath $fileService->getSiteLogoFilePath();
  217.         if ($logoFilePath) {
  218.             return new BinaryFileResponse($logoFilePath);
  219.         }
  220.         return ApiResponse::error();
  221.     }
  222.     /**
  223.      * @return Response
  224.      * @Route("/admin/info",  name="info", methods={"GET"})
  225.      * @IsGranted("IS_AUTHENTICATED_FULLY")
  226.      */
  227.     public function info(): Response
  228.     {
  229.         echo phpinfo();
  230.         return new Response();
  231.     }
  232. }