Modify categories

This commit is contained in:
JBthePenguin 2021-06-03 19:30:07 +02:00
parent 2f9b57a842
commit 5f226732ad
9 changed files with 110 additions and 42 deletions

View File

@ -2,13 +2,10 @@
namespace Garradin\Plugin\Materiels;
use Garradin\Entity;
use Garradin\DB;
class Category extends Entity
class Category
{
const TABLE = 'plugin_materiels_category';
protected $id;
protected $name;
@ -17,8 +14,23 @@ class Category extends Entity
'name' => 'string',
];
static public function listAll(): array
public function add($data = [])
{
return DB::getInstance()->get('SELECT id, name FROM plugin_materiels_category ORDER BY name COLLATE NOCASE;');
DB::getInstance()->insert('plugin_materiels_category', $data);
}
public function delete($id)
{
DB::getInstance()->delete('plugin_facturation_clients', 'id = ' . $id);
}
public function get($id)
{
return DB::getInstance()->first('SELECT * FROM plugin_materiels_category WHERE id = ?;', $id);
}
public function listAll()
{
return DB::getInstance()->get('SELECT id, name FROM plugin_materiels_category ORDER BY name;');
}
}

View File

@ -1,7 +1,7 @@
<nav class="tabs">
<ul>
<li class="{if $current == 'index'}current{/if}"><a href="{plugin_url}">Matériels en stock</a></li>
<li class="{if $current == 'categories'}current{/if}"><a href="{plugin_url file="categories.php"}">Catégories</a></li>
<li class="{if $current == 'categories'}current{/if}"><a href="{plugin_url file="categories/index.php"}">Catégories</a></li>
<li class="{if $current == 'entrees_definitives'}current{/if}"><a href="{plugin_url file="entrees_definitives.php"}"><b>Entrées définitives</b></a></li>
<li class="{if $current == 'entrees_provisoires'}current{/if}"><a href="{plugin_url file="entrees_provisoires.php"}">Entrées provisoires</a></li>
<li class="{if $current == 'sorties_definitives'}current{/if}"><a href="{plugin_url file="sorties_definitives.php"}">Sorties définitives</a></li>

View File

@ -5,6 +5,7 @@
<form method="post" action="{$self_url}">
<fieldset>
<legend>Ajouter une catégorie</legend>
{form_errors}
<dl>
{input type="text" name="name" label="Nom" required=true}
</dl>

View File

@ -0,0 +1,11 @@
{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="%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
alert="Attention, la catégorie ne doit plus contenir de matériels pour pouvoir être supprimée."
}
{include file="admin/_foot.tpl"}

View File

@ -0,0 +1,13 @@
{form_errors}
<form method="post" action="{$self_url}" data-focus="1">
<fieldset>
<legend>{$legend}</legend>
<h3 class="warning">{$warning}</h3>
<p class="block alert">{$alert}</p>
</fieldset>
<p class="submit">
{csrf_field key=$csrf_key}
{button type="submit" name="delete" label="Supprimer" shape="delete" class="main"}
</p>
</form>

View File

@ -1,28 +0,0 @@
<?php
namespace Garradin;
use Garradin\Plugin\Materiels\Category;
use Garradin\Membres\Session;
require_once __DIR__ . '/_inc.php';
$csrf_key = 'cat_create';
$form->runIf('save', function() {
$cat = new Category;
$cat->importForm([
'name' => ucfirst(f('name')),
]);
try {
$cat->save();
} catch (\RuntimeException $e) {
throw new UserException('Cette catégorie existe déjà.');
}
}, $csrf_key, Utils::getSelfURI());
$list = Category::listAll();
$tpl->assign(compact('csrf_key', 'list'));
$tpl->display(PLUGIN_ROOT . '/templates/categories.tpl');

View File

@ -0,0 +1,42 @@
<?php
namespace Garradin;
use Garradin\Plugin\Materiels\Category;
use Garradin\Utils;
require_once __DIR__ . '/../_inc.php';
$cat = new Category;
$csrf_key = 'cat_create';
if (f('save') && $form->check($csrf_key))
{
if (!$form->hasErrors())
{
try
{
$cat->add([
'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());
}
}
}
}
$list = $cat->listAll();
$tpl->assign(compact('csrf_key', 'list'));
$tpl->display(PLUGIN_ROOT . '/templates/categories/index.tpl');

View File

@ -0,0 +1,24 @@
<?php
namespace Garradin;
use Garradin\Plugin\Materiels\Category;
use Garradin\Utils;
require_once __DIR__ . '/../_inc.php';
$cat = new Category;
$cat = $cat->get((int) qg('id'));
if (!$cat) {
throw new UserException("Cette catégorie n'existe pas.");
}
$csrf_key = 'cat_delete_' . qg('id');
$form->runIf('delete', function () use($cat) {
$cat->delete((int) qg('id'));
}, $csrf_key, Utils::redirect(PLUGIN_URL . 'categories/index.php'));
$tpl->assign(compact('cat', 'csrf_key'));
$tpl->display(PLUGIN_ROOT . '/templates/categories/supprimer_categorie.tpl');

View File

@ -1,7 +0,0 @@
<?php
namespace Garradin;
require_once __DIR__ . '/_inc.php';
$tpl->display(PLUGIN_ROOT . '/templates/index.tpl');