[xwiki-devs] How can I dynamically create a panel?
I was trying to figure out how to dynamically create a panel so I was looking at create.vm shown here: #if($tocreate=="panel") #set($docname=$title.replaceAll("/", "%2F")) #if(!$template || $template == "") #set($template="Panels.PanelTemplate") #end #set($newdoc=$xwiki.getDocument($docname)) #if($newdoc.isNew()) #set($pcontent = "#") #set($pcontent = $pcontent.concat("panelheader('${title}')${xwiki.getNl()}${xwiki.getNl()}#")) #set($pcontent = $pcontent.concat("panelfooter()")) #set($pcontent = $util.encodeURL($pcontent)) $response.sendRedirect($newdoc.getURL("inline", "template=${template}&Panels.PanelClass_0_name=${title}&Panels.PanelClass_0_content=$pcontent")) #else $response.sendRedirect($newdoc.getURL("view", "xpage=docalreadyexists")) #end #end . . and I quickly got lost. It looks like you first create a document from in panels space something like XWikiDocument doc = context.getWiki().getDocument("XWiki.MyPanell", context); Then it looks like $pcontent concatenates some calls to velocity macros together. Then it looks like it is url encoded so it can be passed as http response. Then I am completely lost with the sendRedirect statement. Does it redirect to process the templates and then return the response? Is there a better example for me to look at or perhaps someone generous enough to provide me with even more help? You guys have been very generous with help and I would like to Thank you! BTW congratulations on the "Big News" Glenn Everitt -- View this message in context: http://www.nabble.com/How-can-I-dynamically-create-a-panel--tp16406339p16406... Sent from the XWiki- Dev mailing list archive at Nabble.com.
I'm trying to dynamically create a panel to navigate to a newly created space. Then I want to put the panel in the left column of Main.WebHome. This code is how I thought it would work but it doesn't seem like you put Panel objects in Panels.WebHome and I haven't been able to figure out where they should be put. If anyone knows I would appreciate knowing XWikiDocument panelsDoc = context.getWiki().getDocument("Panels.WebHome", context); BaseObject bobj = panelsDoc.newObject("Panels.PanelClass", context); //create a panel String panelName = spaceName+"Pnl"; bobj.setStringValue("name", panelName); bobj.setStringValue("type", "view"); bobj.setStringValue("description", "Dynamic Panel for " + spaceName); //the content of the panel - Note all quotes are escaped bobj.setStringValue("category", "Navigation"); String content = "#panelheader('" + spaceName + "Links')" + " * [Home>" + spaceName + ".WebHome]" + " <p style=\"font-size:0.75em;padding-left:8px;\"> \"$xwiki.getDocument(\"Panels." (Edit this panel) </p>" + " #panelfooter()"; bobj.setStringValue("context", content); context.getWiki().saveDocument(panelsDoc, context.getMessageTool().get("core.comment.createdUser"), context); //add to left panel list XWikiDocument prefsDoc = context.getWiki().getDocument("XWiki.XWikiPreferences", context); BaseObject prefsObj = prefsDoc.getObject("XWiki.XWikiPreferences"); String csvLeftPanelList = prefsObj.getStringValue("leftPanels"); csvLeftPanelList = csvLeftPanelList + ",Panels."+panelName; prefsObj.setStringValue("leftPanel", csvLeftPanelList); context.getWiki().saveDocument(prefsDoc, context.getMessageTool().get("core.comment.createdUser"), context); So, as I mentioned not sure where or how to add the created Panel any help would be appreciated. Thanks Glenn Everitt -- View this message in context: http://www.nabble.com/How-can-I-dynamically-create-a-panel--tp16406339p16467... Sent from the XWiki- Dev mailing list archive at Nabble.com.
Here is what I finally got to work: public void createPanelForSpace(String uid, String spaceName, String wikiGroupName, XWikiContext context) throws XWikiException { //Get the Panels document to add a panel object XWikiDocument panelsDoc = context.getWiki().getDocument("Panels.WebHome", context); BaseObject bobj = panelsDoc.newObject("Panels.PanelClass", context); //create a panel String panelName = spaceName+"Pnl"; bobj.setStringValue("name", panelName); bobj.setStringValue("type", "view"); bobj.setStringValue("description", "Dynamic Panel for " + spaceName); bobj.setStringValue("category", "Navigation"); //format the content of the panel String content = "#panelheader('" + spaceName + "Links')" + " * [Home>" + spaceName + ".WebHome]" + " <p style=\"font-size:0.75em;padding-left:8px;\"> \"$xwiki.getDocument(\"Panels." (Edit this panel) </p>" + " #panelfooter()"; bobj.setStringValue("context", content); context.getWiki().saveDocument(panelsDoc, context.getMessageTool().get("core.comment.createdUser"), context); //add to left panel list in XWiki.XWikiPreferences XWikiDocument prefsDoc = context.getWiki().getDocument("XWiki.XWikiPreferences", context); BaseObject prefsObj = prefsDoc.getObject("XWiki.XWikiPreferences"); String csvLeftPanelList = prefsObj.getStringValue("leftPanels"); csvLeftPanelList = csvLeftPanelList + ",Panels."+panelName; prefsObj.setStringValue("leftPanels", csvLeftPanelList); context.getWiki().saveDocument(prefsDoc, context.getMessageTool().get("core.comment.createdUser"), context); //give the group permission to panels XWikiDocument panelPrefsDoc = context.getWiki().getDocument("Panels.WebPreferences", context); BaseObject panelRightsObject = panelPrefsDoc.newObject("XWiki.XWikiGlobalRights", context); panelRightsObject.setLargeStringValue("groups", wikiGroupName); panelRightsObject.setStringValue("levels", "view"); panelRightsObject.setLargeStringValue("users", ""); panelRightsObject.setIntValue("allow", 1); } -- View this message in context: http://www.nabble.com/How-can-I-dynamically-create-a-panel--tp16406339p16628... Sent from the XWiki- Dev mailing list archive at Nabble.com.
small correction to the below post: should be bobj.setStringValue("content", content); Glenn Everitt wrote:
Here is what I finally got to work:
public void createPanelForSpace(String uid, String spaceName, String wikiGroupName, XWikiContext context) throws XWikiException { //Get the Panels document to add a panel object XWikiDocument panelsDoc = context.getWiki().getDocument("Panels.WebHome", context); BaseObject bobj = panelsDoc.newObject("Panels.PanelClass", context);
//create a panel String panelName = spaceName+"Pnl"; bobj.setStringValue("name", panelName); bobj.setStringValue("type", "view"); bobj.setStringValue("description", "Dynamic Panel for " + spaceName); bobj.setStringValue("category", "Navigation"); //format the content of the panel String content = "#panelheader('" + spaceName + "Links')" + " * [Home>" + spaceName + ".WebHome]" + " <p style=\"font-size:0.75em;padding-left:8px;\"> \"$xwiki.getDocument(\"Panels." (Edit this panel) </p>" + " #panelfooter()"; bobj.setStringValue("context", content); context.getWiki().saveDocument(panelsDoc, context.getMessageTool().get("core.comment.createdUser"), context);
//add to left panel list in XWiki.XWikiPreferences XWikiDocument prefsDoc = context.getWiki().getDocument("XWiki.XWikiPreferences", context); BaseObject prefsObj = prefsDoc.getObject("XWiki.XWikiPreferences"); String csvLeftPanelList = prefsObj.getStringValue("leftPanels"); csvLeftPanelList = csvLeftPanelList + ",Panels."+panelName; prefsObj.setStringValue("leftPanels", csvLeftPanelList); context.getWiki().saveDocument(prefsDoc, context.getMessageTool().get("core.comment.createdUser"), context);
//give the group permission to panels XWikiDocument panelPrefsDoc = context.getWiki().getDocument("Panels.WebPreferences", context); BaseObject panelRightsObject = panelPrefsDoc.newObject("XWiki.XWikiGlobalRights", context); panelRightsObject.setLargeStringValue("groups", wikiGroupName); panelRightsObject.setStringValue("levels", "view"); panelRightsObject.setLargeStringValue("users", ""); panelRightsObject.setIntValue("allow", 1); }
-- View this message in context: http://www.nabble.com/How-can-I-dynamically-create-a-panel--tp16406339p18197... Sent from the XWiki- Dev mailing list archive at Nabble.com.
participants (2)
-
Glenn Everitt -
spanduga