<?php
function createMySydeLog($type, $type_id, $state){
    $curr_date = new DateTime();
    $insertquery = "INSERT INTO mysyde_log
    (main_admin_user_id, datetime, type, type_id, state)
    VALUES (
        " . (int)$GLOBALS["admin_user"]['id'] . ",
        '" . $curr_date->format('Y-m-d H:i:s') . "',
        '" . $type . "',
        " . $type_id . ",
        '" . $state . "'
    )";
    @mysqli_query($GLOBALS['mysql_con'], $insertquery);
}

function loadMySydeLog($log_limit){
    $select = $GLOBALS['pdo_conn']->prepare("SELECT * FROM mysyde_log LEFT JOIN main_admin_user ON main_admin_user.id = mysyde_log.main_admin_user_id ORDER BY datetime DESC LIMIT ".$log_limit);
    $select->execute();
    $res_select = $select->fetchALL(PDO::FETCH_ASSOC);
    foreach ($res_select as $key => $value) {
        $message = '';
        $select_type = $GLOBALS['pdo_conn']->prepare("SELECT * FROM ".$value['type']." WHERE id = ".$value['type_id']);
        $select_type->execute();
        $res_type = $select_type->fetch(PDO::FETCH_ASSOC);

        if($res_type != NULL){
            if($value['type'] == 'main_admin_user'){
                $name = $res_type['name'];
            }else if($value['type'] == 'main_navigation'){
                $name = $res_type['menu_name'];
            }else if($value['type'] == 'main_page'){
                $name = $res_type['title'];
            }else{
                $name = $res_type['description'];
            }
        }else {
            $name = "mit der ID " . $value['type_id'];
        }

        $admin = $value['login'];

        switch ($value['type']) {
            case 'main_admin_user':
                $message = log_main_admin_user($admin, $name, $value['state'], $value['type_id']);
                break;
            case 'main_collection': 
                $message = log_main_collection($admin, $name, $value['state']);
                break;
            case 'main_collection_setup': 
                $message = log_main_collection_setup($admin, $name, $value['state']);
                break;
            case 'main_page': 
                $message = log_main_page($admin, $name, $value['state']);
                break;
            case 'main_navigation': 
                $message = log_main_navigation($admin, $name, $value['state']);
                break;
            case 'main_faq': 
                $message = log_main_faq($admin, $name, $value['state']);
                break;
            case 'main_faq_setup_group': 
                $message = log_main_faq_setup_group($admin, $name, $value['state']);
                break;
            default:
                break;
        }

        $datetime = new Datetime($value['datetime']);
        echo "<p><span class='log_date'>[".$datetime->format('d.m.Y H:i')."]</span> " .$message. "</p>";
    }
}

function log_main_collection($admin, $name, $state){
    switch ($state) {
        case 'INSERT':
            $message = $admin . " hat die Kollektion <span class='log_content'>".$name."</span> hinzugefügt.";
            break;
        case 'UPDATE':
            $message = $admin . " hat die Kollektion <span class='log_content'>".$name."</span> bearbeitet.";
            break;
        case 'DELETE':
            $message = $admin . " hat die Kollektion <span class='log_content'>".$name."</span> gelöscht.";
            break;
        default:
            break;
    }
    return $message;
}

function log_main_collection_setup($admin, $name, $state){
    switch ($state) {
        case 'INSERT':
            $message = $admin . " hat die Kollektion <span class='log_content'>".$name."</span> hinzugefügt.";
            break;
        case 'UPDATE':
            $message = $admin . " hat die Kollektion <span class='log_content'>".$name."</span> bearbeitet.";
            break;
        case 'DELETE':
            $message = $admin . " hat die Kollektion <span class='log_content'>".$name."</span> gelöscht.";
            break;
        default:
            break;
    }
    return $message;
}

function log_main_admin_user($admin, $name, $state, $id){
    switch ($state) {
        case 'LOGIN':
            $message = $name . " hat sich angemeldet.";
            break;
        case 'LOGOUT':
            $message = $name . " hat sich abgemeldet.";
            break;
        case 'INSERT':
            $message = $admin . " hat den Nutzer <a class='' href='". get_menu_link('config/config_users', $GLOBALS['log_site'], $GLOBALS['log_language'], '?action=open_card&input_id='.$id) ."'>".$name."</a> hinzugefügt.";
            break;
        case 'UPDATE':
            $message = $admin . " hat den Nutzer <a class='' href='". get_menu_link('config/config_users', $GLOBALS['log_site'], $GLOBALS['log_language'], '?action=open_card&input_id='.$id) ."'>".$name."</a> bearbeitet.";
            break;
        case 'DELETE':
            $message = $admin . " hat den Nutzer <a class='' href='". get_menu_link('config/config_users', $GLOBALS['log_site'], $GLOBALS['log_language'], '?action=open_card&input_id='.$id) ."'>".$name."</a> gelöscht.";
            break;
        default:
            break;
    }
    return $message;
}

function log_main_page($admin, $name, $state){
    switch ($state) {
        case 'INSERT':
            $message = $admin . " hat die Seite <span class='log_content'>".$name."</span> hinzugefügt.";
            break;
        case 'UPDATE':
            $message = $admin . " hat die Seite <span class='log_content'>".$name."</span> bearbeitet.";
            break;
        case 'DELETE':
            $message = $admin . " hat die Seite <span class='log_content'>".$name."</span> gelöscht.";
            break;
        default:
            break;
    }
    return $message;
}

function log_main_navigation($admin, $name, $state){
    switch ($state) {
        case 'INSERT':
            $message = $admin . " hat die Navigation <span class='log_content'>".$name."</span> hinzugefügt.";
            break;
        case 'UPDATE':
            $message = $admin . " hat die Navigation <span class='log_content'>".$name."</span> bearbeitet.";
            break;
        case 'DELETE':
            $message = $admin . " hat die Navigation <span class='log_content'>".$name."</span> gelöscht.";
            break;
        default:
            break;
    }
    return $message;
}

function log_main_faq($admin, $name, $state){
    switch ($state) {
        case 'INSERT':
            $message = $admin . " hat die FAQ Frage <span class='log_content'>".$name."</span> hinzugefügt.";
            break;
        case 'UPDATE':
            $message = $admin . " hat die FAQ Frage <span class='log_content'>".$name."</span> bearbeitet.";
            break;
        case 'DELETE':
            $message = $admin . " hat die FAQ Frage <span class='log_content'>".$name."</span> gelöscht.";
            break;
        default:
            break;
    }
    return $message;
}

function log_main_faq_setup_group($admin, $name, $state){
    switch ($state) {
        case 'INSERT':
            $message = $admin . " hat die FAQ Gruppe <span class='log_content'>".$name."</span> hinzugefügt.";
            break;
        case 'UPDATE':
            $message = $admin . " hat die FAQ Gruppe <span class='log_content'>".$name."</span> bearbeitet.";
            break;
        case 'DELETE':
            $message = $admin . " hat die FAQ Gruppe <span class='log_content'>".$name."</span> gelöscht.";
            break;
        default:
            break;
    }
    return $message;
}
