Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 3882f5d3

Přidáno uživatelem Filip Jani před téměř 6 roky(ů)

Re #7505 úprava a mazání řádek

Zobrazit rozdíly:

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
    }

Také k dispozici: Unified diff