1
|
|
|
<?php
|
2
|
|
|
namespace Core\Modules\UserList;
|
3
|
|
|
|
4
|
|
|
use Frameworkless\Controllers;
|
5
|
|
|
use Donquixote\Cellbrush\Table\Table;
|
6
|
|
|
|
7
|
|
|
/**
|
8
|
|
|
* Description of UserListController
|
9
|
|
|
*
|
10
|
|
|
* @author d.lanec
|
11
|
|
|
*/
|
12
|
|
|
class UserList extends Controllers\ModuleController implements Controllers\ModuleInterface
|
13
|
|
|
{
|
14
|
|
|
|
15
|
|
|
/**
|
16
|
|
|
*
|
17
|
|
|
* @var \Core\Models\User\UserRepository
|
18
|
|
|
*/
|
19
|
|
|
protected $userRepository;
|
20
|
|
|
|
21
|
|
|
function __construct(\Core\Models\User\UserRepository $userRepository)
|
|
|
|
|
22
|
|
|
{
|
23
|
|
|
$this->userRepository = $userRepository;
|
24
|
|
|
}
|
25
|
|
|
|
26
|
|
|
public function process($args = [])
|
27
|
|
|
{
|
28
|
|
|
|
29
|
|
|
$this->logger->debug('UserList start');
|
30
|
|
|
|
31
|
|
|
if ($args['fn'] == 'add') {
|
32
|
|
|
$this->add();
|
33
|
|
|
}
|
34
|
|
|
|
35
|
|
|
$Users = $this->userRepository->findMany();
|
36
|
|
|
|
37
|
|
|
$table = Table::create();
|
38
|
|
|
$table->addColNames([0, 1, 2]);
|
39
|
|
|
$table->addClass('table table-striped');
|
40
|
|
|
$table->thead()
|
41
|
|
|
->addRowName('head row')
|
42
|
|
|
->th('head row', 0, 'Id')
|
43
|
|
|
->th('head row', 1, 'Имя')
|
44
|
|
|
->th('head row', 2, 'Email');
|
45
|
|
|
$i = 0;
|
46
|
|
|
foreach ($Users as $User) {
|
47
|
|
|
$table->addRow($i)->tdMultiple([
|
48
|
|
|
$User->getId(),
|
49
|
|
|
$User->getName(),
|
50
|
|
|
$User->getEmail()]);
|
51
|
|
|
$i++;
|
52
|
|
|
}
|
53
|
|
|
|
54
|
|
|
return $this->render('default', [
|
55
|
|
|
'table' => $table->render()
|
56
|
|
|
]);
|
57
|
|
|
}
|
58
|
|
|
|
59
|
|
|
protected function add()
|
60
|
|
|
{
|
61
|
|
|
|
62
|
|
|
try {
|
63
|
|
|
|
64
|
|
|
$User = $this->userRepository->build();
|
65
|
|
|
$User->setEmail('[email protected]');
|
66
|
|
|
|
67
|
|
|
if (!$this->UserRepository->save($User)) {
|
|
|
|
|
68
|
|
|
throw new Exception('User not save');
|
69
|
|
|
} else {
|
70
|
|
|
$this->logger->info("Пользователь успешно сохранен!");
|
71
|
|
|
}
|
72
|
|
|
} catch(\Frameworkless\Exceptions\ValidationException $ex) {
|
73
|
|
|
|
74
|
|
|
foreach ($ex->getFailures() as $failure) {
|
75
|
|
|
$this->logger->error("Property " . $failure->getPropertyPath() . ": " . $failure->getMessage() . "\n");
|
76
|
|
|
}
|
77
|
|
|
|
78
|
|
|
$this->logger->info("Произошла ошибка при сохранении пользователя");
|
79
|
|
|
} catch(\Exception $ex) {
|
80
|
|
|
|
81
|
|
|
$this->logger->error("system error:" . $ex->getMessage());
|
82
|
|
|
|
83
|
|
|
$this->logger->info("Произошла ошибка при сохранении пользователя");
|
84
|
|
|
}
|
85
|
|
|
|
86
|
|
|
}
|
87
|
|
|
}
|
88
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.