Revize 3882f5d3
Přidáno uživatelem Filip Jani před více než 5 roky(ů)
app/AdminModule/component/Transliteration/TransliterationDataEditForm.latte | ||
---|---|---|
42 | 42 |
<div class="card mb-1"> |
43 | 43 |
<h5 class="mb-0"> |
44 | 44 |
<a class="btn btn-link" data-toggle="collapse" data-target="#{$name}" |
45 |
aria-expanded="{$expanded}" onclick="return false;">
|
|
45 |
aria-expanded="{$expanded}"> |
|
46 | 46 |
<small> |
47 | 47 |
<span class="fa fa-fw fa-plus"></span> |
48 | 48 |
<span class="fa fa-fw fa-minus"></span> |
... | ... | |
50 | 50 |
</a> |
51 | 51 |
</h5> |
52 | 52 |
|
53 |
<div id="{$name}" class="collapse{$show}"> |
|
53 |
<div id="{$name}" class="collapse{$show}" title="multiplierContent">
|
|
54 | 54 |
<div class="card-body"> |
55 | 55 |
<div n:multiplier="$name" n:class="row, mb-2"> |
56 | 56 |
<input n:name="\App\Model\Repository\LineRepository::COLUMN_ID"> |
... | ... | |
85 | 85 |
</div> |
86 | 86 |
</div> |
87 | 87 |
{/form} |
88 |
|
|
89 |
<script> |
|
90 |
$(document).ready(function () |
|
91 |
{ |
|
92 |
showCollapse(); |
|
93 |
}); |
|
94 |
|
|
95 |
/** |
|
96 |
* Rozbalení stromu po dynamickém přidání prvků a znovunačtení snippetu |
|
97 |
*/ |
|
98 |
function showCollapse() |
|
99 |
{ |
|
100 |
// Pro každej multiplier content |
|
101 |
$('div[title="multiplierContent"]').each(function () |
|
102 |
{ |
|
103 |
// Pokud obsahuje div s třídou .row mb-2, znamená to, že je tam nějaký vstupní prvek |
|
104 |
let inputs = $(this).find(".row.mb-2"); |
|
105 |
if (inputs.length > 0) |
|
106 |
{ |
|
107 |
// Rozbalení seznamu |
|
108 |
let id = this.id; |
|
109 |
$(this).attr('class', 'collapse show'); |
|
110 |
$('a[data-target="#' + id + '"]').attr('aria-expanded', 'true'); |
|
111 |
|
|
112 |
// Rozbalení vrchního seznamu |
|
113 |
$(this).parents().each(function () |
|
114 |
{ |
|
115 |
let thisId = this.id.toString(); |
|
116 |
if (thisId !== "" && id.toString().includes(thisId)) |
|
117 |
{ |
|
118 |
$(this).attr('class', 'collapse show'); |
|
119 |
$('a[data-target="#' + thisId + '"]').attr('aria-expanded', 'true'); |
|
120 |
} |
|
121 |
}) |
|
122 |
} |
|
123 |
}) |
|
124 |
} |
|
125 |
</script> |
|
88 | 126 |
{/snippet} |
89 |
{/ifset} |
|
127 |
{/ifset} |
app/AdminModule/component/Transliteration/TransliterationDataEditForm.php | ||
---|---|---|
4 | 4 |
namespace App\AdminModule\Components; |
5 | 5 |
|
6 | 6 |
|
7 |
use App\Enum\EFlashMessage; |
|
7 | 8 |
use App\Model\Facade\TransliterationFacade; |
8 | 9 |
use App\Model\Repository\LineRepository; |
9 | 10 |
use App\Model\Repository\ObjectTypeRepository; |
... | ... | |
11 | 12 |
use App\Utils\Form; |
12 | 13 |
use Nette\Application\UI\Control; |
13 | 14 |
use Nette\Forms\Container; |
15 |
use Nette\Forms\Controls\SubmitButton; |
|
14 | 16 |
use WebChemistry\Forms\Controls\Multiplier; |
15 | 17 |
|
16 | 18 |
class TransliterationDataEditForm extends Control |
... | ... | |
21 | 23 |
/** @var array Pole objektů a typu povrch pro strom dat transliterace */ |
22 | 24 |
private $containers; |
23 | 25 |
|
24 |
/** @var array Pole s výchozími hodnotami při upravování */ |
|
25 |
private $defaults; |
|
26 |
|
|
27 | 26 |
/** |
28 | 27 |
* @var ObjectTypeRepository |
29 | 28 |
*/ |
... | ... | |
53 | 52 |
$containers = []; |
54 | 53 |
|
55 | 54 |
// Načtení typů objektů a typu povrchů do pole pro vykreslení v šabloně |
56 |
foreach ($objectTypes as $oId =>$objectType) |
|
55 |
foreach ($objectTypes as $oId => $objectType)
|
|
57 | 56 |
{ |
58 | 57 |
$containers[$oId]['title'] = $objectType; |
59 | 58 |
|
... | ... | |
71 | 70 |
$this->template->setFile(__DIR__ . '/TransliterationDataEditForm.latte'); |
72 | 71 |
|
73 | 72 |
$this->template->containers = $this->containers; |
74 |
$this->template->defaults = $this->defaults = $this->getDefaults();
|
|
73 |
$this->template->defaults = $this->getDefaults(); |
|
75 | 74 |
|
76 | 75 |
$this->template->render(); |
77 | 76 |
} |
... | ... | |
109 | 108 |
} |
110 | 109 |
} |
111 | 110 |
|
112 |
$defaults = $this->defaults === NULL ? $this->getDefaults() : $this->defaults; |
|
113 |
$form->setDefaults($defaults); |
|
111 |
$form->setDefaults($this->getDefaults()); |
|
114 | 112 |
|
115 | 113 |
$form->onSuccess[] = [$this, 'formSuccess']; |
116 | 114 |
$form->addSubmit('submit', 'Save'); |
... | ... | |
120 | 118 |
|
121 | 119 |
public function formSuccess(Form $form) |
122 | 120 |
{ |
123 |
$result = $this->transliterationFacade->saveTransliterationData($this->id, $form->getValues()); |
|
121 |
$result = $this->transliterationFacade->saveTransliterationData($this->id, $form); |
|
122 |
|
|
123 |
// Redirect musí být, jinak se v dynamických prvcích nenačtou IDčka |
|
124 |
// a vkládají se nové záznamy místo upravování stávajících |
|
125 |
if ($result) |
|
126 |
{ |
|
127 |
$this->presenter->flashMessage('Transliteration data were edited successfully.', EFlashMessage::SUCCESS); |
|
128 |
} else |
|
129 |
{ |
|
130 |
$this->presenter->flashMessage('Transliteration data were not edited.', EFlashMessage::ERROR); |
|
131 |
} |
|
132 |
$this->presenter->redirect('Transliteration:edit', ['id' => $this->id]); |
|
124 | 133 |
} |
125 | 134 |
|
126 | 135 |
/** |
... | ... | |
144 | 153 |
{ |
145 | 154 |
return $this->transliterationFacade->getTransliterationData($this->id); |
146 | 155 |
} |
156 |
|
|
157 |
return []; |
|
147 | 158 |
} |
148 | 159 |
} |
149 | 160 |
|
app/model/facade/TransliterationFacade.php | ||
---|---|---|
8 | 8 |
use App\Model\Repository\ObjectTypeRepository; |
9 | 9 |
use App\Model\Repository\SurfaceRepository; |
10 | 10 |
use App\Model\Repository\SurfaceTypeRepository; |
11 |
use Nette\Application\UI\Form; |
|
12 |
use Nette\Database\Context; |
|
11 | 13 |
use Nette\Database\Table\ActiveRow; |
12 | 14 |
use Nette\Utils\ArrayHash; |
13 | 15 |
|
... | ... | |
29 | 31 |
* @var SurfaceTypeRepository |
30 | 32 |
*/ |
31 | 33 |
private $surfaceTypeRepository; |
34 |
/** |
|
35 |
* @var Context |
|
36 |
*/ |
|
37 |
private $context; |
|
32 | 38 |
|
33 | 39 |
public function __construct(SurfaceRepository $surfaceRepository, |
34 | 40 |
LineRepository $lineRepository, |
35 | 41 |
ObjectTypeRepository $objectTypeRepository, |
36 |
SurfaceTypeRepository $surfaceTypeRepository |
|
42 |
SurfaceTypeRepository $surfaceTypeRepository, |
|
43 |
Context $context |
|
37 | 44 |
) |
38 | 45 |
{ |
39 | 46 |
$this->surfaceRepository = $surfaceRepository; |
40 | 47 |
$this->lineRepository = $lineRepository; |
41 | 48 |
$this->objectTypeRepository = $objectTypeRepository; |
42 | 49 |
$this->surfaceTypeRepository = $surfaceTypeRepository; |
50 |
$this->context = $context; |
|
43 | 51 |
} |
44 | 52 |
|
45 | 53 |
/** |
46 | 54 |
* Uloží data transliterace |
47 | 55 |
* |
48 | 56 |
* @param int|null $id : ID transliterace |
49 |
* @param ArrayHash $formValues |
|
57 |
* @param Form $form |
|
58 |
* @return bool |
|
50 | 59 |
*/ |
51 |
public function saveTransliterationData(int $id, ArrayHash $formValues)
|
|
60 |
public function saveTransliterationData(int $id, Form $form): bool
|
|
52 | 61 |
{ |
62 |
$formValues = $form->getValues(); |
|
63 |
|
|
64 |
$this->removeDeletedLines($id, $formValues); |
|
65 |
|
|
53 | 66 |
// Škaredý zanoření hodnot z formuláře v kontainerech :( |
54 | 67 |
foreach ($formValues as $objectTypeId => $surfaceContainers) |
55 | 68 |
{ |
... | ... | |
57 | 70 |
{ |
58 | 71 |
foreach ($inputs as $key => $values) |
59 | 72 |
{ |
60 |
$lineId = (int)$values->{LineRepository::COLUMN_ID}; |
|
61 |
|
|
62 |
// Editace již existující řádky |
|
63 |
if (!empty($lineId)) |
|
73 |
try |
|
64 | 74 |
{ |
65 |
$this->lineRepository->fetchById($lineId)->update($values);
|
|
75 |
$lineId = (int)$values->{LineRepository::COLUMN_ID};
|
|
66 | 76 |
|
67 |
} else |
|
68 |
{ |
|
69 |
$surface = $this->surfaceRepository->fetchSurface($id, $objectTypeId, $surfaceTypeId); |
|
77 |
// Editace již existující řádky |
|
78 |
if (!empty($lineId)) |
|
79 |
{ |
|
80 |
$this->lineRepository->fetchById($lineId)->update($values); |
|
70 | 81 |
|
71 |
if ($surface === FALSE)
|
|
82 |
} else
|
|
72 | 83 |
{ |
73 |
// TODO: předělat do repository |
|
74 |
$surface = $this->surfaceRepository->insert( |
|
75 |
[ |
|
76 |
SurfaceRepository::COLUMN_SURFACE_TYPE_ID => $surfaceTypeId, |
|
77 |
SurfaceRepository::COLUMN_OBJECT_TYPE_ID => $objectTypeId, |
|
78 |
SurfaceRepository::COLUMN_TRANSLITERATION_ID => $id |
|
79 |
] |
|
80 |
); |
|
84 |
$surface = $this->surfaceRepository->fetchSurface($id, $objectTypeId, $surfaceTypeId); |
|
85 |
|
|
86 |
if ($surface === FALSE) |
|
87 |
{ |
|
88 |
$surface = $this->surfaceRepository->insert( |
|
89 |
[ |
|
90 |
SurfaceRepository::COLUMN_SURFACE_TYPE_ID => $surfaceTypeId, |
|
91 |
SurfaceRepository::COLUMN_OBJECT_TYPE_ID => $objectTypeId, |
|
92 |
SurfaceRepository::COLUMN_TRANSLITERATION_ID => $id |
|
93 |
] |
|
94 |
); |
|
95 |
|
|
96 |
// Z nějakýho důvodu insert vrací počet vložených řádků a ne ActiveRow, i když je v tabulce PK, |
|
97 |
// proto se ID načítá takhle zvlášť |
|
98 |
$surfaceId = $this->context->getInsertId(SurfaceRepository::COLUMN_ID); |
|
99 |
} |
|
100 |
|
|
101 |
$values->{LineRepository::COLUMN_SURFACE_ID} = isset($surfaceId) ? $surfaceId : $surface->{SurfaceRepository::COLUMN_ID}; |
|
102 |
$this->lineRepository->insert($values); |
|
81 | 103 |
} |
82 |
|
|
83 |
$values->{LineRepository::COLUMN_SURFACE_ID} = $surface->{SurfaceRepository::COLUMN_ID}; |
|
84 |
$this->lineRepository->insert($values); |
|
104 |
} catch (\Exception $exception) |
|
105 |
{ |
|
106 |
\Tracy\Debugger::log('Nepodařilo se uložit data transliterace. Chyba: ' . $exception->getMessage(), 'transliteration-facade'); |
|
107 |
return FALSE; |
|
85 | 108 |
} |
86 | 109 |
} |
87 | 110 |
} |
88 | 111 |
} |
112 |
|
|
113 |
return TRUE; |
|
89 | 114 |
} |
90 | 115 |
|
91 | 116 |
/** |
92 | 117 |
* Vrací data transliterace pro formulář |
93 | 118 |
* |
94 | 119 |
* @param int $id : ID transliterace |
95 |
* @return array |
|
120 |
* @return array : pole výchozích hodnot pro formulář
|
|
96 | 121 |
*/ |
97 |
public function getTransliterationData(int $id) |
|
122 |
public function getTransliterationData(int $id): array
|
|
98 | 123 |
{ |
99 | 124 |
$surfaces = $this->surfaceRepository->findByTransliterationId($id)->fetchAll(); |
100 | 125 |
|
... | ... | |
105 | 130 |
{ |
106 | 131 |
// Načtení všech řádek |
107 | 132 |
$lineRows = $surface->related(LineRepository::TABLE_NAME, SurfaceRepository::COLUMN_ID)->fetchAll(); |
108 |
if ($lineRows === FALSE) |
|
133 |
if ($lineRows === FALSE || empty($lineRows))
|
|
109 | 134 |
{ |
110 | 135 |
continue; |
111 | 136 |
} |
... | ... | |
119 | 144 |
return $defaults; |
120 | 145 |
} |
121 | 146 |
|
147 |
/** |
|
148 |
* Odstraní smazané řádky z DB |
|
149 |
* |
|
150 |
* @param int $id : ID transliterace |
|
151 |
* @param ArrayHash $formValues : hodnoty z formuláře |
|
152 |
* @return int : počet smazaných řádek |
|
153 |
*/ |
|
154 |
public function removeDeletedLines(int $id, ArrayHash $formValues) |
|
155 |
{ |
|
156 |
$deletedIds = $this->getDeletedLineIds($id, $formValues); |
|
157 |
|
|
158 |
return $this->lineRepository->deleteLines($deletedIds); |
|
159 |
} |
|
160 |
|
|
161 |
/** |
|
162 |
* Vrací ID smazaných řádek transliterace |
|
163 |
* |
|
164 |
* @param int $id : ID transliterace |
|
165 |
* @param ArrayHash $formValues : hodnoty z formuláře |
|
166 |
* @return array |
|
167 |
*/ |
|
168 |
private function getDeletedLineIds(int $id, ArrayHash $formValues) |
|
169 |
{ |
|
170 |
$deletedIds = []; |
|
171 |
$oldLines = $this->getTransliterationData($id); |
|
172 |
|
|
173 |
// Tohle je fakt nádhera :( |
|
174 |
foreach ($oldLines as $objectTypeId => $surfaceContainers) |
|
175 |
{ |
|
176 |
foreach ($surfaceContainers as $surfaceTypeId => $activeRows) |
|
177 |
{ |
|
178 |
foreach ($activeRows as $activeRow) |
|
179 |
{ |
|
180 |
if (isset($formValues[$objectTypeId][$surfaceTypeId])) |
|
181 |
{ |
|
182 |
$oldLineFound = FALSE; |
|
183 |
foreach ($formValues[$objectTypeId][$surfaceTypeId] as $array) |
|
184 |
{ |
|
185 |
if ($array[LineRepository::COLUMN_ID] == $activeRow->{LineRepository::COLUMN_ID}) |
|
186 |
{ |
|
187 |
$oldLineFound = TRUE; |
|
188 |
break; |
|
189 |
} |
|
190 |
} |
|
191 |
|
|
192 |
if (!$oldLineFound) |
|
193 |
{ |
|
194 |
$deletedIds[] = $activeRow->{LineRepository::COLUMN_ID}; |
|
195 |
} |
|
196 |
} |
|
197 |
} |
|
198 |
} |
|
199 |
} |
|
200 |
|
|
201 |
return $deletedIds; |
|
202 |
} |
|
122 | 203 |
/** |
123 | 204 |
* Vrací název inputu pro formulář |
124 | 205 |
* |
125 | 206 |
* @param string|NULL $name |
126 | 207 |
* @return string |
127 | 208 |
*/ |
128 |
public function getInputName(string $name) |
|
209 |
public function getInputName(string $name): string
|
|
129 | 210 |
{ |
130 | 211 |
return str_replace(' ', '', $name); |
131 | 212 |
} |
app/model/repository/LineRepository.php | ||
---|---|---|
86 | 86 |
return $this->findBy([self::COLUMN_ID => $id])->fetch(); |
87 | 87 |
} |
88 | 88 |
|
89 |
/** |
|
90 |
* Vrací počet smazaných řádek |
|
91 |
* |
|
92 |
* @param array $lineIds |
|
93 |
* @return int |
|
94 |
*/ |
|
95 |
public function deleteLines(array $lineIds) : int |
|
96 |
{ |
|
97 |
return $this->findBy([self::COLUMN_ID => $lineIds])->delete(); |
|
98 |
} |
|
99 |
|
|
89 | 100 |
} |
Také k dispozici: Unified diff
Re #7505 úprava a mazání řádek