Module:LocationsLanding/List

From TwogPedia
< Module:LocationsLanding
Revision as of 08:39, 11 March 2024 by Couchor (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Documentation for this module may be created at Module:LocationsLanding/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 whereStr = whereStr .. 'Locations.type HOLDS "' .. args.type .. '"' 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 = 'Locations, _pageData, Logos'
	local fields = 'Locations._pageName=_pageName, Locations.franchise=franchise, Locations.country=country, Locations.city=city, Locations.type=type, Logos.logoAll=logoAll, Logos.logoLight=logoLight, Logos.logoDark=logoDark'
	local cargoArgs = {
		join = 'Locations._pageName = _pageData._pageName, Locations._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('locations__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|link=]]')
			local title = mw.html.create('div'):addClass('location__title'):wikitext(mw.ext.displaytitle.get(result._pageName))
			local country = LocationWidget.make(result.country, result.city, 'Locations')
			local type = mw.html.create('div'):wikitext(result.type)
			
			local text = mw.html.create('div'):addClass('location__text'):node(country):node(type)
			local newsItem = mw.html.create('div'):addClass('location__item'):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