From ccdcfb968c053f3d096478be54803748e173d5cb Mon Sep 17 00:00:00 2001 From: Thomas Hooge Date: Fri, 3 Mar 2023 14:36:39 +0100 Subject: [PATCH] Refactored assetclass --- assetclass.php | 136 +++++++++++++++++++++++++++++++++++- assetclassadd.php | 20 ------ assetclassdel.php | 26 ------- assetclassedit.php | 30 -------- assetclassview.php | 37 ---------- lang/de.php | 2 + lang/en.php | 2 + submit.php | 12 ++-- tpl/asset.tpl | 2 +- tpl/assetadd.tpl | 2 +- tpl/assetclass.tpl | 4 +- tpl/assetclassadd.tpl | 7 +- tpl/assetclassdel.tpl | 9 ++- tpl/assetclassedit.tpl | 7 +- tpl/assetclassgroupadd.tpl | 2 +- tpl/assetclassgroupview.tpl | 2 +- tpl/assetclassview.tpl | 6 +- tpl/assetview.tpl | 2 +- tpl/nat.tpl | 2 +- 19 files changed, 165 insertions(+), 145 deletions(-) delete mode 100644 assetclassadd.php delete mode 100644 assetclassdel.php delete mode 100644 assetclassedit.php delete mode 100644 assetclassview.php diff --git a/assetclass.php b/assetclass.php index f146fb5..c0bda51 100644 --- a/assetclass.php +++ b/assetclass.php @@ -8,8 +8,68 @@ SPDX-License-Identifier: GPL-3.0-or-later *****************************************************************************/ include("includes.php"); + +if (isset($_REQUEST['id'])) { + $id = (int) $_REQUEST['id'] or $id = 0; +} + +// ========== ACTIONS START =================================================== +switch ($submit = form_get_action()) { + + case NULL: break; + + case 'add': $action = ACT_ADD; break; + case 'view': $action = ACT_VIEW; break; + case 'edit': $action = ACT_EDIT; break; + case 'del': $action = ACT_DELETE; break; + + case 'insert': + $name = sanitize($_POST['assetclass_name']); + $description = sanitize($_POST['assetclass_description']); + $group_id = sanitize($_POST['assetclassgroup_id']); + + $sql = "INSERT INTO assetclass + (assetclass_name, assetclass_description, assetclassgroup_id) + VALUE + (?, ?, ?)"; + $sth = $dbh->prepare($sql); + $sth->execute([$name, $description, $group_id]); + $id = $dbh->lastInsertId(); + $action = ACT_VIEW; + break; + + case 'update': + $name = sanitize($_POST['assetclass_name']); + $description = sanitize($_POST['assetclass_description']); + $group_id = sanitize($_POST['assetclassgroup_id']); + + $sql = "UPDATE assetclass SET + assetclass_name=?, assetclass_description=?, + assetclassgroup_id=? + WHERE assetclass_id=?"; + $sth = $dbh->prepare($sql); + $sth->execute([$name, $description, $group_id, $id]); + $action = ACT_VIEW; + break; + + case 'delete': + $sth = $dbh->prepare("DELETE FROM assetclass WHERE assetclass_id=?"); + $sth->execute([$id]); + $action = ACT_DEFAULT; + break; + + default: + $g_error->Add(submit_error($submit)); + $valid = FALSE; +} + +// ========== ACTIONS END ===================================================== + include("header.php"); - + +if ($action == ACT_DEFAULT): +// ========== VARIANT: default behavior ======================================= + $sql = "SELECT c.assetclass_id AS id, c.assetclass_name AS name, c.assetclassgroup_id AS group_id, g.assetclassgroup_name AS group_name, g.assetclassgroup_color AS color, COUNT(a.asset_id) AS count_asset @@ -23,5 +83,77 @@ $smarty->assign("assetclasses", $sth->fetchAll(PDO::FETCH_ASSOC)); $smarty->display("assetclass.tpl"); -include("footer.php"); +elseif ($action == ACT_ADD): +// ========== VARIANT: add record ============================================= + +if (isset($_GET['assetclassgroup_id'])) { + $group_id = sanitize($_GET['assetclassgroup_id']); +} else { + $group_id = ''; +} +$smarty->assign("group_id", $group_id); +$smarty->assign("assetclassgroup_options", db_get_options_assetclassgroup()); + +$smarty->display("assetclassadd.tpl"); + +elseif ($action == ACT_VIEW): +// ========== VARIANT: view single record ===================================== + +$sql = "SELECT a.assetclass_id, a.assetclass_name, g.assetclassgroup_id, + a.assetclass_description, + g.assetclassgroup_name, g.assetclassgroup_color + FROM assetclass AS a LEFT OUTER JOIN assetclassgroup AS g USING (assetclassgroup_id) + WHERE a.assetclass_id=?"; +$sth = $dbh->prepare($sql); +$sth->execute([$id]); +$smarty->assign("assetclass", $sth->fetch(PDO::FETCH_OBJ)); + +$sql = "SELECT asset_id, asset_name, + CONCAT(LEFT(asset_info, 80), IF(CHAR_LENGTH(asset_info)>80,'...','')) AS asset_info + FROM asset + WHERE assetclass_id=? + ORDER BY asset_name"; +$sth = $dbh->prepare($sql); +$sth->execute([$assetclass_id]); +$smarty->assign("assets", $sth->fetchAll(PDO::FETCH_ASSOC)); + +$smarty->display("assetclassview.tpl"); + +elseif ($action == ACT_EDIT): +// ========== VARIANT: edit single record ===================================== + +$sql = "SELECT assetclass_id AS id, assetclass_name AS name, + assetclass_description AS description, + assetclassgroup_id AS group_id + FROM assetclass + WHERE assetclass_id=?"; +$sth = $dbh->prepare($sql); +$sth->execute([$id]); +$smarty->assign("assetclass", $sth->fetch(PDO::FETCH_OBJ)); +$smarty->assign("assetclassgroup_options", db_get_options_assetclassgroup()); +$smarty->display("assetclassedit.tpl"); + +elseif ($action == ACT_DELETE): +// ========== VARIANT: delete record ========================================== + +$sql = "SELECT assetclass_id AS id, assetclass_name AS name + FROM assetclass + WHERE assetclass_id=?"; +$sth = $dbh->prepare($sql); +$sth->execute([$id]); +$smarty->assign("assetclass", $sth->fetch(PDO::FETCH_OBJ)); + +$smarty->display("assetclassdel.tpl"); + +else: +// ========== ERROR UNKNOWN VARIANT =========================================== + +echo "

Unknown function call: Please report to system development!

\n"; + +endif; // $action == ... +// ========== END OF VARIANTS ================================================= + +$smarty->display('footer.tpl'); + +// include("footer.php"); ?> diff --git a/assetclassadd.php b/assetclassadd.php deleted file mode 100644 index 62a54f2..0000000 --- a/assetclassadd.php +++ /dev/null @@ -1,20 +0,0 @@ -assign("assetclassgroup_options", db_get_options_assetclassgroup()); -$smarty->display("assetclassadd.tpl"); - -include("footer.php"); -?> \ No newline at end of file diff --git a/assetclassdel.php b/assetclassdel.php deleted file mode 100644 index da8c57e..0000000 --- a/assetclassdel.php +++ /dev/null @@ -1,26 +0,0 @@ -prepare($sql); -$sth->execute([$assetclass_id]); -$smarty->assign("assetclass", $sth->fetch(PDO::FETCH_OBJ)); - -$smarty->display("assetclassdel.tpl"); - -include("footer.php"); -?> diff --git a/assetclassedit.php b/assetclassedit.php deleted file mode 100644 index 4d16503..0000000 --- a/assetclassedit.php +++ /dev/null @@ -1,30 +0,0 @@ -prepare($sql); -$sth->execute([$assetclass_id]); - -$smarty->assign("assetclass", $sth->fetch(PDO::FETCH_OBJ)); - -$smarty->assign("assetclassgroup_options", db_get_options_assetclassgroup()); - -$smarty->display("assetclassedit.tpl"); - -include("footer.php"); -?> diff --git a/assetclassview.php b/assetclassview.php deleted file mode 100644 index b5c17dd..0000000 --- a/assetclassview.php +++ /dev/null @@ -1,37 +0,0 @@ -prepare($sql); -$sth->execute([$assetclass_id]); -$smarty->assign("assetclass", $sth->fetch(PDO::FETCH_OBJ)); - -$sql = "SELECT asset_id, asset_name, - CONCAT(LEFT(asset_info, 80), IF(CHAR_LENGTH(asset_info)>80,'...','')) AS asset_info - FROM asset - WHERE assetclass_id=? - ORDER BY asset_name"; -$sth = $dbh->prepare($sql); -$sth->execute([$assetclass_id]); -$smarty->assign("assets", $sth->fetchAll(PDO::FETCH_ASSOC)); - -$smarty->display("assetclassview.tpl"); - -include("footer.php"); -?> diff --git a/lang/de.php b/lang/de.php index 464a3c8..eb0fa59 100644 --- a/lang/de.php +++ b/lang/de.php @@ -45,6 +45,7 @@ $lang = array( 'lang_statistics' => 'Statistik', 'lang_subitem' => 'Sub-Item', 'lang_submit' => 'Absenden', + 'lang_save' => 'Speichern', 'lang_unassigned' => 'Nicht zugeordnet', 'lang_warning' => 'Warnung', 'lang_description' => 'Bezeichnung', @@ -134,6 +135,7 @@ $lang = array( 'lang_nat_type_1' => 'Verbergen', 'lang_nat_type_2' => 'Statisch', 'lang_nat_type_3' => 'Dynamisch', + 'lang_nat_none' => 'Es sind keine NAT-Regeln vorhanden', 'lang_search_results_found' => 'Anzahl der gefundenen Ergebnisse: ', diff --git a/lang/en.php b/lang/en.php index 1ebf284..b35c341 100644 --- a/lang/en.php +++ b/lang/en.php @@ -45,6 +45,7 @@ $lang = array( 'lang_statistics' => 'Statistics', 'lang_subitem' => 'Sub-Item', 'lang_submit' => 'Submit', + 'lang_save' => 'Save', 'lang_unassigned' => 'Unassigned', 'lang_warning' => 'Warning', 'lang_description' => 'Description', @@ -134,6 +135,7 @@ $lang = array( 'lang_nat_type_1' => 'Hide', 'lang_nat_type_2' => 'Static', 'lang_nat_type_3' => 'Dynamic', + 'lang_nat_none' => 'There are no nat rules defined', 'lang_search_results_found' => 'Total results found', diff --git a/submit.php b/submit.php index 902a870..5aa3033 100644 --- a/submit.php +++ b/submit.php @@ -119,7 +119,7 @@ if (isset($_POST['add'])) { header_location("assetview.php?asset_id=" . $dbh->lastInsertId()); break; - +/* refactored case ("assetclass") : $name = sanitize($_POST['assetclass_name']); $description = sanitize($_POST['assetclass_description']); @@ -133,7 +133,7 @@ if (isset($_POST['add'])) { $sth->execute([$name, $description, $group_id]); header_location("assetclassview.php?assetclass_id=" . $dbh->lastInsertId()); - break; + break; */ case ("assetclassgroup") : $name = sanitize($_POST['acg_name']); @@ -398,7 +398,7 @@ if (isset($_POST['del'])) { header_location("asset.php"); break; - +/* refactored case ("assetclass") : $assetclass_id = sanitize($_POST['assetclass_id']); @@ -406,7 +406,7 @@ if (isset($_POST['del'])) { $sth->execute([$assetclass_id]); header_location("assetclass.php"); - break; + break; */ case ("assetclassgroup") : $assetclassgroup_id = sanitize($_POST['assetclassgroup_id']); @@ -548,7 +548,7 @@ if (isset($_POST['edit'])) { $asset_id]); header_location("assetview.php?asset_id=" . $asset_id); - +/* refactored case ("assetclass") : $id = sanitize($_POST['assetclass_id']); $name = sanitize($_POST['assetclass_name']); @@ -563,7 +563,7 @@ if (isset($_POST['edit'])) { $sth->execute([$name, $description, $group_id, $id]); header_location("assetclassview.php?assetclass_id=" . $id); - break; + break; */ case ("assetclassgroup") : $acg_id = sanitize($_POST['acg_id']); diff --git a/tpl/asset.tpl b/tpl/asset.tpl index 6fd3b61..498289c 100644 --- a/tpl/asset.tpl +++ b/tpl/asset.tpl @@ -40,7 +40,7 @@ {$asset.asset_name} - {$asset.assetclass_name} + {$asset.assetclass_name} {$asset.asset_info} diff --git a/tpl/assetadd.tpl b/tpl/assetadd.tpl index e22d8b3..c61f8a2 100644 --- a/tpl/assetadd.tpl +++ b/tpl/assetadd.tpl @@ -27,7 +27,7 @@ {$lang_asset_name} - + diff --git a/tpl/assetclass.tpl b/tpl/assetclass.tpl index 791e6ff..5b57b29 100644 --- a/tpl/assetclass.tpl +++ b/tpl/assetclass.tpl @@ -6,7 +6,7 @@ {if $suser_add || $suser_admin} - {$lang_assetclass_add} + {$lang_assetclass_add} {/if} @@ -27,7 +27,7 @@ {foreach item=assetclass from=$assetclasses} - {$assetclass.name} + {$assetclass.name} #{$assetclass.color} diff --git a/tpl/assetclassadd.tpl b/tpl/assetclassadd.tpl index 3f2b1c7..2d28892 100644 --- a/tpl/assetclassadd.tpl +++ b/tpl/assetclassadd.tpl @@ -1,5 +1,4 @@ -
- + @@ -8,7 +7,7 @@
{$lang_cancel} - +
@@ -27,7 +26,7 @@ {$lang_assetclass_name} - + diff --git a/tpl/assetclassdel.tpl b/tpl/assetclassdel.tpl index 2f2f762..0e03253 100644 --- a/tpl/assetclassdel.tpl +++ b/tpl/assetclassdel.tpl @@ -1,6 +1,5 @@ - - - + + @@ -9,7 +8,7 @@
{$lang_cancel} - +
@@ -28,7 +27,7 @@ {$lang_assetclass_name} - {$assetclass->name} + {$assetclass->name} diff --git a/tpl/assetclassedit.tpl b/tpl/assetclassedit.tpl index 80096cf..24ab6fb 100644 --- a/tpl/assetclassedit.tpl +++ b/tpl/assetclassedit.tpl @@ -1,6 +1,5 @@ - - - + + @@ -10,7 +9,7 @@
{$lang_cancel} - +
diff --git a/tpl/assetclassgroupadd.tpl b/tpl/assetclassgroupadd.tpl index 2dd7408..d02ac57 100644 --- a/tpl/assetclassgroupadd.tpl +++ b/tpl/assetclassgroupadd.tpl @@ -27,7 +27,7 @@ {$lang_assetclassgroup_name} - + diff --git a/tpl/assetclassgroupview.tpl b/tpl/assetclassgroupview.tpl index df4ec19..65091ae 100644 --- a/tpl/assetclassgroupview.tpl +++ b/tpl/assetclassgroupview.tpl @@ -69,7 +69,7 @@ {foreach item=assetclass from=$assetclasses} - {$assetclass.assetclass_name}
+ {$assetclass.assetclass_name}
{/foreach} diff --git a/tpl/assetclassview.tpl b/tpl/assetclassview.tpl index f2154fe..8f59af8 100644 --- a/tpl/assetclassview.tpl +++ b/tpl/assetclassview.tpl @@ -9,10 +9,10 @@ {$lang_asset_add} {/if} {if $suser_edit} - {$lang_assetclass_edit} + {$lang_assetclass_edit} {/if} {if $suser_delete} - {$lang_assetclass_add} + {$lang_assetclass_add} {/if} @@ -33,7 +33,7 @@ {if $suser_edit} - {$assetclass->assetclass_name} + {$assetclass->assetclass_name} {/if} diff --git a/tpl/assetview.tpl b/tpl/assetview.tpl index 1bfd00b..e7a1a4d 100644 --- a/tpl/assetview.tpl +++ b/tpl/assetview.tpl @@ -84,7 +84,7 @@ {$lang_assetclass_name} - {$asset->assetclass_name} + {$asset->assetclass_name} diff --git a/tpl/nat.tpl b/tpl/nat.tpl index 2d9ab1e..83c9773 100644 --- a/tpl/nat.tpl +++ b/tpl/nat.tpl @@ -42,7 +42,7 @@ {foreachelse} - + {$lang_nat_none}