diff --git a/src/templates/mouvements/entrees/non_repertorie.tpl b/src/templates/mouvements/entrees/non_repertorie.tpl new file mode 100644 index 0000000..5d2854a --- /dev/null +++ b/src/templates/mouvements/entrees/non_repertorie.tpl @@ -0,0 +1,16 @@ + +
+

Matériel

+
+
(obligatoire)
+
+ +
+ {input type="text" name="designation" label="Désignation" required=true maxlength="255"} +
+
+ diff --git a/src/www/admin/mouvements/entrees/non_repertorie.php b/src/www/admin/mouvements/entrees/non_repertorie.php new file mode 100644 index 0000000..a1e7399 --- /dev/null +++ b/src/www/admin/mouvements/entrees/non_repertorie.php @@ -0,0 +1,97 @@ +listEntryKinds(); +$selected_kind = $kinds[0]; + +// make default date (now), default number (1), and default comment (empty) +$date = new \DateTime; +$date->setTimestamp(time()); +$default_date = $date; + +$default_number = "1"; +$default_comment = ""; + +// get list of all categories and make the first one as default +$cat = new Category; +$cats = $cat->listAll(); +$selected_cat = $cats[0]->id; + +// check if add form is submitted + +$csrf_key = 'add_entry'; + +if (f('save')) +{ + // keep the datas submitted as defaults + $selected_kind = f('kind'); + $default_date = f('entry_date'); + $default_number = f('equipment_number'); + $selected_cat = f('category_id'); + $default_comment = f('additional_comment'); + + if ($form->check($csrf_key) && !$form->hasErrors()) + { + // try to add new equipment, get his id, add new entry + // and if error catched add it in form + try + { + // add new equipment and get his id + $eqmt = new Equipment; + $eqmt_id = $eqmt->add([ + 'category_id' => (int) f('category_id'), + 'designation' => ucfirst(strtolower(f('designation'))), + ]); + // make the entry date in the good format + $mvt_date_format = date_create_from_format( + "d/m/Y", f('mvt_date'))->format("Y-m-d"); + // add new entry + $mvt->add([ + 'side' => 0, + 'kind' => f('kind'), + 'equipment_number' => (int) f('equipment_number'), + 'equipment_id' => $eqmt_id, + 'mvt_date' => $mvt_date_format, + 'additional_comment' => f('additional_comment'), + ]); + Utils::redirect(PLUGIN_URL . 'mouvements/entrees/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()); + } + } + } +} + +// make cancel link, legend for the title of the form +// and the template name for equipment to use in form +$cancel_link = PLUGIN_URL . 'mouvements/entrees/index.php'; +$legend_part = "non répertorié"; +$tpl_materiel_name = "non_repertorie"; + +// send all to template + +$tpl->assign(compact( + 'kinds', 'cats', 'selected_kind', 'default_date', + 'default_number', 'default_comment', 'selected_cat', + 'cancel_link', 'legend_part', 'tpl_materiel_name', 'csrf_key' +)); + +$tpl->display(PLUGIN_ROOT . '/templates/mouvements/entrees/ajouter_entree.tpl'); diff --git a/src/www/admin/mouvements/entrees/repertorie.php b/src/www/admin/mouvements/entrees/repertorie.php index 23e56e4..3a1ba34 100644 --- a/src/www/admin/mouvements/entrees/repertorie.php +++ b/src/www/admin/mouvements/entrees/repertorie.php @@ -1,5 +1,7 @@ check($csrf_key) && !$form->hasErrors()) @@ -33,7 +34,6 @@ $eqmt = new Equipment; $eqmts_by_cat = $eqmt->listAllByCategory(); // get the list of entry's kinds - $kinds = $mvt->listEntryKinds(); $selected_kind = $kinds[0]; @@ -47,7 +47,6 @@ $default_comment = ""; // make cancel link, legend for the title of the form // and the template name for equipment to use in form - $cancel_link = PLUGIN_URL . 'mouvements/entrees/index.php'; $legend_part = "répertorié"; $tpl_materiel_name = "repertorie";