From bd2c6f25f5e9d99e9537d3e429443e24dcad88b4 Mon Sep 17 00:00:00 2001 From: JBthePenguin Date: Sat, 5 Jun 2021 11:04:30 +0200 Subject: [PATCH] ok to add entry and new equipment in db. --- src/lib/Entry.php | 10 ++-- src/lib/Equipment.php | 9 +++- src/schema.sql | 2 +- .../entrees/definitives/non_repertorie.tpl | 4 +- .../entrees/definitives/non_repertorie.php | 51 +++++++++++++++++-- 5 files changed, 64 insertions(+), 12 deletions(-) diff --git a/src/lib/Entry.php b/src/lib/Entry.php index bc3c52a..94e0dc2 100644 --- a/src/lib/Entry.php +++ b/src/lib/Entry.php @@ -4,7 +4,7 @@ namespace Garradin\Plugin\Materiels; use Garradin\DB; -class Entry +class Entry // entrées définitives { protected $columns_order = array( 'id', @@ -23,8 +23,8 @@ class Entry ); } - public function listEquipmentIds() - { - // return $this->kinds; - } + public function add($data = []) + { + DB::getInstance()->insert('plugin_materiels_entry', $data); + } } diff --git a/src/lib/Equipment.php b/src/lib/Equipment.php index 5c3ef05..b72757c 100644 --- a/src/lib/Equipment.php +++ b/src/lib/Equipment.php @@ -9,8 +9,15 @@ class Equipment protected $columns_order = array( 'id', 'category_id', - 'localisation_id', + 'location_id', 'number_of_equipments', 'designation' ); + + public function add($data = []) + { + $db = DB::getInstance(); + $db->insert('plugin_materiels_equipment', $data); + return $db->lastInsertRowId(); + } } diff --git a/src/schema.sql b/src/schema.sql index 9a718bd..639d70a 100644 --- a/src/schema.sql +++ b/src/schema.sql @@ -13,7 +13,7 @@ CREATE TABLE IF NOT EXISTS plugin_materiels_location ( CREATE TABLE IF NOT EXISTS plugin_materiels_equipment ( id integer NOT NULL PRIMARY KEY autoincrement, category_id integer NOT NULL, - localisation_id integer NOT NULL, + location_id integer NOT NULL, number_of_equipments integer NOT NULL, designation varchar(255) NOT NULL, CONSTRAINT u_equipment_designation UNIQUE ( designation ), diff --git a/src/templates/entrees/definitives/non_repertorie.tpl b/src/templates/entrees/definitives/non_repertorie.tpl index 9799572..02c297f 100644 --- a/src/templates/entrees/definitives/non_repertorie.tpl +++ b/src/templates/entrees/definitives/non_repertorie.tpl @@ -15,8 +15,8 @@ {/foreach} - {input type="date" name="date_entree" default=$date label="Date d'entrée" required=true } - {input type="number" name="number" label="Nombre d'entrée" required=true step="1" min="1" default="1"} + {input type="date" name="date_entree" default=$default_date label="Date d'entrée" required=true } + {input type="number" name="number" label="Nombre d'entrée" required=true step="1" min="1" default=$default_number}

Matériel

diff --git a/src/www/admin/entrees/definitives/non_repertorie.php b/src/www/admin/entrees/definitives/non_repertorie.php index f94ac14..56d59f8 100644 --- a/src/www/admin/entrees/definitives/non_repertorie.php +++ b/src/www/admin/entrees/definitives/non_repertorie.php @@ -5,6 +5,8 @@ namespace Garradin; use Garradin\Plugin\Materiels\Entry; use Garradin\Plugin\Materiels\Category; use Garradin\Plugin\Materiels\Location; +use Garradin\Plugin\Materiels\Equipment; +use Garradin\Utils; require_once __DIR__ . '/../../_inc.php'; @@ -12,6 +14,12 @@ $entry = new Entry; $kinds = $entry->listKinds(); $selected_kind = $kinds[0]; +$date = new \DateTime; +$date->setTimestamp(time()); +$default_date = $date; + +$default_number = "1"; + $cat = new Category; $cats = $cat->listAll(); $selected_cat = $cats[0]->id; @@ -22,11 +30,48 @@ $selected_loc = $locs[0]->id; $csrf_key = 'add_entry'; -$date = new \DateTime; -$date->setTimestamp(time()); +if (f('save')) +{ + $selected_kind = f('kind'); + $default_date = f('date_entree'); + $default_number = f('number'); + $selected_cat = f('category_id'); + $selected_loc = f('location_id'); + + if ($form->check($csrf_key) && !$form->hasErrors()) + { + try + { + $eqmt = new Equipment; + $eqmt_id = $eqmt->add([ + 'category_id' => (int) f('category_id'), + 'location_id' => (int) f('location_id'), + 'number_of_equipments' => (int) f('number'), + 'designation' => ucfirst(strtolower(f('designation'))), + ]); + $entry->add([ + 'kind' => f('kind'), + 'number_of_equipments' => (int) f('number'), + 'equipment_id' => $eqmt_id, + 'date_of_entry' => f('date_entree'), + ]); + Utils::redirect(PLUGIN_URL . 'entrees/definitives/index.php'); + } + catch (\RuntimeException $e) + { + if (strstr($e->getMessage(), 'UNIQUE constraint failed')) + { + $form->addError('Un matériel avec cette désignation est déjà répertorié.'); + } else + { + $form->addError($e->getMessage()); + } + } + } +} $cancel_link = PLUGIN_URL . 'entrees/definitives/index.php'; -$tpl->assign(compact('kinds', 'cats', 'locs', 'date', 'selected_kind', 'selected_cat', 'selected_loc', 'cancel_link', 'csrf_key')); +$tpl->assign(compact('kinds', 'date', 'cats', 'locs', 'selected_kind', 'default_date', 'default_number', 'selected_cat', 'selected_loc', 'cancel_link', 'csrf_key')); $tpl->display(PLUGIN_ROOT . '/templates/entrees/definitives/non_repertorie.tpl');