Module:T-Campaign: Difference between revisions

// via Wikitext Extension for VSCode
Tag: Reverted
// via Wikitext Extension for VSCode
 
(23 intermediate revisions by the same user not shown)
Line 1: Line 1:
-- T-Campaign.lua
-- Module:T-Campaign.lua
-- Generic campaign template that dynamically loads campaign data from JSON files
-- Generic campaign template that dynamically loads campaign data from JSON files
-- Usage: {{#invoke:T-Campaign|render|campaign_name=ASP2025}}
-- Usage: {{#invoke:T-Campaign|render|campaign_name=NAME}}


local p = {}
local p = {}
Line 172: Line 172:
         local banner = args._campaign_data.banner
         local banner = args._campaign_data.banner
         local bannerContent = banner.content or ""
         local bannerContent = banner.content or ""
        local titleText = template.config.constants.title or "Campaign"
       
         -- Combine generic notice-box class with specific campaign class
         -- Combine generic notice-box class with specific campaign class
         local cssClass = "notice-box"
         local cssClass = "notice-box"
Line 183: Line 185:
         end
         end
          
          
         -- Ensure proper type checking and string conversion for placeholder values
         -- Use the centralized NoticeFactory to create the notice
        local campaignTitle = "Campaign" -- Default fallback
         local noticeOptions = {
       
             type = "campaign-js",
        -- Add detailed debugging to see the campaign data structure
         if args._campaign_data then
            ErrorHandling.addStatus(context, 'campaignBanner', 'Campaign data exists', 'Type: ' .. type(args._campaign_data))
            if args._campaign_data.defaults then
                ErrorHandling.addStatus(context, 'campaignBanner', 'Defaults exists', 'Type: ' .. type(args._campaign_data.defaults))
               
                -- Debug: Show all keys in defaults
                local defaultKeys = {}
                for k, v in pairs(args._campaign_data.defaults) do
                    table.insert(defaultKeys, k .. '=' .. tostring(v))
                end
                ErrorHandling.addStatus(context, 'campaignBanner', 'All defaults', 'Keys: ' .. table.concat(defaultKeys, '; '))
               
                if args._campaign_data.defaults.title then
                    local rawTitle = args._campaign_data.defaults.title
                    ErrorHandling.addStatus(context, 'campaignBanner', 'Title found', 'Raw value: "' .. tostring(rawTitle) .. '", Length: ' .. string.len(tostring(rawTitle)) .. ', Type: ' .. type(rawTitle))
                   
                    -- Ensure we get the title value properly
                    if type(rawTitle) == "string" and rawTitle ~= "" then
                        campaignTitle = rawTitle
                    else
                        ErrorHandling.addStatus(context, 'campaignBanner', 'Title is empty or wrong type', 'Using fallback')
                    end
                else
                    ErrorHandling.addStatus(context, 'campaignBanner', 'Title not found in defaults', 'Available keys: ' .. table.concat(args._campaign_data.defaults and (function() local keys = {} for k,v in pairs(args._campaign_data.defaults) do table.insert(keys, k) end return keys end)() or {}, ', '))
                end
            else
                ErrorHandling.addStatus(context, 'campaignBanner', 'Defaults not found', 'Available keys: ' .. table.concat((function() local keys = {} for k,v in pairs(args._campaign_data) do table.insert(keys, k) end return keys end)(), ', '))
            end
        else
            ErrorHandling.addStatus(context, 'campaignBanner', 'Campaign data missing', 'args._campaign_data is nil')
        end
       
        local placeholderValues = {
            CAMPAIGN_NAME = campaignTitle
        }
       
        -- Add debugging to see what we're actually passing
        ErrorHandling.addStatus(context, 'campaignBanner', 'Placeholder values prepared', 'CAMPAIGN_NAME = "' .. campaignTitle .. '"')
       
        bannerContent = WikitextProcessor.processContentForFrontend(bannerContent, placeholderValues, context)
       
        local noticeData = {
             type = "campaign",
             position = "top",
             position = "top",
             content = bannerContent,
             content = bannerContent,
            title = titleText,
             cssClass = cssClass
             cssClass = cssClass
         }
         }
          
          
         local success, result = pcall(function()
         return WikitextProcessor.createNoticeForJS(noticeOptions) .. ErrorHandling.formatCombinedOutput(context)
            return string.format(
                '<div style="display:none" class="notice-data" data-notice-type="%s" data-notice-position="%s" data-notice-content="%s" data-notice-css="%s"></div>',
                mw.text.encode(noticeData.type),
                mw.text.encode(noticeData.position),
                mw.text.encode(noticeData.content),
                mw.text.encode(noticeData.cssClass)
            )
        end)
       
        if success then
            return result .. ErrorHandling.formatCombinedOutput(context)
        else
            ErrorHandling.addError(context, 'campaignBanner', 'Data attribute creation failed', tostring(result), false)
            return ErrorHandling.formatCombinedOutput(context)
        end
     end
     end
}
}
Line 425: Line 369:
     -- Always show campaign content fields (they'll show placeholder text when empty)
     -- Always show campaign content fields (they'll show placeholder text when empty)
     for _, fieldDef in ipairs(campaignData.field_definitions) do
     for _, fieldDef in ipairs(campaignData.field_definitions) do
         table.insert(fields, {
         -- CRITICAL: Skip 'title' as it is not a content field
            key = fieldDef.key,
        if fieldDef.key ~= "title" then
            label = fieldDef.label,
            table.insert(fields, {
            type = fieldDef.type
                key = fieldDef.key,
        })
                label = fieldDef.label,
                type = fieldDef.type
            })
        end
     end
     end
      
      
Line 449: Line 396:
     end
     end
      
      
     -- Add campaign-specific category
     -- Add campaign-specific category, defaulting to template_id
     template.config.categories.base = {campaignName}
    local category_value = campaignData.category or campaignData.template_id
     template.config.categories.base = {category_value}
      
      
     return args
     return args