Module:CompanyLanding: Difference between revisions

From TwogPedia
(Created page with "local cargo = mw.ext.cargo local VariablesLua = mw.ext.VariablesLua local getArgs = require('Module:Arguments').getArgs local p = {} local limit = 25 function p.Main(frame) local args = getArgs(frame) if args.more then return p.html(args.page or 1) end local tableContainer = mw.html.create('div') tableContainer:node(mw.html.create('h2'):wikitext('Companies')) local listContainer = mw.html.create('div'):attr('id', 'list__container') tableContainer:node(listC...")
 
mNo edit summary
Line 61: Line 61:
local letter = VariablesLua.varexists('firstLetter') and VariablesLua.var('firstLetter') or string.upper(string.sub(results[1].page, 1, 1))
local letter = VariablesLua.varexists('firstLetter') and VariablesLua.var('firstLetter') or string.upper(string.sub(results[1].page, 1, 1))
list = list .. tostring(mw.html.create('div'):css('font-weight', '800'):wikitext(letter))
if VariablesLua.varexists('firstLetter') and VariablesLua.var('firstLetter') ~= string.upper(string.sub(results[1].page, 1, 1)) then list = list .. tostring(mw.html.create('div'):css('font-weight', '800'):wikitext(letter)) end
for i = 1, #results do
for i = 1, #results do
local result = results[i]
local result = results[i]

Revision as of 00:45, 26 September 2023

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

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

local p = {}

local limit = 25

function p.Main(frame)
	local args = getArgs(frame)
	
	if args.more then return p.html(args.page or 1) end
	local tableContainer = mw.html.create('div')
	
	tableContainer:node(mw.html.create('h2'):wikitext('Companies')) 
	local listContainer = mw.html.create('div'):attr('id', 'list__container')
	tableContainer:node(listContainer)
	local html = p.html(args.page or 1)

	if html == nil or html == '' then
		return tableContainer:wikitext('No companies in the database') 
	else
	   	listContainer:node(html)
		local loadMore = frame:callParserFunction{ name = '#widget', args = { 'Pagination', id = 'list__container', template = 'CompanyLanding'} }
		tableContainer:node(loadMore)
	end

	return tableContainer
end

function p.query(page)
	local tables = 'Companies'
	local fields = '_pageName, page, country, logo, logoDark, logoLight'
	local cargoArgs = {
		limit = limit,
		offset = (page - 1) * limit,
		orderBy = 'page ASC'
		-- orderBy = 'SUBSTRING(_pageName, LENGTH("Companies/" + 1)'
	}
	local results = cargo.query(tables, fields, cargoArgs)
	return results
end

local function compareResults(result1, result2)
    return result1._pageName < result2._pageName
end

function p.html(page)
	local results = p.query(page)
	local list = ''
	
	local ulDiv = mw.html.create('div')

	if #results > 0 then
		-- Rename pageNames and then sort
		-- for i = 1, #results do
		-- 	results[i]._pageName = string.gsub(results[i]._pageName, 'Companies/', '')
		-- end
		
		table.sort(results, compareResults)
		
		local letter = VariablesLua.varexists('firstLetter') and VariablesLua.var('firstLetter') or string.upper(string.sub(results[1].page, 1, 1))
		if VariablesLua.varexists('firstLetter') and VariablesLua.var('firstLetter') ~= string.upper(string.sub(results[1].page, 1, 1)) then list = list .. tostring(mw.html.create('div'):css('font-weight', '800'):wikitext(letter)) end
		for i = 1, #results do
			local result = results[i]
			
			local firstLetter = string.upper(string.sub(result.page, 1, 1))
			
			-- Create new list for entries with next starting letter
			if firstLetter ~= letter then 
				list = list .. tostring(mw.html.create('div'):css('font-weight', '800'):wikitext(firstLetter))
				letter = firstLetter
			end

			local company = mw.html.create('li'):wikitext('[[' .. result._pageName .. '|' .. result.page  .. ']]')

			list = list .. tostring(company)
		end
		VariablesLua.vardefine('firstLetter', letter)
	else
		return nil
	end
	return list
end

return p