1 |
1a669408
|
Petr Lukašík
|
<?php
|
2 |
|
|
|
3 |
|
|
namespace App\AdminModule\Presenters;
|
4 |
|
|
|
5 |
|
|
use App\AdminModule\Components\IExportFormFactory;
|
6 |
|
|
use App\AdminModule\Components\IImportFormFactory;
|
7 |
ce045025
|
Petr Lukašík
|
use App\Enum\EFlashMessage;
|
8 |
|
|
use App\Model\Facade\TransportFacade;
|
9 |
2d082bc2
|
Petr Lukašík
|
use App\Model\Repository\ObjectTypeRepository;
|
10 |
|
|
use App\Model\Repository\SurfaceTypeRepository;
|
11 |
2f604f1a
|
Petr Lukašík
|
use App\Model\TransportModel;
|
12 |
2d082bc2
|
Petr Lukašík
|
use App\Utils\TextParser;
|
13 |
1a669408
|
Petr Lukašík
|
|
14 |
|
|
class TransportPresenter extends BaseUserPresenter
|
15 |
|
|
{
|
16 |
ce045025
|
Petr Lukašík
|
/** @var IImportFormFactory */
|
17 |
1a669408
|
Petr Lukašík
|
private $importFormFactory;
|
18 |
|
|
|
19 |
ce045025
|
Petr Lukašík
|
/** @var IExportFormFactory */
|
20 |
1a669408
|
Petr Lukašík
|
private $exportFormFactory;
|
21 |
|
|
|
22 |
2d082bc2
|
Petr Lukašík
|
/** @var SurfaceTypeRepository */
|
23 |
|
|
private $surfaceTypeRepository;
|
24 |
|
|
|
25 |
|
|
/** @var ObjectTypeRepository */
|
26 |
|
|
private $objectTypeRepository;
|
27 |
|
|
|
28 |
ce045025
|
Petr Lukašík
|
/** @var TransportFacade */
|
29 |
|
|
private $transportFacade;
|
30 |
|
|
|
31 |
2f604f1a
|
Petr Lukašík
|
/** @var TransportModel */
|
32 |
|
|
private $transportModel;
|
33 |
|
|
|
34 |
175d726f
|
Filip Jani
|
public function __construct(IImportFormFactory $importFormFactory,
|
35 |
|
|
IExportFormFactory $exportFormFactory,
|
36 |
|
|
SurfaceTypeRepository $surfaceTypeRepository,
|
37 |
|
|
ObjectTypeRepository $objectTypeRepository,
|
38 |
|
|
TransportFacade $transportFacade,
|
39 |
|
|
TransportModel $transportModel
|
40 |
1a669408
|
Petr Lukašík
|
)
|
41 |
|
|
{
|
42 |
|
|
parent::__construct();
|
43 |
|
|
$this->importFormFactory = $importFormFactory;
|
44 |
|
|
$this->exportFormFactory = $exportFormFactory;
|
45 |
2d082bc2
|
Petr Lukašík
|
$this->surfaceTypeRepository = $surfaceTypeRepository;
|
46 |
|
|
$this->objectTypeRepository = $objectTypeRepository;
|
47 |
ce045025
|
Petr Lukašík
|
$this->transportFacade = $transportFacade;
|
48 |
2f604f1a
|
Petr Lukašík
|
$this->transportModel = $transportModel;
|
49 |
2d082bc2
|
Petr Lukašík
|
}
|
50 |
|
|
|
51 |
ce045025
|
Petr Lukašík
|
/**
|
52 |
|
|
* View pro zobrazení importovaných dat před uložením do databáze
|
53 |
|
|
* @param TextParser $parser
|
54 |
|
|
*/
|
55 |
|
|
public function renderPreviewImport(TextParser $parser)
|
56 |
|
|
{
|
57 |
2d082bc2
|
Petr Lukašík
|
$objectTypes = $this->objectTypeRepository->fetchObjectTypes();
|
58 |
|
|
$surfaceTypes = $this->surfaceTypeRepository->fetchSurfaceTypes();
|
59 |
|
|
$parser->setTypes($objectTypes, $surfaceTypes);
|
60 |
|
|
|
61 |
2f604f1a
|
Petr Lukašík
|
$this->transportModel->setParsedText($parser->parseText());
|
62 |
ce045025
|
Petr Lukašík
|
|
63 |
|
|
$this->template->fileName = $parser->getTextFile()->getName();
|
64 |
2f604f1a
|
Petr Lukašík
|
$this->template->parsedFile = $this->transportModel->getParsedText();
|
65 |
2d082bc2
|
Petr Lukašík
|
$this->template->transliterationNum = $this->countTransliterations();
|
66 |
|
|
$this->template->surfaces = $surfaceTypes;
|
67 |
|
|
}
|
68 |
|
|
|
69 |
ce045025
|
Petr Lukašík
|
/** Uloží data z textového souboru do databáze
|
70 |
|
|
* @throws \Nette\Application\AbortException
|
71 |
|
|
*/
|
72 |
|
|
public function actionConfirmPreview()
|
73 |
|
|
{
|
74 |
2f604f1a
|
Petr Lukašík
|
$result = $this->transportFacade->saveImportedData($this->transportModel->getParsedText());
|
75 |
ce045025
|
Petr Lukašík
|
|
76 |
175d726f
|
Filip Jani
|
if ($result)
|
77 |
|
|
{
|
78 |
ce045025
|
Petr Lukašík
|
$this->presenter->flashMessage('All data were imported successfully.', EFlashMessage::SUCCESS);
|
79 |
175d726f
|
Filip Jani
|
} else
|
80 |
|
|
{
|
81 |
ce045025
|
Petr Lukašík
|
$this->presenter->flashMessage('There was an error when importing data.', EFlashMessage::ERROR);
|
82 |
|
|
}
|
83 |
|
|
|
84 |
|
|
$this->redirect("Transport:");
|
85 |
|
|
}
|
86 |
|
|
|
87 |
|
|
/** Počítá počet importovaných transliterací */
|
88 |
|
|
public function countTransliterations()
|
89 |
|
|
{
|
90 |
|
|
$num = 0;
|
91 |
175d726f
|
Filip Jani
|
foreach ($this->template->parsedFile as $book)
|
92 |
|
|
{
|
93 |
2d082bc2
|
Petr Lukašík
|
$num += count($book);
|
94 |
|
|
}
|
95 |
|
|
return $num;
|
96 |
1a669408
|
Petr Lukašík
|
}
|
97 |
|
|
|
98 |
|
|
public function createComponentImportForm()
|
99 |
|
|
{
|
100 |
|
|
return $this->importFormFactory->create();
|
101 |
|
|
}
|
102 |
|
|
|
103 |
|
|
public function createComponentExportForm()
|
104 |
|
|
{
|
105 |
|
|
return $this->exportFormFactory->create();
|
106 |
|
|
}
|
107 |
|
|
}
|