* Handling Custom Flags in a plugin

For users to report plugin bugs and request plugin enhancements; and for authors to test new/new versions of plugins, and to discuss plugin development (in the Programming Technicalities sub-forum). If you want advice on choosing or using a plugin, please ask in General Usage or an appropriate sub-forum.
Post Reply
User avatar
GladToBeGrey
Famous
Posts: 115
Joined: 26 Oct 2004 09:16
Family Historian: V7
Location: Dorset, UK

Handling Custom Flags in a plugin

Post by GladToBeGrey » 02 Mar 2020 15:13

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?
Last edited by GladToBeGrey on 03 Mar 2020 09:51, edited 1 time in total.

User avatar
tatewise
Megastar
Posts: 27080
Joined: 25 May 2010 11:00
Family Historian: V7
Location: Torbay, Devon, UK
Contact:

Re: Handling Custom Flags in a plugin

Post by tatewise » 02 Mar 2020 16:01

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).
Mike Tate ~ researching the Tate and Scott family history ~ tatewise ancestry

User avatar
tatewise
Megastar
Posts: 27080
Joined: 25 May 2010 11:00
Family Historian: V7
Location: Torbay, Devon, UK
Contact:

Re: Handling Custom Flags in a plugin

Post by tatewise » 02 Mar 2020 17:43

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
Mike Tate ~ researching the Tate and Scott family history ~ tatewise ancestry

User avatar
GladToBeGrey
Famous
Posts: 115
Joined: 26 Oct 2004 09:16
Family Historian: V7
Location: Dorset, UK

Re: Handling Custom Flags in a plugin

Post by GladToBeGrey » 03 Mar 2020 10:11

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
	

User avatar
tatewise
Megastar
Posts: 27080
Joined: 25 May 2010 11:00
Family Historian: V7
Location: Torbay, Devon, UK
Contact:

Re: Handling Custom Flags in a plugin

Post by tatewise » 03 Mar 2020 10:59

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.
Mike Tate ~ researching the Tate and Scott family history ~ tatewise ancestry

User avatar
GladToBeGrey
Famous
Posts: 115
Joined: 26 Oct 2004 09:16
Family Historian: V7
Location: Dorset, UK

Re: Handling Custom Flags in a plugin

Post by GladToBeGrey » 05 Mar 2020 17:27

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.

Post Reply