Module:PageIsSponsoring: Difference between revisions

From TwogPedia
(Created page with "local cargo = mw.ext.cargo local Flags = require('Module:Flags') local PageIsSponsoring = {} function PageIsSponsoring.main(frame) local currentTitle = mw.title.getCurrentTitle().text local empty = true -- Check if there are any interviews for this page local tables = 'Sponsors' local fields = '_pageName, id, type, start, end' local currentDate = os.date('%Y-%m-%d') local cargoArgs = { where = 'id ="' .. currentTitle .. '" AND (end is NULL OR end > "' .....")
 
mNo edit summary
Line 23: Line 23:
local container = mw.html.create()
local container = mw.html.create()
:node(mw.html.create('h2'):wikitext('Sponsoring'))
:node(mw.html.create('h2'):wikitext('Sponsoring'))
if #results > 0 then
if #results > 0 then
empty = false
empty = false
         local limit = 1
         local limit = 1
local type = results[1].type
local type = results[1].type
fullString = fullString .. '<tabber>|-|' .. PageIsSponsoring.capitalizeFirstCharacter(type) .. 's='
 
        local hasMultipleTypes = hasMultipleUniqueTypes(results)
       
        if hasMultipleTypes then
  fullString = fullString .. '<tabber>|-|' .. PageIsSponsoring.capitalizeFirstCharacter(type) .. 's='
        end
       
local Table = mw.html.create('table')
local Table = mw.html.create('table')
:addClass('striped-table')
:addClass('striped-table')
Line 46: Line 52:
                 limit = 1
                 limit = 1
country = Flags.CountryName(result.country)
country = Flags.CountryName(result.country)
fullString = fullString .. tostring(mw.clone(Table)) .. '|-|' .. PageIsSponsoring.capitalizeFirstCharacter(result.type) .. 's='
                if hasMultipleTypes then
  fullString = fullString .. tostring(mw.clone(Table)) .. '|-|' .. PageIsSponsoring.capitalizeFirstCharacter(result.type) .. 's='
                else
                  fullString = fullString .. tostring(mw.clone(Table))
                end
Table = mw.html.create('table')
Table = mw.html.create('table')
:addClass('striped-table')
:addClass('striped-table')
Line 70: Line 80:
end
end
fullString = fullString .. '</tabber>'
    if hasMultipleTypes then
mw.log(fullString)
      fullString = fullString .. '</tabber>'
    end
if empty then
if empty then
return '<span class="d-none"></span>'
return '<span class="d-none"></span>'
Line 82: Line 94:
function PageIsSponsoring.capitalizeFirstCharacter(str)
function PageIsSponsoring.capitalizeFirstCharacter(str)
     return str:gsub('^%l', string.upper)
     return str:gsub('^%l', string.upper)
end
function hasMultipleUniqueTypes(results)
    local uniqueTypes = {}
    for _, result in ipairs(results) do
        uniqueTypes[result.type] = true
        -- If there are already more than 1 unique types, return true
        if next(uniqueTypes, next(uniqueTypes)) then
            return true
        end
    end
    -- If we reached here, there's either 1 or 0 unique types
    return false
end
end


return PageIsSponsoring
return PageIsSponsoring

Revision as of 14:57, 8 April 2024

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

local cargo = mw.ext.cargo
local Flags = require('Module:Flags')

local PageIsSponsoring = {}

function PageIsSponsoring.main(frame)
	local currentTitle = mw.title.getCurrentTitle().text
	local empty = true

	-- Check if there are any interviews for this page
	local tables = 'Sponsors'
	local fields = '_pageName, id, type, start, end'
    local currentDate = os.date('%Y-%m-%d')
	local cargoArgs = {
		where = 'id ="' ..  currentTitle .. '" AND (end is NULL OR end > "' .. currentDate .. '")',
		limit=30,
		orderBy = 'type, start'
	}
	local results = cargo.query(tables, fields, cargoArgs)
	
	local fullString = ''

	local container = mw.html.create()
		:node(mw.html.create('h2'):wikitext('Sponsoring'))

	if #results > 0 then
		empty = false
        local limit = 1
		local type = results[1].type

        local hasMultipleTypes = hasMultipleUniqueTypes(results)
        
        if hasMultipleTypes then 
		   fullString = fullString .. '<tabber>|-|' .. PageIsSponsoring.capitalizeFirstCharacter(type) .. 's='
        end
        
		local Table = mw.html.create('table')
			:addClass('striped-table')
	
		local titleRow = mw.html.create('tr')
			:addClass('headerRow')
		local nameCell = mw.html.create('th')
			:wikitext('Name')
		local startCell = mw.html.create('th')
			:wikitext('Start')
		Table:node(titleRow:node(nameCell):node(startCell))
		
		for r = 1, #results do
			local result = results[r]
			
			if results[r-1] and results[r-1].type ~= result.type then 
                limit = 1
				country = Flags.CountryName(result.country)
                if hasMultipleTypes then 
				   fullString = fullString .. tostring(mw.clone(Table)) .. '|-|' .. PageIsSponsoring.capitalizeFirstCharacter(result.type) .. 's='
                else
                   fullString = fullString .. tostring(mw.clone(Table))
                end
				Table = mw.html.create('table')
					:addClass('striped-table')
				titleRow = mw.html.create('tr')
					:addClass('headerRow')
				nameCell = mw.html.create('th')
					:wikitext('Name')
				startCell= mw.html.create('th')
					:wikitext('Start')
				Table:node(titleRow:node(nameCell):node(startCell))
			end
            if limit < 10 then
			local resultRow= mw.html.create('tr'):addClass('bodyRow')
			local name = mw.html.create('td'):wikitext('[[' .. result._pageName .. ']]')
			local start = mw.html.create('td')
				:node(result.start or 'TBD' )
			
			Table:node(resultRow:node(name):node(start))
            end
            limit = limit + 1
		end
		fullString = fullString .. tostring(Table)
	end
	
    if hasMultipleTypes then 
       fullString = fullString .. '</tabber>'
    end
	
	if empty then
		return '<span class="d-none"></span>'
	else
		return container:wikitext(frame:preprocess(fullString))
	end
	
end

function PageIsSponsoring.capitalizeFirstCharacter(str)
    return str:gsub('^%l', string.upper)
end

function hasMultipleUniqueTypes(results)
    local uniqueTypes = {}
    for _, result in ipairs(results) do
        uniqueTypes[result.type] = true
        -- If there are already more than 1 unique types, return true
        if next(uniqueTypes, next(uniqueTypes)) then
            return true
        end
    end
    -- If we reached here, there's either 1 or 0 unique types
    return false
end

return PageIsSponsoring