Module:Figurines

local p = {} local h = {}

local DataTable = require("Module:Data Table") local Franchise = require("Module:Franchise") local utilsArg = require("Module:UtilsArg") local utilsCargo = require("Module:UtilsCargo") local utilsString = require("Module:UtilsString") local utilsTable = require("Module:UtilsTable")

local CARGO_TABLE = "Figurines2" local CATEGORY_INVALID_ARGS = require("Module:Constants/category/invalidArgs") local STORING_PAGE_FORMAT = "Figurines in %" local COLUMN_DESCRIPTION = "Description" local COLUMN_FIGURINE = "Figurine" local COLUMN_NUMBER = "Number" local COLUMN_SUBJECTS = "Subject(s)"

function h.warn(msg, ...) local utilsError = require("Module:UtilsError") msg = string.format(msg, ...) utilsError.warn(msg) end function h.warnEmpty(column, rowIndex) h.warn("The value for column  in row %d should not be empty.", column, rowIndex) return "" end

function p.Main(frame) local args, err = utilsArg.parse(frame:getParent.args, p.Templates["Figurines"]) local categories = err and err.categoryText or "" if err then return "", categories end local page = mw.title.getCurrentTitle.fullText local queryResults = utilsCargo.query(CARGO_TABLE, "figurine, name, number, description", {		where = utilsCargo.allOf( { game = args.game }, string.format("_pageName LIKE '%s'", STORING_PAGE_FORMAT), utilsCargo.holdsAny("subjects", {page}) ),		orderBy = "number, name, _ID" -- sort by ID so that figurines with the same name are listed in the order stored (see Silver Darknut)	}) if #queryResults == 0 then h.warn("No figurines found for subject  in game  .", page, args.game) categories = categories.."" return "", categories end local columnHeaders = {"Figurine", "Description"} if args.game == "TMC" then columnHeaders = {"Figurine", "Number", "Description"} end local dataRows = {} for i, figurine in ipairs(queryResults) do		local row = { cells = {} }		table.insert(row.cells, {			sortValue = figurine.name,			content = figurine.figurine,		}) if args.game == "TMC" then table.insert(row.cells, {				content = figurine.number,			}) end table.insert(row.cells, {			content = mw.text.killMarkers(figurine.description),		}) table.insert(dataRows, row) end local unsortableColumns = utilsTable.keys(columnHeaders) -- make all columns unsortable since we're showing at most 3 rows (most subjects have only 1 figurine) local dataTable = DataTable.printTable(columnHeaders, dataRows, unsortableColumns) local collapsible = frame:expandTemplate({		title = "Hide",		args = {			header = #dataRows == 1 and "Figurine" or "Figurines",			visible = #dataRows == 1,			content = dataTable,			frameless = "true",		}	}) return collapsible, categories end

function p.DataTable(frame) local categories = ""

local dataTable, dataTableCategories = DataTable.Main(frame, function(args, dataRows, columnHeaders)		local requiredColumns = {COLUMN_DESCRIPTION, COLUMN_FIGURINE, COLUMN_SUBJECTS}		local missingColumns = utilsTable.difference(requiredColumns, columnHeaders)		if #missingColumns > 0 then			local utilsMarkup = require("Module:UtilsMarkup")			missingColumns = utilsTable.map(missingColumns, utilsMarkup.code)			h.warn("Columns %s are required.", mw.text.listToText(missingColumns))			categories = categories..""		end		for i, row in ipairs(dataRows) do			local cellsByColumn = utilsTable.keyBy(row.cells, "columnHeader")			local figurineCell = cellsByColumn[COLUMN_FIGURINE] or {}			local subjectsCell = cellsByColumn[COLUMN_SUBJECTS] or {}			local descriptionCell = cellsByColumn[COLUMN_DESCRIPTION] or {}			local numberCell = cellsByColumn[COLUMN_NUMBER] or {}			if figurineCell.raw == nil or figurineCell.raw == "" then categories = categories..h.warnEmpty(COLUMN_FIGURINE, i)			end if subjectsCell.raw == nil or subjectsCell.raw == "" then categories = categories..h.warnEmpty(COLUMN_SUBJECTS, i)			end if descriptionCell.raw == nil or descriptionCell.raw == "" then categories = categories..h.warnEmpty(COLUMN_DESCRIPTION, i)			end if mw.title.getCurrentTitle.nsText ~= "Template" then local figurineName = figurineCell.raw figurineName = figurineName and string.gsub(figurineName, "%[Player Name%]", "Link") figurineName = figurineName and string.gsub(figurineName, "%s%[[%d]%]", "")				frame:expandTemplate({					title = "Figurines/Store",					args = {						game = Franchise.baseGame(args.game) or args.game,						number = numberCell.raw,						name = figurineName,						subjects = subjectsCell.raw,						figurine = figurineCell.content,						description = descriptionCell.content,					}				})			end		end	end)	return dataTable, categories..dataTableCategories end

p.Templates = { ["Figurines"] = { purpose = "Displays the figurines of a particular subject by retrieving data stored at Figurines in The Wind Waker or Figurines in The Minish Cap.", usesData = true, categories = {"Comment Templates"}, params = { [1] = {				name = "game", desc = "A game code ( or  )", required = true, },		},	},	["Data Table/Figurines"] = { purpose = string.format("An extension of Template:Data Table for Figurine listings. Data is stored in the %s Cargo table for retrieval by Template:Figurines.", CARGO_TABLE, CARGO_TABLE), usage = "See Template:Data Table for more information", format = "block", paramOrder = {"name", "game", "columns", "..."}, params = { name = { type = "string", required = true, desc = "See the  parameter of Template:Data Table.", },			game = { type = "string", required = true, desc = "See the  parameter of Template:Data Table.", },			columns = { type = "string", desc = string.format("See the  parameter of Template:Data Table. For Template:Figurines to function correctly, there must be columns named ,  , and  . ", COLUMN_FIGURINE, COLUMN_SUBJECTS, COLUMN_DESCRIPTION), },			["..."] = {				name = "cells", placeholder = "cell", desc = "See the  parameter of Template:Data Table.", },		}	}, }

return p