Module:ItemTable

From Palia Wiki
Jump to navigation Jump to search

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

local common = require('Module:Common')
local l10n   = require('Module:Common/Language')
local tbls   = require('Module:Common/Tables')
local cargo  = require('Module:Common/Cargo')
local items  = require('Module:Item')

local p = {}

-- Method returns a method which accepts 2 parameters,
--  the Table Row 'row' and the SQL row 'data'
local function getColFormat(name)
	if name == 'category' then
		return (function(row, data)
		end)
	elseif name == 'description' then
		return (function(row, data)
		end)
	elseif name == 'focus' then
		return (function(row, data)
			getColFormat('focus')(row, data)
			getColFormat('sqfocus')(row, data)
		end)
	elseif name == 'focuses' then
		return (function(row, data)
		end)
	elseif name == 'image' then
		return (function(row, data)
		end)
	elseif name == 'ingredients' then
		return (function(row, data)
		end)
	elseif name == 'item' then
		return (function(row, data)
		end)
	elseif name == 'location' then
		return (function(row, data)
		end)
	elseif name == 'page' then
		return (function(row, data)
		end)
	elseif name == 'rarity' then
		return (function(row, data)
		end)
	elseif name == 'sqfocus' then
		return (function(row, data)
		end)
	elseif name == 'sqvalue' then
		return (function(row, data)
		end)
	elseif name == 'value' then
		return (function(row, data)
		end)
	elseif name == 'values' then
		return (function(row, data)
			getColFormat('value')(row, data)
			getColFormat('sqvalue')(row, data)
		end)
	else
		return nil
	end
end

local function runQuery()
	return {}
end

function p.main(frame)
	frame = frame or mw.getCurrentFrame()
	
	local args = common.getArgs(frame)
	--args[1] = 'image,page,rarity'
	if type(args[1]) ~= 'string' then
		error('Missing columns', 2)
	end
	
	local cols = mw.text.split(args[1], ',', true)
	local methods = {}
	
	local lang = args['lang'] or l10n.getContentLanguage(frame)
	local tbl = tbls.create({
		['class'] = 'paliatable'
	})
	local head = tbl:row()
	
	-- Get the set of formatters for our columns
	for i, spl in ipairs(cols) do
		local name   = mw.text.trim(spl)
		local method = getColFormat(name)
		
		if method == nil then
			error('Unknown column type "' .. name .. '"', 2)
		end
		
		-- Table heading
		head:th(spl)
		
		-- Store the formatter
		methods[i] = method
	end
	
	-- Query the data we want
	local query = runQuery()
	
	-- Iterate the results
	for _, data in ipairs(query) do
		-- Create a new table row for every query row
		local row = tbl:row()
		
		-- Format each column using the formatter
		for i, method in ipairs(methods) do
			method(row, data)
		end
	end
	
	return tbl:done()
end

--[[ Store values into cargo
--]]
function p.store(frame)
	local map  = common.getFrameArgs(frame)
	local args = common.getParentArgs(frame)
	local store = {}
	
	for k, v in pairs(map) do
		store[k] = args[v] or ''
	end
	
	return mw.dumpObject(store)
	--cargo.store()
end

return p