Imports Microsoft.VisualBasic
Imports System.Data.SqlClient

'Imports SQL.Data.SQLClient


Public Class cDBFunctions
    Public Shared Function GetNewOpenConnection() As SqlConnection


        'MyProviderDbConn
        Dim cn As New SqlConnection()
        '  cn.ConnectionString = "Server=db593295684.db.1and1.com;Database=db593295684;Uid=dbo593295684;Pwd=atilla#2;"
        If HttpContext.Current.Request.ServerVariables("SERVER_NAME") = "localhost" Then
            cn.ConnectionString = "Server=FMZOLL.verag.ost.dmn\SQLFMZOLL;Database=VERAG_HOMEPAGE;Uid=AppUser;Pwd=yp/THDd?xM+pZ$"
            '  cn.ConnectionString = "Server=FMZOLL\SQLFMZOLL;Database=VERAG_HOMEPAGE;Uid=sa;Pwd=BmWr501956"
        Else
            cn.ConnectionString = "Server=DEVELOPER.verag.ost.dmn\DEVSQL;Database=VERAG_HOMEPAGE;Uid=AppUser;Pwd=yp/THDd?xM+pZ$;"
            '    cn.ConnectionString = "Server=db593295684.db.1and1.com;Database=db593295684;Uid=dbo593295684;Pwd=atilla#2;"
        End If
        cn.Open()
        Return cn
    End Function

    '  Public Function qry_menu_tags(Optional laenderkuerzel As String = "de", Optional ref As String = "de") As List(Of cNavigation)
    '     Using con As SQLConnection = GetNewOpenConnection()

    '     End Using
    ' End Function
    Public errorAusg As String = ""
    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 SqlConnection = GetNewOpenConnection()
            Using cmd As New SqlCommand
                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 SqlDataReader = cmd.ExecuteReader

                Try
                    'reader = cmd.ExecuteReader
                    While reader.Read()
                        Dim n As New cNavigation
                        If Not reader.Item("desc_" & laenderkuerzel) Is DBNull.Value Then n.bezeichnung = reader.Item("desc_" & laenderkuerzel)
                        If Not reader.Item("id") Is DBNull.Value Then n.id = reader.Item("id")
                        If Not reader.Item("menu_slogan_" & laenderkuerzel) Is DBNull.Value Then n.slogan = reader.Item("menu_slogan_" & laenderkuerzel)
                        If Not reader.Item("menu_logo") Is DBNull.Value Then 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: " & ex.Message)
                    Console.WriteLine(ex.Message)
                    errorAusg = ex.Message
                End Try
                con.Close()
            End Using
        End Using
        Return navigation
    End Function

    Public Function qry_content(menuID As String, Optional laenderkuerzel As String = "de") As String
        If laenderkuerzel = "" Then laenderkuerzel = "de"
        Dim str As String = ""
        Using con As SqlConnection = GetNewOpenConnection()
            Using cmd As New SqlCommand
                cmd.Connection = con
                Dim sql As String = " SELECT block_id " &
                                    " FROM tbl_blocks " &
                                        " INNER JOIN  menu_tags " &
                                        " ON tbl_blocks.block_pageID_Ref=menu_tags.id " &
                                    " WHERE menu_tags.id = '" & menuID & "'" &
                                    " ORDER BY block_id ASC "
                cmd.CommandText = sql
                Dim reader As SqlDataReader = cmd.ExecuteReader
                Try
                    'reader = cmd.ExecuteReader
                    While reader.Read()

                        str &= "<div class='content_all'>" & qry_blockfields(reader.Item("block_id"), laenderkuerzel) & "</div>"

                    End While
                    reader.Close()
                Catch ex As Exception
                    MsgBox("Fehler: " & ex.Message)
                End Try
                con.Close()
            End Using
        End Using
        Return str
    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

    Public Function qry_contentNew(menuID As String, Optional laenderkuerzel As String = "de") As String
        If laenderkuerzel = "" Then laenderkuerzel = "de"
        Dim spalte As String = getSpaltenBezeichnung(laenderkuerzel)
        Dim str As String = ""
        Using con As SqlConnection = GetNewOpenConnection()
            Using cmd As New SqlCommand
                cmd.Connection = con
                Dim sql As String = " SELECT " & spalte & " " &
                                    " FROM homepage_content " &
                                      " WHERE hp_refId = '" & menuID & "'"
                cmd.CommandText = sql

                Dim reader As SqlDataReader = cmd.ExecuteReader
                Try
                    'reader = cmd.ExecuteReader
                    While reader.Read()

                        str &= "<div class='content_all'>" & reader.Item(spalte) & "</div>"

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

    Public Function qry_blockfields(block_id As String, Optional laenderkuerzel As String = "de") As String
        If laenderkuerzel = "" Then laenderkuerzel = "de"
        Dim str As String = ""
        Using con As SqlConnection = GetNewOpenConnection()
            Using cmd As New SqlCommand
                cmd.Connection = con
                Dim sql As String = "  SELECT bfld_ID, block_ID, bart_id, bart_desc, bart_beginParam, bart_endParam" &
                                    " FROM  " &
                                       " tbl_blocks as t1,  " &
                                       " tbl_blockfields as t2, " &
                                       " tbl_blockfieldart as t3 " &
                                    "  WHERE  " &
                                          "  t1.block_ID=" & block_id & "" &
                                    " AND  " &
                                          " t2.bfld_blockID_Ref=t1.block_ID " &
                                    " AND " &
                                          "  t3.bart_ID=t2.bfld_bartID_Ref " &
                                    " ORDER BY t2.bfld_ID ASC "
                cmd.CommandText = sql
                Dim reader As SqlDataReader = cmd.ExecuteReader
                Try
                    'reader = cmd.ExecuteReader
                    While reader.Read()
                        str &= reader.Item("bart_beginParam") & qry_blockfield_content(reader.Item("bfld_ID"), laenderkuerzel) & reader.Item("bart_endParam")
                    End While
                    reader.Close()
                Catch ex As Exception
                    MsgBox("Fehler: " & ex.Message)
                End Try
                con.Close()
            End Using
        End Using
        Return str
    End Function

    Public Function qry_blockfield_content(bf_ID As String, Optional laenderkuerzel As String = "de") As String
        If laenderkuerzel = "" Then laenderkuerzel = "de"
        Dim str As String = ""
        Using con As SqlConnection = GetNewOpenConnection()
            Using cmd As New SqlCommand
                cmd.Connection = con
                Dim sql As String = "  SELECT *" &
                                    "  FROM tbl_blockfieldargumentcontent " &
                                            " INNER JOIN  tbl_blockfieldarguments " &
                                            " ON tbl_blockfieldargumentcontent.bfac_bargID_Ref=tbl_blockfieldarguments.barg_ID " &
                                    "  WHERE " &
                                            " bfac_bfldID_Ref = " & bf_ID & " " &
                                    " ORDER BY tbl_blockfieldargumentcontent.bfac_order ASC"
                cmd.CommandText = sql
                Dim reader As SqlDataReader = cmd.ExecuteReader
                Try
                    'reader = cmd.ExecuteReader
                    While reader.Read()
                        str &= reader.Item("barg_beginArgument") & reader.Item("bfac_" & laenderkuerzel) & reader.Item("barg_endArgument")
                    End While
                    reader.Close()
                Catch ex As Exception
                    MsgBox("Fehler: " & ex.Message)
                End Try
                con.Close()
            End Using
        End Using
        Return str
    End Function
End Class




Public Class cDBADMINFunctions



    Public Shared Function GetNewOpenConnection() As SqlConnection
        Return cDBFunctions.GetNewOpenConnection
    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 SQLConnection = GetNewOpenConnection()
            Using cmd As New SQLCommand
                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 SQLDataReader = 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 SqlConnection = GetNewOpenConnection()
                Using cmd As New SqlCommand
                    cmd.Connection = con
                    Dim sql As String = " SELECT * FROM verag_homepage.dbo.menu_tags LEFT JOIN verag_homepage.dbo.homepage_content ON menu_tags.id=homepage_content.hp_refId WHERE  menu_tags.id =  '" & menuID & "'"

                    cmd.CommandText = sql
                    Dim reader As SqlDataReader = cmd.ExecuteReader
                    Try
                        While reader.Read()
                            Dim b As New cBlock
                            hp_content.hp_id = reader.Item("id")
                            If reader.Item(getSpaltenBezeichnung(laenderkuerzel)) IsNot DBNull.Value Then hp_content.hp_content = reader.Item(getSpaltenBezeichnung(laenderkuerzel))
                            If reader.Item(getSlogan(laenderkuerzel)) IsNot DBNull.Value Then hp_content.hp_slogan = reader.Item(getSlogan(laenderkuerzel))
                            If reader.Item(getTitle(laenderkuerzel)) IsNot DBNull.Value Then hp_content.hp_title = reader.Item(getTitle(laenderkuerzel))
                            If reader.Item("menu_logo") IsNot DBNull.Value Then hp_content.hp_imgPath = reader.Item("menu_logo")
                            If reader.Item("instance") IsNot DBNull.Value Then hp_content.hp_instance = reader.Item("instance")
                            If reader.Item("ref_id") IsNot DBNull.Value Then hp_content.hp_refIdToUpperMenue = reader.Item("ref_id")
                            Return hp_content
                        End While
                        reader.Close()
                    Catch ex As Exception
                        System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE=""JavaScript"">alert('ERR: " & ex.Message & "')</SCRIPT>")
                    End Try
                    con.Close()
                End Using
            End Using
        Catch ex As Exception
            System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE=""JavaScript"">alert('ERR')</SCRIPT>")
            System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE=""JavaScript"">alert('ERR: " & ex.Message & "')</SCRIPT>")
        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 = " begin tran" &
                                " if NOT exists (select * from homepage_content with (updlock,serializable) where hp_refId=@hp_refId ) " &
                                        " begin " &
                                          "INSERT INTO  homepage_content " &
                                            " (hp_refId) " &
                                            " VALUES(@hp_refId) " &
                                " End " &
                                " UPDATE homepage_content " &
                                         " SET [" & getSpaltenBezeichnung(laenderkuerzel) & "]=@hp WHERE hp_refId=@hp_refId " &
                                " UPDATE menu_tags " &
                                    "SET " & getSlogan(laenderkuerzel) & "=@slogan, " & getTitle(laenderkuerzel) & "=@title, menu_logo=@img  WHERE id=@id " &
                            " commit tran "
        '  MsgBox(sql)
        Using conn As SqlConnection = GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@hp", hp_content.hp_content)
                cmd.Parameters.AddWithValue("@hp_refId", hp_content.hp_id)
                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 SqlException
                    MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": ERR01 Datensatz kann nicht gespeichert werden!" & vbCrLf & vbCrLf & ex.Message, vbExclamation, "Fehler beim Speichern Programm")
                End Try
            End Using
            conn.Close()
        End Using
        If False Then

            Dim sql2 As String = " UPDATE homepage_content " &
                                             " SET [" & getSpaltenBezeichnung(laenderkuerzel) & "]=@hp WHERE hp_refId=@hp_refId "

            sql2 &= " UPDATE menu_tags " &
                                  "SET " & getSlogan(laenderkuerzel) & "=@slogan, " & getTitle(laenderkuerzel) & "=@title, menu_logo=@img  WHERE id=@id "
            Using conn As SqlConnection = GetNewOpenConnection()
                Using cmd As New SqlCommand(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)
                    cmd.Parameters.AddWithValue("@hp", hp_content.hp_content)
                    cmd.Parameters.AddWithValue("@hp_refId", hp_content.hp_id)
                    Try
                        cmd.ExecuteNonQuery()

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

        End If

    End Sub


    Public Sub doTheSql(sql As String)
        Using conn As SqlConnection = GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)
                Try
                    cmd.ExecuteNonQuery()
                Catch ex As SqlException
                    MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": ERROR: " & vbCrLf & vbCrLf & ex.Message, vbExclamation, "Fehler beim SQL")
                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 SQLConnection = GetNewOpenConnection()
                Using cmd As New SQLCommand
                    cmd.Connection = con
                    Dim sql As String = " SELECT instance FROM menu_tags WHERE id='" & id & "' "

                    cmd.CommandText = sql
                    Dim reader As SQLDataReader = 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 Scope_Identity()" 'ON DUPLICATE KEY UPDATE hp_de = VALUES(@hp_de)"
        'dedet_abt
        '; SELECT LAST_INSERT_ID()
        Using conn As SQLConnection = GetNewOpenConnection()
            Using cmd As New SQLCommand(sql, conn)
                cmd.Parameters.AddWithValue("@title", title)
                cmd.Parameters.AddWithValue("@refId", refId)
                Try
                    ' cmd.ExecuteNonQuery()
                    newID = CInt(cmd.ExecuteScalar())

                Catch ex As SQLException
                    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 SQLConnection = GetNewOpenConnection()
            Using cmd As New SQLCommand(sql2, conn)
                cmd.Parameters.AddWithValue("@hp_refId", newID)
                Try
                    cmd.ExecuteNonQuery()

                Catch ex As SQLException
                    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 SQLConnection = GetNewOpenConnection()
            Using cmd As New SQLCommand(sql, conn)
                cmd.Parameters.AddWithValue("@id", id)

                Try
                    cmd.ExecuteNonQuery()

                Catch ex As SQLException
                    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

