Imports Telerik.Web.UI

Public Class Projekty_rocnik_upravit
    Inherits System.Web.UI.UserControl

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

    Public sAdresaSave As String = gadrProjekty_kategorie_seznam
    Public sAdresaStorno As String = gadrProjekty_kategorie_seznam

    Public sTable As String = "[projekty].[kategorie]"

    Private sFotoCestaRelativni As String
    Private sFotoCestaAbsolutni As String

    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 & "');")

        cTitle._Nadpis("Reference - kategorie")

        sID = Request.QueryString("id")

        sFotoCestaRelativni = _PathFotoRelative & "/Projekty/" & sID & "/Rocnik"
        sFotoCestaAbsolutni = _PathFoto & "\Projekty\" & sID & "\Rocnik"

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

        lblBC.Text = "<a href=""" & gadrProjekty_kategorie_seznam & """>Reference - kategorie</a>"

        If sID = "0" Then sID = ""

        If sID <> "" Then
            lblCaption.Text = "Reference - upravit kategorii"
            cTitle._Podnadpis("upravit")
        Else
            lblCaption.Text = "Reference - nová kategorie"
            cTitle._Podnadpis("nová kategorie")
            lblBC.Text &= "➞ Nová kategorie"
            chkZobrazit.Checked = True
            cmdFotoDelete.Visible = False
        End If

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

        txtNadpis.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)

        'txtRok.Text = cRow("rok")
        txtNadpis.Text = cRow("nadpis" & _Language)
        txtText.Text = cRow("text" & _Language)
        chkZobrazit.Checked = cRow("zobrazit" & _Language)

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

        '# Nahled fotografie

        If bPovolitVerzeFotografii = True Then
            sFotoCestaAbsolutni &= "\v" & cRow("fotoverze")
            sFotoCestaRelativni &= "/v" & cRow("fotoverze")
        End If

        If IO.File.Exists(sFotoCestaAbsolutni & "\Nahled3\" & cRow("fotonazev") & cRow("fotopripona")) Then
            lblFoto.Text = "<img src=""" & sFotoCestaRelativni & "/Nahled3/" & cRow("fotonazev") & cRow("fotopripona") & """ alt=""" & cRow("fotonazev") & """ />"
            cmdFoto.Visible = False
            lblFoto.Visible = True
            cmdFotoDelete.Visible = True
        Else
            lblFoto.Visible = False
            cmdFotoDelete.Visible = False
            cmdFoto.Visible = True
        End If
        '@

        lblBC.Text &= "➞ " & txtNadpis.Text

        LoadLinkRocnik(200, 0, sID, txtTitle.Text, txtLink.Text, "", txtDescription.Text)

    End Sub

#End Region

#Region "  NAHLED FOTOGRAFIE  "

    Private Sub cmdFoto_FileUploaded(sender As Object, e As FileUploadedEventArgs) Handles cmdFoto.FileUploaded

        Me.UlozitZaznam()

        sFotoCestaRelativni = _PathFotoRelative & "/Projekty/" & sID & "/Rocnik"
        sFotoCestaAbsolutni = _PathFoto & "\Projekty\" & sID & "\Rocnik"

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

            Dim pDoc As New PCSolution.Document

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

            Dim pDeleteDir As New PCSolution.DirectoryDelete
            pDeleteDir.Delete(sFotoCestaAbsolutni) 'Odstranit stare soubory

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

            IO.Directory.CreateDirectory(sFotoCestaAbsolutni) 'Vytvorit slozku

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

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

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

        Next

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

    End Sub

    Private Sub cmdFotoDelete_Click(sender As Object, e As EventArgs) Handles cmdFotoDelete.Click

        Me.UlozitZaznam()

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

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

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

    End Sub

#End Region

#Region "  ULOZIT  "

    Sub UlozitZaznam()

        If sID = "" Then '# Vytvorit novy zaznam

            'DB.ExecuteCommand("INSERT INTO " & sTable & " (rok) VALUES (" & txtRok.Text & ")")

            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 & " (sort) VALUES (" & iSort & ")")

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

        End If

        DB.ExecuteCommand("UPDATE " & sTable & " SET " &
                           "nadpis" & _Language & " = '" & _PrevestTextNaBezpecny(txtNadpis.Text) & "', " &
                           "text" & _Language & " = '" & _PrevestTextNaBezpecny(txtText.Text) & "', " &
                           "zobrazit" & _Language & " = '" & chkZobrazit.Checked & "' " &
                           "WHERE id = " & sID)

        '"rok = '" & _PrevestTextNaBezpecny(txtRok.Text) & "', " &

        SaveLinkRocnik(200, 0, sID, txtTitle.Text, txtLink.Text, "", txtDescription.Text)

    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