Imports MySql.Data.MySqlClient


Public Class cDBHomepage



    Public Shared Function GetNewOpenConnection() As MySqlConnection
        Dim cn As New MySqlConnection()
        cn.ConnectionString = "Server=localhost;Database=verag_homepage;Uid=sec_user;Pwd=eKcGZr59zAa2BEWU;"
        cn.Open()
        Return cn
    End Function

    Public Function qry_menu_tags(Optional laenderkuerzel As String = "de", Optional ref As String = "") As List(Of cNavigation)
        If laenderkuerzel = "" Then laenderkuerzel = "de"
        Dim navigation As New List(Of cNavigation)
        Using con As MySqlConnection = GetNewOpenConnection()
            Using cmd As New MySqlCommand
                cmd.Connection = con
                Dim sql As String = "SELECT * FROM menu_tags WHERE " ' ORDER BY `menu_tags`.`order` ASC"
                If ref <> "" Then
                    sql &= " ref_id='" & ref & "' "
                Else
                    sql &= " instance='0' "
                End If
                sql &= " ORDER BY `menu_tags`.`order` ASC "
                cmd.CommandText = sql
                Dim reader As MySqlDataReader = cmd.ExecuteReader
                Try
                    'reader = cmd.ExecuteReader
                    While reader.Read()
                        Dim n As New cNavigation
                        n.bezeichnung = reader.Item("desc_" & laenderkuerzel)
                        n.id = reader.Item("id")
                        n.slogan = reader.Item("menu_slogan_" & laenderkuerzel)
                        n.menu_logo = reader.Item("menu_logo")
                        n.subnavigation = qry_menu_tags(laenderkuerzel, n.id)
                        navigation.Add(n)
                    End While
                    reader.Close()
                Catch ex As Exception
                    MsgBox("Fehler 01: " & ex.Message)
                End Try
                con.Close()
            End Using
        End Using
        Return navigation
    End Function


 

    Public Function qry_contentNew(menuID As String, laenderkuerzel As String) As hp_content
        Try
            Dim hp_content As New hp_content
            Using con As MySqlConnection = GetNewOpenConnection()
                Using cmd As New MySqlCommand
                    cmd.Connection = con
                    Dim sql As String = " SELECT * " &
                                        " FROM menu_tags, homepage_content " &
                                        " WHERE menu_tags.id=homepage_content.hp_refId AND menu_tags.id = '" & menuID & "'"

                    cmd.CommandText = sql
                    Dim reader As MySqlDataReader = cmd.ExecuteReader
                    Try
                        'reader = cmd.ExecuteReader
                        While reader.Read()
                            Dim b As New cBlock

                            hp_content.hp_id = reader.Item("id")
                            hp_content.hp_content = reader.Item(getSpaltenBezeichnung(laenderkuerzel))
                            hp_content.hp_slogan = reader.Item(getslogan(laenderkuerzel))
                            hp_content.hp_title = reader.Item(getTitle(laenderkuerzel))
                            hp_content.hp_imgPath = reader.Item("menu_logo")
                            hp_content.hp_instance = reader.Item("instance")
                            hp_content.hp_refIdToUpperMenue = reader.Item("ref_id")

                            Return hp_content
                        End While
                        reader.Close()
                    Catch ex As Exception
                        MsgBox("Fehler 02: " & ex.Message)
                    End Try
                    con.Close()
                End Using
            End Using
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        Return Nothing
    End Function






    Function getSpaltenBezeichnung(laenderkuerzel As String) As String
        Dim spalte = "hp_de"
        Select Case laenderkuerzel
            Case "de" : spalte = "hp_de"
            Case "bg" : spalte = "hp_bg"
            Case "yu" : spalte = "hp_yu"
            Case "ro" : spalte = "hp_ro"
            Case "tr" : spalte = "hp_tr"
            Case "en" : spalte = "hp_en"
        End Select
        Return spalte
    End Function

    Function getSlogan(laenderkuerzel As String) As String
        Dim spalte = "hp_de"
        Select Case laenderkuerzel
            Case "de" : spalte = "menu_slogan_de"
            Case "bg" : spalte = "menu_slogan_bg"
            Case "yu" : spalte = "menu_slogan_yu"
            Case "ro" : spalte = "menu_slogan_ro"
            Case "tr" : spalte = "menu_slogan_tr"
            Case "en" : spalte = "menu_slogan_en"
        End Select
        Return spalte
    End Function

    Function getTitle(laenderkuerzel As String) As String
        Dim spalte = "hp_de"
        Select Case laenderkuerzel
            Case "de" : spalte = "desc_de"
            Case "bg" : spalte = "desc_bg"
            Case "yu" : spalte = "desc_yu"
            Case "ro" : spalte = "desc_ro"
            Case "tr" : spalte = "desc_tr"
            Case "en" : spalte = "desc_en"
        End Select
        Return spalte
    End Function


    Public Sub update_HPContentNew(hp_content As hp_content, laenderkuerzel As String)
        Dim sql As String = " UPDATE homepage_content " &
                            "SET " & getSpaltenBezeichnung(laenderkuerzel) & "=@hp WHERE hp_refId=@hp_refId "

        Using conn As MySqlConnection = GetNewOpenConnection()
            Using cmd As New MySqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@hp", hp_content.hp_content)
                cmd.Parameters.AddWithValue("@hp_refId", hp_content.hp_id)
                Try
                    cmd.ExecuteNonQuery()

                Catch ex As MySqlException
                    MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Datensatz kann nicht gespeichert werden!" & vbCrLf & vbCrLf & ex.Message, vbExclamation, "Fehler beim Speichern Programm")
                End Try
            End Using
            conn.Close()
        End Using


        Dim sql2 As String = " UPDATE menu_tags " &
                            "SET " & getSlogan(laenderkuerzel) & "=@slogan, " & getTitle(laenderkuerzel) & "=@title, menu_logo=@img  WHERE id=@id "

        Using conn As MySqlConnection = GetNewOpenConnection()
            Using cmd As New MySqlCommand(sql2, conn)
                cmd.Parameters.AddWithValue("@slogan", hp_content.hp_slogan)
                cmd.Parameters.AddWithValue("@title", hp_content.hp_title)
                cmd.Parameters.AddWithValue("@img", hp_content.hp_imgPath)
                cmd.Parameters.AddWithValue("@id", hp_content.hp_id)
                Try
                    cmd.ExecuteNonQuery()

                Catch ex As MySqlException
                    MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Datensatz kann nicht gespeichert werden!" & vbCrLf & vbCrLf & ex.Message, vbExclamation, "Fehler beim Speichern Programm")
                End Try
            End Using
            conn.Close()
        End Using
    End Sub

    Public Function getInstance(id As String) As Integer
        Dim instance As Integer = -1
        Try
            Dim hp_content As New hp_content
            Using con As MySqlConnection = GetNewOpenConnection()
                Using cmd As New MySqlCommand
                    cmd.Connection = con
                    Dim sql As String = " SELECT instance FROM menu_tags WHERE id='" & id & "' "

                    cmd.CommandText = sql
                    Dim reader As MySqlDataReader = cmd.ExecuteReader
                    Try
                        'reader = cmd.ExecuteReader
                        If reader.HasRows Then
                            reader.Read()
                            instance = CInt(reader.Item("instance")) + 1
                        End If
                        reader.Close()
                    Catch ex As Exception
                        MsgBox("Fehler 03: " & ex.Message)
                    End Try
                    con.Close()
                End Using
            End Using
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        Return instance
    End Function



    Public Function insert_HPContentNew(hp_content As hp_content, laenderkuerzel As String, title As String, refId As String, instance As String) As Integer
        Dim newID As Integer = -1
        Dim sql As String = " INSERT INTO menu_tags ( " & getTitle(laenderkuerzel) & " , ref_id, instance) VALUES( @title,@refId,'" & (instance + 1) & "' ); SELECT LAST_INSERT_ID()" 'ON DUPLICATE KEY UPDATE hp_de = VALUES(@hp_de)"
        'dedet_abt
        Using conn As MySqlConnection = GetNewOpenConnection()
            Using cmd As New MySqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@title", title)
                cmd.Parameters.AddWithValue("@refId", refId)
                Try
                    ' cmd.ExecuteNonQuery()
                    newID = CInt(cmd.ExecuteScalar())
                Catch ex As MySqlException
                    MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Datensatz kann nicht gespeichert werden!" & vbCrLf & vbCrLf & ex.Message, vbExclamation, "Fehler beim Speichern Programm")
                End Try
            End Using
            conn.Close()
        End Using


        Dim sql2 As String = " INSERT INTO homepage_content (hp_refId) VALUES( @hp_refId) " 'ON DUPLICATE KEY UPDATE hp_de = VALUES(@hp_de)"

        Using conn As MySqlConnection = GetNewOpenConnection()
            Using cmd As New MySqlCommand(sql2, conn)
                cmd.Parameters.AddWithValue("@hp_refId", newID)
                Try
                    cmd.ExecuteNonQuery()

                Catch ex As MySqlException
                    MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Datensatz kann nicht gespeichert werden!" & vbCrLf & vbCrLf & ex.Message, vbExclamation, "Fehler beim Speichern Programm")
                End Try
            End Using
            conn.Close()
        End Using
        Return newID
    End Function

    Public Sub deleteHpMenueAndContent(id)
        Dim sql As String = " DELETE FROM menu_tags WHERE id=@id; DELETE FROM homepage_content WHERE hp_refId=@id " 'ON DUPLICATE KEY UPDATE hp_de = VALUES(@hp_de)"
        'dedet_abt
        Using conn As MySqlConnection = GetNewOpenConnection()
            Using cmd As New MySqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@id", id)

                Try
                    cmd.ExecuteNonQuery()

                Catch ex As MySqlException
                    MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Datensatz kann nicht gelöscht werden!" & vbCrLf & vbCrLf & ex.Message, vbExclamation, "Fehler beim Speichern Programm")
                End Try
            End Using
        End Using
    End Sub

End Class
