1
|
<?php
|
2
|
|
3
|
|
4
|
namespace App\Model\Facade;
|
5
|
|
6
|
use App\Model\Repository\BookRepository;
|
7
|
use App\Model\Repository\LineRepository;
|
8
|
use App\Model\Repository\SurfaceRepository;
|
9
|
use App\Model\Repository\TransliterationRepository;
|
10
|
use Nette\Database\Context;
|
11
|
use Nette\Database\Table\ActiveRow;
|
12
|
|
13
|
/**
|
14
|
* Class TransportFacade sloužící pro import/export práce s db
|
15
|
* @package App\Model\Facade
|
16
|
*/
|
17
|
class TransportFacade
|
18
|
{
|
19
|
/** @var BookRepository */
|
20
|
private $bookRepository;
|
21
|
|
22
|
/** @var TransliterationRepository */
|
23
|
private $transliterationRepository;
|
24
|
|
25
|
/** @var SurfaceRepository */
|
26
|
private $surfaceRepository;
|
27
|
|
28
|
/** @var LineRepository */
|
29
|
private $lineRepository;
|
30
|
|
31
|
/** @var Context */
|
32
|
private $context;
|
33
|
|
34
|
|
35
|
public function __construct(BookRepository $bookRepository,
|
36
|
TransliterationRepository $transliterationRepository,
|
37
|
SurfaceRepository $surfaceRepository,
|
38
|
LineRepository $lineRepository,
|
39
|
Context $context
|
40
|
)
|
41
|
{
|
42
|
$this->bookRepository = $bookRepository;
|
43
|
$this->transliterationRepository = $transliterationRepository;
|
44
|
$this->surfaceRepository = $surfaceRepository;
|
45
|
$this->lineRepository = $lineRepository;
|
46
|
$this->context = $context;
|
47
|
}
|
48
|
|
49
|
/**
|
50
|
* Uloží data transliterace z importu
|
51
|
*
|
52
|
* @param $data
|
53
|
* @return bool
|
54
|
*/
|
55
|
public function saveImportedData($data)
|
56
|
{
|
57
|
// Škaredý zanoření hodnot z dat v importu :( Tohle bude pro db náročný
|
58
|
foreach ($data as $book => $chapters) {
|
59
|
try {
|
60
|
$bookId = $this->getBookIdByName($book);
|
61
|
if (!$bookId) {
|
62
|
$bookId = $this->bookRepository->insert([BookRepository::COLUMN_BOOK_ABREV => $book])->getPrimary();
|
63
|
}
|
64
|
} catch (\Exception $exception) {
|
65
|
\Tracy\Debugger::log('Nepodařila se uložit nová kniha. Chyba: ' . $exception->getMessage(), 'transport-facade');
|
66
|
return FALSE;
|
67
|
}
|
68
|
foreach ($chapters as $chapter => $surfaces) {
|
69
|
try {
|
70
|
$transliterationId = $this->getTransliterationIdCurrentBookChapter($bookId, trim(explode(" ", $chapter)[0]));
|
71
|
if (!$transliterationId) {
|
72
|
$chapterReg = explode(" ", $chapter);
|
73
|
if (isset($chapterReg[1])) {
|
74
|
$chapterReg[1] = str_replace(array('(', ')'), '', $chapterReg[1]);
|
75
|
$this->transliterationRepository->insert([
|
76
|
TransliterationRepository::COLUMN_BOOK_ID => $bookId,
|
77
|
TransliterationRepository::COLUMN_CHAPTER => $chapterReg[0],
|
78
|
TransliterationRepository::COLUMN_REG_NO => $chapterReg[1]
|
79
|
]);
|
80
|
$transliterationId = $this->context->getInsertId(TransliterationRepository::COLUMN_ID);
|
81
|
} else {
|
82
|
$this->transliterationRepository->insert([
|
83
|
TransliterationRepository::COLUMN_BOOK_ID => $bookId,
|
84
|
TransliterationRepository::COLUMN_CHAPTER => $chapterReg[0]
|
85
|
]);
|
86
|
$transliterationId = $this->context->getInsertId(TransliterationRepository::COLUMN_ID);
|
87
|
}
|
88
|
}
|
89
|
} catch (\Exception $exception) {
|
90
|
\Tracy\Debugger::log('Nepodařila se uložit nová transliterace. Chyba: ' . $exception->getMessage(), 'transport-facade');
|
91
|
return FALSE;
|
92
|
}
|
93
|
foreach ($surfaces as $surface => $values) {
|
94
|
try {
|
95
|
$surfaceId = $this->getSurfaceIdByCurrentTransSurfType($transliterationId, $surface);
|
96
|
if (!$surfaceId) {
|
97
|
$surfaceId = $this->surfaceRepository->insert([
|
98
|
SurfaceRepository::COLUMN_TRANSLITERATION_ID => $transliterationId,
|
99
|
SurfaceRepository::COLUMN_SURFACE_TYPE_ID => $surface,
|
100
|
SurfaceRepository::COLUMN_OBJECT_TYPE_ID => 1
|
101
|
])->getPrimary();
|
102
|
}
|
103
|
} catch (\Exception $exception) {
|
104
|
\Tracy\Debugger::log('Nepodařil se uložit nový surface. Chyba: ' . $exception->getMessage(), 'transport-facade');
|
105
|
return FALSE;
|
106
|
}
|
107
|
|
108
|
foreach ($values as $line) {
|
109
|
try {
|
110
|
$lineId = $this->getLineIdByCurrentSurfLineNum($surfaceId, (explode(' ', $line)[0]));
|
111
|
if (!$lineId) {
|
112
|
$lineId = $this->lineRepository->insert([
|
113
|
LineRepository::COLUMN_SURFACE_ID => $surfaceId,
|
114
|
LineRepository::COLUMN_LINE_NUMBER => (explode(' ', $line)[0]),
|
115
|
LineRepository::COLUMN_TRANSLITERATION => substr(strchr($line, " "), 1),
|
116
|
])->{LineRepository::COLUMN_ID};
|
117
|
} else {
|
118
|
$this->lineRepository->fetchById($lineId)->update([
|
119
|
LineRepository::COLUMN_TRANSLITERATION => substr(strchr($line, " "), 1)
|
120
|
]);
|
121
|
}
|
122
|
} catch (\Exception $exception) {
|
123
|
\Tracy\Debugger::log('Nepodařil se uložit nová řádka (line) transliterace. Chyba: ' . $exception->getMessage(), 'transport-facade');
|
124
|
return FALSE;
|
125
|
}
|
126
|
}
|
127
|
}
|
128
|
}
|
129
|
}
|
130
|
return TRUE;
|
131
|
}
|
132
|
|
133
|
|
134
|
/**
|
135
|
* Vrací id knihy podle jména zkratky
|
136
|
* @param $bookName
|
137
|
* @return bool|mixed|ActiveRow
|
138
|
*/
|
139
|
public
|
140
|
function getBookIdByName(string $bookName)
|
141
|
{
|
142
|
$book = $this->bookRepository->getBookByBookAbbrev($bookName)->fetch();
|
143
|
if (!empty($book)) {
|
144
|
return $book->getPrimary();
|
145
|
} else {
|
146
|
return false;
|
147
|
}
|
148
|
}
|
149
|
|
150
|
|
151
|
private
|
152
|
function getTransliterationIdCurrentBookChapter(int $bookId, string $chapterName)
|
153
|
{
|
154
|
$transliteration = $this->transliterationRepository->getTransliterationByChapterNameAndBookId($bookId, $chapterName)->fetch();
|
155
|
if (!empty($transliteration)) {
|
156
|
return $transliteration->getPrimary();
|
157
|
} else {
|
158
|
return false;
|
159
|
}
|
160
|
}
|
161
|
|
162
|
private function getSurfaceIdByCurrentTransSurfType(int $transliterationId, int $surfaceId)
|
163
|
{
|
164
|
$surface = $this->surfaceRepository->getSurfaceByTransliterationIdAndSurfaceTypeId($transliterationId, $surfaceId)->fetch();
|
165
|
|
166
|
if (!empty($surface)) {
|
167
|
return $surface->getPrimary();
|
168
|
} else {
|
169
|
return false;
|
170
|
}
|
171
|
}
|
172
|
|
173
|
private function getLineIdByCurrentSurfLineNum(int $surfaceId, $lineNum)
|
174
|
{
|
175
|
$line = $this->lineRepository->getLineBySurfaceIdAndLineNumber($surfaceId, $lineNum)->fetch();
|
176
|
if (!empty($line)) {
|
177
|
return $line->{LineRepository::COLUMN_ID};
|
178
|
} else {
|
179
|
return false;
|
180
|
}
|
181
|
}
|
182
|
}
|