Skip to content

Commit

Permalink
MDL-14589 new function returning area files in tree structure
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed Jun 21, 2009
1 parent 4937508 commit 752b9f4
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions lib/file/file_storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,49 @@ public function get_area_files($contextid, $filearea, $itemid=false, $sort="item
return $result;
}

/**
* Returns array based tree structure of area files
* @param int $contextid
* @param string $filearea
* @param int $itemid
* @return array each dir represented by dirname, subdirs, files and dirfile array elements
*/
public function get_area_tree($contextid, $filearea, $itemid) {
$result = array('dirname'=>'', 'dirfile'=>null, 'subdirs'=>array(), 'files'=>array());
$files = $this->get_area_files($contextid, $filearea, $itemid, $sort="itemid, filepath, filename", true);
// first create directory structure
foreach ($files as $hash=>$dir) {
if (!$dir->is_directory()) {
continue;
}
unset($files[$hash]);
if ($dir->get_filepath() === '/') {
$result['dirfile'] = $dir;
continue;
}
$parts = explode('/', trim($dir->get_filepath(),'/'));
$pointer =& $result;
foreach ($parts as $part) {
if (!isset($pointer['subdirs'][$part])) {
$pointer['subdirs'][$part] = array('dirname'=>$part, 'dirfile'=>null, 'subdirs'=>array(), 'files'=>array());
}
$pointer =& $pointer['subdirs'][$part];
}
$pointer['dirfile'] = $dir;
unset($pointer);
}
foreach ($files as $hash=>$file) {
$parts = explode('/', trim($file->get_filepath(),'/'));
$pointer =& $result;
foreach ($parts as $part) {
$pointer =& $pointer['subdirs'][$part];
}
$pointer['files'][$file->get_filename()] = $file;
unset($pointer);
}
return $result;
}

/**
* Returns all files and otionally directories
* @param int $contextid
Expand Down

0 comments on commit 752b9f4

Please sign in to comment.