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
|
{
|
60
|
try
|
61
|
{
|
62
|
$bookId = $this->getBookIdByName($book);
|
63
|
if (!$bookId)
|
64
|
{
|
65
|
$bookId = $this->bookRepository->insert([BookRepository::COLUMN_BOOK_ABREV => $book])->getPrimary();
|
66
|
}
|
67
|
}
|
68
|
catch (\Exception $exception)
|
69
|
{
|
70
|
\Tracy\Debugger::log('Nepodařila se uložit nová kniha. Chyba: ' . $exception->getMessage(), 'transport-facade');
|
71
|
return FALSE;
|
72
|
}
|
73
|
foreach ($chapters as $chapter => $surfaces)
|
74
|
{
|
75
|
try
|
76
|
{
|
77
|
$transliterationId = $this->getTransliterationIdCurrentBookChapter($bookId, trim(explode(" ", $chapter)[0]));
|
78
|
if (!$transliterationId)
|
79
|
{
|
80
|
$chapterReg = explode(" ", $chapter);
|
81
|
if (isset($chapterReg[1]))
|
82
|
{
|
83
|
$chapterReg[1] = str_replace(array('(', ')'), '', $chapterReg[1]);
|
84
|
$this->transliterationRepository->insert([
|
85
|
TransliterationRepository::COLUMN_BOOK_ID => $bookId,
|
86
|
TransliterationRepository::COLUMN_CHAPTER => $chapterReg[0],
|
87
|
TransliterationRepository::COLUMN_REG_NO => $chapterReg[1]
|
88
|
]);
|
89
|
$transliterationId = $this->context->getInsertId(TransliterationRepository::COLUMN_ID);
|
90
|
} else
|
91
|
{
|
92
|
$this->transliterationRepository->insert([
|
93
|
TransliterationRepository::COLUMN_BOOK_ID => $bookId,
|
94
|
TransliterationRepository::COLUMN_CHAPTER => $chapterReg[0]
|
95
|
]);
|
96
|
$transliterationId = $this->context->getInsertId(TransliterationRepository::COLUMN_ID);
|
97
|
}
|
98
|
}
|
99
|
}
|
100
|
catch (\Exception $exception)
|
101
|
{
|
102
|
\Tracy\Debugger::log('Nepodařila se uložit nová transliterace. Chyba: ' . $exception->getMessage(), 'transport-facade');
|
103
|
return FALSE;
|
104
|
}
|
105
|
foreach ($surfaces as $surface => $values)
|
106
|
{
|
107
|
try
|
108
|
{
|
109
|
$surfaceId = $this->getSurfaceIdByCurrentTransSurfType($transliterationId, $surface);
|
110
|
if (!$surfaceId)
|
111
|
{
|
112
|
$surfaceId = $this->surfaceRepository->insert([
|
113
|
SurfaceRepository::COLUMN_TRANSLITERATION_ID => $transliterationId,
|
114
|
SurfaceRepository::COLUMN_SURFACE_TYPE_ID => $surface,
|
115
|
SurfaceRepository::COLUMN_OBJECT_TYPE_ID => 1
|
116
|
])->getPrimary();
|
117
|
}
|
118
|
}
|
119
|
catch (\Exception $exception)
|
120
|
{
|
121
|
\Tracy\Debugger::log('Nepodařil se uložit nový surface. Chyba: ' . $exception->getMessage(), 'transport-facade');
|
122
|
return FALSE;
|
123
|
}
|
124
|
|
125
|
foreach ($values as $line)
|
126
|
{
|
127
|
try
|
128
|
{
|
129
|
$lineId = $this->getLineIdByCurrentSurfLineNum($surfaceId, (explode(' ', $line)[0]));
|
130
|
if (!$lineId)
|
131
|
{
|
132
|
$this->lineRepository->insert([
|
133
|
LineRepository::COLUMN_SURFACE_ID => $surfaceId,
|
134
|
LineRepository::COLUMN_LINE_NUMBER => (explode(' ', $line)[0]),
|
135
|
LineRepository::COLUMN_TRANSLITERATION => substr(strchr($line, " "), 1),
|
136
|
])->{LineRepository::COLUMN_ID};
|
137
|
} else
|
138
|
{
|
139
|
$this->lineRepository->fetchById($lineId)->update([
|
140
|
LineRepository::COLUMN_TRANSLITERATION => substr(strchr($line, " "), 1)
|
141
|
]);
|
142
|
}
|
143
|
}
|
144
|
catch (\Exception $exception)
|
145
|
{
|
146
|
\Tracy\Debugger::log('Nepodařil se uložit nová řádka (line) transliterace. Chyba: ' . $exception->getMessage(), 'transport-facade');
|
147
|
return FALSE;
|
148
|
}
|
149
|
}
|
150
|
}
|
151
|
}
|
152
|
}
|
153
|
return TRUE;
|
154
|
}
|
155
|
|
156
|
/**
|
157
|
* Vrací id knihy podle jména zkratky
|
158
|
* @param $bookName
|
159
|
* @return bool|mixed|ActiveRow
|
160
|
*/
|
161
|
public function getBookIdByName(string $bookName)
|
162
|
{
|
163
|
$book = $this->bookRepository->getBookByBookAbbrev($bookName)->fetch();
|
164
|
if (!empty($book))
|
165
|
{
|
166
|
return $book->getPrimary();
|
167
|
} else
|
168
|
{
|
169
|
return false;
|
170
|
}
|
171
|
}
|
172
|
|
173
|
private function getTransliterationIdCurrentBookChapter(int $bookId, string $chapterName)
|
174
|
{
|
175
|
$transliteration = $this->transliterationRepository->getTransliterationByChapterNameAndBookId($bookId, $chapterName)->fetch();
|
176
|
if (!empty($transliteration))
|
177
|
{
|
178
|
return $transliteration->getPrimary();
|
179
|
} else
|
180
|
{
|
181
|
return false;
|
182
|
}
|
183
|
}
|
184
|
|
185
|
private function getSurfaceIdByCurrentTransSurfType(int $transliterationId, int $surfaceId)
|
186
|
{
|
187
|
$surface = $this->surfaceRepository->getSurfaceByTransliterationIdAndSurfaceTypeId($transliterationId, $surfaceId)->fetch();
|
188
|
|
189
|
if (!empty($surface))
|
190
|
{
|
191
|
return $surface->getPrimary();
|
192
|
} else
|
193
|
{
|
194
|
return false;
|
195
|
}
|
196
|
}
|
197
|
|
198
|
private function getLineIdByCurrentSurfLineNum(int $surfaceId, $lineNum)
|
199
|
{
|
200
|
$line = $this->lineRepository->getLineBySurfaceIdAndLineNumber($surfaceId, $lineNum)->fetch();
|
201
|
if (!empty($line))
|
202
|
{
|
203
|
return $line->{LineRepository::COLUMN_ID};
|
204
|
} else
|
205
|
{
|
206
|
return false;
|
207
|
}
|
208
|
}
|
209
|
}
|