Ok for Add Modify Delete categories, next location

This commit is contained in:
JBthePenguin 2021-06-04 00:49:19 +02:00
parent 5f226732ad
commit a3c7a67660
11 changed files with 134 additions and 28 deletions

Binary file not shown.

View File

@ -19,9 +19,16 @@ class Category
DB::getInstance()->insert('plugin_materiels_category', $data);
}
public function edit($id, $data = [])
{
$db = DB::getInstance();
$db->update('plugin_materiels_category', $data, $db->where('id', $id));
}
public function delete($id)
{
DB::getInstance()->delete('plugin_facturation_clients', 'id = ' . $id);
DB::getInstance()->delete('plugin_materiels_category', 'id = ' . $id);
}
public function get($id)
@ -31,6 +38,6 @@ class Category
public function listAll()
{
return DB::getInstance()->get('SELECT id, name FROM plugin_materiels_category ORDER BY name;');
return DB::getInstance()->get('SELECT * FROM plugin_materiels_category ORDER BY name;');
}
}

View File

@ -26,6 +26,8 @@
<tr>
<th>{$cat.name}</th>
<td class="actions">
{linkbutton shape="upload" label="Liste des materiels" href="materiels_par_categorie.php?id=%d"|args:$cat.id}
{linkbutton shape="edit" label="Modifier" href="modifier_categorie.php?id=%d"|args:$cat.id}
{linkbutton shape="delete" label="Supprimer" href="supprimer_categorie.php?id=%d"|args:$cat.id}
</td>
</tr>

View File

@ -0,0 +1,5 @@
{include file="admin/_head.tpl" title="%s"|args:$plugin.nom current="plugin_%s"|args:$plugin.id}
{include file="%s_nav.tpl"|args:$plugin_tpl current="categories"}
{include file="admin/_foot.tpl"}

View File

@ -0,0 +1,20 @@
{include file="admin/_head.tpl" title="%s"|args:$plugin.nom current="plugin_%s"|args:$plugin.id}
{include file="%s_nav.tpl"|args:$plugin_tpl current="categories"}
<form method="post" action="{$self_url}" data-focus="1">
{form_errors}
<fieldset>
<legend>Modifier cette catégorie</legend>
<dl>
{input type="text" name="name" label="Nom" required=true source=$c}
</dl>
</fieldset>
<p class="submit">
{csrf_field key=$csrf_key}
{button type="submit" name="save" label="Enregistrer" shape="right" class="main"}
{linkbutton label="Annuler" shape="export" href=$cancel_link}
</p>
</form>
{include file="admin/_foot.tpl"}

View File

@ -4,7 +4,7 @@
{include file="%scommon/delete_form.tpl"|args:$plugin_tpl
legend="Supprimer cette catégorie de matériels ?"
warning="Êtes-vous sûr de vouloir supprimer la catégorie « %s » ?"|args:$cat.name
warning="Êtes-vous sûr de vouloir supprimer la catégorie « %s » ?"|args:$c.name
alert="Attention, la catégorie ne doit plus contenir de matériels pour pouvoir être supprimée."
}

View File

@ -1,6 +1,5 @@
{form_errors}
<form method="post" action="{$self_url}" data-focus="1">
{form_errors}
<fieldset>
<legend>{$legend}</legend>
<h3 class="warning">{$warning}</h3>
@ -9,5 +8,6 @@
<p class="submit">
{csrf_field key=$csrf_key}
{button type="submit" name="delete" label="Supprimer" shape="delete" class="main"}
{linkbutton label="Annuler" shape="export" href=$cancel_link}
</p>
</form>

View File

@ -11,26 +11,23 @@ $cat = new Category;
$csrf_key = 'cat_create';
if (f('save') && $form->check($csrf_key))
if (f('save') && $form->check($csrf_key) && !$form->hasErrors())
{
if (!$form->hasErrors())
try
{
try
$cat->add([
'name' => ucfirst(f('name'))
]);
Utils::redirect(PLUGIN_URL . 'categories/index.php');
}
catch (\RuntimeException $e)
{
if (strstr($e->getMessage(), 'UNIQUE constraint failed'))
{
$cat->add([
'name' => ucfirst(f('name'))
]);
Utils::redirect(PLUGIN_URL . 'categories/index.php');
}
catch (\RuntimeException $e)
$form->addError('Cette catégorie existe déjà.');
} else
{
if (strstr($e->getMessage(), 'UNIQUE constraint failed'))
{
$form->addError('Cette catégorie existe déjà.');
} else
{
$form->addError($e->getMessage());
}
$form->addError($e->getMessage());
}
}
}

View File

@ -0,0 +1,19 @@
<?php
namespace Garradin;
use Garradin\Plugin\Materiels\Category;
use Garradin\Utils;
require_once __DIR__ . '/../_inc.php';
$cat = new Category;
$c = $cat->get((int) qg('id'));
if (!$c) {
throw new UserException("Cette catégorie n'existe pas.");
}
$tpl->assign(compact('c'));
$tpl->display(PLUGIN_ROOT . '/templates/categories/materiels_par_categorie.tpl');

View File

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

View File

@ -7,18 +7,30 @@ use Garradin\Utils;
require_once __DIR__ . '/../_inc.php';
$cat = new Category;
$cat = $cat->get((int) qg('id'));
if (!$cat) {
$c = $cat->get((int) qg('id'));
if (!$c) {
throw new UserException("Cette catégorie n'existe pas.");
}
$csrf_key = 'cat_delete_' . qg('id');
$csrf_key = 'delete_categorie_' . $c->id;
$form->runIf('delete', function () use($cat) {
$cat->delete((int) qg('id'));
}, $csrf_key, Utils::redirect(PLUGIN_URL . 'categories/index.php'));
if (f('delete') && $form->check($csrf_key) && !$form->hasErrors())
{
try
{
$cat->delete($c->id);
Utils::redirect(PLUGIN_URL . 'categories/index.php');
}
catch (\RuntimeException $e)
{
$form->addError($e->getMessage());
}
}
$tpl->assign(compact('cat', 'csrf_key'));
$cancel_link = PLUGIN_URL . 'categories/index.php';
$tpl->assign(compact('c', 'csrf_key', 'cancel_link'));
$tpl->display(PLUGIN_ROOT . '/templates/categories/supprimer_categorie.tpl');