diff --git a/materiels.tar.gz b/materiels.tar.gz index 0913d59..fd267dd 100644 Binary files a/materiels.tar.gz and b/materiels.tar.gz differ diff --git a/src/lib/Category.php b/src/lib/Category.php index b1da912..57b4d04 100644 --- a/src/lib/Category.php +++ b/src/lib/Category.php @@ -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;'); } } diff --git a/src/templates/categories/index.tpl b/src/templates/categories/index.tpl index 97c888c..5ae85c1 100644 --- a/src/templates/categories/index.tpl +++ b/src/templates/categories/index.tpl @@ -26,6 +26,8 @@ {$cat.name} + {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} diff --git a/src/templates/categories/materiels_par_categorie.tpl b/src/templates/categories/materiels_par_categorie.tpl new file mode 100644 index 0000000..f356113 --- /dev/null +++ b/src/templates/categories/materiels_par_categorie.tpl @@ -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"} diff --git a/src/templates/categories/modifier_categorie.tpl b/src/templates/categories/modifier_categorie.tpl new file mode 100644 index 0000000..07b40eb --- /dev/null +++ b/src/templates/categories/modifier_categorie.tpl @@ -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_errors} +
+ Modifier cette catégorie +
+ {input type="text" name="name" label="Nom" required=true source=$c} +
+
+

+ {csrf_field key=$csrf_key} + {button type="submit" name="save" label="Enregistrer" shape="right" class="main"} + {linkbutton label="Annuler" shape="export" href=$cancel_link} +

+
+ +{include file="admin/_foot.tpl"} diff --git a/src/templates/categories/supprimer_categorie.tpl b/src/templates/categories/supprimer_categorie.tpl index dbed701..dd8a462 100644 --- a/src/templates/categories/supprimer_categorie.tpl +++ b/src/templates/categories/supprimer_categorie.tpl @@ -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." } diff --git a/src/templates/common/delete_form.tpl b/src/templates/common/delete_form.tpl index 5fa144f..c3be6dd 100644 --- a/src/templates/common/delete_form.tpl +++ b/src/templates/common/delete_form.tpl @@ -1,6 +1,5 @@ -{form_errors} -
+ {form_errors}
{$legend}

{$warning}

@@ -9,5 +8,6 @@

{csrf_field key=$csrf_key} {button type="submit" name="delete" label="Supprimer" shape="delete" class="main"} + {linkbutton label="Annuler" shape="export" href=$cancel_link}

diff --git a/src/www/admin/categories/index.php b/src/www/admin/categories/index.php index ac5e2fa..64bfac2 100644 --- a/src/www/admin/categories/index.php +++ b/src/www/admin/categories/index.php @@ -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()); } } } diff --git a/src/www/admin/categories/materiels_par_categorie.php b/src/www/admin/categories/materiels_par_categorie.php new file mode 100644 index 0000000..0337b92 --- /dev/null +++ b/src/www/admin/categories/materiels_par_categorie.php @@ -0,0 +1,19 @@ +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'); diff --git a/src/www/admin/categories/modifier_categorie.php b/src/www/admin/categories/modifier_categorie.php new file mode 100644 index 0000000..7d0f1ae --- /dev/null +++ b/src/www/admin/categories/modifier_categorie.php @@ -0,0 +1,44 @@ +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'); diff --git a/src/www/admin/categories/supprimer_categorie.php b/src/www/admin/categories/supprimer_categorie.php index 3aa93f0..73e609d 100644 --- a/src/www/admin/categories/supprimer_categorie.php +++ b/src/www/admin/categories/supprimer_categorie.php @@ -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');