webmcp
view framework/env/encode/url.lua @ 2:72860d232f32
Version 1.0.2
Fixed bug with explicit garbage collection (requests > 256kB caused an error)
Views prefixed with an underscore can't be called externally
ui.paginate now displays the last page, if the selected page number is too high.
Fixed bug with explicit garbage collection (requests > 256kB caused an error)
Views prefixed with an underscore can't be called externally
ui.paginate now displays the last page, if the selected page number is too high.
| author | jbe/bsw | 
|---|---|
| date | Thu Dec 10 12:00:00 2009 +0100 (2009-12-10) | 
| parents | 9fdfb27f8e67 | 
| children | d76a8857ba62 | 
 line source
     1 --[[--
     2 url_string =              -- a string containing an URL
     3 encode.url{
     4   external  = external,   -- external URL (instead of specifying base, module, etc. below)
     5   base      = base,       -- optional string containing a base URL of a WebMCP application
     6   static    = static,     -- an URL relative to the static file directory
     7   module    = module,     -- a module name of the WebMCP application
     8   view      = view,       -- a view name of the WebMCP application
     9   action    = action,     -- an action name of the WebMCP application
    10   id        = id,         -- optional id to be passed to the view or action to select a particular data record
    11   params    = params      -- optional parameters to be passed to the view or action
    12 }
    14 This function creates URLs to external locations, to static files within the WebMCP application or to a certain view or action inside a module.
    16 --]]--
    18 function encode.url(args)
    19   local external  = args.external
    20   local base      = args.base or request.get_relative_baseurl()
    21   local static    = args.static
    22   local module    = args.module
    23   local view      = args.view
    24   local action    = args.action
    25   local id        = args.id
    26   local params    = args.params or {}
    27   local result    = {}
    28   local id_as_param = false
    29   local function add(...)
    30     for i = 1, math.huge do
    31       local v = select(i, ...)
    32       if v == nil then break end
    33       result[#result+1] = v
    34     end
    35   end
    36   if external then
    37     add(external)
    38   else
    39     add(base)
    40     if not string.find(base, "/$") then
    41       add("/")
    42     end
    43     if static then
    44       add("static/")
    45       add(static)
    46     elseif module or view or action or id then
    47       assert(module, "Module not specified.")
    48       add(encode.url_part(module), "/")
    49       if view and not action then
    50         local view_base, view_suffix = string.match(
    51           view,
    52           "^([^.]*)(.*)$"
    53         )
    54         add(encode.url_part(view_base))
    55         if args.id then
    56           add("/", encode.url_part(id))
    57         end
    58         if view_suffix == "" then
    59           add(".html")
    60         else
    61           add(view_suffix)  -- view_suffix includes dot as first character
    62         end
    63       elseif action and not view then
    64         add(encode.url_part(action))
    65         id_as_param = true
    66       elseif view and action then
    67         error("Both a view and an action was specified.")
    68       end
    69     end
    70     do
    71       local new_params = request.get_perm_params()
    72       for key, value in pairs(params) do
    73         new_params[key] = value
    74       end
    75       params = new_params
    76     end
    77   end
    78   if next(params) ~= nil or (id and id_as_param) then
    79     add("?")
    80     if id and id_as_param then
    81       add("_webmcp_id=", encode.url_part(id), "&")
    82     end
    83     for key, value in pairs(params) do
    84       add(encode.url_part(key), "=", encode.url_part(value), "&")
    85     end
    86     result[#result] = nil  -- remove last '&' or '?'
    87   end
    88   return table.concat(result)
    89 end
