<?php
/*****************************************************************************
IP Reg, a PHP/MySQL IPAM tool
Copyright (C) 2007-2009 Wietse Warendorff (up to v0.5)
Copyright (C) 2011-2023 Thomas Hooge
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);
try {
$sth->execute([$name, $description, $group_id]);
} catch (PDOException $e) {
$g_error->Add($e->getMessage());
}
$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);
try {
$sth->execute([$name, $description, $group_id, $id]);
} catch (PDOException $e) {
$g_error->Add($e->getMessage());
} $action = ACT_VIEW;
break;
case 'delete':
$sth = $dbh->prepare("SELECT COUNT(*) FROM asset WHERE assetclass_id=?");
$sth->execute([$id]);
if ($sth->fetchColumn() > 0) {
$g_warning->Add("Objektklasse kann nicht gelöscht werden, da noch zugeordnete Objekte vorhanden sind.");
$action = ACT_VIEW;
break;
}
$sth = $dbh->prepare("DELETE FROM assetclass WHERE assetclass_id=?");
try {
$sth->execute([$id]);
} catch (PDOException $e) {
$g_error->Add($e->getMessage());
}
$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
FROM assetclass AS c
LEFT JOIN assetclassgroup AS g USING (assetclassgroup_id)
LEFT JOIN asset AS a USING (assetclass_id)
GROUP BY id, name, group_id, group_name, color
ORDER BY c.assetclass_name";
$sth = $dbh->query($sql);
$smarty->assign("assetclasses", $sth->fetchAll(PDO::FETCH_ASSOC));
$smarty->display("assetclass.tpl");
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([$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 "< p > Unknown function call: Please report to system development!< / p > \n";
endif; // $action == ...
// ========== END OF VARIANTS =================================================
$smarty->display('footer.tpl');