Module:LocationsSearch/Countries

From TwogPedia
< Module:LocationsSearch
Revision as of 18:38, 12 February 2024 by Couchor (talk | contribs) (Created page with "local getArgs = require('Module:Arguments').getArgs local cargo = mw.ext.cargo local CountryData = mw.loadData('Module:Localisation/Data') local p = {} function p.main(frame) local args = getArgs(frame) local tables = 'Locations' local fields = 'country' local cargoArgs = { groupBy = 'Locations.country' } local results = cargo.query(tables, fields, cargoArgs) local container = mw.html.create('div'):addClass('dropdown__content'):attr('id', 'country--dropdow...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

local getArgs = require('Module:Arguments').getArgs
local cargo = mw.ext.cargo
local CountryData = mw.loadData('Module:Localisation/Data')

local p = {}

function p.main(frame)
	local args = getArgs(frame)
	
	local tables = 'Locations'
	local fields = 'country'
	local cargoArgs = {
		groupBy = 'Locations.country'
	}
	local results = cargo.query(tables, fields, cargoArgs)
	
	local container = mw.html.create('div'):addClass('dropdown__content'):attr('id', 'country--dropdown')
	
	if #results > 0 then
		local countries = {}
		for _, result in ipairs(results) do
		    table.insert(countries, result.country)
		end
		
		local uniqueCountries = {}
		for _, country in ipairs(countries) do
		    if not uniqueCountries[country] then
		        uniqueCountries[country] = true
		    end
		end
		
		-- Convert unique country values to a list
		local uniqueCountriesList = {}
		for country, _ in pairs(uniqueCountries) do
		    table.insert(uniqueCountriesList, country)
		end
		
		if #uniqueCountriesList > 0 then 
			for i = 1, #uniqueCountriesList do
				local country = CountryData[string.upper(uniqueCountriesList[i])]
				container:node(mw.html.create('div'):addClass('dropdown__item'):attr('data-country', uniqueCountriesList[i]):wikitext(country))
			end
			return container
		end
	end
end

return p