/
UserAdminController.php
175 lines (151 loc) · 5.4 KB
/
UserAdminController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
namespace ZfcUserAdmin\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\Paginator;
use Zend\Stdlib\Hydrator\ClassMethods;
use ZfcUser\Mapper\UserInterface;
use ZfcUser\Options\ModuleOptions as ZfcUserModuleOptions;
use ZfcUserAdmin\Options\ModuleOptions;
class UserAdminController extends AbstractActionController
{
protected $options, $userMapper;
protected $zfcUserOptions;
/**
* @var \ZfcUserAdmin\Service\User
*/
protected $adminUserService;
public function listAction()
{
$userMapper = $this->getUserMapper();
$users = $userMapper->findAll();
if (is_array($users)) {
$paginator = new Paginator\Paginator(new Paginator\Adapter\ArrayAdapter($users));
} else {
$paginator = $users;
}
$paginator->setItemCountPerPage(100);
$paginator->setCurrentPageNumber($this->getEvent()->getRouteMatch()->getParam('p'));
return array(
'users' => $paginator,
'userlistElements' => $this->getOptions()->getUserListElements()
);
}
public function createAction()
{
/** @var $form \ZfcUserAdmin\Form\CreateUser */
$form = $this->getServiceLocator()->get('zfcuseradmin_createuser_form');
$request = $this->getRequest();
/** @var $request \Zend\Http\Request */
if ($request->isPost()) {
$zfcUserOptions = $this->getZfcUserOptions();
$class = $zfcUserOptions->getUserEntityClass();
$user = new $class();
$form->setHydrator(new ClassMethods());
$form->bind($user);
$form->setData($request->getPost());
if ($form->isValid()) {
$user = $this->getAdminUserService()->create($form, (array)$request->getPost());
if ($user) {
$this->flashMessenger()->addSuccessMessage('The user was created');
return $this->redirect()->toRoute('zfcadmin/zfcuseradmin/list');
}
}
}
return array(
'createUserForm' => $form
);
}
public function editAction()
{
$userId = $this->getEvent()->getRouteMatch()->getParam('userId');
$user = $this->getUserMapper()->findById($userId);
/** @var $form \ZfcUserAdmin\Form\EditUser */
$form = $this->getServiceLocator()->get('zfcuseradmin_edituser_form');
$form->setUser($user);
/** @var $request \Zend\Http\Request */
$request = $this->getRequest();
if ($request->isPost()) {
$form->setData($request->getPost());
if ($form->isValid()) {
$user = $this->getAdminUserService()->edit($form, (array)$request->getPost(), $user);
if ($user) {
$this->flashMessenger()->addSuccessMessage('The user was edited');
return $this->redirect()->toRoute('zfcadmin/zfcuseradmin/list');
}
}
} else {
$form->populateFromUser($user);
}
return array(
'editUserForm' => $form,
'userId' => $userId
);
}
public function removeAction()
{
$userId = $this->getEvent()->getRouteMatch()->getParam('userId');
/** @var $identity \ZfcUser\Entity\UserInterface */
$identity = $this->zfcUserAuthentication()->getIdentity();
if ($identity && $identity->getId() == $userId) {
$this->flashMessenger()->addErrorMessage('You can not delete yourself');
} else {
$user = $this->getUserMapper()->findById($userId);
if ($user) {
$this->getUserMapper()->remove($user);
$this->flashMessenger()->addSuccessMessage('The user was deleted');
}
}
return $this->redirect()->toRoute('zfcadmin/zfcuseradmin/list');
}
public function setOptions(ModuleOptions $options)
{
$this->options = $options;
return $this;
}
public function getOptions()
{
if (!$this->options instanceof ModuleOptions) {
$this->setOptions($this->getServiceLocator()->get('zfcuseradmin_module_options'));
}
return $this->options;
}
public function getUserMapper()
{
if (null === $this->userMapper) {
$this->userMapper = $this->getServiceLocator()->get('zfcuser_user_mapper');
}
return $this->userMapper;
}
public function setUserMapper(UserInterface $userMapper)
{
$this->userMapper = $userMapper;
return $this;
}
public function getAdminUserService()
{
if (null === $this->adminUserService) {
$this->adminUserService = $this->getServiceLocator()->get('zfcuseradmin_user_service');
}
return $this->adminUserService;
}
public function setAdminUserService($service)
{
$this->adminUserService = $service;
return $this;
}
public function setZfcUserOptions(ZfcUserModuleOptions $options)
{
$this->zfcUserOptions = $options;
return $this;
}
/**
* @return \ZfcUser\Options\ModuleOptions
*/
public function getZfcUserOptions()
{
if (!$this->zfcUserOptions instanceof ZfcUserModuleOptions) {
$this->setZfcUserOptions($this->getServiceLocator()->get('zfcuser_module_options'));
}
return $this->zfcUserOptions;
}
}