| rev | 
   line source | 
| 
jbe/bsw@0
 | 
     1 --[[--
 | 
| 
jbe/bsw@0
 | 
     2 url_string =              -- a string containing an URL
 | 
| 
jbe/bsw@0
 | 
     3 encode.url{
 | 
| 
jbe/bsw@0
 | 
     4   external  = external,   -- external URL (instead of specifying base, module, etc. below)
 | 
| 
jbe/bsw@0
 | 
     5   base      = base,       -- optional string containing a base URL of a WebMCP application
 | 
| 
jbe/bsw@0
 | 
     6   static    = static,     -- an URL relative to the static file directory
 | 
| 
jbe/bsw@0
 | 
     7   module    = module,     -- a module name of the WebMCP application
 | 
| 
jbe/bsw@0
 | 
     8   view      = view,       -- a view name of the WebMCP application
 | 
| 
jbe/bsw@0
 | 
     9   action    = action,     -- an action name of the WebMCP application
 | 
| 
jbe/bsw@0
 | 
    10   id        = id,         -- optional id to be passed to the view or action to select a particular data record
 | 
| 
jbe/bsw@0
 | 
    11   params    = params      -- optional parameters to be passed to the view or action
 | 
| 
jbe/bsw@0
 | 
    12 }
 | 
| 
jbe/bsw@0
 | 
    13 
 | 
| 
jbe/bsw@0
 | 
    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.
 | 
| 
jbe/bsw@0
 | 
    15 
 | 
| 
jbe/bsw@0
 | 
    16 --]]--
 | 
| 
jbe/bsw@0
 | 
    17 
 | 
| 
jbe/bsw@0
 | 
    18 function encode.url(args)
 | 
| 
jbe/bsw@0
 | 
    19   local external  = args.external
 | 
| 
jbe/bsw@0
 | 
    20   local base      = args.base or request.get_relative_baseurl()
 | 
| 
jbe/bsw@0
 | 
    21   local static    = args.static
 | 
| 
jbe/bsw@0
 | 
    22   local module    = args.module
 | 
| 
jbe/bsw@0
 | 
    23   local view      = args.view
 | 
| 
jbe/bsw@0
 | 
    24   local action    = args.action
 | 
| 
jbe/bsw@0
 | 
    25   local id        = args.id
 | 
| 
jbe/bsw@0
 | 
    26   local params    = args.params or {}
 | 
| 
jbe/bsw@0
 | 
    27   local result    = {}
 | 
| 
jbe/bsw@0
 | 
    28   local id_as_param = false
 | 
| 
jbe/bsw@0
 | 
    29   local function add(...)
 | 
| 
jbe/bsw@0
 | 
    30     for i = 1, math.huge do
 | 
| 
jbe/bsw@0
 | 
    31       local v = select(i, ...)
 | 
| 
jbe/bsw@0
 | 
    32       if v == nil then break end
 | 
| 
jbe/bsw@0
 | 
    33       result[#result+1] = v
 | 
| 
jbe/bsw@0
 | 
    34     end
 | 
| 
jbe/bsw@0
 | 
    35   end
 | 
| 
jbe/bsw@0
 | 
    36   if external then
 | 
| 
jbe/bsw@0
 | 
    37     add(external)
 | 
| 
jbe/bsw@0
 | 
    38   else
 | 
| 
jbe/bsw@0
 | 
    39     add(base)
 | 
| 
jbe/bsw@0
 | 
    40     if not string.find(base, "/$") then
 | 
| 
jbe/bsw@0
 | 
    41       add("/")
 | 
| 
jbe/bsw@0
 | 
    42     end
 | 
| 
jbe/bsw@0
 | 
    43     if static then
 | 
| 
jbe/bsw@0
 | 
    44       add("static/")
 | 
| 
jbe/bsw@0
 | 
    45       add(static)
 | 
| 
jbe/bsw@0
 | 
    46     elseif module or view or action or id then
 | 
| 
jbe/bsw@0
 | 
    47       assert(module, "Module not specified.")
 | 
| 
jbe/bsw@0
 | 
    48       add(encode.url_part(module), "/")
 | 
| 
jbe/bsw@0
 | 
    49       if view and not action then
 | 
| 
jbe/bsw@0
 | 
    50         local view_base, view_suffix = string.match(
 | 
| 
jbe/bsw@0
 | 
    51           view,
 | 
| 
jbe/bsw@0
 | 
    52           "^([^.]*)(.*)$"
 | 
| 
jbe/bsw@0
 | 
    53         )
 | 
| 
jbe/bsw@0
 | 
    54         add(encode.url_part(view_base))
 | 
| 
jbe/bsw@0
 | 
    55         if args.id then
 | 
| 
jbe/bsw@0
 | 
    56           add("/", encode.url_part(id))
 | 
| 
jbe/bsw@0
 | 
    57         end
 | 
| 
jbe/bsw@0
 | 
    58         if view_suffix == "" then
 | 
| 
jbe/bsw@0
 | 
    59           add(".html")
 | 
| 
jbe/bsw@0
 | 
    60         else
 | 
| 
jbe/bsw@0
 | 
    61           add(view_suffix)  -- view_suffix includes dot as first character
 | 
| 
jbe/bsw@0
 | 
    62         end
 | 
| 
jbe/bsw@0
 | 
    63       elseif action and not view then
 | 
| 
jbe/bsw@0
 | 
    64         add(encode.url_part(action))
 | 
| 
jbe/bsw@0
 | 
    65         id_as_param = true
 | 
| 
jbe/bsw@0
 | 
    66       elseif view and action then
 | 
| 
jbe/bsw@0
 | 
    67         error("Both a view and an action was specified.")
 | 
| 
jbe/bsw@0
 | 
    68       end
 | 
| 
jbe/bsw@0
 | 
    69     end
 | 
| 
jbe/bsw@0
 | 
    70     do
 | 
| 
jbe/bsw@0
 | 
    71       local new_params = request.get_perm_params()
 | 
| 
jbe/bsw@0
 | 
    72       for key, value in pairs(params) do
 | 
| 
jbe/bsw@0
 | 
    73         new_params[key] = value
 | 
| 
jbe/bsw@0
 | 
    74       end
 | 
| 
jbe/bsw@0
 | 
    75       params = new_params
 | 
| 
jbe/bsw@0
 | 
    76     end
 | 
| 
jbe/bsw@0
 | 
    77   end
 | 
| 
jbe/bsw@0
 | 
    78   if next(params) ~= nil or (id and id_as_param) then
 | 
| 
jbe/bsw@0
 | 
    79     add("?")
 | 
| 
jbe/bsw@0
 | 
    80     if id and id_as_param then
 | 
| 
jbe/bsw@0
 | 
    81       add("_webmcp_id=", encode.url_part(id), "&")
 | 
| 
jbe/bsw@0
 | 
    82     end
 | 
| 
jbe/bsw@0
 | 
    83     for key, value in pairs(params) do
 | 
| 
jbe/bsw@11
 | 
    84       -- TODO: better way to detect arrays?
 | 
| 
jbe/bsw@11
 | 
    85       if string.match(key, "%[%]$") then
 | 
| 
jbe/bsw@11
 | 
    86         for idx, entry in ipairs(value) do
 | 
| 
jbe/bsw@11
 | 
    87           add(encode.url_part(key), "=", encode.url_part(entry), "&")
 | 
| 
jbe/bsw@11
 | 
    88         end
 | 
| 
jbe/bsw@11
 | 
    89       else
 | 
| 
jbe/bsw@11
 | 
    90         add(encode.url_part(key), "=", encode.url_part(value), "&")
 | 
| 
jbe/bsw@11
 | 
    91       end
 | 
| 
jbe/bsw@0
 | 
    92     end
 | 
| 
jbe/bsw@0
 | 
    93     result[#result] = nil  -- remove last '&' or '?'
 | 
| 
jbe/bsw@0
 | 
    94   end
 | 
| 
jbe/bsw@0
 | 
    95   return table.concat(result)
 | 
| 
jbe/bsw@0
 | 
    96 end
 |