Projekt

Obecné

Profil

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

    
3

    
4
namespace App\AdminModule\Components;
5

    
6

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

    
17
class TransliterationGrid extends DataGrid
18
{
19
    /**
20
     * @var TransliterationRepository
21
     */
22
    private $transliterationRepository;
23
    /**
24
     * @var BookRepository
25
     */
26
    private $bookRepository;
27
    /**
28
     * @var MuseumRepository
29
     */
30
    private $museumRepository;
31
    /**
32
     * @var OriginRepository
33
     */
34
    private $originRepository;
35
    /**
36
     * @var BookTypeRepository
37
     */
38
    private $bookTypeRepository;
39

    
40
    public function __construct(TransliterationRepository $transliterationRepository,
41
                                BookRepository $bookRepository,
42
                                MuseumRepository $museumRepository,
43
                                OriginRepository $originRepository,
44
                                BookTypeRepository $bookTypeRepository
45
    )
46
    {
47
        $this->transliterationRepository = $transliterationRepository;
48
        $this->bookRepository = $bookRepository;
49
        $this->museumRepository = $museumRepository;
50
        $this->originRepository = $originRepository;
51
        $this->bookTypeRepository = $bookTypeRepository;
52

    
53
        parent::__construct(FALSE);
54
    }
55

    
56
    /**
57
     * Abstraktní metoda, slouží k nastavení primárního klíče a nastavení datasource
58
     *  1. $this->setPrimaryKey();
59
     *  2. $this->setDataSource();
60
     *
61
     * @throws DataGridException
62
     */
63
    public function init()
64
    {
65
        $this->setPrimaryKey(TransliterationRepository::COLUMN_ID);
66
        $this->setDataSource($this->transliterationRepository->findAll());
67
    }
68

    
69
    /**
70
     * Definice sloupečků, akcí, vyhledávácích filtrů gridu
71
     *
72
     * @throws DataGridException
73
     */
74
    public function define()
75
    {
76
        $bookRepository = $this->bookRepository;
77
        $museumRepository = $this->museumRepository;
78

    
79
        // ==================
80
        // Definice sloupečků
81
        // ==================
82
        $this->addColumnNumber(TransliterationRepository::COLUMN_ID, 'ID')->setDefaultHide(TRUE);
83
        $this->addColumnLink(TransliterationRepository::COLUMN_BOOK_ID, 'Book')
84
            ->setRenderer(function (ActiveRow $activeRow)
85
            {
86
                return $this->getRenderer(
87
                    $activeRow,
88
                    BookRepository::TABLE_NAME,
89
                    BookRepository::COLUMN_ID,
90
                    BookRepository::COLUMN_BOOK_ABREV,
91
                    TransliterationRepository::COLUMN_BOOK_ID
92
                );
93
            });
94
        $this->addColumnNumber(TransliterationRepository::COLUMN_CHAPTER, 'Chapter');
95
        $this->addColumnLink(TransliterationRepository::COLUMN_MUSEUM_ID, 'Museum')
96
            ->setRenderer(function (ActiveRow $activeRow)
97
            {
98
                return $this->getRenderer(
99
                    $activeRow,
100
                    MuseumRepository::TABLE_NAME,
101
                    MuseumRepository::COLUMN_ID,
102
                    MuseumRepository::COLUMN_NAME,
103
                    TransliterationRepository::COLUMN_MUSEUM_ID
104
                );
105
            });
106
        $this->addColumnText(TransliterationRepository::COLUMN_MUSEUM_NO, 'Museum No');
107
        $this->addColumnLink(TransliterationRepository::COLUMN_ORIGIN_ID, 'Origin')
108
            ->setRenderer(function (ActiveRow $activeRow)
109
            {
110
                return $this->getRenderer(
111
                    $activeRow,
112
                    OriginRepository::TABLE_NAME,
113
                    OriginRepository::COLUMN_ID,
114
                    OriginRepository::COLUMN_ORIGIN,
115
                    TransliterationRepository::COLUMN_ORIGIN_ID
116
                );
117
            });
118
        $this->addColumnLink(TransliterationRepository::COLUMN_BOOK_TYPE_ID, 'Book Type')
119
            ->setRenderer(function (ActiveRow $activeRow)
120
            {
121
                return $this->getRenderer(
122
                    $activeRow,
123
                    BookTypeRepository::TABLE_NAME,
124
                    BookTypeRepository::COLUMN_ID,
125
                    BookTypeRepository::COLUMN_BOOK_TYPE,
126
                    TransliterationRepository::COLUMN_BOOK_TYPE_ID
127
                );
128
            });
129
        $this->addColumnText(TransliterationRepository::COLUMN_REG_NO, 'Reg No');
130
        $this->addColumnText(TransliterationRepository::COLUMN_DATE, 'Date');
131

    
132
        // ===============
133
        // Definice filtrů
134
        // ===============
135
        $this->addFilterText(TransliterationRepository::COLUMN_BOOK_ID, 'Book')
136
            ->setCondition(function (Selection $selection, $value) use ($bookRepository)
137
            {
138
                $bookIds = $bookRepository->getBooksLikeBookAbbrev($value)->fetchField(BookRepository::COLUMN_ID);
139
                $bookIds = $bookIds ? $bookIds : NULL;
140

    
141
                $selection->where(BookRepository::COLUMN_ID, $bookIds);
142
            });
143
        $this->addFilterText(TransliterationRepository::COLUMN_CHAPTER, 'Chapter');
144
        $this->addFilterText(TransliterationRepository::COLUMN_MUSEUM_ID, 'Museum')
145
            ->setCondition(function (Selection $selection, $value) use ($museumRepository)
146
            {
147
                $museumIds = $museumRepository->getMuseumsLikeName($value)->fetchField(MuseumRepository::COLUMN_ID);
148
                $museumIds = $museumIds ? $museumIds : NULL;
149

    
150
                $selection->where(MuseumRepository::COLUMN_ID, $museumIds);
151
            });
152
        $this->addFilterText(TransliterationRepository::COLUMN_MUSEUM_NO, 'Museum No');
153
        $this->addFilterSelect(TransliterationRepository::COLUMN_ORIGIN_ID, 'Origin', $this->getOriginFilterArray());
154
        $this->addFilterSelect(TransliterationRepository::COLUMN_BOOK_TYPE_ID, 'Book Type', $this->getBookTypeFilterArray());
155
        $this->addFilterText(TransliterationRepository::COLUMN_REG_NO, 'Reg No');
156
        $this->addFilterText(TransliterationRepository::COLUMN_DATE, 'Date');
157

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

    
161
        // =============
162
        // Definice akcí
163
        // =============
164
//        $this->addAction('edit', 'edit', 'Transliteration:edit', ['id' => TransliterationRepository::COLUMN_ID])
165
//            ->setTitle('Edit');
166

    
167
        $this->addAction('delete', 'delete', 'deleteTransliteration!', ['id' => TransliterationRepository::COLUMN_ID])
168
            ->setConfirm('Do you really want to delete transliteration?')
169
            ->setTitle('Delete')
170
            ->setClass('btn btn-xs btn-danger ajax');
171
    }
172

    
173
    /**
174
     * Vrací pole s možnostmi pro combobox filtr místa původu
175
     *
176
     * @return array
177
     */
178
    private function getOriginFilterArray()
179
    {
180
        $array = $this->originRepository->findAll()->fetchPairs(OriginRepository::COLUMN_ID, OriginRepository::COLUMN_ORIGIN);
181
        sort($array);
182
        return $array;
183
    }
184

    
185
    /**
186
     * Vrací pole s možnostmi pro combobox filtr typu knihy
187
     *
188
     * @return array
189
     */
190
    private function getBookTypeFilterArray()
191
    {
192
        $array = $this->bookTypeRepository->findAll()->fetchPairs(BookTypeRepository::COLUMN_ID, BookTypeRepository::COLUMN_BOOK_TYPE);
193
        sort($array);
194
        return $array;
195
    }
196

    
197
    /**
198
     * Vrací renderer pro vlastní zobrazení názvu místo ID cizího klíče v gridu
199
     *
200
     * @param ActiveRow $activeRow
201
     * @param string $key
202
     * @param string $throughColumn
203
     * @param string $titleColumn
204
     * @param string $idColumn
205
     * @return string
206
     * @throws \Nette\Application\UI\InvalidLinkException
207
     */
208
    private function getRenderer(ActiveRow $activeRow, string $key, string $throughColumn, string $titleColumn, string $idColumn)
209
    {
210
        $ref = $activeRow->ref($key, $throughColumn);
211

    
212
        if ($ref)
213
        {
214
            $title = $ref->{$titleColumn};
215
            return $this->getRendererWithLink($activeRow, $title, 'Book:edit', $activeRow->{$idColumn});
216
        } else
217
        {
218
            return "";
219
        }
220
    }
221
}
222

    
223
interface ITransliterationGridFactory
224
{
225
    /**
226
     * @return TransliterationGrid
227
     */
228
    public function create();
229
}
    (1-1/1)