Jump to:

7935 Posts in 1536 Topics by 943 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » Solved! Download section

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1 2
Go to End
Author Topic: 1324 Views
  • borriej
    Avatar
    Community Member
    267 Posts

    Solved! Download section Link to this post

    Hello,

    I've created a public download section by extending dataObject.
    I want to add some extra functionality, but couldn't find it in the docs/forums so ill post it here.

    Code:

    <?php

    class publicDownloadPage extends Page {

       static $has_many = array (
          'Downloads' => 'Download'
       );
       
       public function getCMSFields() {
    $fields = parent::getCMSFields();
    $manager = new FileDataObjectManager(
    $this,
    'Downloads', // relation name
    'Download', // class name of the DataObject
    'File', // name of the file relation in the DataObject
    array(
             'ID' => 'ID',
              'Title' => 'Title',
              'Comment' => 'Comment'
           ), // headings
    'getCMSFields_forPopup' // name of the function for the popup fields

    );
    $fields->addFieldToTab('Root.Content.Files', $manager);
    return $fields;
    }

    }

    class publicDownloadPage_Controller extends Page_Controller {

    }

    class Download extends DataObject {

       static $db = array (
          'Title' => 'Text',
          'Comment' => 'HTMLText'
       );
       static $has_one = array (
          'File' => 'File',
          'publicDownloadPage' => 'publicDownloadPage'
       );

       public function getCMSFields_forPopup() {
          $fields = new FieldSet();
          $fields->push(new TextField('Title'));
          $fields->push(new SimpleTinyMCEField('Comment'));
          $fields->push(new FileUploadField('File','Upload a file'));
          return $fields;
       }

    }

    Template code

    <% control Downloads %>

                            <% control File %>
                               <a href="$URL" target="_blank"><img src="$URL" width="130" /></a>
                            <% end_control %>
                         </div>
                         <div id="Downloads_text">
                            <table width="100%">
                               <tr height="12px">
                                  <td><h4>FILENAME</h4></td>
                                  <td><h4>SIZE</h4></td>
                               </tr>
                               <tr height="32px">
                                  <td><p>$Title</p></td>
                                  <td><p>$Size</p></td>
                               </tr>
                               <tr height="12px">
                                  <td><h4>COMMENT</h4></td>
                                  <td><h4>TYPE</h4></td>
                               </tr>
                               <tr>
                                  <td><p>$Comment</p></td>
                                  <td><p>$Type</p></td>
                               </tr>
                            </table>

                <% end_control %>
       

    Question 1:
    I would like to be able to recognise the file type/extentions

    How would I do this?

    Question 2:
    When uploading a file you need to fill in the 'Title' and 'Comment' field. I would like some default value's at startup.
    The 'Title' field must be by default the 'filename'.

    Question 3:
    How do i get the filesize displayed in the template?

    Question 4:
    I want to display a thumbnail, normally I just do $imageName.setWidth(130)
    But how do i do this when you use <% control File %> ?

    Solved it now in an ugly way:

    <% control File %>
    <a href="$URL" target="_blank"><img src="$URL" width="130" /></a>
    <% end_control %>

    Any help would be great!
    Thx

  • UncleCheese
    Avatar
    4085 Posts

    Re: Solved! Download section Link to this post

    1) $File.Extension

    2)

    public function onBeforeWrite() {
    parent::onBeforeWrite();
    if(!$this->Title && $this->FileID) {
    $this->Title = $this->File()->Filename;
    }
    if(!$this->Comment) {
    $this->Comment = "Some comment";
    }
    }

    3) $File.Size

    4) <% control YourImageObject %>$SetWidth(130), $CroppedImage(50,50), etc..<% end_control %>

    ---------------
    Silverstripe tips, tutorials, screencasts, and more. http://www.leftandmain.com

  • borriej
    Avatar
    Community Member
    267 Posts

    Re: Solved! Download section Link to this post

    Dude! thx it was quite easy.

    But the setwidth isn't working.

                            <% control File %>
                               <a href="$URL" target="_blank">
                                  <% control YourImageObject %>
                                     $SetWidth(130)
                                  <% end_control %>
                               </a>
                            <% end_control %>

    Any ID?

    (oh btw the thumbnail isnt showing in the browser ;) )

  • borriej
    Avatar
    Community Member
    267 Posts

    Re: Solved! Download section Link to this post

    Oh one more question:

    When using your script with 'Filename' i get the path+file name.

    For instance:

    assets/Public_Downloads/Backgrounds/filename.jpg

    and i would like to only have 'filename.jpg'

  • UncleCheese
    Avatar
    4085 Posts

    Re: Solved! Download section Link to this post

    Do you really have a relation called "YourImageObject" ?? I hope you didn't take that literally.

    ---------------
    Silverstripe tips, tutorials, screencasts, and more. http://www.leftandmain.com

  • borriej
    Avatar
    Community Member
    267 Posts

    Re: Solved! Download section Link to this post

    Ok, srry for asking, but what should it be?

    Jusr control file?

    Or something like control download?
    Or control image?

    Dont see the logic yet.

  • UncleCheese
    Avatar
    4085 Posts

    Re: Solved! Download section Link to this post

    Whatever the name of your image relationship is. If you have:

    $has_one = array (
    'SomeImage' => 'Image'
    );

    then <% control SomeImage %>

  • borriej
    Avatar
    Community Member
    267 Posts

    Re: Solved! Download section Link to this post

    Thats just the point, i dont have has_one image! Im using file.

    1324 Views
Page: 1 2
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.