Modul:TC/Funkce

Z Právnický slovník
Jump to navigation Jump to search
p = {}

local tc = require ('Module:TC/Seznam')
local Collation = require( "Module:Collation" )

local function firstToUpper(str)
    return (str:gsub("^%l", mw.ustring.upper))
end

local function Roman (num)
	if num == 1 then
		return "I"
	elseif num == 2 then
		return "II"
	elseif num == 3 then
		return "III"
	elseif num == 4 then
		return "IV"
	elseif num == 5 then
		return "V"
	elseif num == 6 then
		return "VI"
	elseif num == 7 then
		return "VII"
	elseif num == 8 then
		return "VIII"
	elseif num == 9 then
		return "IX"
	elseif num == 10 then
		return "X"
	elseif num == 11 then
		return "XI"
	elseif num == 12 then
		return "XII"
	elseif num == 13 then
		return "XIII"
	else
		return ""
	end
end

local function PocetDilu (hlava)
	if hlava == 1 then
		return 5
	elseif hlava == 2 then
		return 2
	elseif hlava == 3 then
		return 0
	elseif hlava == 4 then
		return 0
	elseif hlava == 5 then
		return 0
	elseif hlava == 6 then
		return 4
	elseif hlava == 7 then
		return 2
	elseif hlava == 8 then
		return 0
	elseif hlava == 9 then
		return 3
	elseif hlava == 10 then
		return 8
	elseif hlava == 11 then
		return 0
	elseif hlava == 12 then
		return 5
	elseif hlava == 13 then
		return 2
	else	
		return false
	end
end

local function AllTC ()
	local tc = {}
	
	for a in pairs (TC) do
		for b in pairs(TC[a]["dily"]) do
			for c in pairs(TC[a]["dily"][b]["acts"]) do
				local name = TC[a]["dily"][b]["acts"][c]["name"] or ""
				local num = TC[a]["dily"][b]["acts"][c]["num"] or ""
				local tab = {name, num}
				table.insert (tc, tab)
			end
		end
	end
	return tc
end

local function RandomTC ()
	local tc = AllTC()
	local n = #tc
	math.randomseed( os.time() )
	local rand = math.random ( 1, n)
	return tc [rand][1]
	
end

local function HlavaDilByTC (argTc)
	local tc = argTc
	for a in pairs (TC) do
		for b in pairs(TC[a]["dily"]) do
			for c in pairs(TC[a]["dily"][b]["acts"]) do
				local name = TC[a]["dily"][b]["acts"][c]["name"] or ""
				local num = TC[a]["dily"][b]["acts"][c]["num"] or ""
				if name == tc then
					return a, b
				end	
			end
		end
	end
end

local function TCsByHlavaDil (argHlava, argDil)
	local hlava = argHlava
	local dil = argDil
	local tc = {}
	for c in pairs ( TC[hlava]["dily"][dil]["acts"] ) do
		table.insert ( tc, TC[hlava]["dily"][dil]["acts"][c]["name"])
	end
	return tc
		
end

local function DilyByHlava (argHlava)
	local hlava = argHlava
	local dily = {}
	for b in pairs (TC[hlava]["dily"]) do
		table.insert(dily, TC[hlava]["dily"][b]["name"])
	end
	return dily
end
	
local function Hlava (hlava)
	local output
	if hlava ~= nil and hlava ~= "" then
		return TC[hlava]["name"]
	end
	return output
end

local function PrintAll (what)
	local output
	
	if what == "hlavy" then
		output = "<ol>"
		for a in pairs (TC) do
			output = output .. "<li>" .. TC[a]["name"] .. "</li>"
		end
	
		output = output .. "</ol>"
		return output
	elseif what == "hlavy-dily" then
		output = "<ol>"
		for a in pairs (TC) do
			output = output .. "<li>" .. TC[a]["name"] .. "</li>"
			for b in pairs(TC[a]["dily"]) do
				if b == 1 then
					output = output .. "<ol>"
				end
				
				output = output .. "<li>" ..TC[a]["dily"][b]["name"] .. "</li>"
				
				if b == #TC[a]["dily"] then
					output = output .. "</ol>"
				end
			end
		end
	
		output = output .. "</ol>"
		return output
	elseif what == "hlavy-dily-par" then
		local unnumbered = true
		local ulol
		local ulolend
		if unnumbered then
			ulol = "<ul>"
			ulolend = "</ul>"
		else
			ulol = "<ol>"
			ulolend = "</ol>"
		end
		
		output = ulol
		
		for a in pairs (TC) do
			local hlava = "hlava " .. Roman(a) .. " - "
			output = output .. "<li>" .. mw.ustring.upper(hlava) .. mw.ustring.upper(TC[a]["name"]) .. "</li>"
			
			for b in pairs(TC[a]["dily"]) do
				local dil = "Díl " .. b .. " - " .. firstToUpper(TC[a]["dily"][b]["name"])
				if b == 1 then
					if TC[a]["dily"][b]["name"] ~= "" then
						output = output .. ulol .. "<li>" .. dil .. "</li>"
					end
					
				else 
					output = output .. "<li>" .. dil .. "</li>"
				end
				
				for c in pairs(TC[a]["dily"][b]["acts"]) do
						if c == 1 then
							output = output .. ulol
						end
						local name = TC[a]["dily"][b]["acts"][c]["name"] or ""
						local num = TC[a]["dily"][b]["acts"][c]["num"] or ""
						output = output .. "<li>[[Trestné činy:" ..  name ..  "|" ..name .. "]] (§ " .. num .. ")</li>"
						if c == #TC[a]["dily"][b]["acts"] then
							output = output .. ulolend
						end
				end
				
				
				if b == #TC[a]["dily"] and TC[a]["dily"][b]["name"] ~= ""then
					output = output .. ulolend
				end
			end
		end
	
		output = output .. ulolend
		return output

	
	end
	
end

local function PrintAl (what)
	local output
	local ciny = {}
	for a in pairs (TC) do
		for b in pairs(TC[a]["dily"]) do
			for c in pairs(TC[a]["dily"][b]["acts"]) do
				table.insert(ciny,{TC[a]["dily"][b]["acts"][c]["name"],TC[a]["dily"][b]["acts"][c]["num"]})
			end
		end
	end
	table.sort (ciny)
	output = "<ol>"
	for x in pairs (ciny) do
		output = output .. "<li>[[Trestné činy:" ..  ciny[x][1] ..  "|" ..ciny[x][1] .. "]] (§ " .. ciny[x][2] .. ")</li>"
	end
	output = output .. "</ol>"
	return output
end

function p.printAllNormal (frame)
	
--	return PrintAll("hlavy-dily-par")
	return PrintAll("hlavy-dily-par")
end

function p.printAllAbc (frame)
	local output = ""
	local tc = AllTC()
	table.sort(tc, function(x, y) return  Collation.sortCompare (x[1], y[1])  end)
	
	output = "<ol>"
	for a in pairs (tc) do
		output = output .. "<li>[[Trestné činy:" .. tc[a][1] .. "|" .. tc[a][1] .. "]]</li>"
	end
	output = output .. "</ol>"
--	return PrintAll("hlavy-dily-par")
	return output
end


function p.RandomTC ()
	local frame = frame or mw.getCurrentFrame()
	local output = ""
	math.randomseed( os.time() )
--	local hlava = math.random ( 1, 13)
--	local dil
	local tc = AllTC()
	local randomtc = RandomTC()
	local cin = "Trestné činy:" .. randomtc
	output = output .. frame:expandTemplate{ title = cin }	
	--, args = { 'arg1', 'arg2', name = 'arg3' }
--	for a in pairs(tc) do
--		output = output .. tc[a][1] .. "<br />"
--	end

--	local hlava, dil = HlavaDilByTC(randomtc)
--  	local dily = DilyByHlava ( hlava )
--  	output = output .. randomtc .. "<br />"
--  	output = output .. "Hlava " .. Roman ( hlava ) ..", Díl " .. dil
--  	output = output .. "<br />"
--  	local sousedni = TCsByHlavaDil ( hlava, dil )
--  	for a in pairs (sousedni) do
--  		output = output .. sousedni [a] .. "<br />"
--  	end

--	output =
	return output
end
return p