diff --git a/src/admin/categories/index.php b/src/admin/categories/index.php index 52fff77..da7cdb6 100644 --- a/src/admin/categories/index.php +++ b/src/admin/categories/index.php @@ -10,17 +10,14 @@ use Paheko\Utils; $csrf_key = 'cat_create'; -if (f('save') && $form->check($csrf_key) && !$form->hasErrors()) -{ - // try to add new category and if error catched add it in form - try - { - $cat->add([ +$form->runIf(f('save') && !$form->hasErrors(), function() use ($cat, $form) { + try { + $cat->add([ 'name' => ucfirst(f('name')) ]); - Utils::redirect(PLUGIN_URL . 'categories/index.php'); - } - catch (\RuntimeException $e) + Utils::redirect(Utils::getSelfURL()); + } + catch (\RuntimeException $e) { if (strstr($e->getMessage(), 'UNIQUE constraint failed')) { @@ -30,7 +27,8 @@ if (f('save') && $form->check($csrf_key) && !$form->hasErrors()) $form->addError($e->getMessage()); } } -} + +}, $csrf_key); // get the list of all categories and send it to template diff --git a/src/admin/categories/materiels_par_categorie.php b/src/admin/categories/materiels_par_categorie.php index 9f95564..ea75b75 100644 --- a/src/admin/categories/materiels_par_categorie.php +++ b/src/admin/categories/materiels_par_categorie.php @@ -26,7 +26,7 @@ $eqmt = new Equipment; list($eqmts_owned, $eqmts_no_owned, $eqmts_just_listed) = $eqmt->AllListsAll($eqmts); -$return_link = PLUGIN_URL . 'categories/index.php'; +$return_link = Utils::plugin_url() . 'categories/index.php'; // send to template the category's name and all lists of its equipment diff --git a/src/admin/categories/modifier_categorie.php b/src/admin/categories/modifier_categorie.php index 37e88a2..a351d06 100644 --- a/src/admin/categories/modifier_categorie.php +++ b/src/admin/categories/modifier_categorie.php @@ -18,7 +18,8 @@ if (!$cat_requested) { // check if edit form is submitted $csrf_key = 'edit_category_' . $cat_requested->id; -if (f('save') && $form->check($csrf_key) && !$form->hasErrors()) +$form->runIf(f('save') && !$form->hasErrors(), + function() use($cat, $cat_requested, $form) { // try to edit category selected and if error catched add it in form try @@ -26,7 +27,7 @@ if (f('save') && $form->check($csrf_key) && !$form->hasErrors()) $cat->edit($cat_requested->id, [ 'name' => ucfirst(f('name')) ]); - Utils::redirect(PLUGIN_URL . 'categories/index.php'); + Utils::redirect(Utils::plugin_url() . 'categories/index.php'); } catch (\RuntimeException $e) { @@ -38,9 +39,9 @@ if (f('save') && $form->check($csrf_key) && !$form->hasErrors()) $form->addError($e->getMessage()); } } -} +}); -$cancel_link = PLUGIN_URL . 'categories/index.php'; +$cancel_link = Utils::plugin_url() . 'categories/index.php'; // send all to template $tpl->assign(compact('cat_requested', 'csrf_key', 'cancel_link')); diff --git a/src/admin/categories/supprimer_categorie.php b/src/admin/categories/supprimer_categorie.php index 80b0ac8..287fc74 100644 --- a/src/admin/categories/supprimer_categorie.php +++ b/src/admin/categories/supprimer_categorie.php @@ -18,13 +18,14 @@ if (!$cat_requested) { $csrf_key = 'delete_category_' . $cat_requested->id; -if (f('delete') && $form->check($csrf_key) && !$form->hasErrors()) +$form->runIf(f('delete') && !$form->hasErrors(), + function() use ($cat, $cat_requested, $form) { try { // try to delete category selected and if error catched add it in form $cat->delete($cat_requested->id); - Utils::redirect(PLUGIN_URL . 'categories/index.php'); + Utils::redirect(Utils::plugin_url() . 'categories/index.php'); } catch (\RuntimeException $e) { @@ -36,9 +37,9 @@ if (f('delete') && $form->check($csrf_key) && !$form->hasErrors()) $form->addError($e->getMessage()); } } -} +}); -$cancel_link = PLUGIN_URL . 'categories/index.php'; +$cancel_link = Utils::plugin_url() . 'categories/index.php'; // send to template the category requested diff --git a/src/admin/historique.php b/src/admin/historique.php index 4202c3e..f024abf 100644 --- a/src/admin/historique.php +++ b/src/admin/historique.php @@ -19,7 +19,7 @@ $eqmt_cat = $cat->get($eqmt_requested->category_id); $eqmt_requested->category = $eqmt_cat->name; $mvts = $mvt->AllEqmtMovements($eqmt_requested->id); -$return_link = PLUGIN_URL . 'index.php'; +$return_link = Utils::plugin_url() . 'index.php'; // send all to template $tpl->assign(compact('eqmt_requested', 'mvts', 'return_link')); diff --git a/src/admin/modifier_materiel.php b/src/admin/modifier_materiel.php index 1f1aa54..28f3428 100644 --- a/src/admin/modifier_materiel.php +++ b/src/admin/modifier_materiel.php @@ -26,7 +26,8 @@ $selected_cat = $eqmt_requested->category_id; // check if edit form is submitted $csrf_key = 'edit_equipment_' . $eqmt_requested->id; -if (f('save') && $form->check($csrf_key) && !$form->hasErrors()) +$form->runIf (f('save') && !$form->hasErrors(), + function () use ($eqmt, $eqmt_requested) { // try to edit equipment selected and if error catched add it in form try @@ -35,7 +36,7 @@ if (f('save') && $form->check($csrf_key) && !$form->hasErrors()) 'category_id' => (int) f('category_id'), 'designation' => ucfirst(strtolower(f('designation'))), ]); - Utils::redirect(PLUGIN_URL . 'index.php'); + Utils::redirect(Utils::plugin_url() . 'index.php'); } catch (\RuntimeException $e) { @@ -47,9 +48,9 @@ if (f('save') && $form->check($csrf_key) && !$form->hasErrors()) $form->addError($e->getMessage()); } } -} +}); -$cancel_link = PLUGIN_URL . 'index.php'; +$cancel_link = Utils::plugin_url() . 'index.php'; // send all to template $tpl->assign(compact('eqmt_requested', 'cats', 'selected_cat', 'csrf_key', 'cancel_link')); diff --git a/src/admin/mouvements/entrees/non_repertorie.php b/src/admin/mouvements/entrees/non_repertorie.php index 6fddf30..ad07f8f 100644 --- a/src/admin/mouvements/entrees/non_repertorie.php +++ b/src/admin/mouvements/entrees/non_repertorie.php @@ -22,46 +22,47 @@ $selected_cat = $cats[0]->id; // check if add form is submitted $csrf_key = 'add_entry'; -if (f('save') && $form->check($csrf_key) && !$form->hasErrors()) +$form->runIf(f('save') && !$form->hasErrors(), + function () use ($mvt, $form) { - // 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()); - } - // keep the datas submitted as selected - $selected_kind = f('kind'); - $selected_cat = f('category_id'); - } -} + // 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(Utils::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()); + } + // keep the datas submitted as selected + $selected_kind = f('kind'); + $selected_cat = f('category_id'); + } +}); // make default date (now) $default_date = new \DateTime; @@ -71,7 +72,7 @@ $comment_placeholder = "ex: don reçu de la part de..."; // 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'; +$cancel_link = Utils::plugin_url() . 'mouvements/entrees/index.php'; $legend_part = "non répertorié"; $tpl_materiel_name = "non_repertorie"; diff --git a/src/admin/mouvements/entrees/repertorie.php b/src/admin/mouvements/entrees/repertorie.php index bf24855..0ff737f 100644 --- a/src/admin/mouvements/entrees/repertorie.php +++ b/src/admin/mouvements/entrees/repertorie.php @@ -12,22 +12,23 @@ use Paheko\Utils; // check if add form is submitted $csrf_key = 'add_entry'; -if (f('save') && $form->check($csrf_key) && !$form->hasErrors()) +$form->runIf(f('save') && !$form->hasErrors(), + function () use ($mvt) { - // 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' => f('equipment_id'), - 'mvt_date' => $mvt_date_format, - 'additional_comment' => f('additional_comment'), - ]); - Utils::redirect(PLUGIN_URL . 'mouvements/entrees/index.php'); -} + // 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' => f('equipment_id'), + 'mvt_date' => $mvt_date_format, + 'additional_comment' => f('additional_comment'), + ]); + Utils::redirect(Utils::plugin_url() . 'mouvements/entrees/index.php'); +}); // get the list of all equipments ordered by category $eqmt = new Equipment; @@ -45,7 +46,7 @@ $comment_placeholder = "ex: don reçu de la part de..."; // 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'; +$cancel_link = Utils::plugin_url() . 'mouvements/entrees/index.php'; $legend_part = "répertorié"; $tpl_materiel_name = "repertorie"; diff --git a/src/admin/mouvements/entrees/retour.php b/src/admin/mouvements/entrees/retour.php index 98442d3..a420848 100644 --- a/src/admin/mouvements/entrees/retour.php +++ b/src/admin/mouvements/entrees/retour.php @@ -17,7 +17,8 @@ $selected_eqmt = ""; // check if add form is submitted $csrf_key = 'add_entry'; -if (f('save') && $form->check($csrf_key) && !$form->hasErrors()) +$form->runIf(f('save') && !$form->hasErrors(), + function() use ($mvt, $eqmt, $selected_eqmt, $form) { $eqmt_id = f('equipment_id'); $eqmt_number = (int) f('equipment_number'); @@ -35,7 +36,7 @@ if (f('save') && $form->check($csrf_key) && !$form->hasErrors()) 'mvt_date' => $mvt_date_format, 'additional_comment' => f('additional_comment'), ]); - Utils::redirect(PLUGIN_URL . 'mouvements/entrees/index.php'); + Utils::redirect(Utils::plugin_url() . 'mouvements/entrees/index.php'); } else { // not possible, add error to form @@ -45,7 +46,7 @@ if (f('save') && $form->check($csrf_key) && !$form->hasErrors()) // keep the datas submitted as selected $selected_eqmt = $eqmt_id; } -} +}); // make default date (now) $default_date = new \DateTime; @@ -55,7 +56,7 @@ $comment_placeholder = "ex: retour de prêt..."; // 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'; +$cancel_link = Utils::plugin_url() . 'mouvements/entrees/index.php'; $legend_part = "en retour de location / prêt"; $tpl_materiel_name = "retour"; diff --git a/src/admin/mouvements/entrees/supprimer_entree.php b/src/admin/mouvements/entrees/supprimer_entree.php index 4efea3f..010eb75 100644 --- a/src/admin/mouvements/entrees/supprimer_entree.php +++ b/src/admin/mouvements/entrees/supprimer_entree.php @@ -21,7 +21,8 @@ $eqmt = new Equipment; $csrf_key = 'delete_entry_' . $entry_to_delete->id; -if (f('delete') && $form->check($csrf_key) && !$form->hasErrors()) +$form->runIf(f('delete') && !$form->hasErrors(), + function() use ($mvt, $eqmt, $entry_to_delete) { // delete the movement required $mvt->delete($entry_to_delete->id); @@ -33,8 +34,8 @@ if (f('delete') && $form->check($csrf_key) && !$form->hasErrors()) } catch (\RuntimeException $e){ } - Utils::redirect(PLUGIN_URL . 'mouvements/entrees/index.php'); -} + Utils::redirect(Utils::plugin_url() . 'mouvements/entrees/index.php'); +}); $corresponding_eqmt = $eqmt->get($entry_to_delete->equipment_id); @@ -42,7 +43,7 @@ $corresponding_eqmt = $eqmt->get($entry_to_delete->equipment_id); if ($mvt->PossibilityDeleteEntry($entry_to_delete)) { // it's possible - $cancel_link = PLUGIN_URL . 'mouvements/entrees/index.php'; + $cancel_link = Utils::plugin_url() . 'mouvements/entrees/index.php'; // construct string to send to template $entry_string = (string) $entry_to_delete->equipment_number . " " . $corresponding_eqmt->designation . " à la date du " . date_create_from_format( "Y-m-d", $entry_to_delete->mvt_date)->format("d/m/y"); diff --git a/src/admin/mouvements/sorties/emprunte.php b/src/admin/mouvements/sorties/emprunte.php index 54ea185..ead5632 100644 --- a/src/admin/mouvements/sorties/emprunte.php +++ b/src/admin/mouvements/sorties/emprunte.php @@ -15,7 +15,8 @@ $selected_eqmt = ""; // check if add form is submitted $csrf_key = 'add_output'; -if (f('save') && $form->check($csrf_key) && !$form->hasErrors()) +$form->runIf (f('save') && !$form->hasErrors(), + function() use ($mvt, $eqmt, $selected_eqmt, $form) { $eqmt_id = f('equipment_id'); $eqmt_number = (int) f('equipment_number'); @@ -33,7 +34,7 @@ if (f('save') && $form->check($csrf_key) && !$form->hasErrors()) 'mvt_date' => $mvt_date_format, 'additional_comment' => f('additional_comment'), ]); - Utils::redirect(PLUGIN_URL . 'mouvements/sorties/index.php'); + Utils::redirect(Utils::plugin_url() . 'mouvements/sorties/index.php'); } else { // not possible, add error to form @@ -43,7 +44,7 @@ if (f('save') && $form->check($csrf_key) && !$form->hasErrors()) // keep the datas submitted as selected $selected_eqmt = $eqmt_id; } -} +}); // get list of borrowed equipments ordered by category $eqmts_by_cat = $eqmt->ListAllBorrowedByCategory(); @@ -56,7 +57,7 @@ $comment_placeholder = "ex: matériel rendu..."; // 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/sorties/index.php'; +$cancel_link = Utils::plugin_url() . 'mouvements/sorties/index.php'; $legend_part = "emprunté"; $tpl_materiel_name = "emprunte"; diff --git a/src/admin/mouvements/sorties/stock_disponible.php b/src/admin/mouvements/sorties/stock_disponible.php index 1143750..b47d934 100644 --- a/src/admin/mouvements/sorties/stock_disponible.php +++ b/src/admin/mouvements/sorties/stock_disponible.php @@ -22,36 +22,37 @@ $selected_kind = $kinds[0]; // check if add form is submitted $csrf_key = 'add_output'; -if (f('save') && $form->check($csrf_key) && !$form->hasErrors()) +$form->runIf(f('save') && !$form->hasErrors(), + function() use ($mvt, $eqmt) { - $eqmt_id = f('equipment_id'); - $eqmt_number = (int) f('equipment_number'); - $mvt_date_format = date_create_from_format( - "d/m/Y", f('mvt_date'))->format("Y-m-d"); - // check if it's possible to output this equipment - if ($mvt->PossibilityOwnedEqmtOutput($eqmt_id, $eqmt_number, $mvt_date_format)) - { - // it's possible, add new output - $mvt->add([ - 'side' => 1, - 'kind' => f('kind'), - 'equipment_number' => $eqmt_number, - 'equipment_id' => $eqmt_id, - 'mvt_date' => $mvt_date_format, - 'additional_comment' => f('additional_comment'), - ]); - Utils::redirect(PLUGIN_URL . 'mouvements/sorties/index.php'); - } else - { - // not possible, add error to form - $equiment = $eqmt->get($eqmt_id); - $form->addError( - "Il est impossible de sortir " . (string) $eqmt_number . " " . $equiment->designation . " à la date du " . (string) f('mvt_date') . '.'); - // keep the datas submitted as selected - $selected_eqmt = $eqmt_id; - $selected_kind = f('kind'); - } -} + $eqmt_id = f('equipment_id'); + $eqmt_number = (int) f('equipment_number'); + $mvt_date_format = date_create_from_format( + "d/m/Y", f('mvt_date'))->format("Y-m-d"); + // check if it's possible to output this equipment + if ($mvt->PossibilityOwnedEqmtOutput($eqmt_id, $eqmt_number, $mvt_date_format)) + { + // it's possible, add new output + $mvt->add([ + 'side' => 1, + 'kind' => f('kind'), + 'equipment_number' => $eqmt_number, + 'equipment_id' => $eqmt_id, + 'mvt_date' => $mvt_date_format, + 'additional_comment' => f('additional_comment'), + ]); + Utils::redirect(Utils::plugin_url() . 'mouvements/sorties/index.php'); + } else + { + // not possible, add error to form + $equiment = $eqmt->get($eqmt_id); + $form->addError( + "Il est impossible de sortir " . (string) $eqmt_number . " " . $equiment->designation . " à la date du " . (string) f('mvt_date') . '.'); + // keep the datas submitted as selected + $selected_eqmt = $eqmt_id; + $selected_kind = f('kind'); + } +}); // make default date (now) $default_date = new \DateTime; @@ -61,7 +62,7 @@ $comment_placeholder = "ex: don fait à..."; // 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/sorties/index.php'; +$cancel_link = Utils::plugin_url() . 'mouvements/sorties/index.php'; $legend_part = "en stock disponible"; $tpl_materiel_name = "stock_disponible"; diff --git a/src/admin/mouvements/sorties/supprimer_sortie.php b/src/admin/mouvements/sorties/supprimer_sortie.php index 090cf22..244d4d4 100644 --- a/src/admin/mouvements/sorties/supprimer_sortie.php +++ b/src/admin/mouvements/sorties/supprimer_sortie.php @@ -19,12 +19,13 @@ if (!$output_to_delete) // check if delete form is submitted $csrf_key = 'delete_output_' . $output_to_delete->id; -if (f('delete') && $form->check($csrf_key) && !$form->hasErrors()) +$form->runIf (f('delete') && !$form->hasErrors(), + function () use ($mvt) { // delete the movement required $mvt->delete($output_to_delete->id); - Utils::redirect(PLUGIN_URL . 'mouvements/sorties/index.php'); -} + Utils::redirect(Utils::plugin_url() . 'mouvements/sorties/index.php'); +}); $eqmt = new Equipment; $corresponding_eqmt = $eqmt->get($output_to_delete->equipment_id); @@ -33,7 +34,7 @@ $corresponding_eqmt = $eqmt->get($output_to_delete->equipment_id); if ($mvt->PossibilityDeleteOutput($output_to_delete)) { // it's possible - $cancel_link = PLUGIN_URL . 'mouvements/sorties/index.php'; + $cancel_link = Utils::plugin_url() . 'mouvements/sorties/index.php'; // construct string to send to template $output_string = (string) $output_to_delete->equipment_number . " " . $corresponding_eqmt->designation . " à la date du " . date_create_from_format( "Y-m-d", $output_to_delete->mvt_date)->format("d/m/y"); diff --git a/src/templates/_nav.tpl b/src/templates/_nav.tpl index 98bd414..2c3fd97 100644 --- a/src/templates/_nav.tpl +++ b/src/templates/_nav.tpl @@ -1,5 +1,5 @@ -{include file="admin/_head.tpl" title="%s"|args:$plugin.nom current="plugin_%s"|args:$plugin.id} +{include file="_head.tpl" title="%s"|args:$plugin.nom current="plugin_%s"|args:$plugin.id}