Zum Inhalt springen

Modul:Wahl Gruppen

Us der alemannische Wikipedia, der freie Dialäkt-Enzyklopedy

Die Dokumentation für dieses Modul kann unter Modul:Wahl Gruppen/Doku erstellt werden

local p = {}
local farbe = require('Module:Partei').farbe
local getArgs = require('Module:Arguments').getArgs

-- frame: SUPPORT VORLAGEN
-- formatnum
local function fn(x)
	if x then return tonumber(x) and mw.getLanguage('de'):formatNum(tonumber(x)) or '–'
	else return '–'
	end
end

function p.ftn(frame)
	local args = getArgs(frame)
	local x = tonumber(args[1])
	return fn(x)
end

-- prozent
local function prozent(x, y)
	local x = tonumber(x)
	local y = tonumber(y)
	if x and y then
		a = string.format('%.1f', x / y * 100)
		b = string.gsub(a, "%.", "%,")
		return b
	else return '–'
	end
end

function p.pzn(frame)
	local args = getArgs(frame)
	local x = tonumber(args[1])
	local y = tonumber(args[2])
	return prozent(x, y)
end

-- differenz
local function df(x, y, k)
	x = tonumber(x)
	y = tonumber(y)
	k = tonumber(k)
	if x and y then
		if k then return fn(x-y-k)
		else return fn(x-y)
		end
	else return '–'
	end
end

function p.dif(frame)
	local args = getArgs(frame)
	local x = tonumber(args[1])
	local y = tonumber(args[2])
	local k = tonumber(args['k']) or 0
	return df(x, y, k)
end

-- differenz %
local function vr(x, y)
	x = tonumber(x) or 0
	y = tonumber(y)
	if x > 0 then
		a = string.format('%.1f', (x - y)/ x * 100)
		b = string.gsub(a, "%.", "%,")
		return b
	else return '–'
	end
end

function p.var(frame)
	local args = getArgs(frame)
	local x = tonumber(args[1])
	local y = tonumber(args[2])
	local k = tonumber(args['k']) or 0
	if x and y then
		if k then return prozent((y-x-k), y)
		else return prozent((y-x), y)
		end
	else return '–'
	end
end

-- sum
local function sum(x, y)
    local x = tonumber(x) or 0
    local y = tonumber(y) or 0
    add = x + y
    if add == 0 then return '–' else return fn(add) end
end

function p.main(frame)
	local args = getArgs(frame)
	local index, data = {}, {}
	local row, row2
	local i = 0
	local num = 1
	local k1 = args['k1']
	local p1 = args['p1']
	local d1 = args['d1']
	local v1 = args['v1']
	if args['allianz'] == '2' or args['system'] == 'ITA2017' or args['kammer'] then nospaltegm = true end
	if args['gesamtd'] then gesamtd = args['gesamtd'] else gesamtd = 0 end
	if args['gesamtv'] then gesamtv = args['gesamtv'] else gesamtv = 0 end
	if args['gesamts'] then gesamts = args['gesamts'] else gesamts = 0 end
	if args['gesamtm'] then gesamtm = args['gesamtm'] else gesamtm = 0 end
	if args['gesamtsz'] then gesamtsz = args['gesamtsz'] else gesamtsz = 0 end
	if args['gesamtmz'] then gesamtmz = args['gesamtmz'] else gesamtmz = 0 end
	local kammer = args['kammer']
	local wahler = args['wahler']
	local wahlerz = args['wahlerz']
	local wahlerd = args['wahlerd']
	local wahlberechtigte = args['wahlberechtigte']
	local wahlberechtigtez = args['wahlberechtigtez']
	local wahlberechtigted = args['wahlberechtigted']
	
	for datum = 1, 50 do i = i + 1
		if args['k'..i] or args['p'..i] or args['as'..i] or args['l'..i] or args['vl'..i] or args['km'..i] or args['lm'..i] then table.insert(index, i) end
		if args['l'..i] then l1 = true end
		if args['p'..i] then p1 = true end
		if args['vl'..i] then verbliste = true end
		if args['n'..i] then farbliste = true end
		if args['sz'..i] then sz1 = true end
		if args['m'..i] then m1 = true end
		if args['mz'..i] then mz1 = true end
	end

	if not args['gesamtd'] and not args['gesamtv'] and not args['gesamts'] and not args['gesamtm'] and not args['gesamtsz'] and not args['gesamtmz'] then
		while (args['l'..num] or args['vl'..num] or args['p'..num] or args['as'..num] or args['lm'..num] or args['km'..num]) do
			data[num] = {
				d = tonumber(args['d'..num]) or 0,
				v = tonumber(args['v'..num]) or 0,
				s = tonumber(args['s'..num]) or 0,
				m = tonumber(args['m'..num]) or 0,
				sz = tonumber((args['sz'..num]) or (verbliste and not args['vl'..num]) and args['s'..num]) or 0,
				mz = tonumber(args['mz'..num]) or 0,
			}
			gesamtd = gesamtd + data[num].d
			gesamtv = gesamtv + data[num].v
			gesamts = gesamts + data[num].s
			gesamtm = gesamtm + data[num].m
			gesamtsz = gesamtsz + data[num].sz
			gesamtmz = gesamtmz + data[num].mz
			num = num + 1
		end
	end

	local function color(a)
		local a = farbe({(a)})
		return "#"..a
	end

	local spalten =
		(d1 and 7 or v1 and 5 or (p1 or k1) and 3 or 2) +
		(sz1 and (mz1 and ((nospaltegm and 6 or 7)) or 4) or (2 + (m1 and 1 or 0) + (mz1 and 1 or 0)))

	local root = mw.html.create('table'):addClass('wikitable sortable')
	row = root:tag('tr')

	local titelgp1 = args['name'] or (((p1 and k1) and 'Parteien') or k1 and 'Kandidaten') or (p1 and 'Wahlbündnisse')
	local titelgp2 = ((k1 and p1) and 'Kandidaten') or 'Listen'

	if d1 or sz1 or mz1 then
		if p1 or k1 then
			row:tag('th')
				:wikitext(titelgp1)
				:attr('colspan', 2)
				:attr('rowspan', 2)
		end
		if d1 then
			row:tag('th')
				:wikitext('2. Wahlgang')
				:attr('colspan', 2)
			row:tag('th')
				:wikitext('1. Wahlgang')
				:attr('colspan', 2)
			elseif v1 then
				row:tag('th')
					:wikitext('Stimmen')
					:attr('rowspan', 2)
				row:tag('th')
					:wikitext('%')
					:attr('rowspan', 2)
		end
		if p1 or k1 then
			row:tag('th')
				:wikitext(titelgp2)
				:attr('rowspan', 2)
		else
			row:tag('th')
				:wikitext('Listen')
				:attr('colspan', 2)
				:attr('rowspan', 2)
		end
		if d1 and not sz1 and not mz1 then
			row:tag('th')
				:wikitext('Stimmen')
				:attr('rowspan', 2)
			row:tag('th')
				:wikitext('%')
				:attr('rowspan', 2)
			if m1 then
				row:tag('th')
					:wikitext('Mandate')
					:attr('rowspan', 2)
			end
		end
	else
		if p1 or k1 then
			row:tag('th')
				:wikitext(titelgp1)
				:attr('colspan', 2)
		end
		if v1 then
			row:tag('th')
				:wikitext('Stimmen')
			row:tag('th')
				:wikitext('%')
		end
		if p1 or k1 then
			row:tag('th')
				:wikitext('Listen')
		else
			row:tag('th')
				:wikitext('Listen')
				:attr('colspan', 2)
		end
		row:tag('th')
			:wikitext('Stimmen')
		row:tag('th')
			:wikitext('%')
		if m1 then
			row:tag('th')
				:wikitext('Mandate')
		end
	end

	if args['system'] == '1' or kammer then erststimme = '1. Wahlgang' zweitstimme = '2. Wahlgang'
	elseif args['system'] == '2' then erststimme = 'Direktstimmen' zweitstimme = 'Listenstimmen'
	elseif args['system'] == '3' or args['system'] == 'ITA2017' then erststimme = 'Proporzwahl' zweitstimme = 'Majorzwahl'
	else erststimme = 'Erststimmen' zweitstimme = 'Zweitstimmen'
	end
	
	if sz1 and mz1 and not kammer then
		row:tag('th')
			:wikitext(erststimme)
			:attr('colspan', 3)
		row:tag('th')
			:wikitext(zweitstimme)
			:attr('colspan', 3)
		if not nospaltegm then
			row:tag('th')
				:wikitext('Mandate<br/>Gesamt')
				:attr('rowspan', 2)
		end
	else
		if sz1 and not mz1 or (sz1 and mz1 and kammer) then
			row:tag('th')
				:wikitext(erststimme)
				:attr('colspan', 2)
			row:tag('th')
				:wikitext(zweitstimme)
				:attr('colspan', 2)
			if sz1 and mz1 and kammer then
				row:tag('th')
					:wikitext('Mandate')
					:attr('colspan', 2)
			end
		else
			if mz1 and not sz1 then
				row:tag('th')
					:wikitext('Stimmen')
					:attr('rowspan', 2)
				row:tag('th')
					:wikitext('%')
					:attr('rowspan', 2)
				row:tag('th')
					:wikitext('Mandate')
					:attr('colspan', 2)
			end
		end
	end

	row2 = root:tag('tr')

	if d1 then
		row2
			:tag('th')
			:wikitext('Stimmen')
		row2
			:tag('th')
			:wikitext('%')
		row2
			:tag('th')
			:wikitext('Stimmen')
		row2
			:tag('th')
			:wikitext('%')
	end
	
	if sz1 then
		row2
			:tag('th')
			:wikitext('Stimmen')
		row2
			:tag('th')
			:wikitext('%')
		if mz1 and not kammer then
			row2:tag('th')
				:wikitext('Mandate')
		end
		row2
			:tag('th')
			:wikitext('Stimmen')
		row2
			:tag('th')
			:wikitext('%')
		if mz1 and not kammer then
			row2:tag('th')
				:wikitext('Mandate')
		end
	end

	if kammer then
		row2:tag('th')
			:wikitext(args['kammer'])
			:css('font-size', '85%')
		row2:tag('th')
			:wikitext(args['kammer2'])
			:css('font-size', '85%')
	end

	for i, v in ipairs(index) do
		row = root:tag('tr')

		if (args['k'..v] and args ['l'..v]) or args['p'..v] or args['a'..v] or (args['l'..v] and verbliste) then
			row:tag('td')
				:attr('rowspan', args['n'..v])
				:css('width', '0px')
				:css('background', color(args['a'..v] or args['p'..v] or args['l'..v]))
		elseif not farbliste then
			row:tag('td')
				:css('width', '0px')
				:css('background', color(args['l'..v]))
		end

		if args['p'..v] then
			if args['l'..v] or not l1 then
				row:tag('td')
					:attr('rowspan', args['n'..v])
					:wikitext(args['p'..v])
			else
				row:tag('td')
					:attr('colspan', 2)
					:attr('rowspan', args['n'..v])
					:wikitext(args['p'..v])
			end
		end

		if args['k'..v] then
			if args['l'..v] then
				row:tag('td')
					:attr('rowspan', args['n'..v])
					:wikitext(args['k'..v])
			else
				row:tag('td')
					:wikitext(args['k'..v])
			end
		end

		if args['k'..v] or args['p'..v] then
			if args['d'..v] then
				row:tag('td')
					:attr('rowspan', args['n'..v])
					:css('text-align', 'right')
					:wikitext(fn(args['d'..v]))
				row:tag('td')
					:attr('rowspan', args['n'..v])
					:css('text-align', 'right')
					:wikitext(prozent(args['d'..v], gesamtd))
			end
			if d1 and not args['d'..v] then
				row:tag('td')
					:attr('colspan', 2)
					:attr('rowspan', args['n'..v])
			end
			if v1 then
				row:tag('td')
					:attr('rowspan', args['n'..v])
					:css('text-align', 'right')
					:wikitext(fn(args['v'..v] or args['s'..v]))
				row:tag('td')
					:attr('rowspan', args['n'..v])
					:css('text-align', 'right')
					:wikitext(prozent(args['v'..v] or args['s'..v], gesamtv))
			end
		end

		if args['as'..v] then liste = '↳ <span style="font-style:italic">Gesamt</span>'
		elseif args['km'..v] then liste = '<span style="font-style:italic;font-size:90%">Nicht gewählte Direktkandidat</span>'
		elseif args['lm'..v] then liste = '<span style="font-style:italic;font-size:90%">Mandate der Listengruppe</span>'
		else liste = args['l'..v] or args['vl'..v]
		end
			
		if args['as'..v] or args['l'..v] or args['vl'..v] or args['km'..v] or args['lm'..v] then
			row:tag('td')
				:wikitext(liste)
		end
		
		row:tag('td')
			:css('text-align', 'right')
			:wikitext(fn(args['s'..v] or args['as'..v]))
		row:tag('td')
			:css('text-align', 'right')
			:wikitext(prozent(args['s'..v] or args['as'..v], gesamts))
		if m1 and not (kammer) then
			row:tag('td')
				:css('text-align', 'right')
				:wikitext(fn(args['m'..v] or args['am'..v] or args['km'..v] or args['lm'..v]))
		end

		if args['system'] == 'ITA2017' and args['l'..v] then args['sz'..v] = args['s'..v] end
	
		if sz1 then
			if nospaltegm then
				if args['p'..v] or args['a'..v] then
					row:tag('td')
						:attr('rowspan', args['n'..v])
						:css('text-align', 'right')
						:wikitext(fn(args['sz'..v] or args['asz'..v]))
					row:tag('td')
						:attr('rowspan', args['n'..v])
						:css('text-align', 'right')
						:wikitext(prozent(args['sz'..v] or args['asz'..v], gesamtsz))
				else
					if not (args['vl'..v]) and not (p1 and (args['l'..v] or args['as'..v])) then
						row:tag('td')
							:css('text-align', 'right')
							:wikitext(fn(args['sz'..v]))
						row:tag('td')
							:css('text-align', 'right')
							:wikitext(prozent(args['sz'..v], gesamtsz))
					end
				end
			else
				row:tag('td')
					:css('text-align', 'right')
					:wikitext(fn(args['sz'..v] or args['asz'..v]))
				row:tag('td')
					:css('text-align', 'right')
					:wikitext(prozent(args['sz'..v] or args['asz'..v], gesamtsz))
			end
		end

		if args['amz'..v] then gesamtmandate = sum(args['am'..v], args['amz'..v]) else gesamtmandate = sum(args['m'..v], args['mz'..v]) end
		
		if kammer then
			if args['p'..v] or args['l'..v] then
				row:tag('td')
					:attr('rowspan', args['n'..v])
					:css('text-align', 'right')
					:wikitext(fn(args['m'..v]))
				if mz1 then
					row:tag('td')
						:attr('rowspan', args['n'..v])
						:css('text-align', 'right')
						:wikitext(fn(args['mz'..v]))
				end
			end
		else
			if mz1 then
				if nospaltegm then
					if args['p'..v] or args['a'..v] then
						row:tag('td')
							:attr('rowspan', args['n'..v])
							:css('text-align', 'right')
							:wikitext(fn(args['mz'..v] or args['amz'..v]))
					else
						if not (args['vl'..v]) and not (p1 and (args['l'..v] or args['as'..v])) then
							row:tag('td')
								:css('text-align', 'right')
								:wikitext(fn(args['mz'..v]))
						end
					end
				else
					row:tag('td')
						:css('text-align', 'right')
						:wikitext(fn(args['mz'..v] or args['amz'..v]))
					row:tag('td')
						:css('text-align', 'right')
						:wikitext(gesamtmandate)
				end
			end
		end
	end

	local spalten0 = ((k1 or p1) and not v1) and 3 or 2;
	row = root:tag('tr')
		:addClass('sortbottom')
	row:tag('th')
		:wikitext('Gesamt')
		:attr('colspan', spalten0)
		:css('text-align', 'left')
	if d1 then
		row:tag('th')
			:wikitext(fn(gesamtd))
			:css('text-align', 'right')
		:tag('th')
			:wikitext(100)
			:css('text-align', 'right')
	end
	if v1 then
		row:tag('th')
			:wikitext(fn(gesamtv))
			:css('text-align', 'right')
		:tag('th')
			:wikitext(100)
			:css('text-align', 'right')
		row:tag('th')
	end
	row:tag('th')
		:wikitext(fn(gesamts))
		:css('text-align', 'right')
	:tag('th')
		:wikitext(100)
		:css('text-align', 'right')
	if m1 and not (sz1 and kammer) then
		row:tag('th')
			:wikitext(fn(gesamtm))
			:css('text-align', 'right')
	end
	if sz1 then
		row:tag('th')
			:wikitext(fn(gesamtsz))
			:css('text-align', 'right')
		:tag('th')
			:wikitext(100)
			:css('text-align', 'right')
	end
	if m1 and sz1 and kammer then
		row:tag('th')
			:wikitext(fn(gesamtm))
			:css('text-align', 'right')
	end
	if mz1 then
		row:tag('th')
			:wikitext(fn(gesamtmz))
			:css('text-align', 'right')
		if not nospaltegm then
			row:tag('th')
				:wikitext(sum(gesamtm, gesamtmz))
				:css('text-align', 'right')
		end
	end

	if wahler then
		row = root:tag('tr')
			:addClass('sortbottom')
		row:tag('td')
			:attr('colspan', spalten)

		row = root:tag('tr')
			:addClass('sortbottom')
		row:tag('td')
			:wikitext('Ungültige Stimmen')
			:attr('colspan', spalten0)
		if wahlerd then
			row:tag('td')
				:wikitext(df(wahlerd, gesamtd))
				:css('text-align', 'right')
			row:tag('td')
				:wikitext(vr(wahlerd, gesamtd))
				:css('text-align', 'right')
		end
		if v1 then
			row:tag('td')
				:wikitext(df(wahler, gesamtv))
				:css('text-align', 'right')
			row:tag('td')
				:wikitext(vr(wahler, gesamtv))
				:css('text-align', 'right')
		else
			row:tag('td')
				:wikitext(df(wahler, gesamts))
				:css('text-align', 'right')
			row:tag('td')
				:wikitext(vr(wahler, gesamts))
				:css('text-align', 'right')
		end
		if wahlerz then
			if mz1 and not (sz1 and kammer) then
				row:tag('td')
			end
			row:tag('td')
				:wikitext(df(wahlerz, gesamtsz))
				:css('text-align', 'right')
			row:tag('td')
				:wikitext(vr(wahlerz, gesamtsz))
				:css('text-align', 'right')
		end

		local spalten1 = spalten - spalten0 - (wahlerd and 2 or 0) - 2 -
			(wahlerz and (((mz1 and not kammer) and 1 or 0) + 2) or 0);
		if (spalten1 > 0) then
			row:tag('td')
				:attr('colspan', spalten1)
				:attr('rowspan', 3)
		end

		row = root:tag('tr')
			:addClass('sortbottom')

		row:tag('th')
			:wikitext('Wähler')
			:attr('colspan', spalten0)
			:css('text-align', 'left')
		if wahlerd then
			row:tag('th')
				:wikitext(fn(wahlerd))
				:css('text-align', 'right')
			row:tag('th')
				:wikitext(prozent(wahlerd, wahlberechtigted))
				:css('text-align', 'right')
		end
		if wahler then
			row:tag('th')
				:wikitext(fn(wahler))
				:css('text-align', 'right')
			row:tag('th')
				:wikitext(prozent(wahler, wahlberechtigte))
				:css('text-align', 'right')
		end
		if wahlerz then
			if mz1 and not (sz1 and kammer) then
				row:tag('th')
			end
			row:tag('th')
				:wikitext(fn(wahlerz))
				:css('text-align', 'right')
			row:tag('th')
				:wikitext(prozent(wahlerz, wahlberechtigtez))
				:css('text-align', 'right')
		end
	end

	if wahlberechtigte then
		row = root:tag('tr')
			:addClass('sortbottom')

		row:tag('td')
			:wikitext('Wahlberechtigte')
			:attr('colspan', spalten0)

		if wahlberechtigted then
			row:tag('td')
				:wikitext(fn(wahlberechtigted))
				:css('text-align', 'right')
			row:tag('td')
		end

		row:tag('td')
			:wikitext(fn(wahlberechtigte))
			:css('text-align', 'right')

		if wahlberechtigtez then
			row:tag('td')
			if mz1 and not (sz1 and kammer) then
				row:tag('td')
			end
			row:tag('td')
				:wikitext(fn(wahlberechtigtez))
				:css('text-align', 'right')
		end

		row:tag('td')
			:attr('colspan', (not wahler and ((v1 or mz1) and 3 or 0) + (m1 and 2 or 1)) or 1)
	end

	if args['url'] then
		row = root:tag('tr')
			:addClass('sortbottom')
		row:tag('td')
			:attr('colspan', spalten)
		row = root:tag('tr')
			:addClass('sortbottom')
		if args['quelle'] then
			row:tag('td')
				:wikitext('Quelle: [', args.url, ' ', args.quelle, ']')
				:attr('colspan', spalten)
				:css('text-align', 'left')
		else
			row:tag('td')
				:wikitext('Quelle: [', args.url, ' Innenministerium]')
				:attr('colspan', spalten)
				:css('text-align', 'left')
		end
	end

	if args['quellen'] then
		row = root:tag('tr')
			:addClass('sortbottom')
		row:tag('td')
			:attr('colspan', spalten)
		row = root:tag('tr')
			:addClass('sortbottom')
		row:tag('td')
			:wikitext('Quellen: ', args.quellen)
			:attr('colspan', spalten)
			:css('text-align', 'left')
	end

	return tostring(root)
end

return p