Projekt

Obecné

Profil

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

    
3

    
4
namespace App\AdminModule\Components;
5

    
6

    
7
use App\Enum\EFlashMessage;
8
use App\Enum\ESurfaceTypes;
9
use App\Model\Repository\BookRepository;
10
use App\Model\Repository\BookTypeRepository;
11
use App\Model\Repository\LineRepository;
12
use App\Model\Repository\MuseumRepository;
13
use App\Model\Repository\OriginRepository;
14
use App\Model\Repository\TransliterationRepository;
15
use App\Utils\DataGrid\DataGrid;
16
use Nette\Database\Table\ActiveRow;
17
use Nette\Database\Table\Selection;
18
use Ublaboo\DataGrid\Exception\DataGridException;
19

    
20
class TransliterationGrid extends DataGrid
21
{
22
    /**
23
     * @var TransliterationRepository
24
     */
25
    private $transliterationRepository;
26
    /**
27
     * @var BookRepository
28
     */
29
    private $bookRepository;
30
    /**
31
     * @var MuseumRepository
32
     */
33
    private $museumRepository;
34
    /**
35
     * @var OriginRepository
36
     */
37
    private $originRepository;
38
    /**
39
     * @var BookTypeRepository
40
     */
41
    private $bookTypeRepository;
42
    /**
43
     * @var LineRepository
44
     */
45
    private $lineRepository;
46

    
47
    const EXPORT_FILE_PATH = __DIR__ . '/../../../../temp/export/';
48
    const EXPORT_FILE_TYPE = '.txt';
49

    
50
    public function __construct(TransliterationRepository $transliterationRepository,
51
                                BookRepository $bookRepository,
52
                                MuseumRepository $museumRepository,
53
                                OriginRepository $originRepository,
54
                                BookTypeRepository $bookTypeRepository,
55
                                LineRepository $lineRepository
56
    )
57
    {
58
        $this->transliterationRepository = $transliterationRepository;
59
        $this->bookRepository = $bookRepository;
60
        $this->museumRepository = $museumRepository;
61
        $this->originRepository = $originRepository;
62
        $this->bookTypeRepository = $bookTypeRepository;
63
        $this->lineRepository = $lineRepository;
64

    
65
        parent::__construct(FALSE);
66
    }
67

    
68
    /**
69
     * Abstraktní metoda, slouží k nastavení primárního klíče a nastavení datasource
70
     *  1. $this->setPrimaryKey();
71
     *  2. $this->setDataSource();
72
     *
73
     * @throws DataGridException
74
     */
75
    public function init()
76
    {
77
        $this->setPrimaryKey(TransliterationRepository::COLUMN_ID);
78
        $this->setDataSource($this->transliterationRepository->findAll());
79
    }
80

    
81
    /**
82
     * Definice sloupečků, akcí, vyhledávácích filtrů gridu
83
     *
84
     * @throws DataGridException
85
     */
86
    public function define()
87
    {
88
        $bookRepository = $this->bookRepository;
89
        $museumRepository = $this->museumRepository;
90

    
91
        // ==================
92
        // Definice sloupečků
93
        // ==================
94
        $this->addColumnNumber(TransliterationRepository::COLUMN_ID, 'ID')->setDefaultHide(TRUE);
95
        $this->addColumnLink(TransliterationRepository::COLUMN_BOOK_ID, 'Book')
96
            ->setRenderer(function (ActiveRow $activeRow)
97
            {
98
                return $this->getRenderer(
99
                    $activeRow,
100
                    BookRepository::TABLE_NAME,
101
                    BookRepository::COLUMN_ID,
102
                    BookRepository::COLUMN_BOOK_ABREV,
103
                    TransliterationRepository::COLUMN_BOOK_ID,
104
                    'Book:edit'
105
                );
106
            });
107
        $this->addColumnNumber(TransliterationRepository::COLUMN_CHAPTER, 'Chapter');
108
        $this->addColumnLink(TransliterationRepository::COLUMN_MUSEUM_ID, 'Museum')
109
            ->setRenderer(function (ActiveRow $activeRow)
110
            {
111
                return $this->getRenderer(
112
                    $activeRow,
113
                    MuseumRepository::TABLE_NAME,
114
                    MuseumRepository::COLUMN_ID,
115
                    MuseumRepository::COLUMN_NAME,
116
                    TransliterationRepository::COLUMN_MUSEUM_ID,
117
                    'Museum:edit'
118
                );
119
            });
120
        $this->addColumnText(TransliterationRepository::COLUMN_MUSEUM_NO, 'Museum No');
121
        $this->addColumnLink(TransliterationRepository::COLUMN_ORIGIN_ID, 'Origin')
122
            ->setRenderer(function (ActiveRow $activeRow)
123
            {
124
                return $this->getRenderer(
125
                    $activeRow,
126
                    OriginRepository::TABLE_NAME,
127
                    OriginRepository::COLUMN_ID,
128
                    OriginRepository::COLUMN_ORIGIN,
129
                    TransliterationRepository::COLUMN_ORIGIN_ID,
130
                    'Origin:edit'
131
                );
132
            });
133
        $this->addColumnLink(TransliterationRepository::COLUMN_BOOK_TYPE_ID, 'Book Type')
134
            ->setRenderer(function (ActiveRow $activeRow)
135
            {
136
                return $this->getRenderer(
137
                    $activeRow,
138
                    BookTypeRepository::TABLE_NAME,
139
                    BookTypeRepository::COLUMN_ID,
140
                    BookTypeRepository::COLUMN_BOOK_TYPE,
141
                    TransliterationRepository::COLUMN_BOOK_TYPE_ID,
142
                    'BookType:edit'
143
                );
144
            });
145
        $this->addColumnText(TransliterationRepository::COLUMN_REG_NO, 'Reg No');
146
        $this->addColumnText(TransliterationRepository::COLUMN_DATE, 'Date');
147

    
148
        // ===============
149
        // Definice filtrů
150
        // ===============
151
        $this->addFilterText(TransliterationRepository::COLUMN_BOOK_ID, 'Book')
152
            ->setCondition(function (Selection $selection, $value) use ($bookRepository)
153
            {
154
                $bookIds = $bookRepository->getBooksLikeBookAbbrev($value)->fetchField(BookRepository::COLUMN_ID);
155
                $bookIds = $bookIds ? $bookIds : NULL;
156

    
157
                $selection->where(BookRepository::COLUMN_ID, $bookIds);
158
            });
159
        $this->addFilterText(TransliterationRepository::COLUMN_CHAPTER, 'Chapter');
160
        $this->addFilterText(TransliterationRepository::COLUMN_MUSEUM_ID, 'Museum')
161
            ->setCondition(function (Selection $selection, $value) use ($museumRepository)
162
            {
163
                $museumIds = $museumRepository->getMuseumsLikeName($value)->fetchField(MuseumRepository::COLUMN_ID);
164
                $museumIds = $museumIds ? $museumIds : NULL;
165

    
166
                $selection->where(MuseumRepository::COLUMN_ID, $museumIds);
167
            });
168
        $this->addFilterText(TransliterationRepository::COLUMN_MUSEUM_NO, 'Museum No');
169
        $this->addFilterSelect(TransliterationRepository::COLUMN_ORIGIN_ID, 'Origin', $this->getOriginFilterArray());
170
        $this->addFilterSelect(TransliterationRepository::COLUMN_BOOK_TYPE_ID, 'Book Type', $this->getBookTypeFilterArray());
171
        $this->addFilterText(TransliterationRepository::COLUMN_REG_NO, 'Reg No');
172
        $this->addFilterText(TransliterationRepository::COLUMN_DATE, 'Date');
173

    
174
        // Zakázání zobrazení všech položek, protože jinak se grid sekne a nelze resetovat
175
        $this->setItemsPerPageList([10, 20, 50, 100], FALSE);
176

    
177
        // =============
178
        // Definice akcí
179
        // =============
180
        $this->addAction('edit', 'edit', 'Transliteration:edit', ['id' => TransliterationRepository::COLUMN_ID])
181
            ->setTitle('Edit');
182

    
183
        $this->addAction('delete', 'delete', 'deleteTransliteration!', ['id' => TransliterationRepository::COLUMN_ID])
184
            ->setConfirm('Do you really want to delete transliteration?')
185
            ->setTitle('Delete')
186
            ->setClass('btn btn-xs btn-danger ajax');
187

    
188
        $this->addExportCallback(
189
            'Export dat',
190
            function ($dataSource, $grid)
191
            {
192
                $this->exportData($dataSource, $grid);
193
            },
194
            TRUE);
195
    }
196

    
197
    /**
198
     * Vrací pole s možnostmi pro combobox filtr místa původu
199
     *
200
     * @return array
201
     */
202
    private function getOriginFilterArray()
203
    {
204
        $array = $this->originRepository->findAll()->order(OriginRepository::COLUMN_ORIGIN)->fetchPairs(OriginRepository::COLUMN_ID, OriginRepository::COLUMN_ORIGIN);
205
        return $array;
206
    }
207

    
208
    /**
209
     * Vrací pole s možnostmi pro combobox filtr typu knihy
210
     *
211
     * @return array
212
     */
213
    private function getBookTypeFilterArray()
214
    {
215
        $array = $this->bookTypeRepository->findAll()->order(BookTypeRepository::COLUMN_BOOK_TYPE)->fetchPairs(BookTypeRepository::COLUMN_ID, BookTypeRepository::COLUMN_BOOK_TYPE);
216
        return $array;
217
    }
218

    
219
    /**
220
     * Vrací renderer pro vlastní zobrazení názvu místo ID cizího klíče v gridu
221
     *
222
     * @param ActiveRow $activeRow
223
     * @param string $key
224
     * @param string $throughColumn
225
     * @param string $titleColumn
226
     * @param string $idColumn
227
     * @param string $destination
228
     * @return string
229
     * @throws \Nette\Application\UI\InvalidLinkException
230
     */
231
    private function getRenderer(ActiveRow $activeRow, string $key, string $throughColumn, string $titleColumn, string $idColumn, string $destination)
232
    {
233
        $ref = $activeRow->ref($key, $throughColumn);
234

    
235
        if ($ref)
236
        {
237
            $title = $ref->{$titleColumn};
238
            return $this->getRendererWithLink($activeRow, $title, $destination, $activeRow->{$idColumn});
239
        } else
240
        {
241
            return "";
242
        }
243
    }
244

    
245
    /**
246
     * @param $dataSource
247
     * @param DataGrid $grid
248
     */
249
    private function exportData($dataSource, $grid)
250
    {
251
        $exportData = [];
252
        /** @var ActiveRow $activeRow */
253
        foreach ($dataSource as $activeRow)
254
        {
255
            $lines = $this->lineRepository->getAllLinesForTransliteration($activeRow->getPrimary());
256
            $chapterLines = [];
257
            foreach ($lines as $line)
258
            {
259
                $chapterLines[$line->object_type][$line->surface_type][] = array('transliteration' => $line->transliteration, 'line_number' => $line->line_number);
260
            }
261
            $exportData[] = [
262
                'bookName' => $activeRow->ref('id_book')->book_abrev,
263
                'chapter' => $activeRow->chapter,
264
                'transliterationData' => $chapterLines
265
            ];
266
        }
267

    
268
        $uniqueBooks = array_unique(array_map(function ($i) { return $i['bookName']; }, $exportData));
269

    
270
        $currentDate = new \DateTime();
271
        $exportFileName =  'text-export-' .
272
            $currentDate->format('Y-m-d-H-i-s') .
273
            self::EXPORT_FILE_TYPE;
274
        try
275
        {
276
            if (!file_exists(self::EXPORT_FILE_PATH)) {
277
                mkdir(self::EXPORT_FILE_PATH, 0777, true);
278
            }
279
            $exportFile = fopen(self::EXPORT_FILE_PATH . $exportFileName, 'w');
280
            foreach ($uniqueBooks as $book)
281
            {
282
                $bookData = [];
283
                foreach ($exportData as $data)
284
                {
285
                    if ($data['bookName'] == $book)
286
                    {
287
                        $bookData[] = $data;
288
                    }
289
                }
290
                fwrite($exportFile, "|" . $book . "\n");
291
                foreach ($bookData as $chapterData)
292
                {
293
                    fwrite($exportFile, "|" . $chapterData['chapter'] . "\n");
294
                    foreach ($chapterData['transliterationData'] as $surfaces)
295
                    {
296
                        foreach ($surfaces as $type => $surface)
297
                        {
298
                            for ($i = 0; $i < sizeof($surface); $i++)
299
                            {
300
                                $line =
301
                                    $surface[$i][LineRepository::COLUMN_LINE_NUMBER] .
302
                                    " " .
303
                                    $surface[$i][LineRepository::COLUMN_TRANSLITERATION];
304
                                if($i == 0 && $type != ESurfaceTypes::OBVERSE) {
305
                                    $line .= " " . ESurfaceTypes::getTypeAbbrevForExport($type);
306
                                }
307
                                fwrite($exportFile, $line . "\n");
308
                            }
309
                        }
310
                    }
311
                }
312
                fwrite($exportFile, "\n");
313
            }
314

    
315
            fclose($exportFile);
316
            header("Content-type: text/plain");
317
            header("Content-disposition: attachment; filename = " . $exportFileName);
318
            readfile(self::EXPORT_FILE_PATH . $exportFileName);
319
            die();
320
        }
321
        catch (\Exception $e)
322
        {
323
            $this->presenter->flashMessage('There was an error creating the export file', EFlashMessage::ERROR);
324
        }
325

    
326
        $this->presenter->flashMessage('There was an error downloading the export file', EFlashMessage::ERROR);
327
    }
328
}
329

    
330
interface ITransliterationGridFactory
331
{
332
    /**
333
     * @return TransliterationGrid
334
     */
335
    public function create();
336
}
(5-5/5)