1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
<?php
header("Content-Type: application/json");
include_once('lib/parser2/dojo2.inc');
// no dojo.require() call made?
$files = dojo_get_files();
foreach ($files as $set){
list($namespace, $file) = $set;
$data[$namespace][] = $file;
}
$namespaces = array_keys($data);
// $data takes the form of $namespace[], where the value is the rest of the file. So let's break it up.
// note that $namespaces is the root of each branch.
$id_counter = 0;
$tree = array();
$folders = array();
$indexed = array();
// temporary
//$namespaces = array("dojo");
foreach($namespaces as $nm){
// loop through the namespaces, build up our tree "json"
$obj = array(
"id" => "data-" . $id_counter++,
"name" => $nm,
"full_name" => $nm,
"type" => "namespace",
"children" => array()
);
$tree[] = &$obj;
foreach($data[$nm] as $f){
// we know each one is unique here, but we have to break it up.
$pieces = explode("/", $f);
$name = array_pop($pieces);
$file_obj = array(
"id" => "data-" . $id_counter++,
"name" => $name,
"full_name" => $f,
"ns" => $nm,
"type" => "file"
);
// push it into the tree regardless
$tree[] = $file_obj;
$indexed[$file_obj["id"]] = $file_obj;
if(!count($pieces)){
// this is a direct child of the namespace, so just add it.
$obj["children"][] = array("_reference" => $file_obj["id"]);
} else {
while(count($pieces)){
$full_name = implode("/", $pieces);
$name = array_pop($pieces);
if(!array_key_exists($full_name, $folders)){
// add this directory object
$folder_obj = array(
"id" => "data-" . $id_counter++,
"name" => $name,
"full_name" => $full_name,
"ns" => $nm,
"type" => "folder",
"added" => false,
"children" => array()
);
// keep track of it.
$folders[$full_name] = $folder_obj;
}
// check to see if there's a parent folder
if(count($pieces)){
// there should be a parent
$tmp = explode("/", $full_name);
array_pop($tmp);
$tmp = implode("/", $tmp);
if(array_key_exists($tmp, $folders) && !$folders[$full_name]["added"]){
$folders[$tmp]["children"][] = array("_reference"=>$folders[$full_name]["id"]);
$folders[$full_name]["added"] = true;
}
}
}
// finally, add our file to the right folder.
$tmp = explode("/", $f);
array_pop($tmp);
$tmp = implode("/", $tmp);
$folders[$tmp]["children"][] = array("_reference" => $file_obj["id"]);
}
}
// add in our folder objects and merge with the main namespace
$tmp = array();
foreach($folders as $f=>$folder){
$tree[] = $folder;
$indexed[$folder["id"]] = $folder;
if(strpos($f, "/") === false){
$tmp[] = array("_reference"=>$folder["id"]);
}
}
$obj["children"] = array_merge($tmp, $obj["children"]);
// fugly sorting by rebuilding all children.
foreach($tree as $key=>&$item){
if(array_key_exists("children", $item) && count($item["children"])){
$folder_objs = array();
$privates = array();
$file_objs = array();
// here's where it gets ugly. Loop through the children, get the indexed object and push
// into the various arrays in order to rebuild the children.
foreach($item["children"] as $child=>$value){
$test = $indexed[$value["_reference"]];
if($test["type"] == "folder"){
$folder_objs[] = $value;
}
else if(strpos($test["name"], "_")===0){
$privates[] = $value;
}
else {
$file_objs[] = $value;
}
}
// TODO: we need the file objects to be sorted case-insensitive.
// rebuild the children array
$item["children"] = array_merge($folder_objs, $privates, $file_objs);
}
}
$folders = array();
$indexed = array();
unset($obj);
}
$storeData = array(
"identifier"=>"id",
"label"=>"name",
"items"=>&$tree
);
print json_encode($storeData);
?>
|