Module:WeeklyDataPages

From Call of Duty Esports Wiki
Jump to: navigation, search

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

local util_args = require('Module:ArgsUtil')
local util_cargo = require('Module:CargoUtil')
local util_esports = require('Module:EsportsUtil')
local util_frame = require('Module:FrameUtil')
local util_html = require('Module:HtmlUtil')
local util_math = require('Module:MathUtil')
local util_table = require("Module:TableUtil")
local util_text = require('Module:TextUtil')
local util_time = require("Module:TimeUtil")
local util_title = require('Module:TitleUtil')
local util_vars = require('Module:VarsUtil')
local i18n = require('Module:I18nUtil')

local m_team = require('Module:Team')
local lang = mw.getLanguage('en')
local DAYS_PER_WEEK = 7
local PAGE_TYPE

local h = {}

local p = {}

function p.start(template)
	PAGE_TYPE = util_title.titleparts(nil, 1, 1)
	local navbox1 = util_frame.navbox('ThisWeekData')
	local navbox2 = util_frame.navbox(template, {year = h.getYearFromTitle()})
	local navbox3 = h.getWithinWeekNavboxIfNeeded(mw.title.getCurrentTitle().text)
	return navbox1, navbox2, navbox3
end

function h.getYearFromTitle()
	local date = util_title.titleparts(nil, 1, -1)
	return date:match('%d%d%d%d')
end

function h.getWithinWeekNavboxIfNeeded(title)
	local date = title:match('%d%d%d%d%-%d%d%-%d%d')
	if not date then return '' end
	if lang:formatDate('w', date) ~= '0' then
		-- if it's not a Sunday then we already know we need the further navigation
		return h.getWithinWeekNavbox(date)
	end
	local tomorrow = lang:formatDate('Y-m-d', ('%s + 1 day'):format(date))
	if util_title.target(h.dateToPage(tomorrow)) == title then
		return ''
	end
	return h.getWithinWeekNavbox(date)
end

function h.getWithinWeekNavbox(date)
	return util_frame.navbox('Data Week', {list=h.getWithinWeekList(date)})
end

function h.getWithinWeekList(date)
	local days = util_time.getDatesInAWeek(date)
	return util_table.concat(days, '\n', h.dateToDisplay)
end

function h.dateToDisplay(date)
	return '*' .. util_text.intLink(h.dateToPage(date), date)
end

function h.dateToPage(date)
	return ('Data:%s/%s'):format(PAGE_TYPE, date)
end

function p.date(args, cols, class)
	local args = util_args.merge()
	util_vars.resetGlobalIndex('N_ItemInDate')
	util_vars.setVar('Date', h.dateFromArgs(args))
	util_vars.setVar('Year', args.y)
	local tr = mw.html.create()
	util_html.printColspanHeader(tr, h.dateFromArgs(args), #cols)
	util_html.printHeaderFromI18n(tr, cols)
	return ('<table class="wikitable hoverable-rows %s">'):format(class or ''), tr
end

function h.dateFromArgs(args)
	return ('%s-%s-%s'):format(args.y, util_math.padleft(args.m, 2), util_math.padleft(args.d, 2))
end

return p