Revize 2d082bc2
Přidáno uživatelem Petr Lukašík před téměř 6 roky(ů)
app/AdminModule/component/Transport/ExportForm.latte | ||
---|---|---|
1 |
<div class="row"> |
|
2 |
<div class="col-4"> |
|
3 |
{control form} |
|
4 |
</div> |
|
5 |
</div> |
|
1 |
{control form} |
app/AdminModule/component/Transport/ImportForm.latte | ||
---|---|---|
1 |
<div class="row"> |
|
2 |
<div class="col-4"> |
|
3 |
{control form} |
|
4 |
</div> |
|
5 |
</div> |
|
1 |
{control form} |
app/AdminModule/component/Transport/ImportForm.php | ||
---|---|---|
3 | 3 |
namespace App\AdminModule\Components; |
4 | 4 |
|
5 | 5 |
use App\Utils\Form; |
6 |
use App\Utils\TextParser; |
|
6 | 7 |
use Nette\Application\UI\Control; |
7 |
use Nette\Utils\ArrayHash;
|
|
8 |
use Tracy\Debugger;
|
|
8 | 9 |
|
9 | 10 |
class ImportForm extends Control |
10 | 11 |
{ |
12 |
/** @var TextParser */ |
|
13 |
private $parser; |
|
14 |
|
|
11 | 15 |
public function __construct() |
12 | 16 |
{ |
13 | 17 |
parent::__construct(); |
14 | 18 |
} |
15 | 19 |
|
16 |
|
|
17 | 20 |
public function render() |
18 | 21 |
{ |
19 | 22 |
$this->template->setFile(__DIR__ . '/ImportForm.latte'); |
... | ... | |
24 | 27 |
{ |
25 | 28 |
$form = new Form(); |
26 | 29 |
|
27 |
$form->addUpload('import', 'File')->addRule(Form::REQUIRED, 'Field %label is required.'); |
|
30 |
$form->addUpload('import', 'File') |
|
31 |
->addRule(Form::REQUIRED, 'Field %label is required.') |
|
32 |
->addRule(Form::MIME_TYPE, "Uploaded file must be text file", "text/plain"); // mozne nekdy pridat text/csv |
|
28 | 33 |
|
29 | 34 |
$form->addSubmit('submit', 'Import file'); |
35 |
|
|
36 |
$form->onValidate[] = [$this, 'formValidate']; |
|
30 | 37 |
$form->onSuccess[] = [$this, 'formSuccess']; |
31 | 38 |
|
32 | 39 |
return $form; |
33 | 40 |
} |
34 | 41 |
|
42 |
public function formValidate(Form $form){ |
|
43 |
$this->parser = new TextParser($form->getValues()['import']); |
|
44 |
if (!$this->parser->isFileParsable()){ |
|
45 |
$form->addError('Wrong format of imported file'); |
|
46 |
} |
|
47 |
} |
|
48 |
|
|
35 | 49 |
public function formSuccess(Form $form) |
36 | 50 |
{ |
37 |
/** @var ArrayHash $values */ |
|
38 |
$values = $form->getValues(); |
|
39 |
$this->transliterationSearchModel->setSearchTerms($values); |
|
40 |
$this->presenter->redirect('Transliteration:searchResult'); |
|
51 |
$this->presenter->forward('Transport:previewImport', $this->parser); |
|
41 | 52 |
} |
42 | 53 |
} |
43 | 54 |
|
app/AdminModule/presenters/TransportPresenter.php | ||
---|---|---|
4 | 4 |
|
5 | 5 |
use App\AdminModule\Components\IExportFormFactory; |
6 | 6 |
use App\AdminModule\Components\IImportFormFactory; |
7 |
use App\Model\Repository\ObjectTypeRepository; |
|
8 |
use App\Model\Repository\SurfaceTypeRepository; |
|
9 |
use App\Utils\TextParser; |
|
7 | 10 |
|
8 | 11 |
class TransportPresenter extends BaseUserPresenter |
9 | 12 |
{ |
... | ... | |
13 | 16 |
/** @var IExportFormFactory */ |
14 | 17 |
private $exportFormFactory; |
15 | 18 |
|
19 |
/** @var SurfaceTypeRepository */ |
|
20 |
private $surfaceTypeRepository; |
|
21 |
|
|
22 |
/** @var ObjectTypeRepository */ |
|
23 |
private $objectTypeRepository; |
|
24 |
|
|
16 | 25 |
public function __construct( |
17 | 26 |
IImportFormFactory $importFormFactory, |
18 |
IExportFormFactory $exportFormFactory |
|
27 |
IExportFormFactory $exportFormFactory, |
|
28 |
SurfaceTypeRepository $surfaceTypeRepository, |
|
29 |
ObjectTypeRepository $objectTypeRepository |
|
19 | 30 |
) |
20 | 31 |
{ |
21 | 32 |
parent::__construct(); |
22 | 33 |
$this->importFormFactory = $importFormFactory; |
23 | 34 |
$this->exportFormFactory = $exportFormFactory; |
35 |
$this->surfaceTypeRepository = $surfaceTypeRepository; |
|
36 |
$this->objectTypeRepository = $objectTypeRepository; |
|
37 |
} |
|
38 |
|
|
39 |
public function actionPreviewImport(TextParser $parser){ |
|
40 |
$objectTypes = $this->objectTypeRepository->fetchObjectTypes(); |
|
41 |
$surfaceTypes = $this->surfaceTypeRepository->fetchSurfaceTypes(); |
|
42 |
$parser->setTypes($objectTypes, $surfaceTypes); |
|
43 |
|
|
44 |
$this->template->fileName = $parser->textFile->getName(); |
|
45 |
$this->template->parsedFile = $parser->parseText(); |
|
46 |
$this->template->transliterationNum = $this->countTransliterations(); |
|
47 |
$this->template->surfaces = $surfaceTypes; |
|
48 |
} |
|
49 |
|
|
50 |
public function countTransliterations(){ |
|
51 |
$num=0; |
|
52 |
foreach ($this->template->parsedFile as $book){ |
|
53 |
$num += count($book); |
|
54 |
} |
|
55 |
return $num; |
|
24 | 56 |
} |
25 | 57 |
|
26 | 58 |
public function createComponentImportForm() |
app/AdminModule/templates/Transport/default.latte | ||
---|---|---|
1 | 1 |
{block content} |
2 |
<div class="row col-12"> |
|
3 |
<div class="display-5">Import text</div> |
|
4 |
</div> |
|
5 |
|
|
6 |
{control importForm} |
|
2 |
<div class="row"> |
|
3 |
<div class="col-3"> |
|
4 |
<div class="display-5">Import or export text</div> |
|
7 | 5 |
|
8 |
<div class="row col-12" style="margin-top: 5%"> |
|
9 |
<div class="display-5">Export text</div> |
|
6 |
<fieldset class="mt-5"> |
|
7 |
<legend>Text import</legend> |
|
8 |
{control importForm} |
|
9 |
</fieldset> |
|
10 |
</div> |
|
10 | 11 |
</div> |
11 | 12 |
|
12 |
{control exportForm} |
|
13 |
<div class="row mt-5"> |
|
14 |
<div class="col-3"> |
|
15 |
<fieldset> |
|
16 |
<legend>Text export</legend> |
|
17 |
{control exportForm} |
|
18 |
</fieldset> |
|
19 |
</div> |
|
20 |
</div> |
|
13 | 21 |
{/block} |
app/AdminModule/templates/Transport/previewImport.latte | ||
---|---|---|
1 |
{block content} |
|
2 |
<div class="row"> |
|
3 |
<div class="col-12"> |
|
4 |
<div class="display-5">Preview of import: {$fileName}</div> |
|
5 |
</div> |
|
6 |
<div class="col-12"> |
|
7 |
Imported {$transliterationNum} transliterations |
|
8 |
</div> |
|
9 |
</div> |
|
10 |
|
|
11 |
<div class="mt-5 border p-3 col-8"> |
|
12 |
<div class="col-12 display-5" n:foreach="$parsedFile as $book=>$chapters">Book {$book}</div> |
|
13 |
<div class="row" n:foreach="$chapters as $chapter=>$references"> |
|
14 |
<div class="col-12 mt-3"> |
|
15 |
<fieldset> |
|
16 |
<legend>Chapter {$chapter}</legend> |
|
17 |
<div n:foreach="$references as $reference=>$element"> |
|
18 |
<i>{$surfaces[$reference]}</i><br> |
|
19 |
{foreach $element as $value} |
|
20 |
{$value}.<br> |
|
21 |
{/foreach} |
|
22 |
<hr> |
|
23 |
</div> |
|
24 |
</fieldset> |
|
25 |
</div> |
|
26 |
</div> |
|
27 |
</div> |
|
28 |
{/block} |
app/utils/TextParser.php | ||
---|---|---|
1 |
<?php |
|
2 |
|
|
3 |
|
|
4 |
namespace App\Utils; |
|
5 |
|
|
6 |
class TextParser |
|
7 |
{ |
|
8 |
public $textFile; |
|
9 |
private $objectTypes; |
|
10 |
private $surfaceTypes; |
|
11 |
|
|
12 |
public function __construct($file) |
|
13 |
{ |
|
14 |
$this->textFile = $file; |
|
15 |
} |
|
16 |
|
|
17 |
public function parseText() |
|
18 |
{ |
|
19 |
$array = explode("\n", file_get_contents($this->textFile)); |
|
20 |
$array = array_map('trim', $array); |
|
21 |
|
|
22 |
$output = array(); |
|
23 |
$currentBook = ""; |
|
24 |
$currentChapter = ""; |
|
25 |
$currentSurface = $this->transformAbbrev("obverse"); |
|
26 |
|
|
27 |
//TODO kontrola a parsovani object types (tablet, envelope) |
|
28 |
foreach ($array as $value) { |
|
29 |
if ($value[0] == '|' && !is_numeric($value[1])) { |
|
30 |
$currentBook = substr($value, 1); |
|
31 |
$currentChapter = ""; |
|
32 |
$currentSurface = $this->transformAbbrev("obverse"); |
|
33 |
} |
|
34 |
elseif ($value[0] == '|' && is_numeric($value[1])){ |
|
35 |
$currentChapter = substr($value, 1); |
|
36 |
$currentSurface = $this->transformAbbrev("obverse"); |
|
37 |
}else{ |
|
38 |
if (strchr($value, '$')) { |
|
39 |
$output[$currentBook][$currentChapter][$currentSurface][] = trim(substr($value,0,stripos($value,"&"))); |
|
40 |
$currentSurface = $this->transformAbbrev(substr(strchr($value, "&"),1, -1)); |
|
41 |
}else{ |
|
42 |
$output[$currentBook][$currentChapter][$currentSurface][] = $value; |
|
43 |
} |
|
44 |
} |
|
45 |
} |
|
46 |
|
|
47 |
return $output; |
|
48 |
} |
|
49 |
|
|
50 |
/** Nejvíc kontrola souboru*/ |
|
51 |
public function isFileParsable() |
|
52 |
{ |
|
53 |
if (file_exists($this->textFile)) { |
|
54 |
$checker = fopen($this->textFile, "r"); |
|
55 |
if (fgets($checker)[0] == '|') { |
|
56 |
return true; |
|
57 |
} else { |
|
58 |
return false; |
|
59 |
} |
|
60 |
} else { |
|
61 |
return false; |
|
62 |
} |
|
63 |
} |
|
64 |
|
|
65 |
public function setTypes($objects, $surfaces) |
|
66 |
{ |
|
67 |
$this->objectTypes = $objects; |
|
68 |
$this->surfaceTypes = $surfaces; |
|
69 |
} |
|
70 |
|
|
71 |
private function transformAbbrev($abbrev){ |
|
72 |
switch ($abbrev){ |
|
73 |
case "rev.": |
|
74 |
return array_search("reverse", $this->surfaceTypes); |
|
75 |
case "u.e.": |
|
76 |
return array_search("upper edge", $this->surfaceTypes); |
|
77 |
case "lo.e.": |
|
78 |
return array_search("lower edge", $this->surfaceTypes); |
|
79 |
case "le.e.": |
|
80 |
return array_search("left edge", $this->surfaceTypes); |
|
81 |
// Domnenky, v test souborech se nenachazi -> udelat podle toho export |
|
82 |
case "r.e.": |
|
83 |
return array_search("right edge", $this->surfaceTypes); |
|
84 |
case "s.i.1": |
|
85 |
return array_search("seal impression 1", $this->surfaceTypes); |
|
86 |
case "s.i.2": |
|
87 |
return array_search("seal impression 2", $this->surfaceTypes); |
|
88 |
case "s.i.3": |
|
89 |
return array_search("seal impression 3", $this->surfaceTypes); |
|
90 |
case "s.i.4": |
|
91 |
return array_search("seal impression 4", $this->surfaceTypes); |
|
92 |
case "s.i.5": |
|
93 |
return array_search("seal impression 5", $this->surfaceTypes); |
|
94 |
case "s.i.6": |
|
95 |
return array_search("seal impression 6", $this->surfaceTypes); |
|
96 |
case "s.i.7": |
|
97 |
return array_search("seal impression 7", $this->surfaceTypes); |
|
98 |
case "s.i.8": |
|
99 |
return array_search("seal impression 8", $this->surfaceTypes); |
|
100 |
default: |
|
101 |
return array_search("obverse", $this->surfaceTypes); |
|
102 |
} |
|
103 |
} |
|
104 |
} |
Také k dispozici: Unified diff
Re #7510 Úprava vzhledu, implementace parsování souboru a funkcionalita importu, preview