1 |
ce045025
|
Petr Lukašík
|
<?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 |
175d726f
|
Filip Jani
|
foreach ($data as $book => $chapters)
|
59 |
|
|
{
|
60 |
|
|
try
|
61 |
|
|
{
|
62 |
ce045025
|
Petr Lukašík
|
$bookId = $this->getBookIdByName($book);
|
63 |
175d726f
|
Filip Jani
|
if (!$bookId)
|
64 |
|
|
{
|
65 |
ce045025
|
Petr Lukašík
|
$bookId = $this->bookRepository->insert([BookRepository::COLUMN_BOOK_ABREV => $book])->getPrimary();
|
66 |
|
|
}
|
67 |
175d726f
|
Filip Jani
|
}
|
68 |
|
|
catch (\Exception $exception)
|
69 |
|
|
{
|
70 |
ce045025
|
Petr Lukašík
|
\Tracy\Debugger::log('Nepodařila se uložit nová kniha. Chyba: ' . $exception->getMessage(), 'transport-facade');
|
71 |
|
|
return FALSE;
|
72 |
|
|
}
|
73 |
175d726f
|
Filip Jani
|
foreach ($chapters as $chapter => $surfaces)
|
74 |
|
|
{
|
75 |
|
|
try
|
76 |
|
|
{
|
77 |
ce045025
|
Petr Lukašík
|
$transliterationId = $this->getTransliterationIdCurrentBookChapter($bookId, trim(explode(" ", $chapter)[0]));
|
78 |
175d726f
|
Filip Jani
|
if (!$transliterationId)
|
79 |
|
|
{
|
80 |
ce045025
|
Petr Lukašík
|
$chapterReg = explode(" ", $chapter);
|
81 |
175d726f
|
Filip Jani
|
if (isset($chapterReg[1]))
|
82 |
|
|
{
|
83 |
ce045025
|
Petr Lukašík
|
$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 |
175d726f
|
Filip Jani
|
} else
|
91 |
|
|
{
|
92 |
ce045025
|
Petr Lukašík
|
$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 |
175d726f
|
Filip Jani
|
}
|
100 |
|
|
catch (\Exception $exception)
|
101 |
|
|
{
|
102 |
ce045025
|
Petr Lukašík
|
\Tracy\Debugger::log('Nepodařila se uložit nová transliterace. Chyba: ' . $exception->getMessage(), 'transport-facade');
|
103 |
|
|
return FALSE;
|
104 |
|
|
}
|
105 |
175d726f
|
Filip Jani
|
foreach ($surfaces as $surface => $values)
|
106 |
|
|
{
|
107 |
|
|
try
|
108 |
|
|
{
|
109 |
ce045025
|
Petr Lukašík
|
$surfaceId = $this->getSurfaceIdByCurrentTransSurfType($transliterationId, $surface);
|
110 |
175d726f
|
Filip Jani
|
if (!$surfaceId)
|
111 |
|
|
{
|
112 |
ce045025
|
Petr Lukašík
|
$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 |
175d726f
|
Filip Jani
|
}
|
119 |
|
|
catch (\Exception $exception)
|
120 |
|
|
{
|
121 |
ce045025
|
Petr Lukašík
|
\Tracy\Debugger::log('Nepodařil se uložit nový surface. Chyba: ' . $exception->getMessage(), 'transport-facade');
|
122 |
|
|
return FALSE;
|
123 |
|
|
}
|
124 |
|
|
|
125 |
175d726f
|
Filip Jani
|
foreach ($values as $line)
|
126 |
|
|
{
|
127 |
|
|
try
|
128 |
|
|
{
|
129 |
ce045025
|
Petr Lukašík
|
$lineId = $this->getLineIdByCurrentSurfLineNum($surfaceId, (explode(' ', $line)[0]));
|
130 |
175d726f
|
Filip Jani
|
if (!$lineId)
|
131 |
|
|
{
|
132 |
1107fbdb
|
Petr Lukašík
|
$this->lineRepository->insert([
|
133 |
ce045025
|
Petr Lukašík
|
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 |
175d726f
|
Filip Jani
|
} else
|
138 |
|
|
{
|
139 |
ce045025
|
Petr Lukašík
|
$this->lineRepository->fetchById($lineId)->update([
|
140 |
|
|
LineRepository::COLUMN_TRANSLITERATION => substr(strchr($line, " "), 1)
|
141 |
|
|
]);
|
142 |
|
|
}
|
143 |
175d726f
|
Filip Jani
|
}
|
144 |
|
|
catch (\Exception $exception)
|
145 |
|
|
{
|
146 |
ce045025
|
Petr Lukašík
|
\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 |
175d726f
|
Filip Jani
|
public function getBookIdByName(string $bookName)
|
162 |
ce045025
|
Petr Lukašík
|
{
|
163 |
|
|
$book = $this->bookRepository->getBookByBookAbbrev($bookName)->fetch();
|
164 |
175d726f
|
Filip Jani
|
if (!empty($book))
|
165 |
|
|
{
|
166 |
ce045025
|
Petr Lukašík
|
return $book->getPrimary();
|
167 |
175d726f
|
Filip Jani
|
} else
|
168 |
|
|
{
|
169 |
ce045025
|
Petr Lukašík
|
return false;
|
170 |
|
|
}
|
171 |
|
|
}
|
172 |
|
|
|
173 |
175d726f
|
Filip Jani
|
private function getTransliterationIdCurrentBookChapter(int $bookId, string $chapterName)
|
174 |
ce045025
|
Petr Lukašík
|
{
|
175 |
|
|
$transliteration = $this->transliterationRepository->getTransliterationByChapterNameAndBookId($bookId, $chapterName)->fetch();
|
176 |
175d726f
|
Filip Jani
|
if (!empty($transliteration))
|
177 |
|
|
{
|
178 |
ce045025
|
Petr Lukašík
|
return $transliteration->getPrimary();
|
179 |
175d726f
|
Filip Jani
|
} else
|
180 |
|
|
{
|
181 |
ce045025
|
Petr Lukašík
|
return false;
|
182 |
|
|
}
|
183 |
|
|
}
|
184 |
|
|
|
185 |
|
|
private function getSurfaceIdByCurrentTransSurfType(int $transliterationId, int $surfaceId)
|
186 |
|
|
{
|
187 |
|
|
$surface = $this->surfaceRepository->getSurfaceByTransliterationIdAndSurfaceTypeId($transliterationId, $surfaceId)->fetch();
|
188 |
|
|
|
189 |
175d726f
|
Filip Jani
|
if (!empty($surface))
|
190 |
|
|
{
|
191 |
ce045025
|
Petr Lukašík
|
return $surface->getPrimary();
|
192 |
175d726f
|
Filip Jani
|
} else
|
193 |
|
|
{
|
194 |
ce045025
|
Petr Lukašík
|
return false;
|
195 |
|
|
}
|
196 |
|
|
}
|
197 |
|
|
|
198 |
|
|
private function getLineIdByCurrentSurfLineNum(int $surfaceId, $lineNum)
|
199 |
|
|
{
|
200 |
|
|
$line = $this->lineRepository->getLineBySurfaceIdAndLineNumber($surfaceId, $lineNum)->fetch();
|
201 |
175d726f
|
Filip Jani
|
if (!empty($line))
|
202 |
|
|
{
|
203 |
ce045025
|
Petr Lukašík
|
return $line->{LineRepository::COLUMN_ID};
|
204 |
175d726f
|
Filip Jani
|
} else
|
205 |
|
|
{
|
206 |
ce045025
|
Petr Lukašík
|
return false;
|
207 |
|
|
}
|
208 |
|
|
}
|
209 |
|
|
}
|