Module:OrgNavbox

local util = require('Module:Util') local util_args = require('Module:ArgsUtil') local util_cargo = require('Module:CargoUtil') local util_dpl = require('Module:DPLUtil') local util_table = require('Module:TableUtil') local util_title = require('Module:TitleUtil') local util_text = require('Module:TextUtil') local util_vars = require('Module:VarsUtil') local m_team = require('Module:Team') local lang = mw.getLanguage('en') local systems = mw.loadData('Module:Systems')

local p = {}

function p.main(frame) local args = util_args.merge(true) if util_vars.getBool('suppressorgnavbox') 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 = util_args.castAsBool(args.suffix), team = args[1], from_player = args.useplayer, state = args.innerstate } end

function p.getVariables(settings) -- gets team & suffix local title = util_title.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_title.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({		tables = 'InfoboxPlayer',		fields = {'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 query = { tables = 'InfoboxTeam', fields = 'IsDisbanded', where = string.format('_pageName="%s"',teamlink) }	local isDisbanded = util_cargo.getOneResult(query) return isDisbanded == 'No' end

function p.getPlayers(thisteamlink, data) local result = mw.ext.cargo.query('ListplayerCurrent', 'Link,ID', {		where = string.format('_pageName="%s" AND ID IS NOT NULL',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 util_text.split(row.ActiveList,'%s*,%s*'), inactive = (row.InactiveList ~= '') and { links = util_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

-- need to figure out how to deal with different systems & links -- but i think that will be handled with team links / sister team concepts function p.processLinks(frame, data, suffix) local links = p.getLinksListFromData(data) util_title.concatSubpages(links, suffix) local whatExists = util_table.hash(util_dpl.whichPagesExist(links, frame)) p.processLinksInData(data, whatExists, suffix) return end

function p.getLinksListFromData(data) local links = {} if data.aboveFold then links[#links+1] = data.aboveFold links[#links+1] = util_title.concatSubpageSystem(data.aboveFold, data.system) 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, whatExists, suffix) p.linkLists(data[data.aboveFold], whatExists, suffix) data.aboveFold = p.linkToObject(data.aboveFold, whatExists, suffix) if data.belowFold then for key, team in ipairs(data.belowFold.active or {}) do			p.linkLists(data.belowFold[team], whatExists, suffix) data.belowFold.active[key] = p.linkToObject(team, whatExists, suffix) end p.linkLists(data.belowFold.inactive, whatExists, suffix) end return end

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

function p.linkLists(list, hash, 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_title.concatSubpage(v, suffix) list.exists[k] = hash[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_title.concatSubpageSystem(thisteam.link,settings.system),			m_team.teammediumname(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 '',		m_team.rightlonglinked(thisteam, { system = system, size = 45 }),		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_title.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