Imports Telerik.Web.UI

Public Class UvodniStrana_Slider_Upravit
	Inherits System.Web.UI.UserControl

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

	Public sAdresaSave As String = gadrUvodniStrana_Slider_Upravit
	Public sAdresaStorno As String = gadrUvodniStrana_Slider

	Public sTable As String = "[uvodni-strana].[slider]"

	Public bShowLocalization As Boolean = True

	Private sFotoCestaRelativni As String = _PathFotoRelative & "/Uvodni-strana/Slider"
	Private sFotoCestaAbsolutni As String = _PathFoto & "\Uvodni-strana\Slider"
	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 & "');")

        cTitle._Nadpis("Slider")

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

        If sID = "" Then
            lblFoto.Visible = False
            cmdFotoDelete.Visible = False
        End If
        '@

        If Request.QueryString("id") <> "" Then
            sID = Request.QueryString("id")
            lblCaption.Text = "Upravit snímek"
            cTitle._Podnadpis("Upravit snímek")
        Else
            lblCaption.Text = "Nový snímek"
            cTitle._Podnadpis("Nový snímek")
            chkZobrazit.Checked = True
        End If

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

        txtNadpis.Focus()

    End Sub

#Region "  NACIST ROZBALOVACI  SEZNAM  "

    Sub NacistRozbalovaciSeznam()

        ddIstexttop.Items.Add(New RadComboBoxItem("Nahoře", "0"))
        ddIstexttop.Items.Add(New RadComboBoxItem("Dole", "1"))

    End Sub

#End Region

#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)

        txtNadpis.Text = cRow("nadpis" & _Language)
        ddIstexttop.SelectedValue = cRow("istexttop")
        'txtPodnadpis.Text = cRow("podnadpis" & _Language)
        'txtText.Text = cRow("text" & _Language)
        'txtOdkaz.Text = cRow("odkaz" & _Language)

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

        '# Nahled snimku
        If bPovolitVerzeFotografii = True Then
			sFotoCestaAbsolutni &= "\" & sID & "\v" & cRow("fotoverze")
			sFotoCestaRelativni &= "/" & sID & "/v" & cRow("fotoverze")
		End If

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

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

    End Sub

#End Region

#Region "  NAHLED SNIMKU  "

	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)

			'# 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 &= "\" & sID & "\v" & cRow("fotoverze")
					sFotoCestaRelativni &= "/" & sID & "/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, 1920, 0, 80, sExt)
			fil.SaveFoto(sFotoCestaAbsolutni, sFotoCestaAbsolutni & "\Nahled2", "original" & sExt, sFileName, 250, 0, 80, sExt)

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

		Next

		Response.Redirect("/id-" & 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("/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 & " (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) & "', " &
                           "istexttop = '" & ddIstexttop.SelectedItem.Value & "', " &
                           "zobrazit" & _Language & " = '" & chkZobrazit.Checked & "' " &
                           "WHERE id = " & sID)

        '"podnadpis" & _Language & " = '" & _PrevestTextNaBezpecny(txtPodnadpis.Text) & "', " &
        '"text" & _Language & " = '" & _PrevestTextNaBezpecny(txtText.Text) & "', " &
        '"odkaz" & _Language & " = '" & _PrevestTextNaBezpecny(txtOdkaz.Text.Replace("http://", "").Replace("https://", "")) & "', " &

    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