|
9 | 9 | redirect($GLOBALS['BASEURL']."/workspace/"); |
10 | 10 | } |
11 | 11 |
|
| 12 | +/** |
| 13 | + * Collect all directories under a project (depth-first) for move targets. |
| 14 | + */ |
| 15 | +function collectMoveTargetDirsRecursive($dirId, $projectPath, &$dirs) |
| 16 | +{ |
| 17 | + $dir = getGSFile_fromId($dirId); |
| 18 | + if (!$dir || !isset($dir['files'])) { |
| 19 | + return; |
| 20 | + } |
| 21 | + |
| 22 | + if (isset($dir['path']) && $dir['path'] == $_SESSION['User']['id']) { |
| 23 | + return; |
| 24 | + } |
| 25 | + |
| 26 | + $relPath = substr($dir['path'], strlen($projectPath) + 1); |
| 27 | + |
| 28 | + $dirs[] = [ |
| 29 | + "id" => $dir["_id"], |
| 30 | + "name" => $relPath, |
| 31 | + "path" => $dir["path"], |
| 32 | + ]; |
| 33 | + |
| 34 | + foreach ($dir['files'] as $childId) { |
| 35 | + $child = getGSFile_fromId($childId); |
| 36 | + if ($child && isset($child['files'])) { |
| 37 | + collectMoveTargetDirsRecursive($childId, $projectPath, $dirs); |
| 38 | + } |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +function collectMoveTargetDirs($projectId, $projectPath) |
| 43 | +{ |
| 44 | + $dirs = []; |
| 45 | + $project = getGSFile_fromId($projectId); |
| 46 | + if (!isset($project['files'])) { |
| 47 | + return $dirs; |
| 48 | + } |
| 49 | + |
| 50 | + foreach ($project['files'] as $childId) { |
| 51 | + collectMoveTargetDirsRecursive($childId, $projectPath, $dirs); |
| 52 | + } |
| 53 | + |
| 54 | + return $dirs; |
| 55 | +} |
| 56 | + |
12 | 57 | switch ($_REQUEST['op']){ |
13 | 58 | case 'rename': |
14 | 59 | $file_raw = getGSFile_fromId($_REQUEST['id']); |
|
19 | 64 | $returnData = ["path" => $path, "name" => $name, "type" => $file["type"]]; |
20 | 65 | print(json_encode($returnData)); |
21 | 66 | break; |
22 | | - case 'move': $file_raw = getGSFile_fromId($_REQUEST['id']); |
| 67 | + case 'move': |
| 68 | + $file_raw = getGSFile_fromId($_REQUEST['id']); |
23 | 69 | $file = formatData($file_raw); |
24 | | - $prjData = ["name" => $file['longfilename'], "execution" => $file['longexecutionname'], "project" => $file['project'], "type" => $file['type'], "projects" => []]; |
| 70 | + $parentPath = ($file['type'] == 'file' && !empty($file['parentDir'])) |
| 71 | + ? $file['parentDir'] |
| 72 | + : ''; |
| 73 | + $parentRelPath = ''; |
| 74 | + if ($parentPath !== '' && preg_match('/(.*\/__PROJ[^\/]*)/', $parentPath, $match)) { |
| 75 | + $parentRelPath = substr($parentPath, strlen($match[1]) + 1); |
| 76 | + } |
| 77 | + |
| 78 | + $prjData = [ |
| 79 | + "name" => $file['longfilename'], |
| 80 | + "execution" => $file['longexecutionname'], |
| 81 | + "project" => $file['project'], |
| 82 | + "type" => $file['type'], |
| 83 | + "parent_path" => $parentPath, |
| 84 | + "parent_rel_path" => $parentRelPath, |
| 85 | + "projects" => [], |
| 86 | + ]; |
25 | 87 | $projects = getProjects_byOwner(); |
26 | 88 | foreach($projects as $pr) { |
27 | | - $excData = []; |
28 | | - foreach($pr["files"] as $execution) { |
29 | | - $exc = getGSFile_fromId($execution); |
30 | | - $p = explode("/",$exc["path"]); |
31 | | - $name = array_pop($p); |
32 | | - $excData[] = ["id" => $exc["_id"], "name" => $name, "path" => $exc["path"]]; |
33 | | - } |
34 | | - $prjData["projects"][] = ["id" => $pr["_id"], "name" => $pr["name"], "path" => $pr["path"], "executions" => $excData]; |
| 89 | + $excData = collectMoveTargetDirs($pr['_id'], $pr['path']); |
| 90 | + $prjData["projects"][] = [ |
| 91 | + "id" => $pr["_id"], |
| 92 | + "name" => $pr["name"], |
| 93 | + "path" => $pr["path"], |
| 94 | + "executions" => $excData, |
| 95 | + ]; |
35 | 96 | } |
36 | 97 | print(json_encode($prjData, JSON_PRETTY_PRINT)); |
37 | 98 | break; |
|
0 commit comments