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