Jump to:

3373 Posts in 998 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Filesize for a Dataobject

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

Page: 1
Go to End
Author Topic: 1762 Views
  • ccburns
    Avatar
    Community Member
    74 Posts

    Filesize for a Dataobject Link to this post

    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>

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Filesize for a Dataobject Link to this post

    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.

  • ccburns
    Avatar
    Community Member
    74 Posts

    Re: Filesize for a Dataobject Link to this post

    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

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Filesize for a Dataobject Link to this post

    $ID.Size

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

  • ccburns
    Avatar
    Community Member
    74 Posts

    Re: Filesize for a Dataobject Link to this post

    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

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Filesize for a Dataobject Link to this post

    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.

    1762 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.