Module:Infobox Shop

From RuneScape Classic Wiki
Jump to: navigation, search

Documentation for this module may be created at Module:Infobox Shop/doc

--------------------------
-- Module for [[Template:Infobox Shop]]
------------------------
local p = {}

local infobox = require('Module:Infobox')
local onmain = require('Module:Mainonly').on_main

function p.main(frame)
	local args = frame:getParent().args
	local ret = infobox.new(args)

	ret:defineParams{
		{ name = 'name', func = 'name' },
		{ name = 'image', func = 'image' },

		{ name = 'release', func = 'release' },
		{ name = 'members', func = 'has_content' },
		{ name = 'type', func = 'has_content' },
		{ name = 'location', func = 'has_content' },
		{ name = 'owner', func = 'has_content' },
		{ name = 'quest', func = 'has_content' },
		{ name = 'skill', func = 'has_content' },
		{ name = 'map', func = maparg },
	}
	
	ret:useSMWSubobject{
		members = 'Store Is members only',
		location = 'Store location',
	}

	ret:setMaxButtons(7)
	ret:create()
	ret:cleanParams()
	ret:customButtonPlacement(true)

	ret:defineLinks({ hide = true })

	ret:addButtonsCaption()

	ret:defineName('Infobox Shop')
	ret:addClass('infobox-shop')

	ret:addRow{
		{ tag = 'argh', content = 'name', class='infobox-header', colspan = '20' }
	}
	:addRow{
		{ tag = 'argd', content = 'image', class = 'infobox-image infobox-full-width-content', colspan = '20' }
	}
	:pad(20)
	:addRow{
		{ tag = 'th', content = 'Released', colspan = '7' },
		{ tag = 'argd', content = 'release', colspan = '13' }
	}
	:addRow{
		{ tag = 'th', content = '[[Members]]', colspan = '7' },
		{ tag = 'argd', content = 'members', colspan = '13' }
	}
	:addRow{
		{ tag = 'th', content = 'Type', colspan = '7' },
		{ tag = 'argd', content = 'type', colspan = '13' }
	}
	:addRow{
		{ tag = 'th', content = 'Location', colspan = '7' },
		{ tag = 'argd', content = 'location', colspan = '13' }
	}
	:addRow{
		{ tag = 'th', content = 'Owner', colspan = '7' },
		{ tag = 'argd', content = 'owner', colspan = '13' }
	}
	if ret:paramDefined('quest') then
		ret:addRow{
			{ tag = 'th', content = 'Quest required', colspan = '7' },
			{ tag = 'argd', content = 'quest', colspan = '13' }
		}
	end
	if ret:paramDefined('skill') then
		ret:addRow{
			{ tag = 'th', content = 'Skill required', colspan = '7' },
			{ tag = 'argd', content = 'skill', colspan = '13' }
		}
	end
	ret:pad(20)

	local map_defined = ret:paramGrep('map', function(x) return (x or 'N/A') ~= 'N/A' end)
	if map_defined then
		ret:addRow{
			{ tag = 'th', content = 'Map', class = 'infobox-subheader', colspan = '20' }
		}
		:addRow{
			{ tag = 'argd', content = 'map', colspan = '20', class = 'infobox-full-width-content infobox-image' }
		}
	end
	
	if onmain() then
		local a1 = ret:param('all')
		local a2 = ret:categoryData()
		ret:wikitext(addcategories(a1, a2))
	end

	return ret:tostring()
end

function maparg(arg)
	if not infobox.isDefined(arg) then
		return nil
	end

	if string.lower(arg) == 'no' then
		return 'N/A'
	end

	return arg
end


function addcategories(args, catargs)
	local ret = { 'Shops' }
	local cat_map = {
		-- Added if the parameter has no content
		notdefined = {
			image = 'Needs image',
			members = 'Needs members status',
			release = 'Needs release date',
		}
	}

	-- undefined categories
	for n, v in pairs(cat_map.notdefined) do
		if catargs[n] and catargs[n].all_defined == false then
			table.insert(ret, v)
		end
	end

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

	return table.concat(ret, '')
end

return p