Archived
1
0
Fork 0

app/controllers/backend/UserController.php: adding edit action.

This commit is contained in:
Henrik Hautakoski 2019-12-03 11:04:23 +01:00
parent e332e1a7e7
commit b1212f5310
No known key found for this signature in database
GPG key ID: 96765B12FEAC4745
4 changed files with 110 additions and 2 deletions

View file

@ -67,6 +67,12 @@ router:
backend-user-edit:
pattern: '/admin/user/{id:([0-9]+)}'
path: backend::user::edit
backend-user-status:
pattern: '/admin/user/{id:([0-9]+)}/{type}'
path:
module: backend
controller: user
action: status
backend-log:
pattern: '/admin/log{page:/?([0-9]+)?}'
path: backend::log::index

View file

@ -2,7 +2,8 @@
namespace App\Controller\Backend;
use App\Model\Data\User;
use App\Model\Data\User,
App\Form\UserSettings as UserSettingsForm;
class UserController extends \Phalcon\Mvc\Controller
{
@ -21,4 +22,42 @@ class UserController extends \Phalcon\Mvc\Controller
$this->view->pagination_url = '/admin/user/list/';
$this->view->page = $paginator->getPaginate();
}
public function editAction($id)
{
$user = User::findFirstById($id);
$form = new UserSettingsForm($user, true);
if ($this->request->isPost()) {
$data = $this->request->getPost();
if ($form->isValid($data)) {
$new_pw = $form->getValue('passwordNew');
if (strlen($new_pw) > 0) {
$hash = $this->security->hash($new_pw, 12);
$user->setPassword($hash);
}
$user->save();
$form->initialize();
$this->flash->message('success', 'User saved!');
} else {
$this->flash->message('error', 'Could not save user');
}
}
$this->view->user = $user;
$this->view->form = $form;
}
public function statusAction($id, $type)
{
$user = User::findFirstById($id);
$user->setStatus(ucfirst($type));
$user->save();
$this->flash->success('The account was: ' . $user->getStatus());
$this->response->redirect('/admin');
}
}

View file

@ -0,0 +1,63 @@
{% if (user.isActive() === false) %}
<div class="row">
<div class="col-sm-offset-2 col-sm-10">
<p class="badge badge-danger">{{ user.getStatus() }}</p>
</div>
</div>
{% endif %}
<form class="form-horizontal" method="post" action="">
<div class="form-group">
{{ form.renderDecorated('username', [ 'length': 7 ]) }}
{{ form.renderDecorated('id', [ 'length': 2, 'label-length' : 1 ]) }}
</div>
<div class="form-group">
{{ form.renderDecorated('name') }}
</div>
<div class="form-group">
{{ form.renderDecorated('email') }}
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<h4>Password</h4>
</div>
</div>
<div class="form-group">
{{ form.renderDecorated('passwordNew') }}
</div>
<div class="form-group">
{{ form.renderDecorated('passwordConfirm') }}
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<hr />
{{ form.render('Save') }}
{% set actions = [ 'Activate': 'Active', 'Suspend': 'Suspended', 'Delete': 'Deleted' ] %}
<div class="pull-right">
{% for label, status in actions %}
{% if (user.status != status) %}
<a class="button button-{{ status == 'Active' ? 'success' : 'danger' }}"
href="{{ url(['for': 'backend-user-status', 'type': status, 'id': user.getId() ]) }}">
{{ label }}
</a>
{% endif %}
{% endfor %}
</div>
</div>
</div>
</form>

View file

@ -27,7 +27,7 @@
<td>{{ item.username }}</td>
<td>{{ item.name }}</td>
<td>{{ item.email }}</td>
<td>{{ item.type }}</td>
<td>{{ item.type | capitalize }}</td>
<td>{{ item.status }}</td>
</tr>
{% endfor %}