| 1 |
<?php |
| 2 |
header("Content-Type: application/json"); |
| 3 |
|
| 4 |
include_once('lib/parser2/dojo2.inc'); |
| 5 |
|
| 6 |
|
| 7 |
$files = dojo_get_files(); |
| 8 |
foreach ($files as $set){ |
| 9 |
list($namespace, $file) = $set; |
| 10 |
$data[$namespace][] = $file; |
| 11 |
} |
| 12 |
$namespaces = array_keys($data); |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
$id_counter = 0; |
| 17 |
$tree = array(); |
| 18 |
$folders = array(); |
| 19 |
$indexed = array(); |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
foreach($namespaces as $nm){ |
| 24 |
|
| 25 |
$obj = array( |
| 26 |
"id" => "data-" . $id_counter++, |
| 27 |
"name" => $nm, |
| 28 |
"full_name" => $nm, |
| 29 |
"type" => "namespace", |
| 30 |
"children" => array() |
| 31 |
); |
| 32 |
$tree[] = &$obj; |
| 33 |
foreach($data[$nm] as $f){ |
| 34 |
|
| 35 |
$pieces = explode("/", $f); |
| 36 |
$name = array_pop($pieces); |
| 37 |
$file_obj = array( |
| 38 |
"id" => "data-" . $id_counter++, |
| 39 |
"name" => $name, |
| 40 |
"full_name" => $f, |
| 41 |
"ns" => $nm, |
| 42 |
"type" => "file" |
| 43 |
); |
| 44 |
|
| 45 |
|
| 46 |
$tree[] = $file_obj; |
| 47 |
$indexed[$file_obj["id"]] = $file_obj; |
| 48 |
if(!count($pieces)){ |
| 49 |
|
| 50 |
$obj["children"][] = array("_reference" => $file_obj["id"]); |
| 51 |
} else { |
| 52 |
while(count($pieces)){ |
| 53 |
$full_name = implode("/", $pieces); |
| 54 |
$name = array_pop($pieces); |
| 55 |
if(!array_key_exists($full_name, $folders)){ |
| 56 |
|
| 57 |
$folder_obj = array( |
| 58 |
"id" => "data-" . $id_counter++, |
| 59 |
"name" => $name, |
| 60 |
"full_name" => $full_name, |
| 61 |
"ns" => $nm, |
| 62 |
"type" => "folder", |
| 63 |
"added" => false, |
| 64 |
"children" => array() |
| 65 |
); |
| 66 |
|
| 67 |
$folders[$full_name] = $folder_obj; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
if(count($pieces)){ |
| 72 |
|
| 73 |
$tmp = explode("/", $full_name); |
| 74 |
array_pop($tmp); |
| 75 |
$tmp = implode("/", $tmp); |
| 76 |
if(array_key_exists($tmp, $folders) && !$folders[$full_name]["added"]){ |
| 77 |
$folders[$tmp]["children"][] = array("_reference"=>$folders[$full_name]["id"]); |
| 78 |
$folders[$full_name]["added"] = true; |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
$tmp = explode("/", $f); |
| 85 |
array_pop($tmp); |
| 86 |
$tmp = implode("/", $tmp); |
| 87 |
$folders[$tmp]["children"][] = array("_reference" => $file_obj["id"]); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
$tmp = array(); |
| 93 |
foreach($folders as $f=>$folder){ |
| 94 |
$tree[] = $folder; |
| 95 |
$indexed[$folder["id"]] = $folder; |
| 96 |
if(strpos($f, "/") === false){ |
| 97 |
$tmp[] = array("_reference"=>$folder["id"]); |
| 98 |
} |
| 99 |
} |
| 100 |
$obj["children"] = array_merge($tmp, $obj["children"]); |
| 101 |
|
| 102 |
|
| 103 |
foreach($tree as $key=>&$item){ |
| 104 |
if(array_key_exists("children", $item) && count($item["children"])){ |
| 105 |
$folder_objs = array(); |
| 106 |
$privates = array(); |
| 107 |
$file_objs = array(); |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
foreach($item["children"] as $child=>$value){ |
| 112 |
$test = $indexed[$value["_reference"]]; |
| 113 |
if($test["type"] == "folder"){ |
| 114 |
$folder_objs[] = $value; |
| 115 |
} |
| 116 |
else if(strpos($test["name"], "_")===0){ |
| 117 |
$privates[] = $value; |
| 118 |
} |
| 119 |
else { |
| 120 |
$file_objs[] = $value; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
$item["children"] = array_merge($folder_objs, $privates, $file_objs); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
$folders = array(); |
| 133 |
$indexed = array(); |
| 134 |
unset($obj); |
| 135 |
} |
| 136 |
$storeData = array( |
| 137 |
"identifier"=>"id", |
| 138 |
"label"=>"name", |
| 139 |
"items"=>&$tree |
| 140 |
); |
| 141 |
print json_encode($storeData); |
| 142 |
?> |