List All Civil Registration Index Citations.fh_lua

--[[
@Title: List All Civil Registration Index Citations
@Type: Standard
@Author: Paul Theobald
@Version: 1.0
@Keywords: 
@LastUpdated: 9 November 2025
@Licence: This plugin is copyright (c)2025 Paul Theobald and is licensed under the MIT License which is hereby
incorporated by reference (see https://pluginstore.family-historian.co.uk/fh-plugin-licence).
@Description:
Lists all citations to all sources created from the Civil Registration Index template. For each citation it displays the
fields: Record, Fact, Source, Name Recorded, Date, Registration District, Reference, Assessment and Note.
It also inserts a separate Sortable Date column to allow the citations to be sorted chronologically to work around an FH
sorting bug (ticket #712190).
]]

------------------------------------------------------------------------------------------------------------------------
-- This plugin is for templated sources, which were introduced in FV v7. So specify that as the minimum version.
------------------------------------------------------------------------------------------------------------------------
fhInitialise(7)

------------------------------------------------------------------------------------------------------------------------
-- Define Main function.
------------------------------------------------------------------------------------------------------------------------
function Main()

	local ptrItem = fhNewItemPtr()		-- Item traversing records to find citations
	local intCount = 0					-- Number of citations found

	local tblRecordList = {}			-- Record column in output result set
	local tblFactList = {}				-- Fact column in output result set
	local tblSourceList = {}			-- Source column in output result set
	local tblNameList = {}				-- Name column in output result set
	local tblDateList = {}				-- Date column in output result set
	local tblSortableDateList = {}		-- Sortable date column in output result set
	local tblDistrictList = {}			-- District column in output result set
	local tblReferenceList = {}			-- Reference column in output result set
	local tblAssessmentList = {}		-- Assessment column in output result set
	local tblNoteList = {}				-- Note column in output result set

    -- Iterate through all Individual(INDI) and Family(FAM) records, looking for ones that cite a source(SOUR)
	for intRecordType, strRecordType in ipairs({"INDI", "FAM"}) do
		ptrItem:MoveToFirstRecord(strRecordType)
		while ptrItem:IsNotNull() do
			if (fhGetTag(ptrItem) == "SOUR") then

				-- Found a record with a source(SOUR) now check if the source's template is the the one we're looking for
				local strTemplateName = fhGetItemText(ptrItem,'~>_SRCT>NAME')
				if (strTemplateName == "Civil Registration Index") then

					-- Found a source created from the Civil Registration Index template, now pull out the fields from the citation
					-- (using date as object rather than string so that it can be sorted chronologically rather than alphabetically)
					local strIndexName       = fhGetItemText(ptrItem, "~.~NM-NAME_RECORDED")
					local ptrDate            = fhGetItemPtr (ptrItem, "~.~DT-DATE")
					local strIndexDistrict   = fhGetItemText(ptrItem, "~.~TX-REGISTRATION_DISTRICT")
					local strIndexReference  = fhGetItemText(ptrItem, "~.~TX-REFERENCE")
					local strIndexAssessment = fhGetItemText(ptrItem, "~.QUAY")
					local strIndexNote       = fhGetItemText(ptrItem, "~.NOTE2")
						
					-- Also pull out the date as a Date Point object so that it can be used to sort the output chronologically
					-- to work round a bug in FH that prevents sorting on date objects in templated fields (ticket #712190)
					local dpIndexDate        = fhGetValueAsDate(ptrDate):GetDatePt1()

					-- Find the fact that cites the source, and the fact's parent record
					local ptrFact = fhNewItemPtr()						-- Fact that contains citation
					ptrFact:MoveToParentItem(ptrItem)					-- The Fact is the parent of the Source item

					local ptrRecord = fhNewItemPtr()					-- Record that contains fact
					ptrRecord:MoveToParentItem(ptrFact)					-- The Record is the parent of the Fact

					-- Record all these fields in a row of the result set table 
					table.insert(tblRecordList,      ptrRecord:Clone())	-- Add to Record Column
					table.insert(tblFactList,        ptrFact:Clone())	-- Add to Fact Column
					table.insert(tblSourceList,      ptrItem:Clone())	-- Add to Source Column
					table.insert(tblNameList,        strIndexName)		-- Add to Name column
					table.insert(tblDateList,        ptrDate:Clone())	-- Add to Date column
					table.insert(tblSortableDateList,dpIndexDate)		-- Add to Sortable Date column
					table.insert(tblDistrictList,    strIndexDistrict)	-- Add to District column
					table.insert(tblReferenceList,   strIndexReference)	-- Add to Reference column
					table.insert(tblAssessmentList,  strIndexAssessment)-- Add to Assessment column
					table.insert(tblNoteList,        strIndexNote)		-- Add to Note column

					-- And increment the count of citations found
					intCount = intCount + 1
				end
			end

			-- Move to the next record and loop round again
			ptrItem:MoveNextSpecial()
		end
	end

	-- We've been through all records. Output the result set of all the citations
	fhOutputResultSetColumn("Record",                "item", tblRecordList,       #tblRecordList)
	fhOutputResultSetColumn("Fact",	                 "item", tblFactList,         #tblRecordList)
	fhOutputResultSetColumn("Source",                "item", tblSourceList,       #tblSourceList)
	fhOutputResultSetColumn("Name Recorded",         "text", tblNameList,         #tblNameList)
	fhOutputResultSetColumn("Date",                  "item", tblDateList,         #tblDateList,60,"align_left",1,true,"date","show")
	fhOutputResultSetColumn("Sortable Date",   "date-point", tblSortableDateList, #tblSortableDateList)
	fhOutputResultSetColumn("Registration District", "text", tblDistrictList,     #tblDistrictList)
	fhOutputResultSetColumn("Reference",             "text", tblReferenceList,    #tblReferenceList)
	fhOutputResultSetColumn("Assessment",            "text", tblAssessmentList,   #tblAssessmentList)
	fhOutputResultSetColumn("Note",                  "text", tblNoteList,         #tblNoteList)

	-- Output the number of citations found
	fhMessageBox("Found " .. intCount .. " citations to Civil Registration Index sources")

end

------------------------------------------------------------------------------------------------------------------------
-- Execute Main function,
------------------------------------------------------------------------------------------------------------------------
Main()

Source:List-All-Civil-Registration-Index-Citations.fh_lua