Module:AddEvent
Jump to navigation
Jump to search
Documentation for this module may be created at Module:AddEvent/doc
local p = {}
local function renderForm()
return [=[
<form action="" method="post">
<label for="date">Date (YYYY-MM-DD):</label>
<input type="text" id="date" name="date" required><br>
<label for="title">Event Title:</label>
<input type="text" id="title" name="title" required><br>
<label for="description">Event Description:</label>
<textarea id="description" name="description"></textarea><br>
<input type="submit" value="Submit">
</form>
]=]
end
function p.processForm(frame)
local args = frame:getParent().args
local date = args.date
local title = args.title
local description = args.description or ""
if not date or not title then
return "Date and title are mandatory fields."
end
local newEvent = string.format(
"{{EventRow\n| date = %s\n| title = %s\n| description = %s\n}}",
date, title, description
)
local page = mw.title.new("GlobalEvents")
local success, err = page:edit(function(content)
local monthYear = mw.ustring.format("%s %d", mw.language.getContentLanguage():formatDate("F", date), mw.language.getContentLanguage():formatDate("Y", date))
local sectionPattern = string.format("{{EventSection.-| title = %s.-| events =.-}}", monthYear)
local updatedContent
if content:find(sectionPattern) then
updatedContent = content:gsub(sectionPattern, function(section)
return section:gsub("(| events =.-)\n}}", "%1\n" .. newEvent .. "\n}}")
end)
else
local newSection = string.format(
"{{EventSection\n | title = %s\n | dateHeader = Date\n | titleHeader = Event Name\n | descriptionHeader = Description\n | newsHeader = Description\n | tableStyle = width: 100%%;\n | tableClass = wikitable\n | events =\n %s\n}}",
monthYear, newEvent
)
updatedContent = content .. "\n" .. newSection
end
return updatedContent
end, "Adding a new event via form")
if not success then
return "Error: " .. err
end
return "Event added successfully!"
end
function p.main(frame)
local args = frame:getParent().args
if mw.text.trim(args.date or "") ~= "" and mw.text.trim(args.title or "") ~= "" then
return p.processForm(frame)
else
return renderForm()
end
end
return p