Modul:Faktenbox/Tabs/Adressen/Legacy

Aus FürthWiki

Die Dokumentation für dieses Modul kann unter Modul:Faktenbox/Tabs/Adressen/Legacy/Doku erstellt werden

local p = {}

local com = require("Modul:Common")
local str = require("Modul:String")
local wik = require("Modul:Wiki")
local lfd = require("Modul:LinkFormDatum")

local fab_tabs = require("Modul:Faktenbox/Tabs/Legacy")

function p.adressenTab_(fbdata)
--	mw.log("adressenTab_()")
--	mw.logObject(fbdata)
	-- Konsolenzeile zum Debuggen:
	-- =p.adressenTab_({fullpagename="Jakob Bock"})
	-- =p.adressenTab_({fullpagename="Ludwig Erhard"})
	-- =p.adressenTab_({fullpagename="Druck & Kopie Hartmann KG"})
	-- =p.adressenTab_({fullpagename="Rathaus"})
	-- =p.adressenTab_({fullpagename="E-Tankstelle Hochstraße"})
	-- =p.adressenTab_({fullpagename="Jakob Wassermann"})
	-- =p.adressenTab_({fullpagename="Hedwig Gellinger"})
	local t = ""

	-- Attribute-Liste
	local attr = {
		["Adressart"]	= { querypostfix = "",		default = ""},
		["Straße"] 		= { querypostfix = "#-",	default = ""},
		["Hausnummer"]	= { querypostfix = "",		default = 0},
		["StraßeHnr"]	= { querypostfix = "#-",	default = ""},
		["VonObjekt"]	= { querypostfix = "#-",	default = ""},
		["AdresseVon"]	= { querypostfix = "",		default = ""},
		["AdresseBis"]	= { querypostfix = "",		default = ""}}

	-- SMW-DB-Abfrage
	local query = "[[-Has subobject::".. fbdata.fullpagename .. "]][[SubObjektTyp::Adresse]]|?#-|limit=100"
	for i, a in pairs(attr) do
		query = query .. "|?" .. i .. a.querypostfix
	end
	local addresses = mw.smw.ask(query)

	if addresses ~= nil then -- Adressen vorhanden?

		-- defaults setzen fehlender Attribute
		for i, _ in pairs(addresses) do
			for j, a in pairs(attr) do
				addresses[i][j] = addresses[i][j] or a.default
			end
		end

		-- Chronologische Sortierung
		if #addresses > 1 then
			-- Sortier-Indizes setzen
			for i, _ in pairs(addresses) do
				addresses[i].von_sort_index, addresses[i].bis_sort_index = p.sortIndex(addresses[i])
			end
			-- Tabelle sortieren
			table.sort(addresses, function(a1, a2) return a1.bis_sort_index < a2.von_sort_index end)
		end

		-- Liste erstellen
		t = t .. "<nowiki></nowiki>\n"												-- nötig, warum auch immer, damit erster Listeeintrag richtig gerendert wird
		for _, e in ipairs(addresses) do
			t = t .. "* '''"														-- Zeilenanfang Liste
			-- Straße/Hausnummer ausgeben
			if wik.pageExists(e["StraßeHnr"]) then									-- Link zu Einzel-Gebäude-Seite
				t = t .. "[[" .. e["StraßeHnr"] .. "]]"
			elseif wik.pageExists(e["VonObjekt"]) then								-- alternativ Link zu Gebäude-Komplex-Seite
				t = t .. "[[" .. e["VonObjekt"] .. "|" .. e["StraßeHnr"] .. "]]"
			elseif wik.pageExists(e["Straße"]) then									-- alternativ Link zu Straße-Seite
				t = t .. "[[" .. e["Straße"] .. "]] " .. tostring(e["Hausnummer"])
			else
				t = t .. "" .. e["StraßeHnr"] .. ""									-- alternativ nur fetter Text
			end
			t = t .. "'''"
			-- Zusatzinfos in Klammern anfügen
			local bracket = {}
			if e["Adressart"] ~= "" then
				table.insert(bracket, e["Adressart"])
			end
			if e["AdresseVon"] ~= "" then
				if e["AdresseBis"] == "" then
					table.insert(bracket, "seit")
				end
				table.insert(bracket, lfd.formatDate(e["AdresseVon"]))
			end
			if e["AdresseBis"] ~= "" and e["AdresseBis"] ~= e["AdresseVon"] then
				if e["AdresseVon"] ~= "" then
					table.insert(bracket, "-")
				else
					table.insert(bracket, "bis")
				end
				table.insert(bracket, lfd.formatDate(e["AdresseBis"]))
			end
			if #bracket > 0 then
				t = t .. " (" .. table.concat(bracket, " ") ..")"
			end
			t = t .. "\n"
		end

	end -- Ende if Adressen vorhanden

	-- Tab draus machen
	t = fab_tabs.tab_(t, "Adressen")
--	mw.log("t = " .. t)
	return t
end

function p.sortIndex(address)
--	mw.log("sortIndex(address)")
--	mw.logObject(address)
	-- Datums-Index holen
	local von_sort_index = p.smwDate2SortIndex(address["AdresseVon"], "von")
	local bis_sort_index = p.smwDate2SortIndex(address["AdresseBis"], "bis")
	-- Ggf. Index von Geburtshaus/-ort setzen
	-- ganz nach vorne => höchste Prio = -99999999
	if string.find(address["Adressart"], "Geburt") == 1 then -- beginnt mit "Geburt"
		von_sort_index = -99999999
	end
	-- Ggf. Index von "Letzter Wohnort" (nicht unbedingt Fürth!) setzen
	-- ganz nach hinten => niedrigste Prio = 99999999
	if address["Adressart"] == "Letzter Wohnort" then -- exakte Übereinstimmung
		von_sort_index = 99999999
		bis_sort_index = 99999999
	-- Ggf. Index von fehlenden von/bis setzen
	-- wenn fehlt, ganz nach hinten (aber vor "Letzter Wohnort") => drittniedrigste Prio = 77777777
	-- "Letzter Wohnort" => zweitniedrigste Prio = 88888888
	elseif von_sort_index == 0 and bis_sort_index == 0 then
		if string.find(address["Adressart"], "Letzter Wohnort") == 1 then -- beginnt mit "Letzter Wohnort"
			von_sort_index = 88888888
			bis_sort_index = 88888888
		else
			von_sort_index = 77777777
			bis_sort_index = 77777777
		end
	-- Übernahme fehlender Index von
	elseif von_sort_index == 0 and bis_sort_index ~= 0 then
		von_sort_index = bis_sort_index
	-- Übernahme fehlender Index bis
	elseif bis_sort_index == 0 and von_sort_index ~= 0 then
		bis_sort_index = von_sort_index
	-- Korrektur von > bis
	elseif von_sort_index > bis_sort_index then
		von_sort_index = bis_sort_index -- den kleineren übernehmen
	end
--	mw.log(von_sort_index .. " - " .. bis_sort_index)
	return von_sort_index, bis_sort_index
end

function p.smwDate2SortIndex(date, vonbis)
--	mw.log("smwDate2SortIndex(" .. date .. ")")
	-- Datum in Index wandeln
	-- Bsp.: 23 Apr. 1989 => 19890423
	-- Konsolenzeile zum Debuggen:
	-- =p.smwDate2SortIndex("2000")
	-- =p.smwDate2SortIndex("Februar 2000")
	-- =p.smwDate2SortIndex("3. Februar 2000")
	local sort_index = ""
	local date=str.split2(date, " ")
	if #date == 1 then																		-- nur Jahr vorhanden
		sort_index = tostring(date[1])
		if vonbis == "von" then
			sort_index = sort_index .. "9999"
		else
			sort_index = sort_index .. "0000"
		end
	elseif #date == 2 then																	-- nur Monat + Jahr vorhanden
		sort_index = tostring(date[2]) .. string.format("%02d", com.getMonthnr(date[1]))
		if vonbis == "von" then
			sort_index = sort_index .. "99"
		else
			sort_index = sort_index .. "00"
		end
	elseif #date == 3 then																	-- komplettes Datum vorhanden
		sort_index = tostring(date[3]) .. string.format("%02d",com.getMonthnr(date[2])) .. string.format("%02d", tonumber(string.match(date[1], "%d*")))
	end
	sort_index = tonumber(sort_index) or 0
--	mw.log("sort_index = " .. tostring(sort_index))
	return sort_index
end

return p