Imports Telerik.Web.UI

Public Class Lide_upravit
    Inherits System.Web.UI.UserControl

    Public cTitle As Admin.Title
    Public sID As String  'ID zaznamu

    Public sAdresaSave As String = gadrLide_upravit
    Public sAdresaStorno As String = gadrLide_seznam

    Public sTable As String = "[kontakt].[lide]"

    Public bShowLocalization As Boolean = True

    Private sFotoCestaRelativni As String = _PathFotoRelative & "/Lide"
    Private sFotoCestaAbsolutni As String = _PathFoto & "\Lide"
    Public bPovolitVerzeFotografii As Boolean = True

    ''' <summary>
    ''' Seznam zaznamu - upravit zaznam
    ''' Verze: 1.0
    ''' </summary>
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        cmdSave.Attributes.Add("onclick", "ShowPopInfo('" & gstrProbihaUkladaniZaznamu & "');")

        '# Inicializace fotografie
        Dim l As New PCSolution.Localize
        l.RadAsyncUpload(cmdFoto1)
        cmdFoto1.OnClientFilesUploaded = "fUploaded"
        cmdFoto1.AllowedFileExtensions = New String(1) {".jpg", ".png"}
        cmdFoto1.Width = Unit.Pixel(50)

        cTitle._Nadpis("Lidé")

        sID = Request.QueryString("id")

        lblBC.Text = "<a href=""" & gadrLide_seznam & """>Lidé</a>"

        If sID = "" Then
            lblFoto1.Visible = False
            cmdFoto1Delete.Visible = False
            chkZobrazit.Checked = True
        End If
        '@

        If sID = "0" Then sID = ""

        If sID <> "" Then
            sID = Request.QueryString("id")
            lblCaption.Text = "Lidé - kontakt"
            cTitle._Podnadpis("upravit")
        Else
            lblCaption.Text = "Lidé - kontakt"
            cTitle._Podnadpis("nový kontakt")
            lblBC.Text &= "➞ Nový kontakt"
        End If

        If IsPostBack = False Then
            Me.NacistData()
        End If

        txtJmeno.Focus()

    End Sub

#Region "  NACIST DATA  "

    Sub NacistData()

        If sID = "" Then
            ajax.ResponseScripts.Add("$('.label-localize').css('display','none');")
            Exit Sub
        End If

        Dim DS As DataTable = DB.ExecuteSelectCommand("SELECT * FROM " & sTable & " WHERE id = '" & sID & "'")
        If DS.Rows.Count = 0 Then Exit Sub

        Dim cRow As DataRow = DS.Rows(0)

        txtTitul1.Text = cRow("titul1" & _Language)
        txtJmeno.Text = cRow("jmeno" & _Language)
        txtTitul2.Text = cRow("titul2" & _Language)
        txtEmail.Text = cRow("email")
        txtTelefon.Text = cRow("telefon")
        txtMobil.Text = cRow("mobil")
        txtPoznamka.Text = cRow("poznamka" & _Language)

        chkZobrazit.Checked = cRow("zobrazit" & _Language)

        If txtJmeno.Text <> "" Then
            lblBC.Text &= "➞ " & txtJmeno.Text
        Else
            lblBC.Text &= "➞ " & cRow("jmenocz")
        End If

        '# Lokalizace
        If _Language <> "cz" Then
            lblJmeno.Text = cRow("jmenocz")
        Else
            ajax.ResponseScripts.Add("$('.label-localize').css('display','none');")
        End If
        '@

        '# Nahled fotografie
        If IO.File.Exists(sFotoCestaAbsolutni & "\" & sID & "\v" & cRow("foto1verze") & "\Nahled1\" & cRow("foto1nazev") & cRow("foto1pripona")) Then
            lblFoto1.Text = "<img src=""" & sFotoCestaRelativni & "/" & sID & "/v" & cRow("foto1verze") & "/Nahled2/" & cRow("foto1nazev") & cRow("foto1pripona") & """ alt=""" & cRow("foto1nazev") & """ />"
            cmdFoto1.Visible = False
            lblFoto1.Visible = True
            cmdFoto1Delete.Visible = True
        Else
            lblFoto1.Visible = False
            cmdFoto1Delete.Visible = False
        End If

    End Sub

#End Region

#Region "  NAHLED FOTOGRAFIE  "

    Private Sub cmdFoto1_FileUploaded(sender As Object, e As FileUploadedEventArgs) Handles cmdFoto1.FileUploaded

        Me.UlozitZaznam()

        For n As Int16 = 0 To cmdFoto1.UploadedFiles.Count - 1

            Dim pDoc As New PCSolution.Document

            Dim sFileName As String = pDoc.OdstranitDiakritiku(IO.Path.GetFileNameWithoutExtension(cmdFoto1.UploadedFiles(n).FileName), "-")
            Dim sExt As String = IO.Path.GetExtension(cmdFoto1.UploadedFiles(n).FileName)

            '# Pokud je potreba vytvaret verze souboru, vytvorit novou cilovou slozku pro verzi
            If bPovolitVerzeFotografii = True Then
                Dim DS As DataTable = DB.ExecuteSelectCommand("SELECT foto1verze FROM " & sTable & " WHERE id = '" & sID & "'")
                If DS.Rows.Count > 0 Then
                    Dim cRow As DataRow = DS.Rows(0)
                    If cRow("foto1verze") = 0 Then
                        cRow("foto1verze") = 1
                    Else
                        cRow("foto1verze") += 1
                    End If
                    sFotoCestaAbsolutni &= "\" & sID & "\v" & cRow("foto1verze")
                    sFotoCestaRelativni &= "/" & sID & "/v" & cRow("foto1verze")
                    DB.ExecuteCommand("UPDATE " & sTable & " SET foto1verze = " & cRow("foto1verze") & " WHERE id = '" & sID & "'")
                End If
            End If
            '@

            IO.Directory.CreateDirectory(sFotoCestaAbsolutni) 'Vytvorit slozku

            '# Nahrat fotografii a zmensit na pozadovany rozmer
            cmdFoto1.UploadedFiles(n).SaveAs(sFotoCestaAbsolutni & "\original" & sExt)

            Dim fil As New PCSolution.Images.Images
            fil.SaveFoto(sFotoCestaAbsolutni, sFotoCestaAbsolutni & "\Nahled1", "original" & sExt, sFileName, 0, 250, 80, sExt)
            fil.SaveFoto(sFotoCestaAbsolutni, sFotoCestaAbsolutni & "\Nahled2", "original" & sExt, sFileName, 250, 0, 80, sExt)
            fil.SaveFoto(sFotoCestaAbsolutni, sFotoCestaAbsolutni & "\Nahled3", "original" & sExt, sFileName, 600, 0, 80, sExt)

            DB.ExecuteCommand("UPDATE " & sTable & " SET foto1nazev = '" & sFileName & "', foto1pripona = '" & sExt & "' WHERE id = '" & sID & "'")

        Next

        Response.Redirect("/id-" & sID & sAdresaSave)

    End Sub

    Private Sub cmdFoto1Delete_Click(sender As Object, e As EventArgs) Handles cmdFoto1Delete.Click

        Me.UlozitZaznam()

        Try
            Dim p As New PCSolution.DirectoryDelete
            p.Delete(sFotoCestaAbsolutni & "\" & sID)
        Catch ex As Exception
        End Try

        DB.ExecuteCommand("UPDATE " & sTable & " SET foto1nazev = '', foto1pripona = '' WHERE id = '" & sID & "'")

        Response.Redirect("/id-" & sID & sAdresaSave)

    End Sub

#End Region

#Region "  ULOZIT  "

    Sub UlozitZaznam()

        If sID = "" Then '# Vytvorit novy zaznam

            Dim iSort As Int16 = 1
            Dim DS As DataTable = DB.ExecuteSelectCommand("SELECT max(sort) FROM " & sTable)
            Try
                iSort = DS.Rows(0)(0) + 1
            Catch ex As Exception
            End Try
            DB.ExecuteCommand("INSERT INTO " & sTable & " (typ, sort) VALUES (0, " & iSort & ")")

            DS = DB.ExecuteSelectCommand("SELECT max(id) FROM " & sTable)
            sID = DS.Rows(0)(0)

        End If

        DB.ExecuteCommand("UPDATE " & sTable & " SET " &
                           "titul1" & _Language & " = '" & _PrevestTextNaBezpecny(txtTitul1.Text) & "', " &
                           "jmeno" & _Language & " = '" & _PrevestTextNaBezpecny(txtJmeno.Text) & "', " &
                           "titul2" & _Language & " = '" & _PrevestTextNaBezpecny(txtTitul2.Text) & "', " &
                           "email = '" & _PrevestTextNaBezpecny(txtEmail.Text) & "', " &
                           "telefon = '" & _PrevestTextNaBezpecny(txtTelefon.Text) & "', " &
                           "mobil = '" & _PrevestTextNaBezpecny(txtMobil.Text) & "', " &
                           "poznamka" & _Language & " = '" & _PrevestTextNaBezpecny(txtPoznamka.Text) & "', " &
                           "zobrazit" & _Language & " = '" & chkZobrazit.Checked & "' " &
                           "WHERE id = " & sID)

    End Sub

    Private Sub cmdSave_Click(sender As Object, e As EventArgs) Handles cmdSave.Click

        Me.UlozitZaznam()
        Response.Redirect(sAdresaStorno)

    End Sub

    Private Sub cmdStorno_Click(sender As Object, e As EventArgs) Handles cmdStorno.Click

        Response.Redirect(sAdresaStorno)

    End Sub

#End Region

End Class