<?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['acg_name']);
        $color = preg_replace("|[^a-zA-Z0-9]|", "", strtoupper(sanitize($_POST['acg_color'])));
        $desc = sanitize($_POST['acg_description']);

        $sql = "INSERT INTO assetclassgroup
                    (assetclassgroup_name, assetclassgroup_color, assetclassgroup_description)
                VALUE
                    (?, ?, ?)";
        $sth = $dbh->prepare($sql);
        try {
            $sth->execute([$name, $color, $desc]);
        } catch (PDOException $e) {
            $g_error->Add($e->getMessage());
        }        $id = $dbh->lastInsertId();
        $action = ACT_VIEW;
        break;

    case 'update':
        $acg_name = sanitize($_POST['acg_name']);
        $acg_desc = sanitize($_POST['acg_description']);
        $acg_color = preg_replace("|[^a-zA-Z0-9]|", "", strtoupper(sanitize($_POST['acg_color'])));

        $sql = "UPDATE assetclassgroup SET
                    assetclassgroup_name=?, assetclassgroup_color=?, assetclassgroup_description=?
                WHERE assetclassgroup_id=?";
        $sth = $dbh->prepare($sql);
        try {
            $sth->execute([$acg_name, $acg_color, $acg_desc, $id]);
        } catch (PDOException $e) {
            $g_error->Add($e->getMessage());
        }
        $action = ACT_VIEW;
        break;

    case 'delete':
        $sth = $dbh->prepare("SELECT COUNT(*) FROM assetclass WHERE assetclassgroup_id=?");
        $sth->execute([$id]);
        if ($sth->fetchColumn() > 0) {
            $g_warning->Add("Objektklassengruppe kann nicht gelöscht werden, da noch zugeordnete Objektklassen vorhanden sind.");
            $action = ACT_VIEW;
            break;
        }
        $sth = $dbh->prepare("DELETE FROM assetclassgroup WHERE assetclassgroup_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 =====================================================

$smarty->assign("scripts", 'jscolor.js');
include("header.php");

if ($action == ACT_DEFAULT):
// ========== VARIANT: default behavior =======================================

$sql = "SELECT g.assetclassgroup_id AS id, g.assetclassgroup_name AS name,
            g.assetclassgroup_color AS color, g.assetclassgroup_description AS description,
            COUNT(c.assetclass_id) AS count_class
        FROM assetclassgroup AS g LEFT JOIN assetclass AS c USING (assetclassgroup_id)
        GROUP BY id, name, color, description
        ORDER BY g.assetclassgroup_name";
$sth = $dbh->query($sql);
$smarty->assign('assetclassgroups', $sth->fetchAll(PDO::FETCH_ASSOC));

$smarty->display("assetclassgroup.tpl");

elseif ($action == ACT_ADD):
// ========== VARIANT: add record =============================================

$smarty->display("assetclassgroupadd.tpl");

elseif ($action == ACT_VIEW):
// ========== VARIANT: view single record =====================================

$sql = "SELECT assetclassgroup_id AS id,
            assetclassgroup_name AS name,
            assetclassgroup_color AS color,
            assetclassgroup_description AS description
        FROM assetclassgroup
        WHERE assetclassgroup_id=?";
$sth = $dbh->prepare($sql);
$sth->execute([$id]);
$smarty->assign("assetclassgroup", $sth->fetch(PDO::FETCH_OBJ));

$sql = "SELECT assetclass_id, assetclass_name
        FROM assetclass
        WHERE assetclassgroup_id=?
        ORDER BY assetclass_name";
$sth = $dbh->prepare($sql);
$sth->execute([$id]);
$smarty->assign("assetclasses", $sth->fetchAll(PDO::FETCH_ASSOC));

$smarty->display("assetclassgroupview.tpl");

elseif ($action == ACT_EDIT):
// ========== VARIANT: edit single record =====================================

$sql = "SELECT assetclassgroup_id AS id, assetclassgroup_name AS name,
            assetclassgroup_color AS color,
            assetclassgroup_description AS description
        FROM assetclassgroup
        WHERE assetclassgroup_id=?";
$sth = $dbh->prepare($sql);
$sth->execute([$id]);
$smarty->assign("assetclassgroup", $sth->fetch(PDO::FETCH_OBJ));

$smarty->display("assetclassgroupedit.tpl");

elseif ($action == ACT_DELETE):
// ========== VARIANT: delete record ==========================================

$sql = "SELECT assetclassgroup_id AS id, assetclassgroup_name AS name
        FROM assetclassgroup
        WHERE assetclassgroup_id=?";
$sth = $dbh->prepare($sql);
$sth->execute([$id]);
$smarty->assign("assetclassgroup", $sth->fetch(PDO::FETCH_OBJ));

$smarty->display("assetclassgroupdel.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');