Module:NewsLanding/Sponsors

From TwogPedia

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

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

local p = {}

function p.main(frame)
	local args = getArgs(frame)
 
	local hotItems = mw.html.create('div'):addClass('news-hot-items')
	local container = mw.html.create('div'):addClass('news-hot'):node(mw.html.create('h3'):addClass('tc'):wikitext('Sponsor Pages')):node(hotItems)
	
	local tables = 'Sponsors, _pageData'
	local fields = 'Sponsors.id=id'
	local cargoArgs = {
		join = 'Sponsors._pageName = _pageData._pageName',
		orderBy = "_pageData._modificationDate DESC",
		groupBy = 'Sponsors.id',
		limit = 8
	}
	local results = cargo.query(tables, fields, cargoArgs)

	if #results > 0 then
		for i = 1, #results do
			local result = results[i]
			local title = mw.html.create('div'):wikitext('[[' .. result.id .. '|' .. mw.ext.displaytitle.get(result.id) .. ']]')
			-- local date = mw.html.create('div'):wikitext('Last edit: ' .. page.date)
			-- local textDiv = mw.html.create('div'):node(title):node(date)
			local newsItem = mw.html.create('div'):attr('style', 'display: flex; flex-direction: column; align-items: center; gap: 0.5rem;'):node(title)
			
			local logoDiv = mw.html.create('div')
			
			local title = mw.html.create('div'):wikitext('[[' .. result.id .. '|' .. mw.ext.displaytitle.get(result.id) .. ']]')
			-- local date = mw.html.create('div'):wikitext('Last edit: ' .. page.date)
			-- local textDiv = mw.html.create('div'):node(title):node(date)
			local newsItem = mw.html.create('div'):attr('style', 'display: flex; flex-direction: column; align-items: center; gap: 0.5rem;'):node(title):node(logoDiv)
			
			hotItems:node(newsItem)
			
			local tables = 'Logos'
			local fields = '_pageName, logoAll, logoDark, logoLight, iconAll, iconLight, iconDark'
			local cargoArgs = {
				where = '_pageName = "Companies/' .. result.id .. '"'
			}
			local logoResults = cargo.query(tables, fields, cargoArgs)
	
			if #logoResults > 0 then
				local result = logoResults[1]
				if logoResults[1].iconAll then
					logoDiv:node(createLogo(result._pageName, logoResults[1].iconAll))
				elseif logoResults[1].iconLight and logoResults[1].iconDark then
					logoDiv:node(createLogo(result._pageName, logoResults[1].iconLight, 'light')):node(createLogo(result._pageName, logoResults[1].iconDark, 'dark'))
				elseif logoResults[1].logoAll then
					logoDiv:node(createLogo(result._pageName, logoResults[1].logoAll))
				elseif logoResults[1].logoLight and logoResults[1].logoDark then
					logoDiv:node(createLogo(result._pageName, logoResults[1].logoLight, 'light')):node(createLogo(result._pageName, logoResults[1].logoDark, 'dark'))
				else  
					local logo = logoResults[1].iconLight or logoResults[1].iconDark or logoResults[1].logoLight or logoResults[1].logoDark
					logoDiv:node(createLogo(result._pageName, logo))
				end
			else
		        logoDiv:node(createLogo(result.id, 'Team_placeholder_dark.png', 'dark')):node(createLogo(result.id, 'Team_placeholder_light.png', 'light'))
			end
		end
	end

	return container
end

function createLogo(page, url, class)
	local className = class and class .. ' h-100' or 'h-100'
	local logoURL = url and url or 'Team_placeholder_dark.png'
	local logoContainer = mw.html.create('div')
		:addClass(className)
	local logo = mw.html.create('div')
		:addClass('team-list-logo')
		:node('[[File:' .. logoURL .. '|80x80px|link=' .. page .. ']]')
	logoContainer:node(logo)
	return logoContainer
end

function getFirstImage(pageContent)
    -- List of parameters to check for image names
    local imageParameters = {"image", "logoAll", "logoLight", "logoDark"}
    -- Iterate through the parameters and find the first one with a value
    for _, paramName in ipairs(imageParameters) do
        local imageValue = string.match(pageContent, "|%s*" .. paramName .. "%s*=%s*([^|\n}]+)")

        if imageValue then
            -- Return the extracted image file name
            return imageValue
        end
    end

    return nil
end

return p