21300 Posts in 5735 Topics by 2603 members
General Questions
SilverStripe Forums » General Questions » How to show latest files uploaded to folder or one of it's subfolders
General questions about getting started with SilverStripe that don't fit in any of the categories above.
Moderators: martimiz, Howard, Sean, Ryan M., biapar, Willr, Ingo, swaiba, simon_w
|
Page:
1
|
Go to End | |
| Author | Topic: | 1108 Views |
-
How to show latest files uploaded to folder or one of it's subfolders

23 July 2011 at 4:14am
How can I show latest 5 files uploaded to a certain folder 'Public' OR uploaded to a subfolder of 'Public' OR uploaded to a subfolder of a subfolder of 'Public' and so on. Kind of like getChildrenAsUl, but than without the UL and LI and with a sort and limit function.
-
Re: How to show latest files uploaded to folder or one of it's subfolders

23 July 2011 at 7:53am
Okay here's an idea. It seems a little clunky to me and only offers two levels of recursion. but it'll work. I've tested it on a system with over 10000 files, its a little slow but it works. Maybe someone can find a way to make it more elegant/faster
function LatestFiles() {
//Use the ID for your "Public" folder for ParentID
$files = new DataObjectSet();
$folders = DataObject::get("Folder", "ParentID = X");
if($folders) {
//loop through folders
foreach($folders as $folder){
//check if folder has sub-folder
if ($folder->hasChildFolders()){
//get sub fodlers
$subFolders = $folder->ChildFolders();
//loop through sub folders
foreach($subFolders as $subFolder) {
//check if has sub-sub folder
if ($subFolder->hasChildFolders()){
//get sub-sub folder
$subSubFolders = $subFolder->ChildFolders();
//push sub-sub folders to subfolder list
$subFolders->merge($subSubFolders);
}
}
//push sub fodler list to folder list
$folders->merge($subFolders);
}
}
//loop through all foldes to find files
foreach($folders as $folder){
$file = DataObject::get("File", "ClassName != 'Folder' AND ParentID=".$folder->ID);
$files->merge($file);
}
}
$files->sort("Created", "DSC");//reverse sort by date created
return $files->getRange(0,5);//return first five in list
} -
Re: How to show latest files uploaded to folder or one of it's subfolders

24 July 2011 at 7:43am Last edited: 24 July 2011 7:44am
Since the File table already 'knows' the path to each file in /assets/, in this case a straight query might get you there as well. Something like:
function NewPublicUploads() {
return DataObject::get(
$name = 'File',
$filter = "ClassName = 'file' and Filename like 'assets/public/%'",
$sort = "Created DESC",
$join = "",
$limit = "5"
);
}<% if NewPublicUploads %>
<ul>
<% control NewPublicUploads %>
<li><a href="$Filename">$Title.XML</a></li>
<% end_control %>
</ul>
<% end_if %> -
Re: How to show latest files uploaded to folder or one of it's subfolders

24 July 2011 at 6:58pm
So simple... I guess I was trying to do it the hard way
Many thanks Martimiz (and also zenmonkey; in this case I'll rate your answer as second-best
| 1108 Views | ||
|
Page:
1
|
Go to Top |


