1 |
cdaf3e0a
|
Filip Jani
|
<?php
|
2 |
|
|
|
3 |
|
|
|
4 |
|
|
namespace App\Model\Facade;
|
5 |
|
|
|
6 |
|
|
|
7 |
29295606
|
Filip Jani
|
use App\AdminModule\Components\TransliterationNewForm;
|
8 |
e9926833
|
hard456
|
use App\Enum\EFlashMessage;
|
9 |
b8fae4af
|
Filip Jani
|
use App\Model\Repository\BookRepository;
|
10 |
cdaf3e0a
|
Filip Jani
|
use App\Model\Repository\LineRepository;
|
11 |
e9926833
|
hard456
|
use App\Model\Repository\LitReferenceRepository;
|
12 |
cdaf3e0a
|
Filip Jani
|
use App\Model\Repository\ObjectTypeRepository;
|
13 |
|
|
use App\Model\Repository\SurfaceRepository;
|
14 |
|
|
use App\Model\Repository\SurfaceTypeRepository;
|
15 |
e9926833
|
hard456
|
use App\Model\Repository\TransliterationRepository;
|
16 |
3882f5d3
|
Filip Jani
|
use Nette\Application\UI\Form;
|
17 |
|
|
use Nette\Database\Context;
|
18 |
cdaf3e0a
|
Filip Jani
|
use Nette\Database\Table\ActiveRow;
|
19 |
32b3fe21
|
Filip Jani
|
use Nette\Utils\ArrayHash;
|
20 |
e9926833
|
hard456
|
use Tracy\Debugger;
|
21 |
cdaf3e0a
|
Filip Jani
|
|
22 |
|
|
class TransliterationFacade
|
23 |
|
|
{
|
24 |
|
|
/**
|
25 |
|
|
* @var SurfaceRepository
|
26 |
|
|
*/
|
27 |
|
|
private $surfaceRepository;
|
28 |
|
|
/**
|
29 |
|
|
* @var LineRepository
|
30 |
|
|
*/
|
31 |
|
|
private $lineRepository;
|
32 |
|
|
/**
|
33 |
|
|
* @var ObjectTypeRepository
|
34 |
|
|
*/
|
35 |
|
|
private $objectTypeRepository;
|
36 |
|
|
/**
|
37 |
|
|
* @var SurfaceTypeRepository
|
38 |
|
|
*/
|
39 |
|
|
private $surfaceTypeRepository;
|
40 |
3882f5d3
|
Filip Jani
|
/**
|
41 |
|
|
* @var Context
|
42 |
|
|
*/
|
43 |
|
|
private $context;
|
44 |
e9926833
|
hard456
|
/**
|
45 |
|
|
* @var TransliterationRepository
|
46 |
|
|
*/
|
47 |
|
|
private $transliterationRepository;
|
48 |
|
|
/**
|
49 |
|
|
* @var LitReferenceRepository
|
50 |
|
|
*/
|
51 |
|
|
private $litReferenceRepository;
|
52 |
b8fae4af
|
Filip Jani
|
/**
|
53 |
|
|
* @var BookFacade
|
54 |
|
|
*/
|
55 |
|
|
private $bookFacade;
|
56 |
cdaf3e0a
|
Filip Jani
|
|
57 |
|
|
public function __construct(SurfaceRepository $surfaceRepository,
|
58 |
|
|
LineRepository $lineRepository,
|
59 |
|
|
ObjectTypeRepository $objectTypeRepository,
|
60 |
3882f5d3
|
Filip Jani
|
SurfaceTypeRepository $surfaceTypeRepository,
|
61 |
e9926833
|
hard456
|
TransliterationRepository $transliterationRepository,
|
62 |
|
|
LitReferenceRepository $litReferenceRepository,
|
63 |
b8fae4af
|
Filip Jani
|
Context $context,
|
64 |
|
|
BookFacade $bookFacade
|
65 |
cdaf3e0a
|
Filip Jani
|
)
|
66 |
|
|
{
|
67 |
|
|
$this->surfaceRepository = $surfaceRepository;
|
68 |
|
|
$this->lineRepository = $lineRepository;
|
69 |
|
|
$this->objectTypeRepository = $objectTypeRepository;
|
70 |
|
|
$this->surfaceTypeRepository = $surfaceTypeRepository;
|
71 |
3882f5d3
|
Filip Jani
|
$this->context = $context;
|
72 |
e9926833
|
hard456
|
$this->transliterationRepository = $transliterationRepository;
|
73 |
|
|
$this->litReferenceRepository = $litReferenceRepository;
|
74 |
b8fae4af
|
Filip Jani
|
$this->bookFacade = $bookFacade;
|
75 |
e9926833
|
hard456
|
}
|
76 |
|
|
|
77 |
b8fae4af
|
Filip Jani
|
/**
|
78 |
|
|
* Vložení nové transliterace do databáze
|
79 |
|
|
*
|
80 |
|
|
* @param \App\Utils\Form $form
|
81 |
|
|
* @return bool|void
|
82 |
|
|
*/
|
83 |
|
|
public function saveNewTransliteration(\App\Utils\Form $form)
|
84 |
e9926833
|
hard456
|
{
|
85 |
b8fae4af
|
Filip Jani
|
$data = $form->getValues();
|
86 |
|
|
$transliteration = $this->saveTransliterationInfo($form);
|
87 |
e9926833
|
hard456
|
|
88 |
29295606
|
Filip Jani
|
if ($data['bookContainer']['book'] == TransliterationNewForm::BOOK_EXISTING)
|
89 |
b8fae4af
|
Filip Jani
|
{
|
90 |
|
|
$bookId = $data['bookContainer'][BookRepository::COLUMN_ID];
|
91 |
|
|
} else
|
92 |
|
|
{
|
93 |
|
|
$book = $this->bookFacade->saveBook($form);
|
94 |
|
|
$bookId = $book[BookRepository::COLUMN_ID];
|
95 |
|
|
}
|
96 |
e9926833
|
hard456
|
|
97 |
b8fae4af
|
Filip Jani
|
$transliteration->update([TransliterationRepository::COLUMN_BOOK_ID => $bookId]);
|
98 |
e9926833
|
hard456
|
|
99 |
b8fae4af
|
Filip Jani
|
if ($transliteration == null)
|
100 |
e9926833
|
hard456
|
{
|
101 |
b8fae4af
|
Filip Jani
|
return FALSE;
|
102 |
e9926833
|
hard456
|
}
|
103 |
|
|
|
104 |
b8fae4af
|
Filip Jani
|
$result = $this->saveTransliterationData($transliteration[TransliterationRepository::COLUMN_ID], $form);
|
105 |
|
|
return $result;
|
106 |
|
|
}
|
107 |
|
|
|
108 |
|
|
/**
|
109 |
|
|
* Uložení informací o transliteraci do databáze
|
110 |
|
|
*
|
111 |
|
|
* @param Form $form
|
112 |
|
|
* @param int|null $id
|
113 |
|
|
* @return ActiveRow|null
|
114 |
|
|
*/
|
115 |
|
|
public function saveTransliterationInfo(Form $form, int $id = null)
|
116 |
|
|
{
|
117 |
|
|
$formValues = $form->getValues();
|
118 |
|
|
$formValues = isset($formValues['infoContainer']) ? $formValues['infoContainer'] : $formValues;
|
119 |
|
|
|
120 |
|
|
$references = $formValues['references'];
|
121 |
e9926833
|
hard456
|
unset($formValues['references']);
|
122 |
|
|
|
123 |
|
|
$newTransliteration = $this->transliterationRepository->save($formValues, $id);
|
124 |
|
|
|
125 |
b8fae4af
|
Filip Jani
|
if ($id != null)
|
126 |
|
|
{
|
127 |
e9926833
|
hard456
|
$this->deleteRemovedReferences($references, $id);
|
128 |
b8fae4af
|
Filip Jani
|
} else
|
129 |
|
|
{
|
130 |
e9926833
|
hard456
|
$id = $newTransliteration[TransliterationRepository::COLUMN_ID];
|
131 |
|
|
}
|
132 |
|
|
|
133 |
|
|
foreach ($references as $item)
|
134 |
|
|
{
|
135 |
|
|
$item[LitReferenceRepository::COLUMN_TRANSLITERATION_ID] = $id;
|
136 |
|
|
$this->litReferenceRepository->save($item, (int)$item[LitReferenceRepository::COLUMN_ID]);
|
137 |
|
|
}
|
138 |
|
|
|
139 |
|
|
return $newTransliteration;
|
140 |
|
|
}
|
141 |
|
|
|
142 |
|
|
/**
|
143 |
|
|
* Zkontroluje jestli není nějaká refence prázdná
|
144 |
|
|
*
|
145 |
|
|
* @param $references array referencí
|
146 |
|
|
* @return bool
|
147 |
|
|
*/
|
148 |
|
|
public function isAnyReferenceEmpty($references)
|
149 |
|
|
{
|
150 |
|
|
foreach ($references as $r)
|
151 |
|
|
{
|
152 |
|
|
if (empty($r[LitReferenceRepository::COLUMN_SERIES]) && empty($r[LitReferenceRepository::COLUMN_NUMBER])
|
153 |
|
|
&& empty($r[LitReferenceRepository::COLUMN_PLATE]))
|
154 |
|
|
{
|
155 |
|
|
return true;
|
156 |
|
|
}
|
157 |
|
|
}
|
158 |
|
|
return false;
|
159 |
|
|
}
|
160 |
|
|
|
161 |
|
|
/**
|
162 |
|
|
* Odstraní odebrané reference transliterace
|
163 |
|
|
*
|
164 |
|
|
* @param $newReferences array nových transliterací
|
165 |
|
|
*/
|
166 |
b8fae4af
|
Filip Jani
|
public function deleteRemovedReferences($newReferences, $id)
|
167 |
e9926833
|
hard456
|
{
|
168 |
|
|
$references = $this->litReferenceRepository->findByTransliterationId($id);
|
169 |
|
|
|
170 |
|
|
foreach ($references as $ref)
|
171 |
|
|
{
|
172 |
|
|
if ($this->isRemovedReference($ref, $newReferences))
|
173 |
|
|
{
|
174 |
|
|
$this->litReferenceRepository->delete($ref[LitReferenceRepository::COLUMN_ID]);
|
175 |
|
|
}
|
176 |
|
|
}
|
177 |
|
|
|
178 |
|
|
}
|
179 |
|
|
|
180 |
|
|
/**
|
181 |
|
|
* Zkontroluje jestli je refence v poli referencí
|
182 |
|
|
*
|
183 |
|
|
* @param $reference ActiveRow reference ke kontrole
|
184 |
|
|
* @param $newReferences array pole referencí
|
185 |
|
|
* @return bool
|
186 |
|
|
*/
|
187 |
|
|
public function isRemovedReference($reference, $newReferences)
|
188 |
|
|
{
|
189 |
|
|
foreach ($newReferences as $ref)
|
190 |
|
|
{
|
191 |
|
|
if ($ref[LitReferenceRepository::COLUMN_ID] == $reference[LitReferenceRepository::COLUMN_ID])
|
192 |
|
|
{
|
193 |
|
|
return false;
|
194 |
|
|
}
|
195 |
|
|
}
|
196 |
|
|
return true;
|
197 |
cdaf3e0a
|
Filip Jani
|
}
|
198 |
|
|
|
199 |
|
|
/**
|
200 |
|
|
* Uloží data transliterace
|
201 |
|
|
*
|
202 |
32b3fe21
|
Filip Jani
|
* @param int|null $id : ID transliterace
|
203 |
3882f5d3
|
Filip Jani
|
* @param Form $form
|
204 |
b8fae4af
|
Filip Jani
|
* @return int $id : Vrací ID transliterace
|
205 |
cdaf3e0a
|
Filip Jani
|
*/
|
206 |
b8fae4af
|
Filip Jani
|
public function saveTransliterationData(int $id, Form $form): int
|
207 |
cdaf3e0a
|
Filip Jani
|
{
|
208 |
3882f5d3
|
Filip Jani
|
$formValues = $form->getValues();
|
209 |
e9926833
|
hard456
|
$formValues = isset($formValues['dataContainer']) ? $formValues['dataContainer'] : $formValues;
|
210 |
|
|
|
211 |
3882f5d3
|
Filip Jani
|
$this->removeDeletedLines($id, $formValues);
|
212 |
|
|
|
213 |
32b3fe21
|
Filip Jani
|
// Škaredý zanoření hodnot z formuláře v kontainerech :(
|
214 |
|
|
foreach ($formValues as $objectTypeId => $surfaceContainers)
|
215 |
|
|
{
|
216 |
|
|
foreach ($surfaceContainers as $surfaceTypeId => $inputs)
|
217 |
|
|
{
|
218 |
|
|
foreach ($inputs as $key => $values)
|
219 |
|
|
{
|
220 |
3882f5d3
|
Filip Jani
|
try
|
221 |
32b3fe21
|
Filip Jani
|
{
|
222 |
3882f5d3
|
Filip Jani
|
$lineId = (int)$values->{LineRepository::COLUMN_ID};
|
223 |
32b3fe21
|
Filip Jani
|
|
224 |
3882f5d3
|
Filip Jani
|
// Editace již existující řádky
|
225 |
|
|
if (!empty($lineId))
|
226 |
|
|
{
|
227 |
|
|
$this->lineRepository->fetchById($lineId)->update($values);
|
228 |
32b3fe21
|
Filip Jani
|
|
229 |
3882f5d3
|
Filip Jani
|
} else
|
230 |
32b3fe21
|
Filip Jani
|
{
|
231 |
a0d2a5ea
|
hard456
|
unset($values->{LineRepository::COLUMN_ID});
|
232 |
|
|
|
233 |
3882f5d3
|
Filip Jani
|
$surface = $this->surfaceRepository->fetchSurface($id, $objectTypeId, $surfaceTypeId);
|
234 |
|
|
|
235 |
|
|
if ($surface === FALSE)
|
236 |
|
|
{
|
237 |
|
|
$surface = $this->surfaceRepository->insert(
|
238 |
|
|
[
|
239 |
|
|
SurfaceRepository::COLUMN_SURFACE_TYPE_ID => $surfaceTypeId,
|
240 |
|
|
SurfaceRepository::COLUMN_OBJECT_TYPE_ID => $objectTypeId,
|
241 |
|
|
SurfaceRepository::COLUMN_TRANSLITERATION_ID => $id
|
242 |
|
|
]
|
243 |
|
|
);
|
244 |
|
|
}
|
245 |
|
|
|
246 |
a0d2a5ea
|
hard456
|
$surfaceId = $this->context->getInsertId(SurfaceRepository::TABLE_NAME);
|
247 |
|
|
|
248 |
|
|
$values->{LineRepository::COLUMN_SURFACE_ID} = !empty($surfaceId) ? $surfaceId : $surface->{SurfaceRepository::COLUMN_ID};
|
249 |
|
|
|
250 |
3882f5d3
|
Filip Jani
|
$this->lineRepository->insert($values);
|
251 |
32b3fe21
|
Filip Jani
|
}
|
252 |
3882f5d3
|
Filip Jani
|
} catch (\Exception $exception)
|
253 |
|
|
{
|
254 |
|
|
\Tracy\Debugger::log('Nepodařilo se uložit data transliterace. Chyba: ' . $exception->getMessage(), 'transliteration-facade');
|
255 |
b8fae4af
|
Filip Jani
|
return 0;
|
256 |
32b3fe21
|
Filip Jani
|
}
|
257 |
|
|
}
|
258 |
|
|
}
|
259 |
|
|
}
|
260 |
3882f5d3
|
Filip Jani
|
|
261 |
b8fae4af
|
Filip Jani
|
return $id;
|
262 |
cdaf3e0a
|
Filip Jani
|
}
|
263 |
|
|
|
264 |
|
|
/**
|
265 |
|
|
* Vrací data transliterace pro formulář
|
266 |
|
|
*
|
267 |
|
|
* @param int $id : ID transliterace
|
268 |
3882f5d3
|
Filip Jani
|
* @return array : pole výchozích hodnot pro formulář
|
269 |
cdaf3e0a
|
Filip Jani
|
*/
|
270 |
3882f5d3
|
Filip Jani
|
public function getTransliterationData(int $id): array
|
271 |
cdaf3e0a
|
Filip Jani
|
{
|
272 |
|
|
$surfaces = $this->surfaceRepository->findByTransliterationId($id)->fetchAll();
|
273 |
|
|
|
274 |
|
|
$defaults = array();
|
275 |
|
|
|
276 |
|
|
/** @var ActiveRow $surface */
|
277 |
|
|
foreach ($surfaces as $surface)
|
278 |
|
|
{
|
279 |
|
|
// Načtení všech řádek
|
280 |
|
|
$lineRows = $surface->related(LineRepository::TABLE_NAME, SurfaceRepository::COLUMN_ID)->fetchAll();
|
281 |
3882f5d3
|
Filip Jani
|
if ($lineRows === FALSE || empty($lineRows))
|
282 |
cdaf3e0a
|
Filip Jani
|
{
|
283 |
|
|
continue;
|
284 |
|
|
}
|
285 |
|
|
|
286 |
32b3fe21
|
Filip Jani
|
$objectId = $surface->{SurfaceRepository::COLUMN_OBJECT_TYPE_ID};
|
287 |
|
|
$surfaceId = $surface->{SurfaceRepository::COLUMN_SURFACE_TYPE_ID};
|
288 |
cdaf3e0a
|
Filip Jani
|
|
289 |
32b3fe21
|
Filip Jani
|
$defaults[$objectId][$surfaceId] = $lineRows;
|
290 |
cdaf3e0a
|
Filip Jani
|
}
|
291 |
|
|
|
292 |
|
|
return $defaults;
|
293 |
|
|
}
|
294 |
|
|
|
295 |
3882f5d3
|
Filip Jani
|
/**
|
296 |
|
|
* Odstraní smazané řádky z DB
|
297 |
|
|
*
|
298 |
|
|
* @param int $id : ID transliterace
|
299 |
|
|
* @param ArrayHash $formValues : hodnoty z formuláře
|
300 |
|
|
* @return int : počet smazaných řádek
|
301 |
|
|
*/
|
302 |
|
|
public function removeDeletedLines(int $id, ArrayHash $formValues)
|
303 |
|
|
{
|
304 |
|
|
$deletedIds = $this->getDeletedLineIds($id, $formValues);
|
305 |
|
|
|
306 |
|
|
return $this->lineRepository->deleteLines($deletedIds);
|
307 |
|
|
}
|
308 |
|
|
|
309 |
|
|
/**
|
310 |
|
|
* Vrací ID smazaných řádek transliterace
|
311 |
|
|
*
|
312 |
|
|
* @param int $id : ID transliterace
|
313 |
|
|
* @param ArrayHash $formValues : hodnoty z formuláře
|
314 |
|
|
* @return array
|
315 |
|
|
*/
|
316 |
|
|
private function getDeletedLineIds(int $id, ArrayHash $formValues)
|
317 |
|
|
{
|
318 |
|
|
$deletedIds = [];
|
319 |
|
|
$oldLines = $this->getTransliterationData($id);
|
320 |
|
|
|
321 |
|
|
// Tohle je fakt nádhera :(
|
322 |
|
|
foreach ($oldLines as $objectTypeId => $surfaceContainers)
|
323 |
|
|
{
|
324 |
|
|
foreach ($surfaceContainers as $surfaceTypeId => $activeRows)
|
325 |
|
|
{
|
326 |
|
|
foreach ($activeRows as $activeRow)
|
327 |
|
|
{
|
328 |
|
|
if (isset($formValues[$objectTypeId][$surfaceTypeId]))
|
329 |
|
|
{
|
330 |
|
|
$oldLineFound = FALSE;
|
331 |
|
|
foreach ($formValues[$objectTypeId][$surfaceTypeId] as $array)
|
332 |
|
|
{
|
333 |
|
|
if ($array[LineRepository::COLUMN_ID] == $activeRow->{LineRepository::COLUMN_ID})
|
334 |
|
|
{
|
335 |
|
|
$oldLineFound = TRUE;
|
336 |
|
|
break;
|
337 |
|
|
}
|
338 |
|
|
}
|
339 |
|
|
|
340 |
|
|
if (!$oldLineFound)
|
341 |
|
|
{
|
342 |
|
|
$deletedIds[] = $activeRow->{LineRepository::COLUMN_ID};
|
343 |
|
|
}
|
344 |
|
|
}
|
345 |
|
|
}
|
346 |
|
|
}
|
347 |
|
|
}
|
348 |
|
|
|
349 |
|
|
return $deletedIds;
|
350 |
|
|
}
|
351 |
cdaf3e0a
|
Filip Jani
|
}
|