Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Filesize for a Dataobject


Go to End


6 Posts   3643 Views

Avatar
ccburns

Community Member, 79 Posts

31 August 2010 at 1:05am

Edited: 31/08/2010 1:06am

Hi Guys,

I am creating a page type that lists out all the files in a given folder in the Assets folder. This is so that my client can simply upload files into here and they will appear on the page automatically. Should be pretty simple and it has been until I have wanted to display the file size next to the download button.

As I loop through the DocumentList control I thought I would be able to use the MyFileSize() function to simply get the file size and then display it. But unfortunately it isn't working. I have seen references to .Filesize appendix to objects but I can't get that working and perhaps I am simply just out of context so that functionality won't work.

If anyone could help it would be appreciated. I have a feeling I will go "doh" once someone points out my error/stupidity :)

Thanks,
Colin

class DocumentListPage extends Page {
	
	public static $db = array(
		'MediaFolder' => 'Text'
	);
	
	public static $has_one = array(
	);
	
	static $icon = "themes/invitro/images/treeicons/application_view_list";
	
	function getCMSFields() {
		
		$fields = parent::getCMSFields();
		
		$fields->addFieldToTab('Root.Content.Main', new DropdownField(
  					'MediaFolder',
  					'Display all Documents in this Folder',
  					array("346" => "MSDS Library", 
  							"347" => "Media Formulations"),
  					'',
  					null,
  					'Display all Documents in this Folder'
					), 'Content');
					
		return $fields;

	}  
	
	
}

class DocumentListPage_Controller extends Page_Controller {
	
	function DocumentList() {
		return DataObject::get("File", "\"ParentID\" = '{$this->MediaFolder}'", '"Title", "Filename", "ID"');
	}
	
	function MyFileSize() {
		// return $this->getSize();
                return "BLAH";
	}

}

TEMPLATE SECTION

			<table id="">
				<caption><h2>$Title <span style="float:right;padding-right: 10px;">$DocumentList.Count Document(s)</span></h2></caption>
				<% control DocumentList %>
					<tr>
						<td class="filename">
							<p>$Title</p>
							<a href="$Filename">$Filename</a>
						</td>
						<td class="filesize"><span><a href="$Filename" style="background:none;padding:none;">$MyFileSize </a></span><br/><span class="download"><a href="$Filename" style="background:none;padding:none;">DOWNLOAD</a></span></td>
					</tr>
				<% end_control %>
			</table>

Avatar
Willr

Forum Moderator, 5523 Posts

31 August 2010 at 9:04pm

You should be able to use the .Size function from File (http://api.silverstripe.org/2.4/sapphire/filesystem/File.html#methodgetSize). i.e $File.Size. As long as $File is of class 'File' then you should be able to call that function.

Avatar
ccburns

Community Member, 79 Posts

1 September 2010 at 3:37pm

Hi Willr,

Thanks for your response. I have tried (.Size) but it doesn't seem to work.

<% control DocumentList %>
Title = $Title<br/>
Filename = $Filename<br/>
Filesize = $ID.Size<br/>
<% end_control %>

You said it should work provided it is a "File" object. Which is exactly what I THINK I am getting back given the initial function I am calling...

function DocumentList() { 
   return DataObject::get("File", "\"ParentID\" = '{$this->MediaFolder}'", '"Title", "Filename", "ID"'); 
} 

I appreciate your help. Any other suggestions?

Regards,
Colin

Avatar
Willr

Forum Moderator, 5523 Posts

1 September 2010 at 4:00pm

$ID.Size

No you should just use $Size in that case (since you're already in the File scope (with that control)

Avatar
ccburns

Community Member, 79 Posts

1 September 2010 at 4:13pm

Perfect - thanks Willr.

To date the hardest thing I have found with SilverStripe (and mostly it is brilliant) is that I don't quite know what values are available for objects in the templating engine and don't really know where to find info about it.

Is there a particular piece of documentation or even code that you can point me to or the ability to output the object in someway in the template so that I can see what is and isn't available for use in the templating engine.

For example you pointed me to the documentation for the getSize function but from there I couldn't find anything that would show me that I should use $Size in the template to invoke that method on the object. http://api.silverstripe.org/2.4/sapphire/filesystem/File.html#methodgetSize

Does that make any sense?

Either way, thank you very much for your help. I appreciate it :)

Cheers,
Colin

Avatar
Willr

Forum Moderator, 5523 Posts

1 September 2010 at 4:40pm

Well in the template you can all any of the methods (functions) on the object or any of the classes it extended. So take your example -

<% control DocumentList %>
<!-- You now have a File -->
<% end_control %>

Inside that control you are 'in' the file object or think of it as $ is now a File. One thing that might not be clear is that in SilverStripe getX() automatically translates to X() as well. Its quite nifty like that so if you wanted to be exact you would have $getSize to call $(File) -> getSize(); but that looks terrible so you can call $Size and SS will look for a getSize() function as well.

Its not terribly consistent (eg http://api.silverstripe.org/2.4/sapphire/filesystem/File.html has getFilename, getSize but then simply an Icon() function) In my view all getters / settings like getSize should be prefixed with get.

We don't maintain cheatsheets or lists of all the functions you can call as that is always changing. Best place to look at is the API docs for a class. These docs are much more useful than possibly out of date list of methods on a wiki. We're working on bringing the 2 tighter together with our doc rewrite project which is under way.