Module:CompanyLanding

From TwogPedia
Revision as of 08:23, 16 January 2024 by Couchor (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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, country'
	local cargoArgs = {
		limit = limit,
		offset = (page - 1) * limit,
		orderBy = '_pageName ASC'
		-- orderBy = 'SUBSTRING(_pageName, LENGTH("Companies/" + 1)'
	}
	local results = cargo.query(tables, fields, cargoArgs)
	return results
end

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

	if #results > 0 then
        local pageDisplayName = string.gsub(results[1]._pageName, 'Companies/', '')
		local letter = VariablesLua.varexists('firstLetter') and VariablesLua.var('firstLetter') or string.upper(string.sub(pageDisplayName, 1, 1))
		if VariablesLua.varexists('firstLetter') and VariablesLua.var('firstLetter') ~= string.upper(string.sub(pageDisplayName, 1, 1)) then list = list .. tostring(mw.html.create('div'):css('font-weight', '800'):wikitext(letter)) end
        if page == 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]
			pageDisplayName = mw.ext.displaytitle.get(result._pageName)
			local firstLetter = string.upper(string.sub(pageDisplayName, 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 .. ']]')

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

return p