Skip to content

Commit

Permalink
Edit and delete users
Browse files Browse the repository at this point in the history
  • Loading branch information
mklkj committed Aug 7, 2018
1 parent 69a2286 commit 3df73a2
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 1 deletion.
49 changes: 49 additions & 0 deletions src/module/Users/One/Admin/Profile/AddController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Rudolf\Modules\Users\One\Admin\Profile;

use Rudolf\Component\Alerts\Alert;
use Rudolf\Component\Alerts\AlertsCollection;
use Rudolf\Framework\Controller\AdminController;

class AddController extends AdminController
{
/**
* @throws \Exception
*/
public function add()
{
if (isset($_POST['add'])) {
if (empty($_POST['password']) || trim($_POST['password']) !== $_POST['password_again']) {
AlertsCollection::add(
new Alert(
'error',
'Podane hasła nie są identyczne!'
)
);
$this->redirect(DIR.'/admin/users/add');
}

$id = (new AddModel())->add($_POST);

if ($id) {
AlertsCollection::add(
new Alert(
'success',
'Dodano użytkownika'
)
);
$this->redirect(DIR.'/admin/users/edit/'.$id);
}
AlertsCollection::add(
new Alert(
'error',
'Wystąpił nieoczekiwany błąd'
)
);
}
$view = new AddView();
$view->display();
$view->render();
}
}
28 changes: 28 additions & 0 deletions src/module/Users/One/Admin/Profile/AddModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Rudolf\Modules\Users\One\Admin\Profile;

use Rudolf\Component\Auth\Auth;
use Rudolf\Framework\Model\AdminModel;

class AddModel extends AdminModel
{
public function add($p)
{
$auth = new Auth($this->pdo, $this->prefix);
$hash = $auth->getPasswordHash($p['password']);

$stmt = $this->pdo->prepare("INSERT INTO {$this->prefix}users SET
nick = :nick, first_name = :first_name, surname = :surname, email = :email,
password = :password, active = :active");
$stmt->bindValue(':nick', $p['nick']);
$stmt->bindValue(':first_name', $p['first_name']);
$stmt->bindValue(':surname', $p['surname']);
$stmt->bindValue(':email', $p['email']);
$stmt->bindValue(':password', $hash);
$stmt->bindValue(':active', $p['active']);
$stmt->execute();

return $this->pdo->lastInsertId();
}
}
24 changes: 24 additions & 0 deletions src/module/Users/One/Admin/Profile/AddView.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Rudolf\Modules\Users\One\Admin\Profile;

use Rudolf\Framework\View\AdminView;
use Rudolf\Modules\Users\One\Admin\User;

class AddView extends AdminView
{
protected $user;

protected $path;

public function display()
{
$this->pageTitle = _('Profile');
$this->head->setTitle($this->pageTitle);

$this->user = new User();
$this->path = DIR.'/admin/users/add';
$this->templateType = 'add';
$this->template = 'user-one';
}
}
37 changes: 37 additions & 0 deletions src/module/Users/One/Admin/Profile/DelController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Rudolf\Modules\Users\One\Admin\Profile;

use Rudolf\Component\Alerts\Alert;
use Rudolf\Component\Alerts\AlertsCollection;
use Rudolf\Framework\Controller\AdminController;

class DelController extends AdminController
{
/**
* @param $id
*
* @throws \Exception
*/
public function del($id)
{
if (isset($_POST['delete'])) {
if ((new DelModel())->del($id)) {
AlertsCollection::add(new Alert(
'success',
'Poprawnie usunięto użytkownika'
));
$this->redirect(DIR.'/admin/users');
} else {
AlertsCollection::add(new Alert(
'error',
'Wystąpił nieoczekiwany błąd'
));
}
}

$view = new DelView();
$view->display((new EditModel())->getUserInfoById($id));
$view->render();
}
}
16 changes: 16 additions & 0 deletions src/module/Users/One/Admin/Profile/DelModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Rudolf\Modules\Users\One\Admin\Profile;

use Rudolf\Framework\Model\AdminModel;

class DelModel extends AdminModel
{
public function del($id)
{
$query = $this->pdo->prepare("DELETE FROM {$this->prefix}users WHERE id = :id");
$query->bindValue(':id', $id, \PDO::PARAM_INT);

return $query->execute();
}
}
24 changes: 24 additions & 0 deletions src/module/Users/One/Admin/Profile/DelView.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Rudolf\Modules\Users\One\Admin\Profile;

use Rudolf\Framework\View\AdminView;
use Rudolf\Modules\Users\One\Admin\User;

class DelView extends AdminView
{
/** @var User */
protected $user;

protected $path;

public function display(array $user)
{
$this->pageTitle = _('Profile');
$this->head->setTitle($this->pageTitle);
$this->user = new User($user);

$this->path = $this->user->delUrl();
$this->template = 'users-del';
}
}
13 changes: 12 additions & 1 deletion src/module/Users/routing.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,20 @@
['page' => 0]
));

// edit
// profile
$collection->add('users/edit', new Route(
'/admin/users/edit/<id>$',
'Rudolf\Modules\Users\One\Admin\Profile\EditController::edit',
['id' => '[1-9][0-9]*']
));

$collection->add('users/add', new Route(
'/admin/users/add?',
'Rudolf\Modules\Users\One\Admin\Profile\AddController::Add'
));

$collection->add('users/del', new Route(
'/admin/users/del/<id>$',
'Rudolf\Modules\Users\One\Admin\Profile\DelController::del',
['id' => '[1-9][0-9]*']
));

0 comments on commit 3df73a2

Please sign in to comment.