Projekt

Obecné

Profil

Stáhnout (6.95 KB) Statistiky
| Větev: | Tag: | Revize:
1
<?php
2

    
3

    
4
namespace App\Model\Facade;
5

    
6

    
7
use App\Model\Repository\LineRepository;
8
use App\Model\Repository\ObjectTypeRepository;
9
use App\Model\Repository\SurfaceRepository;
10
use App\Model\Repository\SurfaceTypeRepository;
11
use Nette\Application\UI\Form;
12
use Nette\Database\Context;
13
use Nette\Database\Table\ActiveRow;
14
use Nette\Utils\ArrayHash;
15

    
16
class TransliterationFacade
17
{
18
    /**
19
     * @var SurfaceRepository
20
     */
21
    private $surfaceRepository;
22
    /**
23
     * @var LineRepository
24
     */
25
    private $lineRepository;
26
    /**
27
     * @var ObjectTypeRepository
28
     */
29
    private $objectTypeRepository;
30
    /**
31
     * @var SurfaceTypeRepository
32
     */
33
    private $surfaceTypeRepository;
34
    /**
35
     * @var Context
36
     */
37
    private $context;
38

    
39
    public function __construct(SurfaceRepository $surfaceRepository,
40
                                LineRepository $lineRepository,
41
                                ObjectTypeRepository $objectTypeRepository,
42
                                SurfaceTypeRepository $surfaceTypeRepository,
43
                                Context $context
44
    )
45
    {
46
        $this->surfaceRepository = $surfaceRepository;
47
        $this->lineRepository = $lineRepository;
48
        $this->objectTypeRepository = $objectTypeRepository;
49
        $this->surfaceTypeRepository = $surfaceTypeRepository;
50
        $this->context = $context;
51
    }
52

    
53
    /**
54
     * Uloží data transliterace
55
     *
56
     * @param int|null $id : ID transliterace
57
     * @param Form $form
58
     * @return bool
59
     */
60
    public function saveTransliterationData(int $id, Form $form): bool
61
    {
62
        $formValues = $form->getValues();
63

    
64
        $this->removeDeletedLines($id, $formValues);
65

    
66
        // Škaredý zanoření hodnot z formuláře v kontainerech :(
67
        foreach ($formValues as $objectTypeId => $surfaceContainers)
68
        {
69
            foreach ($surfaceContainers as $surfaceTypeId => $inputs)
70
            {
71
                foreach ($inputs as $key => $values)
72
                {
73
                    try
74
                    {
75
                        $lineId = (int)$values->{LineRepository::COLUMN_ID};
76

    
77
                        // Editace již existující řádky
78
                        if (!empty($lineId))
79
                        {
80
                            $this->lineRepository->fetchById($lineId)->update($values);
81

    
82
                        } else
83
                        {
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);
103
                        }
104
                    } catch (\Exception $exception)
105
                    {
106
                        \Tracy\Debugger::log('Nepodařilo se uložit data transliterace. Chyba: ' . $exception->getMessage(), 'transliteration-facade');
107
                        return FALSE;
108
                    }
109
                }
110
            }
111
        }
112

    
113
        return TRUE;
114
    }
115

    
116
    /**
117
     * Vrací data transliterace pro formulář
118
     *
119
     * @param int $id : ID transliterace
120
     * @return array : pole výchozích hodnot pro formulář
121
     */
122
    public function getTransliterationData(int $id): array
123
    {
124
        $surfaces = $this->surfaceRepository->findByTransliterationId($id)->fetchAll();
125

    
126
        $defaults = array();
127

    
128
        /** @var ActiveRow $surface */
129
        foreach ($surfaces as $surface)
130
        {
131
            // Načtení všech řádek
132
            $lineRows = $surface->related(LineRepository::TABLE_NAME, SurfaceRepository::COLUMN_ID)->fetchAll();
133
            if ($lineRows === FALSE || empty($lineRows))
134
            {
135
                continue;
136
            }
137

    
138
            $objectId = $surface->{SurfaceRepository::COLUMN_OBJECT_TYPE_ID};
139
            $surfaceId = $surface->{SurfaceRepository::COLUMN_SURFACE_TYPE_ID};
140

    
141
            $defaults[$objectId][$surfaceId] = $lineRows;
142
        }
143

    
144
        return $defaults;
145
    }
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
    }
203
    /**
204
     * Vrací název inputu pro formulář
205
     *
206
     * @param string|NULL $name
207
     * @return string
208
     */
209
    public function getInputName(string $name): string
210
    {
211
        return str_replace(' ', '', $name);
212
    }
213
}
(1-1/3)