Imports Telerik.Web.UI

Public Class Aktuality_foto_upravit
    Inherits System.Web.UI.UserControl

    Public cTitle As Admin.Title
    Public sStrankaID As String = "600"
    Public sZaznamID As String = ""
    Public sID As String 'ID fotografie

    Public sAdresaSave As String = gadrAktuality_foto_upravit
    Public sAdresaStorno As String = gadrAktuality_upravit

    Public sTable As String = "[aktuality].[fotogalerie]"

    Public bShowLocalization As Boolean = True

    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 & "');")
        cmdStorno.Attributes.Add("onclick", "ShowPopInfo('" & gstrProbihaNacitaniStranky & "');")

        sZaznamID = Request.QueryString("kat")
        sID = Request.QueryString("id")

        If sZaznamID = "" Or sZaznamID = "0" Then Exit Sub

        sFotoCestaRelativni = _PathFotoRelative & "/Aktuality/" & sZaznamID
        sFotoCestaAbsolutni = _PathFoto & "\Aktuality\" & sZaznamID

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

        cTitle._Nadpis("Aktualita")

        lblBC.Text = "<a href=""" & gadrAktuality & """>Aktuality</a>"

        Dim DS As DataTable = DB.ExecuteSelectCommand("SELECT * FROM [aktuality].[list] WHERE id = '" & sZaznamID & "'")
        If DS.Rows.Count > 0 Then

            lblBC.Text &= "➞<a href=""/id-" & sZaznamID & gadrAktuality_upravit & """>" & DS.Rows(0)("nadpis" & _Language) & "</a>"
            lblBC.Text &= "➞ Upravit fotografii"

        End If

        If sID <> "" Then
            lblCaption.Text = "Upravit fotografii"
            cTitle._Podnadpis("upravit")
        End If

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

        'txtPopis.Focus()

    End Sub

#Region "  NACIST UMISTENI FOTOGRAFIE  "

    Sub UmisteniFotografie()

        Dim p As New PCSolution.Foto_umisteni
        p.NaplnitRozbalovaciSeznam(ddFoto_umisteni)

    End Sub

#End Region

#Region "  NACIST DATA  "

    Sub NacistData()

        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)

        'txtPopis.Text = cRow("popis")
        ddFoto_umisteni.SelectedValue = cRow("foto_umisteni")

        '# Nahled fotografie

        sFotoCestaAbsolutni &= "\" & sID
        sFotoCestaRelativni &= "/" & sID

        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
        '@

    End Sub

#End Region

#Region "  ULOZIT FOTOGRAFII  "

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

        Me.UlozitZaznam()

        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 iFotoID As String = sID

            '# Upravit cestu
            Dim sCesta As String = _PathFoto & "\Aktuality\" & sZaznamID & "\" & iFotoID
            '@

            '# Pokud je potreba vytvaret verze souboru, vytvorit novou cilovou slozku pro verzi
            Dim DS As DataTable = DB.ExecuteSelectCommand("SELECT fotoverze FROM " & sTable & " WHERE id = '" & iFotoID & "'")
            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
                sCesta &= "\v" & cRow("fotoverze")
                DB.ExecuteCommand("UPDATE " & sTable & " SET fotoverze = " & cRow("fotoverze") & " WHERE id = '" & iFotoID & "'")
            End If
            '@

            IO.Directory.CreateDirectory(sCesta) 'Vytvorit slozku

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

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

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

        Next

        Response.Redirect("/zaznamid-" & sZaznamID & "-" & sID & sAdresaSave)

    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 & "\" & sID)
        Catch ex As Exception
        End Try

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

        Response.Redirect("/zaznamid-" & sZaznamID & "-" & sID & sAdresaSave)

    End Sub

#End Region

#Region "  ULOZIT  "

    Sub UlozitZaznam()

        DB.ExecuteCommand("UPDATE " & sTable & " SET " &
                           "foto_umisteni = " & ddFoto_umisteni.SelectedItem.Value & " " &
                           "WHERE id = " & sID)

    End Sub

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

        Me.UlozitZaznam()

        Response.Redirect("/id-" & sZaznamID & sAdresaStorno)

    End Sub

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

        Response.Redirect("/id-" & sZaznamID & sAdresaStorno)

    End Sub

#End Region

End Class