Projekt

Obecné

Profil

Stáhnout (3.3 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\RoleRepository;
9
use App\Model\Repository\UserRepository;
10
use App\Model\Repository\UserRoleRepository;
11
use App\Utils\DataGrid\DataGrid;
12
use Nette\Database\Table\ActiveRow;
13
use Nette\Database\Table\Selection;
14
use Ublaboo\DataGrid\Exception\DataGridException;
15

    
16
class UserGrid extends DataGrid
17
{
18
    /**
19
     * @var UserRepository
20
     */
21
    private $userRepository;
22
    /**
23
     * @var RoleRepository
24
     */
25
    private $roleRepository;
26

    
27
    public function __construct(UserRepository $userRepository,
28
                                RoleRepository $roleRepository)
29
    {
30
        $this->userRepository = $userRepository;
31
        $this->roleRepository = $roleRepository;
32

    
33
        parent::__construct(false);
34
    }
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
        $this->addColumnText(UserRepository::COLUMN_USERNAME, 'User');
59
        $this->addColumnText(UserRepository::COLUMN_LOGIN, 'Login');
60
        $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
        $this->addColumnText(UserRepository::COLUMN_EMAIL, 'Email');
77

    
78
        // Definice filtrů
79
        $this->addFilterText(UserRepository::COLUMN_USERNAME, 'User');
80
        $this->addFilterText(UserRepository::COLUMN_LOGIN, 'Login');
81
        $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
        $this->addFilterText(UserRepository::COLUMN_EMAIL, 'Email');
87

    
88
        // Akce
89
        $this->addAction('edit', 'edit', 'User:edit', ['id' => UserRepository::COLUMN_ID])
90
            ->setTitle('Edit');
91

    
92
        $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
            ->setClass('btn btn-xs btn-danger ajax');
96
    }
97
}
98

    
99
interface IUserGridFactory
100
{
101
    /**
102
     * @return UserGrid
103
     */
104
    public function create();
105
}
(3-3/3)