Module:Databox City

From the Tesseract Wiki, the wiki for all things Marvel Cinematic Universe
Jump to navigation Jump to search

Documentation for this module may be created at Module:Databox City/doc

--------------------------
-- Module for [[Template:Databox City]]
------------------------
local p = {}

local onmain = require('Module:Mainonly').on_main
local yesno = require('Module:Yesno')
local paramtest = require('Module:Paramtest')
local infobox = require('Module:Infobox')

-- Main function called with invokes
function p.main(frame)
	local args = frame:getParent().args
	local ret = infobox.new(args)

	ret:defineParams{
		{ name = 'name', func = 'name' },
		{ name = 'leader', func = 'has_content' },
		{ name = 'race', func = 'has_content' },
		{ name = 'demonym', func = 'has_content' },
	}

	ret:setMaxButtons(4)
	ret:setAddTSWDataboxClass(true)
	ret:create()
	ret:cleanParams()
	ret:customButtonPlacement(true)
	
	ret:defineLinks({ links = {{ 'Template:%s', 'Databox' },
		{ 'Template_talk:%s', 'Talk page' }}, colspan = '2' })

	ret:defineName('Databox City')
	ret:addClass('wikitable')

	ret:addButtonsCaption()

	-- PARAMETER: name
	ret:addRow{
		{ tag = 'argh', content = 'name', class='infobox-header',  colspan = '2' }
	}
	
	-- PARAMETER: leader
	if ret:paramDefined('leader') then
		ret:addRow{
			{ tag = 'th', content = 'Leader' },
			{ tag = 'argd', content = 'leader' }
		}
	end
	
	-- PARAMETER: race
	if ret:paramDefined('race') then
		ret:addRow{
			{ tag = 'th', content = 'Inhabitants' },
			{ tag = 'argd', content = 'race' }
		}
	end
	
	-- PARAMETER: demonym
	if ret:paramDefined('demonym') then
		ret:addRow{
			{ tag = 'th', content = 'Demonym' },
			{ tag = 'argd', content = 'demonym' }
		}
	end

	ret:finish()

	if onmain() then
		local a1 = ret:param('all')
		local a2 = ret:categoryData()
		ret:wikitext(addcategories(a1, a2))
	end
	return ret:tostring()
end

function addcategories(args, catargs)
	local ret = { 'Cities' }

	-- Add the associated category if the parameter doesn't have content
	local notdefined_args = {

	}
	
	for n, v in pairs(notdefined_args) do
		if catargs[n] and catargs[n].all_defined == false then
			table.insert(ret, v)
		end
	end
	
	-- Add the associated category if the parameter does have content
	local defined_args = {
		
	}
	for n, v in pairs(defined_args) do
		if catargs[n] and catargs[n].one_defined then
			table.insert(ret, v)
		end
	end

	-- combine table and format category wikicode
	for i, v in ipairs(ret) do
		if (v ~= '') then
			ret[i] = string.format('[[Category:%s]]', v)
		end
	end

	return table.concat(ret, '')
end

return p