a As Map
Private xui As XUI
Private editCol As B4XTableColumn
Private const CSVFile As String = "naspi.csv"
Dim data As List
Private provider As FileProvider
Private old_filefirma,current_filefirma As String =""
Private signature_column As B4XTableColumn
End Sub
'You can add more parameters here.
Public Sub Initialize As Object
B4XPages.GetManager.LogEvents = True
Return Me
End Sub
'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
Root = Root1
Root.LoadLayout("1")
xui.SetDataFolder(File.DirInternal)
Log(xui.DefaultFolder)
editCol = B4XTable1.AddColumn("Edit", B4XTable1.COLUMN_TYPE_TEXT)
editCol.Sortable = False
editCol.Width = 127dip
B4XTable1.RowHeight = 50dip
B4XTable1.HeadersHeight =70dip
B4XTable1.AddColumn("Naspo Numero", B4XTable1.COLUMN_TYPE_TEXT)
B4XTable1.AddColumn("Data Verifica", B4XTable1.COLUMN_TYPE_TEXT)
B4XTable1.AddColumn("Ubicazione", B4XTable1.COLUMN_TYPE_TEXT)
B4XTable1.AddColumn("Livello", B4XTable1.COLUMN_TYPE_TEXT)
B4XTable1.AddColumn("Riferimento Planimetria", B4XTable1.COLUMN_TYPE_TEXT)
B4XTable1.AddColumn("Naspo presente ed indicato con cartello", B4XTable1.COLUMN_TYPE_TEXT)
B4XTable1.AddColumn("Pannello frontale integro e facilmente apribile", B4XTable1.COLUMN_TYPE_TEXT)
B4XTable1.AddColumn("Etichette presenti e correttamente compilate", B4XTable1.COLUMN_TYPE_TEXT)
B4XTable1.AddColumn("Componenti integri", B4XTable1.COLUMN_TYPE_TEXT)
B4XTable1.AddColumn("Lancia presente", B4XTable1.COLUMN_TYPE_TEXT)
B4XTable1.AddColumn("Esito della Verifica", B4XTable1.COLUMN_TYPE_TEXT)
B4XTable1.AddColumn("Prossima Verifica entro il", B4XTable1.COLUMN_TYPE_TEXT)
B4XTable1.AddColumn("Incaricato", B4XTable1.COLUMN_TYPE_TEXT)
B4XTable1.AddColumn("Qualifica", B4XTable1.COLUMN_TYPE_TEXT)
B4XTable1.AddColumn("Responsabile", B4XTable1.COLUMN_TYPE_TEXT)
signature_column=B4XTable1.AddColumn("Firma",B4XTable1.COLUMN_TYPE_TEXT)
B4XTable1.NumberOfFrozenColumns= 1
xSelections.Initialize(B4XTable1)
xSelections.Mode = xSelections.MODE_SINGLE_LINE_PERMANENT
xSelections.SelectionColor=xui.Color_Red 'you can change the color of the row selected with this
For i = 1 To signature_column.CellsLayouts.Size - 1
Dim pnl As B4XView = signature_column.CellsLayouts.Get(i)
Dim iv As B4XView
pnl.AddView(iv, 5dip, 5dip, signature_column.Width - 10dip, B4XTable1.RowHeight - 10dip)
Next
LoadData
PrefDialog.Initialize(Root, "Schede verifiche di sorveglianza", 300dip, 300dip)
PrefDialog.LoadFromJson(File.ReadString(File.DirAssets, "naspo.json"))
PrefDialog.Dialog.BlurBackground=True
PrefDialog.Dialog.PutAtTop=True
PrefDialog.Dialog.TitleBarColor=Colors.Red
PrefDialog.Dialog.TitleBarTextColor=Colors.White
PrefDialog.Dialog.TitleBarHeight=130
PrefDialog.Dialog.ButtonsColor=Colors.Red
PrefDialog.Dialog.ButtonsTextColor=Colors.White
PrefDialog.SearchTemplate.Resize(90%x,50%y)
PrefDialog.SearchTemplate.SearchField.HintText="Cerca..."
PrefDialog.SearchTemplate.SearchField.Update
PrefDialog.SearchTemplate.MaxNumberOfItemsToShow = 300
B4XTable1.MaximumRowsPerPage = 10
B4XTable1.BuildLayoutsCache(B4XTable1.MaximumRowsPerPage)
For i = 1 To editCol.CellsLayouts.Size - 1
Dim p As B4XView = editCol.CellsLayouts.Get(i)
p.AddView(CreateButton("btnEdit", Chr(0xF044)), 2dip, 5dip, 40dip, 40dip)
p.AddView(CreateButton("btnDelete", Chr(0xF00D)), 44dip, 5dip, 40dip, 40dip)
p.AddView(CreateButton("btnDuplicate",Chr(0xF0C5)), 85dip, 5dip, 40dip, 40dip)
Next
provider.Initialize
End Sub
Private Sub B4XPage_Background
' 'ExportTableToCSV
End Sub
Sub LoadData
If File.Exists(xui.DefaultFolder, CSVFile) Then
Dim su As StringUtils
data = su.LoadCSV(xui.DefaultFolder, CSVFile, ",")
Else
data.Initialize
End If
B4XTable1.HeaderFont = xui.CreateDefaultBoldFont(14)
B4XTable1.LabelsFont = xui.CreateDefaultFont(14)
B4XTable1.SetData(data)
End Sub
Sub btnAdd_Click
ShowDialog(CreateMap(), 0)
End Sub
Sub btnPrint_Click
If File.Exists(provider.SharedFolder,"report.pdf")=True Then
File.Delete(provider.SharedFolder,"report.pdf")
End If
Dim html2pdf As PalmoHtmlToPdf
html2pdf.Initialize("html2pdf")
html2pdf.ConvertFromString(ExportToHtml(B4XTable1),provider.SharedFolder,"report.pdf")
Sleep(0)
End Sub
Sub btnDelete_Click
Dim RowId As Long = GetRowId(Sender)
Dim Item As Map = B4XTable1.GetRow(RowId)
Dim sf As Object = xui.Msgbox2Async($"Cancello Naspo n.ro: ${Item.Get("Naspo Numero")}?"$, "", "Si", "", "No", Null)
Wait For (sf) Msgbox_Result (Result As Int)
If Result = xui.DialogResponse_Positive Then
Dim filefirma As String
filefirma=Item.Get("Incaricato") & "_" & Item.Get("Naspo Numero") & "_" & Item.Get("Data") & ".png"
If File.Exists(File.DirInternal,filefirma)=True Then
File.Delete(File.DirInternal,filefirma)
End If
B4XTable1.sql1.ExecNonQuery2("DELETE FROM data WHERE rowid = ?", Array(RowId))
B4XTable1.UpdateTableCounters
ExportTableToCSV
End If
End Sub
Sub btnEdit_Click
Dim RowId As Long = GetRowId(Sender)
Dim Item As Map = B4XTable1.GetRow(RowId)
old_filefirma=Item.Get("Incaricato") & "_" & Item.Get("Naspo Numero") & "_" & Item.Get("Data") & ".png"
Dim swap As Map
swap.Initialize
DateTime.DateFormat="dd-MM-yyyy"
swap.Put("naspo_numero",Item.Get("Naspo Numero"))
swap.Put("data",DateTime.DateParse(Item.Get("Data Verifica")))
swap.Put("ubicazione",Item.Get("Ubicazione"))
swap.Put("livello",Item.Get("Livello"))
swap.Put("riferimenti_planimetria",Item.Get("Riferimento Planimetria"))
swap.Put("check1",IIf(Item.Get("Naspo presente ed indicato con cartello")="Positivo",True,False))
swap.Put("check2",IIf(Item.Get("Pannello frontale integro e facilmente apribile")="Positivo",True,False))
swap.Put("check3",IIf(Item.Get("Etichette presenti e correttamente compilate")="Positivo",True,False))
swap.Put("check4",IIf(Item.Get("Componenti integri")="Positivo",True,False))
swap.Put("check5",IIf(Item.Get("Lancia presente")="Positivo",True,False))
swap.Put("esito",Item.Get("Esito della Verifica"))
swap.Put("data_next",DateTime.DateParse(Item.Get("Prossima Verifica entro il")))
swap.Put("incaricato",Item.Get("Incaricato"))
swap.Put("qualifica",Item.Get("Qualifica"))
swap.Put("responsabile",Item.Get("Responsabile"))
swap.Put("firma",Item.Get("Firma"))
ShowDialog(swap, RowId)
End Sub
Sub btnDuplicate_Click
Dim RowId As Long = GetRowId(Sender)
Dim Item As Map = B4XTable1.GetRow(RowId)
Dim swap As Map
swap.Initialize
DateTime.DateFormat="dd-MM-yyyy"
swap.Put("naspo_numero",Item.Get("Naspo Numero"))
swap.Put("data",DateTime.DateParse(Item.Get("Data Verifica")))
swap.Put("ubicazione",Item.Get("Ubicazione"))
swap.Put("livello",Item.Get("Livello"))
swap.Put("riferimenti_planimetria",Item.Get("Riferimento Planimetria"))
swap.Put("check1",IIf(Item.Get("Naspo presente ed indicato con cartello")="Positivo",True,False))
swap.Put("check2",IIf(Item.Get("Pannello frontale integro e facilmente apribile")="Positivo",True,False))
swap.Put("check3",IIf(Item.Get("Etichette presenti e correttamente compilate")="Positivo",True,False))
swap.Put("check4",IIf(Item.Get("Componenti integri")="Positivo",True,False))
swap.Put("check5",IIf(Item.Get("Lancia presente")="Positivo",True,False))
swap.Put("esito",Item.Get("Esito della Verifica"))
swap.Put("data_next",DateTime.DateParse(Item.Get("Prossima Verifica entro il")))
swap.Put("incaricato",Item.Get("Incaricato"))
swap.Put("qualifica",Item.Get("Qualifica"))
swap.Put("responsabile",Item.Get("Responsabile"))
swap.Put("firma",Item.Get("Firma"))
ShowDialog(swap, 0) 'RowId = 0 means that a new item will be created
End Sub
Sub GetRowId (View As B4XView) As Long
Dim RowIndex As Int = editCol.CellsLayouts.IndexOf(View.Parent)
Dim RowId As Long = B4XTable1.VisibleRowIds.Get(RowIndex - 1) ' 1 because of the header
Return RowId
End Sub
Sub CreateButton (EventName As String, Text As String) As B4XView
Dim Btn As Button
Dim FontSize As Int = 14
#if B4i
Btn.InitializeCustom(EventName, xui.Color_Black, xui.Color_White)
FontSize = 16
#else
Btn.Initialize(EventName)
#end if
Dim x As B4XView =Btn
x.SetColorAndBorder(Colors.Red,1dip,Colors.Red,40dip)
x.Font = xui.CreateFontAwesome(FontSize)
'x.Color=Colors.Red
x.TextColor=Colors.White
x.Visible = False
x.Text = Text
Return x
End Sub
Private Sub B4XTable1_DataUpdated
For i = 0 To B4XTable1.VisibleRowIds.Size - 1
Dim p As B4XView = editCol.CellsLayouts.Get(i + 1)
p.GetView(1).Visible = B4XTable1.VisibleRowIds.Get(i) > 0
p.GetView(2).Visible = p.GetView(1).Visible
p.GetView(3).Visible = p.GetView(1).Visible
Next
For i = 0 To B4XTable1.VisibleRowIds.Size - 1
Dim RowId As Long = B4XTable1.VisibleRowIds.Get(i)
Dim pnl As B4XView = signature_column.CellsLayouts.Get(i + 1) '+1 because the first cell is the header
Dim iv As B4XView = pnl.GetView(1) 'ImageView will be the 2nd child of the panel. The built-in label is the first.
If RowId > 0 Then
Dim row As Map = B4XTable1.GetRow(RowId)
Dim signature_load_file As String
signature_load_file=row.Get("Incaricato") & "_" & row.Get("Naspo Numero") & "_" & row.Get("Data Verifica") & ".png"
Log(signature_load_file)
If File.Exists(File.DirInternal,signature_load_file)=True Then
iv.setBitmap(xui.LoadBitmapResize(File.DirInternal,signature_load_file, iv.Width, iv.Height, True))
End If
Else
'empty row
iv.setBitmap(Null)
End If
Next
xSelections.Refresh
End Sub
Sub B4XTable1_CellClicked (ColumnId As String, RowId As Long)
xSelections.CellClicked(ColumnId, RowId)
B4XRowData.Initialize
B4XRowData= B4XTable1.GetRow(RowId)
End Sub
Private Sub ShowDialog(Item As Map, RowId As Long)
DateTime.DateFormat="dd-MM-yyyy"
Dim sf As Object =PrefDialog.ShowDialog(Item, "OK", "CANCELLA")
PrefDialog.CustomListView1.AnimationDuration = 0
For i = 0 To PrefDialog.PrefItems.Size - 1
Dim pi As B4XPrefItem = PrefDialog.PrefItems.Get(i)
If pi.ItemType = PrefDialog.TYPE_BOOLEAN Then
PrefDialog.CustomListView1.ResizeItem(i, 80dip)
Dim pnl As B4XView = PrefDialog.CustomListView1.GetPanel(i)
pnl.Height = pnl.Parent.Height
pnl.GetView(0).Height = pnl.Height - 20dip
pnl.GetView(0).TextSize=12
pnl.GetView(1).Top = 20dip
End If
Next
Wait For (sf) Complete (Result As Int)
If Result = xui.DialogResponse_Positive Then
Dim params As List
params.Initialize
params.AddAll(Array(Item.Get("naspo_numero"), DateTime.Date(Item.Get("data")), Item.Get("ubicazione"), Item.Get("livello"),Item.Get("riferimenti_planimetria"),IIf(Item.Get("check1")=True,"Positivo","Negativo"),IIf(Item.Get("check2")=True,"Positivo","Negativo"),IIf(Item.Get("check3")=True,"Positivo","Negativo"),IIf(Item.Get("check4")=True,"Positivo","Negativo"),IIf(Item.Get("check5")=True,"Positivo","Negativo"),Item.Get("esito"),DateTime.Date(Item.Get("data_next")),Item.Get("incaricato"),Item.Get("qualifica"),Item.Get("responsabile"),IIf(Item.Get("firma")=Null,"",""))) 'keys based on the template json file
If RowId = 0 Then 'new row
B4XTable1.sql1.ExecNonQuery2($"INSERT INTO data VALUES("", ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"$, params)
B4XTable1.Refresh
B4XTable1.UpdateTableCounters
Else
params.Add(RowId)
'first column is c0. We skip it as this is the "edit" column
B4XTable1.sql1.ExecNonQuery2("UPDATE data SET c1 = ?, c2 = ?, c3 = ?, c4 = ?,c5 = ?,c6 = ?,c7 = ?,c8 = ?,c9 = ?,c10 = ?,c11 = ?,c12 = ?,c13 = ?,c14 = ?,c15 = ?,c16 = ? WHERE rowid = ?", params)
B4XTable1.Refresh
B4XTable1.UpdateTableCounters
End If
Log(Item)
current_filefirma=Item.Get("Incaricato") & "_" & Item.Get("Naspo Numero") & "_" & Item.Get("Data Verifica") & ".png"
If old_filefirma<>"" Then
If File.Exists(File.DirInternal,old_filefirma)=True Then
File.Delete(File.DirInternal,old_filefirma)
End If
End If
Dim Dialog As B4XDialog
Dim Base As B4XView
Base = B4XPages.GetNativeParent(Me)
Dialog.Initialize (Base)
Dialog.Title = "Firma"
Dialog.TitleBarColor=xui.Color_Red
Dialog.TitleBarTextColor=xui.Color_White
Dim SignatureTemplate As B4XSignatureTemplate
SignatureTemplate.Initialize
SignatureTemplate.AddDateAndTime=False
Do While True
Dim rs As ResumableSub = Dialog.ShowTemplate(SignatureTemplate, "Conferma", "","Reset")
Dialog.GetButton(xui.DialogResponse_Positive).TextColor = xui.Color_Green
Dialog.GetButton(xui.DialogResponse_Cancel).TextColor = xui.Color_Red
Wait For (rs) Complete (Result As Int)
If Result = xui.DialogResponse_Positive Then
If SignatureTemplate.NumberOfPoints>0 Then
Dim out As OutputStream = File.OpenOutput(File.DirInternal, current_filefirma, False)
SignatureTemplate.Bitmap.WriteToStream(out,100,"PNG")
out.Close
Exit
End If
End If
Loop
ExportTableToCSV
End If
Log(data)
End Sub
Public Sub ExportTableToCSV
Dim data As List
data.Initialize
Dim rs As ResultSet = B4XTable1.sql1.ExecQuery("SELECT * FROM data")
Do While rs.NextRow
Dim row(B4XTable1.Columns.Size) As String
For i = 0 To B4XTable1.Columns.Size - 1
Dim c As B4XTableColumn = B4XTable1.Columns.Get(i)
row(i) = rs.GetString(c.SQLID)
Next
data.Add(row)
Loop
rs.Close
Dim su As StringUtils
Log(data)
su.SaveCSV(xui.DefaultFolder, CSVFile, ",", data)
End Sub
Private Sub ExportToHtml (tbl As B4XTable) As String
Dim HtmlCSS As String = $"@page {size: legal landscape;}
table {width: 100%;border: 1px solid #cef;text-align: center; }
th { font-weight: bold; font-size: 12px; background-color: #acf; border-bottom: 1px solid #cef; }
td { font-size: 12px; width:auto; white-space:nowrap}
td,th { padding: 4px 5px; }
.odd {background-color: #def; }
.odd td {border-bottom: 1px solid #cef; }
a { text-decoration:none; color: #000; }"$
Dim sb As StringBuilder
sb.Initialize
sb.Append("<html><body>").Append(CRLF)
sb.Append("<style type='text/css'>").Append(HtmlCSS).Append("</style>").Append(CRLF)
sb.Append("<table><thead><tr>").Append(CRLF)
For Each col As B4XTableColumn In tbl.Columns
sb.Append("<th>").Append($"$Xml{col.Title}"$).Append("</th>")
Next
sb.Append("</thead>")
sb.Append("</tr>").Append(CRLF)
Dim rs As ResultSet = tbl.sql1.ExecQuery("SELECT * FROM data")
Dim row As Int
Do While rs.NextRow
If row Mod 2 = 0 Then
sb.Append("<tr>")
Else
sb.Append("<tr class='odd'>")
End If
For i = 0 To rs.ColumnCount - 1
sb.Append("<td>")
sb.Append($"$Xml{rs.GetString2(i)}"$)
sb.Append("</td>")
Next
sb.Append("</tr>").Append(CRLF)
row = row + 1
Loop
rs.Close
sb.Append("</table></body></html>")
Return sb.ToString
End Sub
Private Sub html2pdf_Finished(Success As Boolean)
Log(Success)
If Success =True Then
Dim in As Intent
in.Initialize(in.ACTION_VIEW, "")
provider.SetFileUriAsIntentData(in, "report.pdf")
in.SetType("application/pdf")
StartActivity(in)
End If
End Sub