1 |
d5a16eb6
|
Filip Jani
|
<?php
|
2 |
|
|
|
3 |
|
|
|
4 |
|
|
namespace App\Model\Facade;
|
5 |
|
|
|
6 |
|
|
use App\AdminModule\Components\UserEditForm;
|
7 |
|
|
use App\Model\Repository\UserRepository;
|
8 |
|
|
use App\Model\Repository\UserRoleRepository;
|
9 |
|
|
use Nette\Utils\ArrayHash;
|
10 |
|
|
|
11 |
|
|
class UserFacade
|
12 |
|
|
{
|
13 |
|
|
/**
|
14 |
|
|
* @var UserRepository
|
15 |
|
|
*/
|
16 |
|
|
private $userRepository;
|
17 |
|
|
/**
|
18 |
|
|
* @var UserRoleRepository
|
19 |
|
|
*/
|
20 |
|
|
private $userRoleRepository;
|
21 |
|
|
|
22 |
|
|
public function __construct(UserRepository $userRepository,
|
23 |
|
|
UserRoleRepository $userRoleRepository
|
24 |
|
|
)
|
25 |
|
|
{
|
26 |
|
|
$this->userRepository = $userRepository;
|
27 |
|
|
$this->userRoleRepository = $userRoleRepository;
|
28 |
|
|
}
|
29 |
|
|
|
30 |
|
|
/**
|
31 |
|
|
* Uloží informace o uživateli do DB
|
32 |
|
|
*
|
33 |
|
|
* @param array $values : hodnoty z formuláře UserEditForm
|
34 |
|
|
* @param int|null $userId : ID uživatele, pokud se edituje
|
35 |
|
|
* @return bool|false|int|\Nette\Database\Table\ActiveRow
|
36 |
|
|
*/
|
37 |
|
|
public function saveUser(array $values, int $userId = NULL)
|
38 |
|
|
{
|
39 |
|
|
$roleId = $values[UserRoleRepository::COLUMN_ROLE_ID];
|
40 |
|
|
unset($values[UserRoleRepository::COLUMN_ROLE_ID]);
|
41 |
|
|
unset($values[UserEditForm::PASSWORD_CONFIRM]);
|
42 |
|
|
|
43 |
|
|
if (empty($values[UserRepository::COLUMN_PASSWORD]))
|
44 |
|
|
{
|
45 |
|
|
unset($values[UserRepository::COLUMN_PASSWORD]);
|
46 |
|
|
} else
|
47 |
|
|
{
|
48 |
|
|
$values[UserRepository::COLUMN_PASSWORD] = md5($values[UserRepository::COLUMN_PASSWORD]);
|
49 |
|
|
}
|
50 |
|
|
|
51 |
|
|
if (!empty($userId))
|
52 |
|
|
{
|
53 |
|
|
$userRow = $this->userRepository->findById($userId)->fetch();
|
54 |
|
|
|
55 |
|
|
if ($userRow)
|
56 |
|
|
{
|
57 |
|
|
$userRow->update($values);
|
58 |
|
|
}
|
59 |
|
|
|
60 |
|
|
$roleRow = $userRow->related(UserRoleRepository::TABLE_NAME, UserRoleRepository::COLUMN_USER_ID)->fetch();
|
61 |
|
|
|
62 |
|
|
if ($roleRow)
|
63 |
|
|
{
|
64 |
|
|
$roleRow->update([UserRoleRepository::COLUMN_ROLE_ID => $roleId]);
|
65 |
|
|
}
|
66 |
|
|
} else
|
67 |
|
|
{
|
68 |
|
|
$userRow = $this->userRepository->insert($values);
|
69 |
|
|
$this->userRoleRepository->addNew($userRow->{UserRepository::COLUMN_ID}, $roleId);
|
70 |
|
|
}
|
71 |
|
|
|
72 |
|
|
return $userRow;
|
73 |
|
|
}
|
74 |
|
|
|
75 |
|
|
/**
|
76 |
|
|
* Odstraní uživatele
|
77 |
|
|
*
|
78 |
|
|
* @param int $userId
|
79 |
|
|
* @return int
|
80 |
|
|
*/
|
81 |
|
|
public function deleteUser(int $userId)
|
82 |
|
|
{
|
83 |
|
|
return $this->userRepository->delete($userId);
|
84 |
|
|
}
|
85 |
|
|
}
|