Source: site.view [edit]
Function name: editFunction
Arguments: name,readonly,renderpage,values,createWithName
Description: Edits an existing WubHub function, or if name = "", creates a new function. If readonly is true, views existing function. Uses renderpage if specified
Page type: webl
Render function:  
Module: siteutil

Page source:

var rowInfo = Str_Split(name, ">");
if (Size(rowInfo) > 0) then
   name = rowInfo[0];
end;
  
var finfo = (Wub_GetFunctionInfo(name) ? nil);
var module = (finfo.module ? wubinfo.user);
var res = nil;

if (wubinfo.user == nil) and !readonly then
   res = WubCall("sitemessageref", ["mustlogin", name])
elsif (finfo == nil) and (name != "") and !createWithName then
   res = WubCall("sitemessageref", ["unknowncmdtoedit", name])
elsif (finfo == "INSUFFICIENT_PERMISSIONS") then
   res = WubCall("sitemessageref", ["insufficientpermissions", name])
elsif (!readonly) then
   if !(module member Wub_WritableModules()) then
      res = WubCall("sitemessageref", ["insufficientpermissions", name])
   end
end;

if (res == nil) then
   if !readonly then
      if (finfo == nil) then
         finfo = [. module=module, name="", ctype="snippet", description="",
           render="", test="", version="0", tags="", flags="", exec="", cacheexpire="", cacherefreshrate="", usecache="", createdby=wubinfo.user, modified="", created="", modifiedby=wubinfo.user,arglist="" .]
      end;
      finfo.oldtags := (finfo.tags ? "");
      finfo.oldflags := (finfo.flags? "");
      var u = (finfo.usecache ? "");
      if (u == "usecache") then
         finfo.oldspecialtags := `["system.cached","` + "type." + finfo.ctype + `"]`
      else
         finfo.oldspecialtags := `["` + "type." + finfo.ctype + `"]`
      end
   end;
   finfo.usecache := (finfo.usecache ? "");
   
   // Initialize fields with passed values
   every val in values do
      finfo[val] := values[val]
   end;

   var wholepage = false;

   if (renderpage == nil) or (renderpage == "") then
      if readonly then
         renderpage = "siteviewform"
      elsif Str_StartsWith(finfo.flags, "bookmark") then  // special type
         renderpage = "sitebookmarkform"  
      elsif Str_StartsWith(finfo.flags, "issue") then  // special type
         renderpage = "siteeditissue";
         wholepage = true
      else
         // renderpage = "siteeditform"
         renderpage = "siteeditpage";
         wholepage = true;
      end
   end;

   var scrollTo = 1;
   if (Size(rowInfo) == 2) then
      scrollTo = rowInfo[1];
   end;
   var s = Markup(WubCall(renderpage, [finfo.ctype, finfo.module, finfo.usecache, scrollTo, finfo.flags]));
 
   // Quote actual contents of function using HTML char entities, since
   // <input> textareas should not contain HTML markup chars.  Browser will expand
   // these entities, and they will be saved back to server as before.
    Wub_StringToHTMLEveryField(finfo);  // slow?

   // Substitute in all other fields
   s = Wub_ReplaceTokens(finfo, s);

   var v = ToInt(finfo.version) ? 0;
   s = Wub_ReplaceTokens([. prevVersion=ToString(v-1), nextVersion=ToString(v+1) .], s);

   if (wholepage) then
      res = NewPage(s, "text/html")
   else
      res = NewPiece(s, "text/html")
   end;

end;

res;