<%@ page import="org.json.simple.JSONArray,org.json.simple.JSONObject" %><%! public JSONArray getJSONArrayFromString(String str, String delimiter) { String[] userArray = str.split(delimiter); JSONArray jsonUserArray = new JSONArray(); for (int j = 0; j < userArray.length; j++) { jsonUserArray.add(userArray[j]); } return jsonUserArray; } %><% JSONObject root = new JSONObject(); root.put("children", new JSONObject()); root.put("users", "all"); // JSONArray allFiles = new JSONArray(); // create root node String rzIntranetFilter = "pageid!=null" + slug; rz.listsetup("intranet_permissions", "noscript", rzIntranetFilter, ""); while (rz.listnext() && rz.listindex > -1) { String pageid = rz.fetch("intranet_permissions", "pageid"); boolean isLinks = pageid.indexOf("links-") == 0; String users = rz.fetch("intranet_permissions", "users"); String fileName = isLinks ? rz.fetch("links", "linkfilename", "", "links._recordid="+pageid.substring(6)) : "work something out for unique templates?"; // allFiles.add(fileName); String[] parts = fileName.split("/"); // Start from root for every new record // For each file path part, set currentNode to new node or existing matched node JSONObject currentNode = (JSONObject) root; for (int i = 0; i < parts.length; i++) { boolean isLast = (i + 1) == parts.length; String part = parts[i].toLowerCase(); // if last part is default page, set users as specified JSONObject children = (JSONObject) currentNode.get("children"); if (!children.containsKey(part) && !(isLast && "index.php".equals(part))) { JSONObject newPartNode = new JSONObject(); newPartNode.put("children", new JSONObject()); newPartNode.put("users", "inherit"); children.put(part, newPartNode); } // specify users on final part or base if(isLast) { // advance to subpage if not default if (!"index.php".equals(part)) { currentNode = (JSONObject) children.get(part); } if (users.indexOf("all") != -1 || users.indexOf("inherit") != -1) { currentNode.put("users", users); } else { JSONArray jsonUserArray = getJSONArrayFromString(users, "\\|"); currentNode.put("users", jsonUserArray); } // to skip trying to advance another node continue; } currentNode = (JSONObject) children.get(part); } } rz.listcomplete(); // May be useful to debug // root.put("files", allFiles); out.println(root.toString()); %>