Page 1 of 1

Handling Custom Flags in a plugin

Posted: 02 Mar 2020 15:13
by GladToBeGrey
I'm attempting to write my first plugin. I want to sort an Individual's Flags into ascending alphanumeric sequence. My though was to loop through the INDI records, llocate the _FLGS tag (if present), then load the Flags themselves into a table, sort that and then replace the Flags back in the file.

The Flags appear thus in the GEDCOM file:

Code: Select all

1 _FLGS
2 __1891_UK_CENSU 1891 UK Census
2 __1871_UK_CENSU 1871 UK Census
I can get the TAG (__1852_UK_CENSU) for each Flag easily enough, but cannot work out how to retrieve the following text (1851 UK Census) as well.

Can anyone give me a steer, please?

Re: Handling Custom Flags in a plugin

Posted: 02 Mar 2020 16:01
by tatewise
That is an interesting question, especially as a little experimentation shows Flags behave a bit differently from normal.

Although the Name of the Flag is saved as its Text Value in the GEDCOM file the usual fhGetValueAsText(ptrFlag) does not return the value of such as 2 __1891_UK_CENSU 1891 UK Census.
However, fhGetDisplayText(ptrFlag) does return the Name but suffixed with : Y.
So in the above example would give 1891 UK Census: Y.

However, since all Flag Tags must be unique, you could probably get away with sorting the tags.
i.e. __1891_UK_CENSU which is obtained using fhGetTag(ptrFlag).

Re: Handling Custom Flags in a plugin

Posted: 02 Mar 2020 17:43
by tatewise
Here is a sample script to illustrate the methods:

Code: Select all

ptrIndi = fhNewItemPtr()
ptrIndi:MoveToFirstRecord('INDI')
while ptrIndi:IsNotNull() do
	local ptrFlag = fhGetItemPtr(ptrIndi,"~._FLGS")
	if ptrFlag:IsNotNull() then
		ptrFlag:MoveToFirstChildItem(ptrFlag) 
		while ptrFlag:IsNotNull() do
			local strTag  = fhGetTag(ptrFlag)
			local strText = fhGetDisplayText(ptrFlag)
			print( "Tag: ", strTag, "\t Flag: ", strText )
			ptrFlag:MoveNext()
		end
	end
	ptrIndi:MoveNext()
end
That prints such as:

Code: Select all

Tag: 	__1891_UK_CENSU		 Flag: 	1891 UK Census: Y
Tag: 	__1871_UK_CENSU		 Flag: 	1871 UK Census: Y

Re: Handling Custom Flags in a plugin

Posted: 03 Mar 2020 10:11
by GladToBeGrey
Thanks for that Mike, very helpful (and pleased it interested you!). It perhaps also gives a clue why my initial attempt using fhGetItemText() just returned a "Y".

Code: Select all

	ptrFlag:MoveToFirstChildItem(ptrFLGS)
	while not ptrFlag:IsNull() do
		local strFlagText = fhGetItemText(ptrFlag, "~.")
		print(strFlagText.."\n")
		ptrFlag:MoveNext()
	end
	

Re: Handling Custom Flags in a plugin

Posted: 03 Mar 2020 10:59
by tatewise
Yes, Flags are interesting as the usual ways of getting values operate differently from other fields.
The usual API functions are:
local strText = fhGetDisplayText(ptrFlag) returns "1891 UK Census: Y"
local strMin = fhGetDisplayText(ptrFlag,"~.","MIN") returns just "Y"
local strItem = fhGetItemText(ptrFlag,"~.") also returns just "Y"
local strVal = fhGetValueAsText(ptrFlag) returns empty string ""

Whereas, a field with a value such as Address produces:
local strText = fhGetDisplayText(ptrItem) returns "Address: 12 Sharps Lane, Cheltenham"
local strText = fhGetDisplayText(ptrItem,"~.","MIN") returns "12 Sharps Lane, Cheltenham"
local strItem = fhGetItemText(ptrItem, "~.") also returns "12 Sharps Lane, Cheltenham"
local strVal = fhGetValueAsText(ptrItem) also returns "12 Sharps Lane, Cheltenham"
i.e. They all supply the field value.

Re: Handling Custom Flags in a plugin

Posted: 05 Mar 2020 17:27
by GladToBeGrey
Fundamentally, I wonder why ":Y" is appended to the Display Text for a Flag in the first place?

It might be interesting to know why Flags are handled differently - the ":Y" presumably serves some internal purpose? It might it be something that could be utilised in a plugin if we knew its purpose. But wouldn't it be better added as a third, separate string, not returned by fhGetDisplayText(ptr) by default, but only if specifically requested?

That said, it's obviously fairly easy to handle (strip off) the unwanted trailing ":Y" if present.