Module:OrgNavbox

local util = require('Module:Util') local util_cargo = require('Module:CargoUtil') local util_dpl = require('Module:DPLUtil') local util_table = require('Module:TableUtil') local m_text = require('Module:Text') local Team = require('Module:Team').team local lang = mw.getLanguage('en') local systems = mw.loadData('Module:Systems')

local p = {}

function p.main(frame) if frame == mw.getCurrentFrame then args = require('Module:ProcessArgs').merge(true) else frame = mw.getCurrentFrame end if frame:callParserFunction('#var','suppressorgnavbox') == 'true' then return end -- get inputs local settings = p.getSettings(args) local vars = p.getVariables(settings) return p.makeNavbox(vars, frame, settings) end

function p.getSettings(args) return { suffix = (args.suffix == 'Yes'), team = args[1], from_player = args.useplayer, state = args.innerstate } end

function p.getVariables(settings) -- gets team & suffix local title = util.titleTable local tbl = {} local titleteam = table.remove(title,1) tbl.team = settings.team or titleteam tbl.system = title[1] and systems[title[1]] and systems[table.remove(title,1)] if settings.from_player then p.teamFromPlayer(tbl) end tbl.teamlink = util.concatSubpage(tbl.team, tbl.system) tbl.suffix = settings.suffix and next(title) and table.concat(title,'/') tbl.state = settings.state or 'mw-collapsed' return tbl end

function p.teamFromPlayer(tbl) local result = util_cargo.getOneRow(		'InfoboxPlayer',		{'Team','TeamSystem'},		{ where = string.format('_pageName="%s"',tbl.team) }	) tbl.team = result.Team tbl.system = result.TeamSystem or '' return end

-- below this can be called directly from a module function p.makeNavbox(vars, frame) if not vars.team then return '' end if not frame then frame = mw.getCurrentFrame end -- get data, local data = p.getData(vars) if not data then return '' end p.processLinks(frame, data, vars.suffix) -- print data return p.printNavbox(frame, data, vars) end

function p.getData(vars) local data = {} local localsettings = {} local thisteam = vars.team local thisteamlink = vars.teamlink if thisteamlink and mw.title.makeTitle('',thisteamlink).exists then if p.isActive(thisteamlink) then localsettings.active = true data.aboveFold = thisteam data[thisteam] = { links = {}, names = {} } p.getPlayers(thisteamlink, data[thisteam]) end else localsettings.exists = false end localsettings.hasSister = p.getSisterTeamInfo(vars, data) return (localsettings.active or localsettings.hasSister) and data end

function p.isActive(teamlink) local isDisbanded = util_cargo.getOneResult(		'InfoboxTeam',		'IsDisbanded',		{ where = string.format('_pageName="%s"',teamlink) }		) return isDisbanded == 'No' end

function p.getPlayers(thisteamlink, data) local result = mw.ext.cargo.query('ListplayerCurrent', 'Link,ID', {		where = string.format('_pageName="%s"',thisteamlink),		groupBy = 'Link',		orderBy = 'N'	}) for key, row in ipairs(result) do		data.links[key] = row.Link data.names[key] = row.ID	end end

function p.getSisterTeamInfo(vars, data) local row = p.sisterTeamsCargo(vars.team) if not row then return nil end data.belowFold = { active = (row.ActiveList ~= '') and m_text.split(row.ActiveList,'%s*,%s*'), inactive = (row.InactiveList ~= '') and { links = m_text.split(row.InactiveList,'%s*,%s*') } }	for _, team in ipairs(data.belowFold.active or {}) do		data.belowFold[team] = { links = {}, names = {} } p.getPlayers(team, data.belowFold[team]) end if data.belowFold.inactive then data.belowFold.inactive.names = mw.clone(data.belowFold.inactive.links) end return true end

function p.sisterTeamsCargo(team) local fieldstable = { "Status=Status", "CONCAT(_pageName)=Pagename", "ActiveList=ActiveList", "InactiveList=InactiveList" }	local cargoresult = mw.ext.cargo.query(		"CCSisterTeams",		table.concat(fieldstable, ","),		{ where = 'Team="' .. team .. '"' }	) return cargoresult[1] end

-- process links

-- currently this is not looking for existence for teams with systems & subpages properly -- (at all...) -- so should do that eventually function p.processLinks(frame, data, suffix) local links = p.getLinksListFromData(data) util.concatSubpages(links, suffix) local hash = util_table.hash(util_dpl.whichPagesExist(links, frame)) p.processLinksInData(data, hash, suffix) return end

function p.getLinksListFromData(data) local links = {} if data.aboveFold then links[#links+1] = data.aboveFold util_table.mergeArrays(links,data[data.aboveFold].links) end if data.belowFold then if data.belowFold.inactive then util_table.mergeArrays(links,data.belowFold.inactive.links) end for k, v in ipairs(data.belowFold.active or {}) do			links[#links+1] = v			util_table.mergeArrays(links,data.belowFold[v].links) end end return links end

function p.processLinksInData(data, hash, suffix) p.linkLists(data[data.aboveFold], hash, suffix) data.aboveFold = p.linkToObject(data.aboveFold, hash, suffix) if data.belowFold then for key, team in ipairs(data.belowFold.active or {}) do			p.linkLists(data.belowFold[team], hash, suffix) data.belowFold.active[key] = p.linkToObject(team, hash, suffix) end p.linkLists(data.belowFold.inactive, hash, suffix) end return end

function p.linkToObject(link, hash, suffix) if not link then return nil end local newlink = util.concatSubpage(link, suffix) local exists = hash[lang:ucfirst(newlink)] return { name = link, exists = exists, link = exists and newlink or link } end

function p.linkLists(list, lookup, suffix) if not list then return end if not list.exists then list.exists = {} end for k, v in ipairs(list.links) do		newlink = util.concatSubpage(v, suffix) list.exists[k] = lookup[lang:ucfirst(newlink)] list.links[k] = list.exists[k] and newlink or v	end return end

-- below is printing only function p.printNavbox(frame, data, settings) local thisteam = data.aboveFold local navboxargs = { name = 'OrgNavbox', state = 'mw-collapsible' }	local i = 1 if thisteam then navboxargs.group1 = p.makeOutput(			util.concatSubpageSystem(thisteam.link,settings.system),			thisteam.name,			(not settings.suffix) or thisteam.exists		) navboxargs.list1 = p.makeListOutput(data[thisteam.name], settings.suffix) i = i + 1 navboxargs.title = p.makeNavboxTitle(thisteam.name, settings.suffix, settings.system, 'Roster') else navboxargs.title = p.makeNavboxTitle(settings.team, settings.suffix, settings.system, 'Organization') end if data.belowFold then navboxargs['list' .. i] = p.afterFold(frame, data.belowFold, settings) end return frame:expandTemplate{ title = 'Navbox', args = navboxargs } end

function p.makeNavboxTitle(thisteam, suffix, system, defaulttext) return string.format(		' %s %s',		suffix and 'no-subpage' or '',		Team{thisteam, system = system },		suffix and ('- ' .. suffix) or defaulttext	) end

function p.afterFold(frame, data, settings) local navboxargs = { 'child', title = 'Other Teams In Organization (Click [Show] to the Right)', state = settings.state }	for k, v in ipairs(data.active or {}) do navboxargs['group' .. k] = p.makeOutput(			util.concatSubpageSystem(v.link,settings.system),			v.name,			(not settings.suffix) or v.exists		) navboxargs['list' .. k] = p.makeListOutput(data[v.name], settings.suffix) end if data.inactive then navboxargs.below = p.makeListOutput(data.inactive, settings.suffix) end return frame:expandTemplate{ title = 'Navbox', args = navboxargs } end

function p.makeListOutput(list, suffix) local tbl = {} for k, link in ipairs(list.links or {}) do		tbl[k] = p.makeOutput(link, list.names[k], (not suffix) or list.exists[k]) end local output = table.concat(tbl,' &#8226; ') return output end

function p.makeOutput(link, name, exists) if exists then return string.format('%s',link,name) else return string.format(			' %s ',			link,			name		) end end

return p