Note Records with Options.fh_lua

--[[
@Title:			Note Records with Options
@Type:				Report
@Author:			Mike Tate
@Contributors:	Calico Pie
@Version:			1.2
@Keywords:		
@LastUpdated:	26 Oct 2021
@Description:	Report on one or more Note records with Custom ID or Note Text or user chosen Heading Level 1 text and Privacy bracket text options.
@V1.2:				Heading honours privacy bracketed text settings and is truncated at 100 characters; Handles Books better; Restore Defaults for Sources and Page Layout tabs;
@V1.1:				First published in the Plugin Store;
]]

-- Local variables defined here are available in all functions.
local sReportOptsFile = fhGetContextInfo("CI_REPORT_OPTIONS_FILE")
local report_settings = {}  -- Table for Report settings used in the program
local sDefaultDataRef = "%NOTE.REFN%"

------------------------------------------------------------
-- FH_Requirements
------------------------------------------------------------
function FH_Requirements()
	local t = {}

	-- default startup options
	t.record_type = "NOTE"
	t.generations = false	

	-- report options dialog requirements
	t.tabs = "sources,format"				-- report options tabs required (may not show in rpt opts when report is part of a book)
	t.fonts = "hdg1,secdata,seclabel"	-- fonts shown in format tab of report options
	t.heading_levels = 1					-- affects no. of heading levels shown in format tab of report options
	t.column_indents = 0					-- no. of column indent value required in layout tab of rpt opts
	t.para_indent = false					-- para indent value required in layout tab of rpt opts
	t.options =								-- Heading and [[Private]] notes options
		"~headingoption|Select Heading Level 1 content|dropdownedit(%NOTE.REFN%;%NOTE.TEXT%)|"..sDefaultDataRef.."|110:200\n" ..
		"~privatenotes|Inc. [[Private]] Notes|checkbox|true|340\n" ..
		"~removebrackets|    Remove [[ and ]]|checkbox|false|340\n"
	return t
end

---------------------------------------------------------------------------
-- FH_Reset
---------------------------------------------------------------------------
function FH_Reset(sTab)

	if not(sTab) or sTab == "main" then
		fhSetIniFileValue(sReportOptsFile, "Plugin"			,"headingoption"						,"text", sDefaultDataRef	)
		fhSetIniFileValue(sReportOptsFile, "Plugin"			,"privatenotes"							,"bool", true		)
		fhSetIniFileValue(sReportOptsFile, "Plugin"			,"removebrackets"						,"bool", false		)

		fhSetIniFileValue(sReportOptsFile, "Report Options","Select Heading Level 1 content"	,"text", sDefaultDataRef	)
		fhSetIniFileValue(sReportOptsFile, "Report Options","Inc [[private]] Notes"				,"bool", true		)
		fhSetIniFileValue(sReportOptsFile, "Report Options","Remove [[ and ]]"						,"bool", false		)
	end
	if not(sTab) or sTab == "format" then
		fhSetIniFileValue(sReportOptsFile, "Report Options","Font - Section Hdg"					,"text", "12,0,0,0,700,0,0,0,0,3,2,1,18,Times New Roman"	)
		fhSetIniFileValue(sReportOptsFile, "Report Options","Font - Label"							,"text", "11,0,0,0,700,0,0,0,0,3,2,1,18,Times New Roman"	)
	end	
	if not(sTab) or sTab == "sources" then
		fhSetIniFileValue(sReportOptsFile, "Report Options","Include Sources"						,"bool", true		)
		fhSetIniFileValue(sReportOptsFile, "Report Options","Include Bibliography"				,"bool", true		)
		-- and many more...
	end
	if not(sTab) or sTab == "layout" then
		fhSetIniFileValue(sReportOptsFile, "Report Options","Landscape"								,"bool", false		)
		fhSetIniFileValue(sReportOptsFile, "Report Options","Header - Left"							,"text", ""			)
		fhSetIniFileValue(sReportOptsFile, "Report Options","Header - Centre"						,"text", "=Title()"	)
		fhSetIniFileValue(sReportOptsFile, "Report Options","Header - Right"						,"text", "=Date()"	)
		fhSetIniFileValue(sReportOptsFile, "Report Options","Footer - Left"							,"text", ""			)
		fhSetIniFileValue(sReportOptsFile, "Report Options","Footer - Centre"						,"text", "=Page()"	)
		fhSetIniFileValue(sReportOptsFile, "Report Options","Footer - Right"						,"text", "Produced by Family Historian"	)
		-- and many more...
	end

	fhSetIniFileValue(sReportOptsFile, "Report Options","Heading Rec-Text"	,"text",report_settings.headingoption)

	return true;
end

---------------------------------------------------------------------------
-- getSettings
---------------------------------------------------------------------------
function getSettings()
	report_settings.headingoption	= fhGetIniFileValue(sReportOptsFile,"Plugin","headingoption" ,"text","")
	report_settings.privatenotes 	= fhGetIniFileValue(sReportOptsFile,"Plugin","privatenotes"  ,"bool",true)
	report_settings.removebrackets	= fhGetIniFileValue(sReportOptsFile,"Plugin","removebrackets","bool",false)
end

---------------------------------------------------------------------------
-- handle [[Private]] text
---------------------------------------------------------------------------
function getPrivate(sText)
	if not(report_settings.privatenotes) then
		-- Strip private
		sText = sText:gsub("%[%[.-%]%]","")
		sText = sText:gsub("%[%[.-$","")
	end
	if report_settings.removebrackets then
		-- Strip [[ ]]
		sText = sText:gsub("%[%[","")
		sText = sText:gsub("%]%]","")
	end
	return sText
end

------------------------------------------------------------
-- FH_GetRecordSectionContent
------------------------------------------------------------
function FH_GetRecordSectionContent(snTop, rec, index, count)

	getSettings()

	fhSetIniFileValue(sReportOptsFile, "Report Options","Heading Rec-Text"	,"text",report_settings.headingoption)

	local sHeading1 = ""
	if fhGetContextInfo("CI_BOOK_CONTEXT") then
		sHeading1 = fhGetContextInfo("CI_BOOK_ITEM_HEADING")
		fhMessageBox(sHeading1)
	else
		-- Parse heading option into text and %dataref% components
		local sOpt = report_settings.headingoption
		local tOpt = {}
		for sPre,sRef in sOpt:gmatch("(.-)(%%.-%%)") do
			table.insert(tOpt,sPre)
			if sRef == "%%" then sRef = "%"
			elseif fhIsValidDataRef(sRef) then
				sRef = fhGetItemText(rec,sRef)
				sRef = getPrivate(sRef)
			end
			table.insert(tOpt,sRef)
		end
		local sEnd = sOpt:match("([^%%]-)$")
		table.insert(tOpt,sEnd)
		sHeading1 = table.concat(tOpt)
	end

	-- Set heading of report to user option but truncate at 100 characters
	local rtHeading = fhNewRichText()
	local sHeading1 = fhCallBuiltInFunction("LeftText",sHeading1,100,"ELLIPSIS")
	rtHeading:SetText(fhFtfEncode(sHeading1))
	snTop:SetHeading(rtHeading)

	-- Set body of report to the Note record text
	local rt = fhNewRichText()
	pr = fhNewItemPtr()
	pr:MoveTo(rec,"~.TEXT")

	if pr:IsNull() then
		rt = fhNewRichText("(no text)")
	else
		local tx = ""
		local strDataClass = fhGetDataClass(pr)
		if strDataClass == "richtext" then
			local rt = fhGetValueAsRichText(pr)
			tx = rt:GetText()
		else
			tx = fhGetValueAsText()
		end
		tx = getPrivate(tx)
		rt:AddText(tx,(strDataClass == "richtext"))
	end

	-- Append all Text Level and Record Level Sources to end of report
	rt:AddCitation(pr,true)
	rt:AddCitation(rec)

	snTop:SetBodyText(rt)
end   

------------------------------------------------------------
-- Main
------------------------------------------------------------
if DEBUG_MODE then
	sn = fhNewSection()
	pr = fhNewItemPtr()
	pr:MoveToFirstRecord("NOTE")
	FH_GetRecordSectionContent(sn, pr, 1, 1)
end

Source:Note-Records-with-Options-1.fh_lua