materiels/src/admin/modifier_materiel.php

59 lines
1.4 KiB
PHP

<?php
// edit a specific equipment
namespace Paheko;
require_once __DIR__ . '/_inc.php';
use Paheko\Plugin\Materiels\Equipment;
use Paheko\Plugin\Materiels\Category;
use Paheko\Utils;
// get the equipment requested
$eqmt = new Equipment;
$eqmt_requested = $eqmt->get((int) qg('id'));
if (!$eqmt_requested) {
throw new UserException("Ce matériel n'existe pas.");
}
// get all categories and set the selected one
$cat = new Category;
$cats = $cat->listAll();
$selected_cat = $eqmt_requested->category_id;
// check if edit form is submitted
$csrf_key = 'edit_equipment_' . $eqmt_requested->id;
$form->runIf (f('save') && !$form->hasErrors(),
function () use ($eqmt, $eqmt_requested, $form)
{
// try to edit equipment selected and if error catched add it in form
try
{
$eqmt->edit($eqmt_requested->id, [
'category_id' => (int) f('category_id'),
'designation' => ucfirst(strtolower(f('designation'))),
]);
Utils::redirect(Utils::plugin_url() . 'index.php');
}
catch (\RuntimeException $e)
{
if (strstr($e->getMessage(), 'UNIQUE constraint failed'))
{
$form->addError('Ce matériel avec cette désignation existe déjà.');
} else
{
$form->addError($e->getMessage());
}
}
});
$cancel_link = Utils::plugin_url() . 'index.php';
// send all to template
$tpl->assign(compact('eqmt_requested', 'cats', 'selected_cat', 'csrf_key', 'cancel_link'));
$tpl->display(PLUGIN_ROOT . '/templates/modifier_materiel.tpl');