Imports Telerik.Web.UI

Public Class Projekty_foto_upravit
    Inherits System.Web.UI.UserControl

    Public cTitle As Admin.Title
    Public sKategorieID As String 'ID kategorie
    Public sProjektID As String 'ID projektu
    Public sID As String 'ID fotografie

    Public sAdresaSave As String = gadrProjekt_foto_upravit
    Public sAdresaStorno As String = gadrProjekt_upravit

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

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

        sKategorieID = Request.QueryString("strankaid")
        sProjektID = Request.QueryString("blokid")
        sID = Request.QueryString("id")

        sFotoCestaRelativni = _PathFotoRelative & "/Projekty/" & sKategorieID & "/" & sProjektID & "/" & sID
        sFotoCestaAbsolutni = _PathFoto & "\Projekty\" & sKategorieID & "\" & sProjektID & "\" & sID

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

        '# Breadcrumb
        lblBC.Text = "<a href=""/id-" & sKategorieID & gadrProjekty_seznam & """>Reference</a>"

        Dim DSStranka As DataTable = DB.ExecuteSelectCommand("SELECT nadpiscz, nadpis" & _Language & " FROM [projekty].[list] WHERE id = " & sProjektID)
        If DSStranka.Rows.Count > 0 Then
            Dim sNazev As String = DSStranka.Rows(0)("nadpis" & _Language)
            If sNazev = "" Then sNazev = DSStranka.Rows(0)("nadpiscz")
            lblBC.Text &= "➞ <a href=""/zaznamid-" & sKategorieID & "-" & sProjektID & gadrProjekt_upravit & """>" & sNazev & "</a>"
            cTitle._Nadpis(sNazev)
        End If
        lblBC.Text &= "➞ Upravit fotografii"
        '@

        If sID <> "" Then

            lblCaption.Text = "Upravit fotografii"
            cTitle._Podnadpis("upravit fotografii")

        End If

        If IsPostBack = False Then

            Me.UmisteniFotografie()
            Me.NacistData()

        End If

    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" & _Language)
        ddFoto_umisteni.SelectedValue = cRow("foto_umisteni")

        '# Lokalizace
        If _Language <> "cz" Then
            lblPopis.Text = cRow("popiscz")
        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
        '@


    End Sub

#End Region

#Region "  NAHLED FOTOGRAFIE  "

    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 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, 450, 0, 80, sExt)
            fil.SaveFoto(sFotoCestaAbsolutni, sFotoCestaAbsolutni & "\Nahled3", "original" & sExt, sFileName, 0, 200, 80, sExt)

            '# Nahrat fotografii a oriznout na pozadovany rozmer
            'Dim oBMP As New Drawing.Bitmap(sFotoCestaAbsolutni & "\Nahled2\timg" & sExt)
            'If oBMP.Width > 300 Then
            '	fil.CropImage(sFotoCestaAbsolutni & "\Nahled2", sFotoCestaAbsolutni & "\Nahled2", "timg" & sExt, sFileName, 0, 0, 450, 300, sExt)
            'Else
            '	IO.File.Copy(sFotoCestaAbsolutni & "\Nahled2\timg" & sExt, sFotoCestaAbsolutni & "\Nahled2\" & sFileName & sExt)
            'End If

            'Try
            '	IO.File.Delete(sFotoCestaAbsolutni & "\Nahled2\timg" & sExt)
            'Catch ex As Exception
            'End Try

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

        Next

        Response.Redirect("/strankaid-" & sKategorieID & "-" & sProjektID & "-" & 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)
        Catch ex As Exception
        End Try

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

        Response.Redirect("/strankaid-" & sKategorieID & "-" & sProjektID & "-" & sID & sAdresaSave)

    End Sub

#End Region

#Region "  ULOZIT  "

    Sub UlozitZaznam()

        DB.ExecuteCommand("UPDATE " & sTable & " SET " &
                           "popis" & _Language & " = '" & _PrevestTextNaBezpecny(txtPopis.Text) & "', " &
                           "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("/zaznamid-" & sKategorieID & "-" & sProjektID & sAdresaStorno & "#fotogalerie")

    End Sub

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

        Response.Redirect("/zaznamid-" & sKategorieID & "-" & sProjektID & sAdresaStorno & "#fotogalerie")

    End Sub

#End Region

End Class