Projekt

Obecné

Profil

Stáhnout (3.3 KB) Statistiky
| Větev: | Tag: | Revize:
1 d5a16eb6 Filip Jani
<?php
2
3
4
namespace App\AdminModule\Components;
5
6
7 2190aaa4 Filip Jani
use App\Model\Repository\BookRepository;
8
use App\Model\Repository\RoleRepository;
9 d5a16eb6 Filip Jani
use App\Model\Repository\UserRepository;
10 2190aaa4 Filip Jani
use App\Model\Repository\UserRoleRepository;
11 d5a16eb6 Filip Jani
use App\Utils\DataGrid\DataGrid;
12 2190aaa4 Filip Jani
use Nette\Database\Table\ActiveRow;
13
use Nette\Database\Table\Selection;
14 d5a16eb6 Filip Jani
use Ublaboo\DataGrid\Exception\DataGridException;
15
16
class UserGrid extends DataGrid
17
{
18
    /**
19
     * @var UserRepository
20
     */
21
    private $userRepository;
22 2190aaa4 Filip Jani
    /**
23
     * @var RoleRepository
24
     */
25
    private $roleRepository;
26 d5a16eb6 Filip Jani
27 2190aaa4 Filip Jani
    public function __construct(UserRepository $userRepository,
28
                                RoleRepository $roleRepository)
29 d5a16eb6 Filip Jani
    {
30
        $this->userRepository = $userRepository;
31 2190aaa4 Filip Jani
        $this->roleRepository = $roleRepository;
32 d5a16eb6 Filip Jani
33 c93cf853 Petr Lukašík
        parent::__construct(false);
34 d5a16eb6 Filip Jani
    }
35
36
    /**
37
     * Abstraktní metoda, slouží k nastavení primárního klíče a nastavení datasource
38
     *  1. $this->setPrimaryKey();
39
     *  2. $this->setDataSource();
40
     *
41
     * @throws DataGridException
42
     */
43
    public function init()
44
    {
45
        $this->setPrimaryKey(UserRepository::COLUMN_ID);
46
        $this->setDataSource($this->userRepository->findAll());
47
    }
48
49
    /**
50
     * Definice sloupečků, akcí, vyhledávácích filtrů gridu
51
     *
52
     * @throws DataGridException
53
     */
54
    public function define()
55
    {
56
        // Definice sloupečků
57
        $this->addColumnNumber(UserRepository::COLUMN_ID, 'ID')->setDefaultHide(TRUE);
58 c93cf853 Petr Lukašík
        $this->addColumnText(UserRepository::COLUMN_USERNAME, 'User');
59 d5a16eb6 Filip Jani
        $this->addColumnText(UserRepository::COLUMN_LOGIN, 'Login');
60 2190aaa4 Filip Jani
        $this->addColumnText(RoleRepository::COLUMN_NAME, 'Role')
61
            ->setRenderer(function (ActiveRow $activeRow)
62
            {
63
                try
64
                {
65
                    $userRoleRow = $activeRow->related(UserRoleRepository::TABLE_NAME)->fetch();
66
                    $roleRow = $userRoleRow->ref(RoleRepository::TABLE_NAME);
67
68
                    return $roleRow->{RoleRepository::COLUMN_NAME};
69
70
                } catch (\Exception $ex)
71
                {
72
                    \Tracy\Debugger::log($ex->getMessage(), 'user-grid');
73
                    return "not found";
74
                }
75
            });
76 d5a16eb6 Filip Jani
        $this->addColumnText(UserRepository::COLUMN_EMAIL, 'Email');
77
78
        // Definice filtrů
79 c93cf853 Petr Lukašík
        $this->addFilterText(UserRepository::COLUMN_USERNAME, 'User');
80 d5a16eb6 Filip Jani
        $this->addFilterText(UserRepository::COLUMN_LOGIN, 'Login');
81 2190aaa4 Filip Jani
        $this->addFilterSelect(RoleRepository::COLUMN_NAME, 'Role', ['' => 'all'] + $this->roleRepository->getRolesForSelect())
82
            ->setCondition(function (Selection $selection, $value)
83
            {
84
                $selection->where(':role.role_id', $value);
85
            });
86 d5a16eb6 Filip Jani
        $this->addFilterText(UserRepository::COLUMN_EMAIL, 'Email');
87
88
        // Akce
89 c93cf853 Petr Lukašík
        $this->addAction('edit', 'edit', 'User:edit', ['id' => UserRepository::COLUMN_ID])
90
            ->setTitle('Edit');
91 d5a16eb6 Filip Jani
92 c93cf853 Petr Lukašík
        $this->addAction('delete', 'delete', 'deleteUser!', ['id' => UserRepository::COLUMN_ID])
93
            ->setConfirm('Do you really want to delete user "%s"?', UserRepository::COLUMN_LOGIN)
94
            ->setTitle('Delete')
95 d5a16eb6 Filip Jani
            ->setClass('btn btn-xs btn-danger ajax');
96
    }
97
}
98
99
interface IUserGridFactory
100
{
101
    /**
102
     * @return UserGrid
103
     */
104
    public function create();
105
}