2020-01-23 12:10:33 +00:00
|
|
|
<?php
|
2020-01-27 15:07:44 +00:00
|
|
|
// DEPENDENCIES
|
2020-01-27 15:39:57 +00:00
|
|
|
use severak\forms\form;
|
|
|
|
|
2020-01-27 15:07:44 +00:00
|
|
|
$dependencies['config'] = $config;
|
|
|
|
$singletons['pdo'] = function() {
|
|
|
|
$config = di('config');
|
|
|
|
return new PDO('sqlite:' . __DIR__ . '/' . $config['database']);
|
|
|
|
};
|
|
|
|
$singletons['rows'] = function(){
|
|
|
|
return new severak\database\rows(di('pdo'));
|
|
|
|
};
|
|
|
|
|
|
|
|
// ROUTY
|
2020-01-23 12:10:33 +00:00
|
|
|
|
2020-01-27 15:39:57 +00:00
|
|
|
// HP & LOGIN
|
2020-01-23 12:10:33 +00:00
|
|
|
route('', '/', function (){
|
2020-01-27 15:39:57 +00:00
|
|
|
if (!user()) return redirect('/login/');
|
|
|
|
return render('home');
|
|
|
|
});
|
|
|
|
|
|
|
|
route('', '/login/', function ($req){
|
|
|
|
/** @var Psr\Http\Message\ServerRequestInterface $req */
|
|
|
|
/** @var severak\database\rows $rows */
|
|
|
|
$rows = di('rows');
|
|
|
|
$form = new form(['method'=>'POST']);
|
|
|
|
$form->field('username', ['required'=>true, 'label'=>'Jméno']);
|
|
|
|
$form->field('password', ['type'=>'password', 'required'=>true, 'label'=>'Heslo']);
|
|
|
|
$form->field('_login', ['type'=>'submit', 'label'=>'Přihlásit se']);
|
|
|
|
|
|
|
|
if ($req->getMethod()=='POST') {
|
|
|
|
$form->fill($req->getParsedBody());
|
|
|
|
if ($form->validate()) {
|
|
|
|
$uz = $rows->one('users', ['username'=>$form->values['username']]);
|
|
|
|
if (!$uz) {
|
|
|
|
$form->error('username', 'Uživatel nenalezen');
|
|
|
|
} elseif (password_verify($form->values['password'], $uz['password'])) {
|
|
|
|
unset($uz['password']);
|
|
|
|
$_SESSION['user'] = $uz;
|
|
|
|
return redirect('/');
|
|
|
|
} else {
|
|
|
|
$form->error('password', 'Špatné heslo.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return render('form', ['form'=>$form]);
|
2020-01-23 12:10:33 +00:00
|
|
|
});
|
|
|
|
|
2020-01-27 15:39:57 +00:00
|
|
|
route('', '/logout/', function ($req){
|
|
|
|
unset($_SESSION['user']);
|
|
|
|
unset($_SESSION['flashes']);
|
|
|
|
return redirect('/');
|
|
|
|
});
|
|
|
|
|
|
|
|
route('', '/heslo/', function (){
|
|
|
|
return '123 - ' . password_hash('123', PASSWORD_BCRYPT);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// NABÍDKA
|
|
|
|
|
2020-01-23 15:09:58 +00:00
|
|
|
route('GET', '/items', function ($req){
|
|
|
|
/** @var severak\database\rows $rows */
|
|
|
|
$rows = di('rows');
|
|
|
|
$items = $rows->page('items', [], ['ord'=>'asc']);
|
|
|
|
|
|
|
|
return render('items', ['items'=>$items]);
|
|
|
|
});
|
|
|
|
|
|
|
|
route('', '/items/add', function ($req){
|
|
|
|
/** @var Psr\Http\Message\ServerRequestInterface $req */
|
|
|
|
/** @var severak\database\rows $rows */
|
|
|
|
$rows = di('rows');
|
|
|
|
|
|
|
|
$form = new severak\forms\form(['method'=>'POST']);
|
|
|
|
$form->field('name', ['required'=>true, 'label'=>'Název']);
|
|
|
|
$form->field('price', ['type'=>'number', 'label'=>'Cena']);
|
|
|
|
$form->field('note', ['type'=>'textarea', 'label'=>'Poznámka']);
|
|
|
|
$form->field('ord', ['type'=>'number', 'label'=>'Pořadí']);
|
|
|
|
$form->field('_save', ['type'=>'submit', 'label'=>'Přidat']);
|
|
|
|
|
|
|
|
$form->rule('price', function ($f){
|
|
|
|
return $f > 0 || $f < 0;
|
|
|
|
}, 'Cena nemůže být nulová.');
|
|
|
|
|
|
|
|
if ($req->getMethod()=='POST') {
|
|
|
|
$form->fill($req->getParsedBody());
|
|
|
|
if ($form->validate()) {
|
|
|
|
$rows->insert('items', [
|
|
|
|
'name'=>$form->values['name'],
|
|
|
|
'price'=>$form->values['price'],
|
|
|
|
'note'=>$form->values['note'],
|
|
|
|
'ord'=>$form->values['ord'],
|
|
|
|
]);
|
|
|
|
return redirect('/items');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return render('form', ['form'=>$form]);
|
2020-01-23 12:10:33 +00:00
|
|
|
});
|