source/View/Controller/FileManagementController.php line 152

Open in your IDE?
  1. <?php
  2. namespace App\View\Controller;
  3. use App\Crud\Crudable;
  4. use App\Domain\Model\Codebook\DatasetVariables;
  5. use App\Domain\Model\Filemanagement\AdditionalMaterial;
  6. use App\Domain\Model\Filemanagement\Dataset;
  7. use App\Io\Formats\Csv\CsvImportable;
  8. use App\Io\Formats\Sav\SavImportable;
  9. use App\Questionnaire\Questionnairable;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Psr\Log\LoggerInterface;
  12. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  13. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  14. use Symfony\Component\HttpFoundation\JsonResponse;
  15. use Symfony\Component\HttpFoundation\RedirectResponse;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\Routing\Annotation\Route;
  19. /**
  20.  * @Route("/filemanagement", name="File-")
  21.  * @IsGranted("ROLE_USER")
  22.  *
  23.  * Class FileManagementController
  24.  * @package App\View\Controller
  25.  */
  26. class FileManagementController extends AbstractController
  27. {
  28.     private Crudable $crud;
  29.     private Questionnairable $questionnaire;
  30.     private CsvImportable $csvImportable;
  31.     private SavImportable $savImportable;
  32.     private EntityManagerInterface $em;
  33.     private LoggerInterface $logger;
  34.     /**
  35.      * @param Crudable $crud
  36.      * @param Questionnairable $questionnaire
  37.      * @param CsvImportable $csvImportable
  38.      * @param SavImportable $savImportable
  39.      * @param EntityManagerInterface $em
  40.      * @param LoggerInterface $logger
  41.      */
  42.     public function __construct(
  43.         Crudable $crud,
  44.         Questionnairable $questionnaire,
  45.         CsvImportable $csvImportable,
  46.         SavImportable $savImportable,
  47.         EntityManagerInterface $em,
  48.         LoggerInterface $logger
  49.     ) {
  50.         $this->crud $crud;
  51.         $this->questionnaire $questionnaire;
  52.         $this->csvImportable $csvImportable;
  53.         $this->savImportable $savImportable;
  54.         $this->em $em;
  55.         $this->logger $logger;
  56.     }
  57.     /**
  58.      * @Route("/preview/sav/{fileId}", name="preview-sav")
  59.      *
  60.      * @param string $fileId
  61.      * @return JsonResponse
  62.      */
  63.     public function previewSavAction(string $fileId): JsonResponse
  64.     {
  65.         $this->logger->debug("Enter FileManagementController::previewSavAction with [FileId: $fileId]");
  66.         $dataset $this->em->find(Dataset::class, $fileId);
  67.         $data null;
  68.         if ($dataset) {
  69.             $data $this->savImportable->savToArray($dataset);
  70.         }
  71.         return new JsonResponse($data ?? [], null != $data Response::HTTP_OK Response::HTTP_UNPROCESSABLE_ENTITY);
  72.     }
  73.     /**
  74.      * @Route("/submit/sav/{fileId}", name="submit-sav")
  75.      *
  76.      * @param string $fileId
  77.      * @param Request $request
  78.      * @return JsonResponse
  79.      */
  80.     public function submitSavAction(string $fileIdRequest $request): JsonResponse
  81.     {
  82.         $this->logger->debug("Enter FileManagementController::previewSavAction with [FileId: $fileId]");
  83.         $dataset $this->em->find(Dataset::class, $fileId);
  84.         $data null;
  85.         if ($dataset) {
  86.             $data $this->savImportable->savToArray($dataset);
  87.         }
  88.         if (isset($dataset) && isset($data) && !empty($data)) {
  89.             if ($data && is_iterable($data) && key_exists('codebook'$data)) {
  90.                 foreach ($data['codebook'] as $var) {
  91.                     $matrixHeader[] = $var['name'];
  92.                     $this->em->persist(
  93.                         DatasetVariables::createNew(
  94.                             $dataset,
  95.                             $var['id'],
  96.                             $var['name'],
  97.                             $var['label'],
  98.                             $var['itemText'],
  99.                             $var['values'],
  100.                             $var['missings'],
  101.                         )
  102.                     );
  103.                 }
  104.                 $this->em->flush();
  105.                 if (key_exists('records'$data)) {
  106.                     $this->crud->saveDatasetMatrix($data['records'], $dataset->getId());
  107.                 }
  108.             }
  109.         }
  110.         return new JsonResponse($data ?? [], null != $data Response::HTTP_OK Response::HTTP_UNPROCESSABLE_ENTITY);
  111.     }
  112.     /**
  113.      * @Route("/preview/csv/{fileId}", name="preview-csv")
  114.      *
  115.      * @param string $fileId
  116.      * @param Request $request
  117.      * @return JsonResponse
  118.      */
  119.     public function previewCsvAction(string $fileIdRequest $request): JsonResponse
  120.     {
  121.         $this->logger->debug("Enter FileManagementController::previewCSVAction with [FileId: $fileId]");
  122.         $delimiter $request->get('dataset-import-delimiter') ?? ",";
  123.         $escape $request->get('dataset-import-escape') ?? "double";
  124.         $headerRows filter_var($request->get('dataset-import-header-rows'), FILTER_VALIDATE_INT) ?? 0;
  125.         $file $this->em->find(Dataset::class, $fileId);
  126.         $data null;
  127.         if ($file) {
  128.             $data $this->csvImportable->csvToArray($file->getStorageName(), $delimiter$escape$headerRows5);
  129.         }
  130.         return new JsonResponse($data$data Response::HTTP_OK Response::HTTP_UNPROCESSABLE_ENTITY);
  131.     }
  132.     /**
  133.      * @Route("/submit/csv/{fileId}", name="submit-csv")
  134.      *
  135.      * @param string $fileId
  136.      * @param Request $request
  137.      * @return JsonResponse
  138.      */
  139.     public function submitCSVAction(string $fileIdRequest $request): JsonResponse
  140.     {
  141.         $this->logger->debug("Enter FileManagementController::submitCSVAction with [FileId: $fileId]");
  142.         $delimiter $request->get('dataset-import-delimiter') ?? ",";
  143.         $escape $request->get('dataset-import-escape') ?? "double";
  144.         $remove $request->get('dataset-import-remove') ?? null;
  145.         $headerRows filter_var($request->get('dataset-import-header-rows'), FILTER_VALIDATE_INT) ?? 0;
  146.         $dataset $this->em->find(Dataset::class, $fileId);
  147.         $data null;
  148.         $error null;
  149.         if ($dataset) {
  150.             if ($remove) {
  151.                 $error $this->crud->deleteDataset($dataset) ? false "error.import.csv.codebook.delete";
  152.             } else {
  153.                 $data $this->csvImportable->csvToArray($dataset->getStorageName(), $delimiter$escape$headerRows);
  154.                 if ($data && key_exists('header'$data) && is_iterable($data['header']) && sizeof($data['header']) > 0) {
  155.                     $varId 1;
  156.                     foreach ($data['header'] as $var) {
  157.                         $this->em->persist(DatasetVariables::createNew($dataset$varId++, $var));
  158.                     }
  159.                     $this->em->flush();
  160.                 } elseif ($data && key_exists('records'$data) && is_iterable($data['records']) && sizeof($data['records']) > 0) {
  161.                     $varId 1;
  162.                     foreach ($data['records'][0] as $ignored) {
  163.                         $this->em->persist(DatasetVariables::createNew($dataset$varId"var_$varId"));
  164.                         $varId++;
  165.                     }
  166.                     $this->em->flush();
  167.                 } else {
  168.                     $error "error.import.csv.codebook.empty";
  169.                 }
  170.                 if (null == $error && $data && key_exists('records'$data) && is_iterable($data['records']) && sizeof($data['records']) > 0) {
  171.                     $this->crud->saveDatasetMatrix($data['records'], $dataset->getId());
  172.                 } else {
  173.                     $error "error.import.csv.matrix.empty";
  174.                 }
  175.             }
  176.         } else {
  177.             $error "error.import.csv.dataset.empty";
  178.         }
  179.         if (null != $error && !$remove) {
  180.             $this->crud->deleteDataset($dataset);
  181.         }
  182.         return new JsonResponse(
  183.             null != $error $error $data,
  184.             null != $error Response::HTTP_UNPROCESSABLE_ENTITY Response::HTTP_OK
  185.         );
  186.     }
  187.     /**
  188.      * @Route("/{uuid}/delete/dataset", name="delete_dataset")
  189.      *
  190.      * @param string $uuid
  191.      * @return RedirectResponse|Response
  192.      */
  193.     public function deleteDatasetAction(string $uuid)
  194.     {
  195.         $this->logger->debug("Enter FileManagementController::deleteMaterialAction with [UUID: $uuid]");
  196.         $dataset $this->em->find(Dataset::class, $uuid);
  197.         $experimentId $dataset->getExperiment()->getId();
  198.         $this->crud->deleteDataset($dataset);
  199.         return $this->redirectToRoute('Study-datasets', ['uuid' => $experimentId]);
  200.     }
  201.     /**
  202.      * @Route("/{uuid}/delete/material", name="delete_material")
  203.      *
  204.      * @param string $uuid
  205.      * @param Request $request
  206.      * @return RedirectResponse
  207.      */
  208.     public function deleteMaterialAction(string $uuidRequest $request): RedirectResponse
  209.     {
  210.         $this->logger->debug("Enter FileManagementController::deleteMaterialAction with [UUID: $uuid]");
  211.         $material $this->em->find(AdditionalMaterial::class, $uuid);
  212.         $experimentId $material->getExperiment()->getId();
  213.         $this->crud->deleteMaterial($material);
  214.         return $this->redirectToRoute('Study-materials', ['uuid' => $experimentId]);
  215.     }
  216.     /**
  217.      * @Route("/{uuid}/update/description", name="update_description", methods={"POST"})
  218.      *
  219.      * @param string $uuid
  220.      * @param Request $request
  221.      * @return JsonResponse
  222.      */
  223.     public function updateDescriptionAction(string $uuidRequest $request): JsonResponse
  224.     {
  225.         $this->logger->debug("Enter FileManagementController::updateDescriptionAction with [UUID: $uuid]");
  226.         $entity $this->em->find(AdditionalMaterial::class, $uuid);
  227.         $success false;
  228.         if ($entity == null) {
  229.             $entity $this->em->find(Dataset::class, $uuid);
  230.         }
  231.         if ($entity) {
  232.             $description $request->getContent();
  233.             $entity->setDescription($description);
  234.             $this->em->persist($entity);
  235.             $this->em->flush();
  236.             $success true;
  237.         }
  238.         return new JsonResponse(['success' => $success], $success Response::HTTP_OK Response::HTTP_UNPROCESSABLE_ENTITY);
  239.     }
  240.     /**
  241.      * @Route("/{uuid}/details", name="details")
  242.      *
  243.      * @param string $uuid
  244.      * @param Request $request
  245.      * @return Response
  246.      */
  247.     public function materialDetailsAction(string $uuidRequest $request): Response
  248.     {
  249.         $this->logger->debug("Enter FileManagementController::materialDetailsAction with [UUID: $uuid]");
  250.         $entityAtChange $this->em->find(AdditionalMaterial::class, $uuid);
  251.         $experiment $entityAtChange->getExperiment();
  252.         $form $this->questionnaire->askAndHandle(
  253.             $entityAtChange,
  254.             'save',
  255.             $request
  256.         );
  257.         if ($this->questionnaire->isSubmittedAndValid($form)) {
  258.             $this->em->persist($entityAtChange);
  259.             $this->em->flush();
  260.         }
  261.         return $this->render(
  262.             'Pages/FileManagement/materialDetails.html.twig',
  263.             [
  264.                 'form' => $form->createView(),
  265.                 'file' => $entityAtChange,
  266.                 'experiment' => $experiment,
  267.             ]
  268.         );
  269.     }
  270. }