Rem CSMC Employee Directory HTML File Generator
Rem Copyright (C) 1994-95 Ray Duncan

Option Compare Database   ' use database order for string comparisons
Option Explicit           ' force declaration of all variables


Function Main ()

  ' This is the main routine of the conversion program; it can be invoked with
  ' a command button or by running a macro. It generates several different
  ' sorted indexes of employee names, then generates an individual file for
  ' each employee containing the detailed information about that employee.

  Call WriteHTMLNameIndexFiles("CSMC Directory by Employee Name", "idxname", ".htm", "EntireName", "LastName", 1.5)
  Call WriteHTMLIndexFile("CSMC Directory by VAX Account", "idxvax.htm", "VaxMail", "VaxMail", 1.5)
  Call WriteHTMLIndexFile("CSMC Directory by Extension", "idxext.htm", "Extension", "Extension", 1.5)
  Call WriteHTMLIndexFile("CSMC Directory by Location", "idxlocn.htm", "Location", "Location", 1.5)
  Call WriteHTMLIndexFile("CSMC Directory by Department", "idxdept.htm", "Dept", "Department", 3)
  Call WriteHTMLDetailFiles("EntireName")

End Function


Sub WriteHTMLDetailFiles (IndexField As String)

  ' This routine writes the HTML code for the employee detail information.
  ' An individual file is generated for each employee. The name of the file
  ' is a function of a counter field in the employee record.  The caller
  ' specifies the sort order (which isn't really too important).

  Dim Dbase As Database, Dset1 As Recordset, FileName As String
  Dim TopicTitle As String

  ' open database and employee information table, set the sort order
  Set Dbase = DBEngine.Workspaces(0).Databases(0)
  Set Dset1 = Dbase.OpenRecordset("CSMC Directory", DB_OPEN_TABLE)
  Dset1.Index = IndexField
  
  ' now walk through the sorted recordset and write the detail records
  While Dset1.EOF <> True
  
    ' build topic title & employee detail filename;
    ' the filename is synthesized from a counter field
    TopicTitle = (Mcase(Dset1![LastName]) & ", " & Mcase(Dset1![FirstName]))
    FileName = "emp" & LTrim$(Str$(Dset1![ID])) & ".htm"
    UpdateStatusBar ("Writing " & FileName)
    
    ' write the HTML header for the employee detail file,
    ' along with the full employee name as a header for the
    ' window's client area
    Call EmitHTMLHeader(1, FileName, TopicTitle)

    ' force use of a nonproportional font so things
    ' can be lined up vertically
    Print #1, "<PRE>"

    ' write the employee detailed information to the file
    Call WriteHTMLDetailItem(1, "Credential", Dset1![Credential])
    Call WriteHTMLDetailItem(1, "Title #1", Dset1![Title1])
    Call WriteHTMLDetailItem(1, "Title #2", Dset1![Title2])
    Print #1,
    Call WriteHTMLDetailItem(1, "Department", Dset1![Department])
    Call WriteHTMLDetailItem(1, "Division", Dset1![Division])
    Call WriteHTMLDetailItem(1, "Location", Dset1![Location])
    Call WriteHTMLDetailItem(1, "Mail Stop", Dset1![MailStop])
    Print #1,
    Call WriteHTMLDetailItem(1, "Extension", Dset1![Extension])
    Call WriteHTMLDetailItem(1, "Dept. Ext.", Dset1![DeptExt])
    Call WriteHTMLDetailItem(1, "FAX", Dset1![FAX])
    Call WriteHTMLDetailItem(1, "Pager", Dset1![Pager])
    Call WriteHTMLDetailItem(1, "VAXMail", Dset1![VaxMail])
    Print #1,
    Call WriteHTMLDetailItem(1, "Other Info", Dset1![Other])
    
    ' turn off the nonproportional font
    Print #1, "</PRE>"

    ' write footer information and HTML file terminators
    Call EmitHTMLTrailer(1)
    
    ' go to the next record
    Dset1.MoveNext

  Wend
  
  ' close the sorted employee recordset
  Dset1.Close

End Sub


Sub WriteHTMLDetailItem (FileNum As Integer, ItemName As String, ItemData As Variant)

  ' This handy little routine writes a field title, tabs over (by
  ' use of nonproportional spaces), displays a detail item in boldface,
  ' and forces the end of line (no <BR> or <P> is needed since this
  ' routine is used within a <PRE> block).

  Dim TabChars As Integer

  ' calculate number of spaces needed to simulate tab
  TabChars = 20 - Len(ItemName)

  ' if the item name is too long, force at least one
  ' space to separate the item name from the item data
  If TabChars < 1 Then Let TabChars = 1

  ' generate the HTML code for the item detail line, putting
  ' the item data in boldface if it is not null
  If IsNull(ItemData) Then
    Print #FileNum, ItemName & ":"
  Else
    Print #FileNum, ItemName & ":" & Space$(TabChars) & "<B>" & ItemData & "</B>"
  End If


End Sub


Sub WriteHTMLIndexFile (TopicTitle As String, FileName As String, IndexField As String, 
                        InfoField As String, TabStop As Single)

  ' This routine writes an HTML employee index file, consisting of a list
  ' of hyperlinks, sorted on the specified database field.

  Dim Dbase As Database, Dset1 As Recordset
  Dim HotLinkString As String, TargetString As String
  Dim TabChars As Integer
  Const TabWidth = 10

  ' open database and employee data table, set the sort order
  Set Dbase = DBEngine.Workspaces(0).Databases(0)
  Set Dset1 = Dbase.OpenRecordset("CSMC Directory", DB_OPEN_TABLE)
  Dset1.Index = IndexField
  
  ' create the index file, write HTML header information
  Call EmitHTMLHeader(1, FileName, TopicTitle)
  UpdateStatusBar ("Writing " & FileName)

  ' turn on preformatted text attribute
  Print #1, "<PRE>"

  ' now walk through the sorted recordset and write the index
  ' as a list of hyperlinks to the employee detail files
  While Dset1.EOF <> True

    ' synthesize the name of the employee detail file from
    ' the counter field in the employee record
    TargetString = "emp" & LTrim$(Str$(Dset1![ID])) & ".htm"
    
    ' skip records where the "InfoField" is empty or nonalphanumeric
    If Dset1(InfoField) >= "0" Then

      ' format the indexed field and employee name for the index entry
      HotLinkString = LTrim$(RTrim$(Dset1(InfoField)))
      TabChars = Int(TabWidth * TabStop) - Len(HotLinkString)
      If TabChars < 1 Then Let TabChars = 1
      HotLinkString = HotLinkString & Space$(TabChars)
      HotLinkString = HotLinkString & Mcase(Dset1![LastName]) & ", " & Mcase(Dset1![FirstName])
      
      ' generate the hotlink and force end-of-line
      Call EmitHTMLHotLink(1, HotLinkString, TargetString)
      Print #1,
    
    End If

    ' go to the next employee record
    Dset1.MoveNext

  Wend
  
  ' turn off preformatted text attribute
  Print #1, "</PRE>"

  ' write the HTML file terminator and close the file
  Call EmitHTMLTrailer(1)
  
  ' close the recordset
  Dset1.Close

End Sub


Sub WriteHTMLNameIndexFiles (TopicTitle As String, FileName As String, 
                             Extension As String, IndexField As String, 
                             InfoField As String, TabStop As Single)

  ' This routine generates a first-level index file and a variable number
  ' of second-level index files for lookups by employee last name.
  ' The 2-level indexing allows much faster accesses over the network,
  ' especially for non-multithreaded first-generation Web browsers
  ' such as Mosaic 1.0.

  Dim Dbase As Database, Dset1 As Recordset
  Dim HotLinkString As String, TargetString As String
  Dim CurFirstChar As String, CurFileName As String
  Dim TabChars As Integer
  Const TabWidth = 10

  CurFirstChar = ""

  ' Create the master (1st level) name index file
  CurFileName = FileName & Extension
  Call EmitHTMLHeader(2, CurFileName, TopicTitle)

  ' write some explanatory information to the 1st level index
  ' file; this will be displayed above the hotlinks to the
  ' 2nd-level index files.
  Print #2, "Click on a letter of the alphabet to view the "
  Print #2, "employee last names beginning with that letter.<P>"

  ' open database and employee data table, set the sort order
  Set Dbase = DBEngine.Workspaces(0).Databases(0)
  Set Dset1 = Dbase.OpenRecordset("CSMC Directory", DB_OPEN_TABLE)
  Dset1.Index = IndexField
  
  ' Now walk through the sorted recordset and write the 2nd level
  ' index files. There is a 2nd-level index for each letter of the
  ' alphabet for which there exists at least one employee last name
  ' starting with that letter. Each 2nd-level index file is a list
  ' of hyperlinks to the employee detail files.
  While Dset1.EOF <> True

    ' synthesize the name of the file containing the employee detail
    ' information from a counter field in the employee record.
    TargetString = "emp" & LTrim$(Str$(Dset1![ID])) & ".htm"
    
    ' skip records where the "InfoField" is empty or nonalphanumeric
    If Dset1(InfoField) >= "0" Then

      ' is the first letter of this employee's last name the same as the
      ' previously processed last name?
      If UCase$(Left$(Dset1![LastName], 1)) <> CurFirstChar Then

	' unless this is the very first time through this
	' loop, close the previous 2nd-level index file.
	If CurFirstChar <> "" Then
  
	   ' turn off preformatted text attribute
	   Print #1, "</PRE>"

	   ' write the HTML file terminator and close the file
	   Call EmitHTMLTrailer(1)

	End If

	' build the filename for this specific 2nd-level index
	CurFirstChar = UCase$(Left$(Dset1![LastName], 1))
	CurFileName = FileName & CurFirstChar & Extension

	' add an entry to the 1st-level index file
	' to point to this 2nd-level index file
	Print #2, "| ";
	Call EmitHTMLHotLink(2, CurFirstChar & "  ", CurFileName)
	Print #2,

	' create the 2nd-level index file and write the
	' various necessary HTML header information into it
	Call EmitHTMLHeader(1, CurFileName, TopicTitle)
	UpdateStatusBar ("Writing " & CurFileName)

	' turn on preformatted text attribute to force use
	' of a nonproportional font
	Print #1, "<PRE>"

      End If

      ' now format the employee name for the 2nd-level index file.
      ' if a phone extension is available, include that too.
      If IsNull(Dset1![Extension]) Then
	  HotLinkString = Space$(TabWidth * TabStop)
	  HotLinkString = HotLinkString & Mcase(Dset1![LastName]) & ", " & Mcase(Dset1![FirstName])
      Else
	  HotLinkString = LTrim$(RTrim$(Dset1![Extension]))
	  TabChars = Int(TabWidth * TabStop) - Len(HotLinkString)
	  If TabChars < 1 Then Let TabChars = 1
	  HotLinkString = HotLinkString & Space$(TabChars)
	  HotLinkString = HotLinkString & Mcase(Dset1![LastName]) & ", " & Mcase(Dset1![FirstName])
      End If

      ' generate the hotlink and write it to the 2nd-level index file
      Call EmitHTMLHotLink(1, HotLinkString, TargetString)
      Print #1,
    
    End If

    ' go to the next employee record
    Dset1.MoveNext

  Wend
  
  ' close out the last 2nd-level index file by
  ' turning off the preformatted text attribute and
  ' writing the necessary HTML terminator info
  Print #1, "</PRE>"
  Call EmitHTMLTrailer(1)
  
  ' close out the 1st-level (master) index file
  Print #2, "| <P>"
  Call EmitHTMLTrailer(2)

  ' close the recordset
  Dset1.Close

End Sub

