materiels/src/www/admin/localisations/modifier_localisation.php
2021-06-04 10:47:13 +02:00

45 lines
910 B
PHP

<?php
namespace Garradin;
use Garradin\Plugin\Materiels\Location;
use Garradin\Utils;
require_once __DIR__ . '/../_inc.php';
$loc = new Location;
$l = $loc->get((int) qg('id'));
if (!$l) {
throw new UserException("Cette localisation n'existe pas.");
}
$csrf_key = 'edit_location_' . $l->id;
if (f('save') && $form->check($csrf_key) && !$form->hasErrors())
{
try
{
$loc->edit($l->id, [
'name' => ucfirst(f('name'))
]);
Utils::redirect(PLUGIN_URL . 'localisations/index.php');
}
catch (\RuntimeException $e)
{
if (strstr($e->getMessage(), 'UNIQUE constraint failed'))
{
$form->addError('Cette localisation existe déjà.');
} else
{
$form->addError($e->getMessage());
}
}
}
$cancel_link = PLUGIN_URL . 'localisations/index.php';
$tpl->assign(compact('l', 'csrf_key', 'cancel_link'));
$tpl->display(PLUGIN_ROOT . '/templates/localisations/modifier_localisation.tpl');