Module:CompaniesLanding/List

From TwogPedia
Revision as of 18:44, 12 February 2024 by Couchor (talk | contribs) (Created page with "local getArgs = require('Module:Arguments').getArgs local cargo = mw.ext.cargo local LocationWidget = require('Module:Infobox/Widget/Location') local p = {} function p.main(frame) local args = getArgs(frame) local whereStr = '' if args.type then if args.type == 'Other' then whereStr = whereStr .. "Companies.db_type HOLDS NOT 'Organization' AND Companies.db_type HOLDS NOT 'Sponsor' AND Companies.db_type HOLDS NOT 'Locations' AND Companies.db_type HOLDS NOT '...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

local getArgs = require('Module:Arguments').getArgs
local cargo = mw.ext.cargo
local LocationWidget = require('Module:Infobox/Widget/Location')

local p = {}

function p.main(frame)
	local args = getArgs(frame)
	
	local whereStr = ''
	
	if args.type then
		if args.type == 'Other' then
			whereStr = whereStr .. "Companies.db_type HOLDS NOT 'Organization' AND Companies.db_type HOLDS NOT 'Sponsor' AND Companies.db_type HOLDS NOT 'Locations' AND Companies.db_type HOLDS NOT 'Organizer'"
		else
			whereStr = whereStr .. 'Companies.db_type HOLDS "' .. args.type .. '"' 
		end
	end
	if args.country then 
		local countries = mw.text.split(args.country, ',')
		if whereStr ~= '' then whereStr = whereStr .. ' AND ' end
		whereStr = whereStr .. '('
		for i = 1, #countries do
			if i > 1 then whereStr = whereStr .. ' OR ' end
			whereStr = whereStr .. 'Locations.country="' .. countries[i] .. '"'
		end
		whereStr = whereStr .. ')'
	end
	
	if args.name then whereStr = whereStr .. (whereStr ~= '' and ' AND ' or '') .. 'Locations._pageName LIKE "%' .. args.name .. '%"'  end
	
	local tables = 'Companies, _pageData, Logos'
	local fields = 'Companies._pageName=_pageName, Companies.country=country, Companies.db_type=type, Logos.logoAll=logoAll, Logos.logoLight=logoLight, Logos.logoDark=logoDark'
	local cargoArgs = {
		join = 'Companies._pageName = _pageData._pageName, Companies._pageName=Logos._pageName',
		where = whereStr,
		limit = 20,
		orderBy = "_pageData._modificationDate DESC",
	}
	local results = cargo.query(tables, fields, cargoArgs)
	
	local container = mw.html.create('div'):addClass('companies__container')
	
	if #results > 0 then
		for i = 1, #results do
			local result = results[i]
			
			local img = result.logoAll or result.logoLight or result.logoDark
			
			if not img and result.franchise then
				tables = 'Logos'
				fields = '_pageName, logoLight, logoDark, logoAll, iconAll, iconLight, iconDark, start, end'
				currentDate = os.date('%Y-%m-%d')
				cargoArgs = {
					where = '(_pageName = "' .. result.franchise .. '") AND  (start is NULL OR start < "' .. currentDate .. '") AND (end is NULL OR end > "' .. currentDate .. '")'
				}
				local logoResults = cargo.query(tables, fields, cargoArgs)
				
				if #logoResults > 0 then
					local res = logoResults[1]
					img = res.logoAll or res.logoLight or res.logoDark or res.iconAll or res.iconLight or res.iconDark
				end
			end
			
			if not img then img = 'Team_placeholder_light.png' end

			local image = mw.html.create('div'):wikitext('[[File:' .. img .. '|250x250px]]')
			local title = mw.html.create('div'):addClass('company__title'):wikitext(mw.ext.displaytitle.get(result._pageName))
			local country = (result.country and #result.country > 0 ) and LocationWidget.make(result.country, result.city, 'Companies') or nil
			local type = mw.html.create('div'):wikitext(result.type)
			
			local text = mw.html.create('div'):addClass('company__text'):node(country):node(type)
			local newsItem = mw.html.create('div'):addClass('company'):node(title):node(image):node(text):node(mw.html.create('div'):addClass('link-overlay'):wikitext('[[File:Team_placeholder_light.png|1x1px|link=' .. result._pageName .. ']]'))
			container:node(newsItem)
		end
		return container
	end
end

return p