Module:Sponsored teams

From TwogPedia
Revision as of 23:09, 5 May 2023 by Couchor (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

local cargo = mw.ext.cargo

local Sponsored = {}

function Sponsored.get(frame)
	local container = mw.html.create('div')
		:addClass('sponsored')
	local currentTitle = mw.title.getCurrentTitle().text

	-- Look up Cargo table teams with current page as sponsor
	local tables = 'Sponsors'
		local fields = '_pageName, start, end, startRef, startRefName, endRef, endRefName'
	local currentDate = os.date('%Y-%m-%d')

	local cargoArgs = {
		where = 'id ="' ..  currentTitle .. '" AND type="team" AND (end is NULL OR end > "' .. currentDate .. '")',
		limit=10,
		orderBy='start'
	}
	local results = cargo.query(tables, fields, cargoArgs)
	local list = mw.html.create('table')
		:addClass('sortable striped-table')

	if #results > 0 then
		local titleRow = mw.html.create('tr')
			:addClass('headerRow')
		local nameCell = mw.html.create('th')
			:wikitext('Team')
		local startCell = mw.html.create('th')
			:wikitext('Sponsored since')
		local endCell = mw.html.create('th')
			:wikitext('Sponsored until')
		list:node(titleRow:node(nameCell):node(startCell):node(endCell))
		for r = 1, #results do
			local result = results[r]
			
	        local teamRow = mw.html.create('tr')
	        	:addClass('bodyRow')
	        local teamName = mw.html.create('td')
	        	:addClass('linkID')
	        
	        tables = 'Logos'
			fields = '_pageName, logoLight, logoDark, logoAll, iconLight, iconDark, iconAll'

			local pageNameSplit = mw.text.split(result._pageName,'/')[2] or ''

			cargoArgs = {
				where = '(_pageName="' ..  result._pageName .. '" OR _pageName ="' .. pageNameSplit.. '" ) AND (start is null or start < "' .. currentDate.. '") AND (end is NULL OR end > "' .. currentDate .. '")'
			}
			local iconResult = cargo.query(tables, fields, cargoArgs)

	        if #iconResult > 0 then
	        	if iconResult[1].iconAll then
	        		teamName:wikitext('<span class="team-icon">[[File:' .. iconResult[1].iconAll .. '|20px]]</span>')
	        	else 
	        		if iconResult[1].iconLight and iconResult[1].iconDark then
	        			teamName:wikitext('<span class="team-icon light">[[File:' .. iconResult[1].iconLight .. '|18px]]</span>')
	        			teamName:wikitext('<span class="team-icon dark-inline-flex">[[File:' .. iconResult[1].iconDark .. '|18px]]</span>')
	        		elseif iconResult[1].iconLight or iconResult[1].iconDark then
	        			local file = iconResult[1].iconLight or iconResult[1].iconDark
	        			teamName:wikitext('<span class="team-icon">[[File:' .. file .. '|20px]]</span>')
	        		end
	        	end
	        end
	        teamName:wikitext('[[' .. result._pageName .. ']]')
	        if result.startRef then
	        	local startRefName = result.startRefName or ''
	        	teamName:wikitext(frame:preprocess('<ref name='.. startRefName .. '>' ..result.startRef .. '</ref>'))
	        end
	        
	        local startDate = mw.html.create('td')
	        	:addClass('tc')
	        	:wikitext(result.start)
	        local endDate = mw.html.create('td')
	        	:addClass('tc')
	        	:wikitext(result['end'])
	        
	        list:node(teamRow:node(teamName):node(startDate):node(endDate))
	    end
	else 
		return nil
	end
	
	return container:node(list)
end

return Sponsored