Module:User:Jmorgan (WMF)/Sandbox

Module documentation
--
-- This module implements {{Participant_profile}}
-- It is based on the legacy template Template:IdeaLab/Introductions/Profile
--
local getArgs = require('Module:Arguments').getArgs
local TableTools = require('Module:TableTools')
local p = {}

-- function chooseCategory(cat_table, cat_table_name, cat_path, args)
-- 	-- adds categories to the child page based on template param values
-- 	local cat
-- 	if (args[cat_table_name] and string.len(args[cat_table_name]) > 0) then
-- 		for k,v in pairs(cat_table) do
-- 			if stringSpacesToUnderscores(stringToLowerCase(args[cat_table_name])) == k then
-- 				cat = cat_path .. v
-- 				break
-- 			end
-- 		end
-- 	end
-- 	return cat
-- end

-- function getCategories(data, args)
--  	-- looks for args that have associated categories,
--  	-- returns a string with a list of wikilinked categories
--  	-- also appends any default categories you specify
-- 	local cat_list = {}
-- 	if data.categories.default then
-- 		table.insert(cat_list, data.categories.default)
-- 	end
-- 	for k,v in pairs(data.fields) do
-- 		if data.fields[k].hasCategories == true then
-- 			for l,w in pairs(data.categories) do
-- 				if type(data.categories[l]) == "table" then
-- 					--fixme! hardcoding cat_path because it's throwing a weird error.
-- 					table.insert(cat_list, chooseCategory(data.categories[l], l, "Category:IdeaLab members interested in ", args))
-- 					-- table.insert(cat_list, chooseCategory(data.categories[l], l, data.fields[k].cat_path, args))
-- 				elseif (args[l] and stringToLowerCase(args[l]) == "1") then
-- 					table.insert(cat_list, w)
-- 				end	
-- 			end
-- 		end
-- 	end
-- 	local page_categories = " [[" .. tostring(table.concat(cat_list, "]] [[")) .. "]]"
-- 	return page_categories
-- end

function nodeBuilder(arg_data, arg_value)
	local field_div = mw.html.create('div')
	if arg_data.ftype == "image" then	
		field_div
			:cssText(arg_data.style1)
			:wikitext("[[" .. arg_value .. "|" .. arg_data.style2 .. "]]")
			:done()		
	elseif arg_data.ftype == "wikilink" then
		field_div
			:cssText(arg_data.style1)
			:wikitext("[[User:" .. arg_value .. "|" .. arg_value .. "]]")
			:done()
	elseif arg_data.ftype == "userlink" then
		field_div
			:cssText(arg_data.style1)
			:wikitext("[[User:" .. arg_value .. "|")
			:tag('span')
				:cssText("color: #F9A01B; border-bottom: 1px dotted #CCC")
				:wikitext(arg_data.display)
				:done()
				:wikitext("]]")
			:done()			
	elseif arg_data.ftype == "text" then
		field_div
			:cssText(arg_data.style1)
			:wikitext(arg_data.prefix .. arg_value)
			:done()	
	elseif arg_data.ftype == "choice" then
		field_div
			:cssText(arg_data.style1)
			:wikitext("• " .. arg_data.prefix)
			:done()	
	else
		-- do nothing
	end
	field_div:allDone()
	return field_div
end			

function insertSectionFields(ranked_fields, sec_div)
	ranked_fields = TableTools.compressSparseArray(ranked_fields)
	for k, v in ipairs(ranked_fields) do
		sec_div:node(v)
	end	
	return sec_div
end

function makeProfileSection(args, data, sec)
	local sec_div = mw.html.create('div')
	-- define section styles
	sec_div:cssText(data.styles.box[sec])	
	-- find fields that belong to that section and add them in order
	local ranked_fields = {}
	for k, v in pairs(args) do
		if (data.fields[k] and data.fields[k].section == sec) then
			if (data.fields[k].isRequired or string.len(v) > 0) then
				local field_div = nodeBuilder(data.fields[k], v)
				ranked_fields[data.fields[k].rank] = field_div	
			end
		end
	end
	sec_div = insertSectionFields(ranked_fields, sec_div)
	sec_div:allDone()			
	return sec_div
end

function makeProfile(data, args)
    -- builds the template in html
	local profile_div = mw.html.create('div')
	profile_div
		:cssText(data.styles.box.outer)
		:addClass("plainlinks")
	local sec_head = makeProfileSection(args, data, "head")
	profile_div:node(sec_head)
	local sec_main = makeProfileSection(args, data, "main")
	profile_div:node(sec_main)
	local sec_foot = makeProfileSection(args, data, "foot")
	profile_div:node(sec_foot)
	profile_div:allDone()
	return profile_div	
end

function subNicknameForDisplay(data, args)
	-- if the user has entered a value for name=,
	-- display that instead of a username when creating
	-- the userlink in the profile
	if (args.name and string.len(args.name) > 0) then 
		data.fields.username.display = args.name
	else
		data.fields.username.display = args.username --username will be stored twice, now		
	end
	return data
end		

function deepCopyTable(data)
	-- the deep copy is a workaround step to avoid the restrictions placed on
	-- tables imported through loadData
	if type(data) ~= 'table' then
	    return data
	end
	local data_copy = {}
	for k,v in pairs(data) do
		if type(v) == 'table' then
		    v = deepCopyTable(v)
		end
		data_copy[k] = v
	end
	return data_copy
end

function getProfileData(args)
	-- loads the relevant stylesheet, if a sub-template was called with a portal
	-- argument and a stylesheet exists with the same name. For example, calling
	-- {{#invoke:Participant_profile|main|type=IdeaLab}} would load the
	-- Module:Participant_profile/IdeaLab stylesheet
	-- member stylesheet is the default if no param set
	local data_readOnly = {}
	local data_writable = {}
	-- if (args.profile_type and mw.title.makeTitle( 'Module', 'Participant_profile/' .. args.profile_type).exists) then
	-- 	data_readOnly = mw.loadData("Module:Participant_profile/" .. args.profile_type)
	-- else
	-- 	data_readOnly = mw.loadData("Module:Participant_profile/IdeaLab")
	-- end
	data_readOnly = mw.loadData("Module:User:Jmorgan_(WMF)/Sandbox/1")
	data_writable = deepCopyTable(data_readOnly)
	return data_writable
end

-- helper functions --

function stringToLowerCase(value)
    -- returns a string in all lowercase chars
	return mw.ustring.lower(value)
end

function stringSpacesToUnderscores(value)
    -- converts spaces to underscores in a string
	return mw.ustring.gsub(value, " ", "_")
end

function stringFirstCharToUpper(str)
    -- converts the first char of a string to uppercase
    return (str:gsub("^%l", string.upper))
end

function addMissingArgs(data, args)
    --if required args are not included in the calling template, adds
    -- them with default values
    for k,v in pairs(data.fields) do
        if data.fields[k].isRequired then
            if not args[k] then
                args[k] = data.fields[k].default
            end
        end
    end            
    return args
end

function setDefaultValues(data, args)
	for k,v in pairs(args) do
	    if (string.len(args[k]) == 0 and data.fields[k] and data.fields[k].isRequired == true) then
	        args[k] = data.fields[k].default
        end
    end
    return args
end

-- main --

function p.main(frame)
	local args = getArgs(frame, {removeBlanks = false})
	local data = getProfileData(args)
	args = setDefaultValues(data, args)
	args = addMissingArgs(data, args) --make this a sub-call of setDefaultValues
	data = subNicknameForDisplay(data, args)
	local profile = tostring(makeProfile(data, args)) --added data param
	 --if mw.title.getCurrentTitle().nsText == "Grants" then
		--  profile = profile .. getCategories(data, args)
	 --end    
	return profile
end

return p