Projekt

Obecné

Profil

« Předchozí | Další » 

Revize f0d8e0f6

Přidáno uživatelem Jan Šedivý před téměř 6 roky(ů)

Re #7511 Export vyfiltrovaných textů v gridu

Zobrazit rozdíly:

app/AdminModule/component/Transliteration/TransliterationGrid.php
4 4
namespace App\AdminModule\Components;
5 5

  
6 6

  
7
use App\Enum\EFlashMessage;
8
use App\Enum\ESurfaceTypes;
7 9
use App\Model\Repository\BookRepository;
8 10
use App\Model\Repository\BookTypeRepository;
11
use App\Model\Repository\LineRepository;
9 12
use App\Model\Repository\MuseumRepository;
10 13
use App\Model\Repository\OriginRepository;
11 14
use App\Model\Repository\TransliterationRepository;
......
36 39
     * @var BookTypeRepository
37 40
     */
38 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';
39 49

  
40 50
    public function __construct(TransliterationRepository $transliterationRepository,
41 51
                                BookRepository $bookRepository,
42 52
                                MuseumRepository $museumRepository,
43 53
                                OriginRepository $originRepository,
44
                                BookTypeRepository $bookTypeRepository
54
                                BookTypeRepository $bookTypeRepository,
55
                                LineRepository $lineRepository
45 56
    )
46 57
    {
47 58
        $this->transliterationRepository = $transliterationRepository;
......
49 60
        $this->museumRepository = $museumRepository;
50 61
        $this->originRepository = $originRepository;
51 62
        $this->bookTypeRepository = $bookTypeRepository;
63
        $this->lineRepository = $lineRepository;
52 64

  
53 65
        parent::__construct(FALSE);
54 66
    }
......
172 184
            ->setConfirm('Do you really want to delete transliteration?')
173 185
            ->setTitle('Delete')
174 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);
175 195
    }
176 196

  
177 197
    /**
......
221 241
            return "";
222 242
        }
223 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
    }
224 328
}
225 329

  
226 330
interface ITransliterationGridFactory
app/enum/ESurfaceTypes.php
1
<?php
2

  
3
namespace App\Enum;
4

  
5

  
6
class ESurfaceTypes
7
{
8
    const REVERSE = 'reverse';
9
    const UPPER_EDGE = 'upper edge';
10
    const LOWER_EDGE = 'lower edge';
11
    const LEFT_EDGE = 'left edge';
12
    const RIGHT_EDGE = 'right edge';
13
    const SEAL_IMPRESSION_1 = 'seal impression 1';
14
    const SEAL_IMPRESSION_2 = 'seal impression 2';
15
    const SEAL_IMPRESSION_3 = 'seal impression 3';
16
    const SEAL_IMPRESSION_4 = 'seal impression 4';
17
    const SEAL_IMPRESSION_5 = 'seal impression 5';
18
    const SEAL_IMPRESSION_6 = 'seal impression 6';
19
    const SEAL_IMPRESSION_7 = 'seal impression 7';
20
    const SEAL_IMPRESSION_8 = 'seal impression 8';
21
    const OBVERSE = 'obverse';
22

  
23
    public static $exportFileAbbrev = [
24
        self::REVERSE => 'rev.',
25
        self::UPPER_EDGE => 'u.e.',
26
        self::LOWER_EDGE => 'lo.e.',
27
        self::LEFT_EDGE => 'le.e.',
28
        self::RIGHT_EDGE => 'r.e.',
29
        self::SEAL_IMPRESSION_1 => 's.i.1',
30
        self::SEAL_IMPRESSION_2 => 's.i.2',
31
        self::SEAL_IMPRESSION_3 => 's.i.3',
32
        self::SEAL_IMPRESSION_4 => 's.i.4',
33
        self::SEAL_IMPRESSION_5 => 's.i.5',
34
        self::SEAL_IMPRESSION_6 => 's.i.6',
35
        self::SEAL_IMPRESSION_7 => 's.i.7',
36
        self::SEAL_IMPRESSION_8 => 's.i.8',
37
    ];
38

  
39
    public static function getTypeAbbrevForExport($type)
40
    {
41
        if($type == self::OBVERSE)
42
        {
43
            return '';
44
        }
45

  
46
        return "&" . self::$exportFileAbbrev[$type] . "$";
47
    }
48
}

Také k dispozici: Unified diff