Imports Telerik.Web.UI

Public Class Projekt_upravit
    Inherits System.Web.UI.UserControl

    Public cTitle As Admin.Title
    Public sStrankaID As String = "200"
    Public sKategorieID As String = ""
    Public sID As String = ""

    Public sAdresaSave As String = gadrProjekt_upravit
    Public sAdresaSeznam As String = gadrProjekty_seznam

    Public sAdresaUpravit As String = gadrProjekt_foto_upravit
    Public sAdresaOdstranit As String = gadrProjekt_foto_odstranit

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

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

    ''' <summary>
    ''' Zamecky hotel uvodni text
    ''' 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('" & gstrProbihaUkladaniStranky & "');")
        cmdSave2.Attributes.Add("onclick", "ShowPopInfo('" & gstrProbihaUkladaniStranky & "');")

        '# Inicializace fotografie
        Dim l As New PCSolution.Localize
        l.RadAsyncUpload(cmdFoto)
        cmdFoto.Localization.Select = "Přidat fotografii"
        cmdFoto.OnClientFilesUploaded = "fUploaded"
        cmdFoto.AllowedFileExtensions = New String(1) {".jpg", ".png"}
        cmdFoto.Width = Unit.Pixel(50)

        l.RadAsyncUpload(cmdFoto1)
        cmdFoto1.OnClientFilesUploaded = "fUploaded"
        cmdFoto1.AllowedFileExtensions = New String(1) {".jpg", ".png"}
        cmdFoto1.Width = Unit.Pixel(50)

        sKategorieID = Request.QueryString("kat")

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

        sID = Request.QueryString("id")
        If sID = "0" Then sID = ""

        If sID <> "" Then
            cTitle._Podnadpis("upravit referenci")
        Else
            cTitle._Nadpis("Reference")
            cTitle._Podnadpis("nová reference")
            lblBC.Text &= "➞ Nová reference"
            lblFoto1.Visible = False
            cmdFoto1Delete.Visible = False
            chkZobrazit.Checked = True
        End If

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

        Me.NacistData()

        Dim pSort As New PCSolution.SortItems
        pSort.InsertSorting(sTableFoto, PCSolution.SortItems.eRazeni.Seznam)

        txtNadpis.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 FOTOGRAFIE  "

    Sub NacistData()

        Dim s As String = ""

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

            Dim cRow As DataRow = DS.Rows(0)

            Dim sNazev As String = cRow("nadpis" & _Language)
            sNazev = sNazev.Replace("#", " ")

            If sNazev = "" Then
                sNazev = cRow("nadpiscz").ToString.Replace("#", " ")
            End If

            lblBC.Text &= "➞ " & sNazev

            If sNazev.Length > 30 Then
                sNazev = sNazev.Substring(0, 29) & "..."
            End If
            cTitle._Nadpis(sNazev)

            txtNadpis.Text = cRow("nadpis" & _Language)
            txtKratkytext.Text = cRow("kratkytext" & _Language)
            txtText.Text = cRow("text" & _Language)
            chkZobrazit.Checked = cRow("zobrazit" & _Language)
            ddFoto_umisteni.SelectedValue = cRow("foto_umisteni")

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

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

        Else

            ajax.ResponseScripts.Add("$('.label-localize').css('display','none');")

        End If

        DS = DB.ExecuteSelectCommand("SELECT * FROM " & sTableFoto & " WHERE projektid = " & sID & " ORDER BY sort")

        If DS.Rows.Count > 0 Then
            s &= "<ul id=""sort"">"
        End If

        For Each cRow As DataRow In DS.Rows

            s &= "<li id=""sort" & cRow("id") & """>"
            s &= "<div class=""image"">" 'Fotografie
            s &= "<img src=""" & sFotoCestaRelativni & "/" & sKategorieID & "/" & sID & "/" & cRow("id") & "/v" & cRow("fotoverze") & "/Nahled3/" & cRow("fotonazev") & cRow("fotopripona") & """ alt=""" & cRow("fotonazev") & """ class=""thumb"" />"
            s &= "</div>"
            s &= "<div class=""popis"">"
            s &= cRow("popis" & _Language)
            s &= "</div>"
            s &= "<div class=""cmdEditovat width-50""><a href=""/strankaid-" & sKategorieID & "-" & sID & "-" & cRow("id") & sAdresaUpravit & """>Upravit</a></div>" 'Tlacitko upravit
            s &= "<div class=""cmdOdstranit width-50"" onclick=""ConfirmDelete('Odstranit fotografii', 'Opravdu chcete odstranit fotografii " & cRow("fotonazev") & cRow("fotopripona") & "?','/strankaid-" & sKategorieID & "-" & sID & "-" & cRow("id") & sAdresaOdstranit & "','','');return false;"">" 'Tlacitko odstranit
            s &= "Odstranit"
            s &= "</div>"
            s &= "</li>"

        Next

        If DS.Rows.Count > 0 Then
            s &= "</ul>"
        End If

        phItems.Controls.Add(New LiteralControl(s)) : s = ""

        If DS.Rows.Count = 0 Then
            phItems.Visible = False
            phNoItem.Controls.Add(New LiteralControl("<div class=""noitem"">Není nalezená žádná fotografie.</div>"))
        End If

        If sID <> "" Then
            LoadLink(sStrankaID, sID, txtTitle.Text, txtLink.Text, "", txtDescription.Text)
        End If

    End Sub

#End Region

#Region "  NAHLED PROJEKTU  "

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

        Me.Ulozit()

        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 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 &= "\" & sKategorieID & "\" & sID & "\Nahled\v" & cRow("fotoverze")
                    sFotoCestaRelativni &= "/" & sKategorieID & "/" & sID & "/Nahled/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
            cmdFoto1.UploadedFiles(n).SaveAs(sFotoCestaAbsolutni & "\original" & sExt)

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

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

        Next

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

    End Sub

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

        Me.Ulozit()

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

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

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

    End Sub

#End Region

#Region "  ULOZIT FOTOGRAFIE  "

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

        Me.Ulozit()

        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)

            '# Vytvorit novou fotografii v databazi
            Dim iFotoID As Int16 = 0
            Dim iSort As Int16 = 0
            Dim DS As DataTable = DB.ExecuteSelectCommand("SELECT max(sort) FROM " & sTableFoto & " WHERE zaznamid = " & sID)
            Try
                iSort = DS.Rows(0)(0) + 1
            Catch ex As Exception
                iSort = 1
            End Try

            DB.ExecuteCommand("INSERT INTO " & sTableFoto & " (projektid, sort) VALUES (" & sID & ", '" & iSort & "')")
            DS = DB.ExecuteSelectCommand("SELECT max(id) FROM " & sTableFoto)
            iFotoID = DS.Rows(0)(0)
            '@

            '# Upravit cestu
            Dim sCesta As String = sFotoCestaAbsolutni & "\" & sKategorieID & "\" & sID & "\" & iFotoID
            '@

            '# Pokud je potreba vytvaret verze souboru, vytvorit novou cilovou slozku pro verzi
            If bPovolitVerzeFotografii = True Then
                DS = DB.ExecuteSelectCommand("SELECT fotoverze FROM " & sTableFoto & " 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 " & sTableFoto & " SET fotoverze = " & cRow("fotoverze") & " WHERE id = '" & iFotoID & "'")
                End If
            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, 200, 80, sExt)

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

        Next

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

    End Sub

#End Region

#Region "  UDALOSTI  "

    Sub Ulozit()

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

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

        End If

        If txtTitle.Text = "" Then txtTitle.Text = txtNadpis.Text
        If txtLink.Text = "" Then txtLink.Text = txtNadpis.Text

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

        SaveLink(sStrankaID, sID, txtTitle.Text, txtLink.Text, "", txtDescription.Text)

    End Sub

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

        _NotifyText = "Reference je uložená."

        Me.Ulozit()

        Response.Redirect("/id-" & sKategorieID & sAdresaSeznam)

    End Sub

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

        Response.Redirect("/id-" & sKategorieID & sAdresaSeznam)

    End Sub

#End Region

End Class