Jump to:

22997 Posts in 11853 Topics by 2828 members

General Questions

SilverStripe Forums » General Questions » Menu item link to download document

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1 2
Go to End
Author Topic: 2109 Views
  • servalman
    Avatar
    Community Member
    211 Posts

    Menu item link to download document Link to this post

    Hello

    Is there a simple way of doing the following :

    Creating a menu item from the site tree that would download a file from the assests folders
    Would trying to change the redirector page behaviour a good idea ?

    Thank you

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: Menu item link to download document Link to this post

    I'd use a redirector page and then add this code within it...
    http://www.silverstripe.org/general-questions/show/15832

    or you could just take the URL from the asset itself and specify the entire url (I think it's say link to another site) but it should work.

  • servalman
    Avatar
    Community Member
    211 Posts

    Re: Menu item link to download document Link to this post

    Thanks for your answer it seems to be the begining of a solution
    hoewer I need to be able to choose the document to download in the assets folder in a similar I would do it in a regular link

    tHANKS

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: Menu item link to download document Link to this post

    ok, create a new page type "file download page" in db add has_one file to it, update cmsfields to pick the file
    also add the controller function as described in the link (http://www.silverstripe.org/general-questions/show/15832)
    that page can then be placed directly into the sitetree

  • servalman
    Avatar
    Community Member
    211 Posts

    Re: Menu item link to download document Link to this post

    Thanks

    I will try to make it work eventough I think it is beyond my skills especially the getcmsfileds part ;)

    thanks

    T

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: Menu item link to download document Link to this post

    maybe this will help then...

    <?php

    class FilePage extends Page {

       static $has_one = array(
          'MyFile' => 'File'
       );

       function getCMSFields(){
          $fields = parent::getCMSFields();

          $fields->addFieldsToTab('Root.Content', array(
             new FileIFrameField('MyFile', 'My File')
          ));

          return $fields;
       }
    }

    class FilePage_Controller extends Page_Controller {
       //do send_file stuff here
    }

  • servalman
    Avatar
    Community Member
    211 Posts

    Re: Menu item link to download document Link to this post

    Hello Swaiba

    I could not try your code before but I did today and I get an error
    so here is my code now (I have added the file download part)

    <?php

    class FilePage extends Page {

    static $has_one = array(
    'MyFile' => 'File'
    );

    function getCMSFields(){
    $fields = parent::getCMSFields();

    $fields->addFieldsToTab('Root.Content', array(
    new FileIFrameField('MyFile', 'My File')
    ));

    return $fields;
    }
    }

    class FilePage_Controller extends Page_Controller {
    public function download(){

    $assetID = $this->request->param('ID');

    $do = DataObject::get_by_id('File', $assetID);

    if($do && file_exists($do->URL)){
    return SS_HTTPRequest::send_file(file_get_contents($do->URL), $do->Name);
    }else{
    echo 'error with ' . $do->URL . ' (' . file_exists($do->URL) . ')';
    return false;
    }

    }
    }

    and this is the error I get

    ERROR [User Error]: Uncaught Exception: Object->__call(): the method 'settabset' does not exist on 'FileIFrameField'
    IN POST /newsite/admin/getitem?ID=60&ajax=1
    Line 724 in /var/www/vhosts/kjebb2.com/httpdocs/newsite/sapphire/core/Object.php

    Source
    ======
    715:             
    716:             default :
    717:                throw new Exception (
    718:                   "Object->__call(): extra method $method is invalid on $this->class:" . var_export($config,
    true)
    719:                );
    720:          }
    721:       } else {
    722:          // Please do not change the exception code number below.
    723:          
    * 724:          throw new Exception("Object->__call(): the method '$method' does not exist on '$this->class'",
    2175);
    725:       }
    726:    }
    727:    
    728:    //
    -----------------------------------------------------------------------------------------------------------------
    729:    
    730:    /**

    Trace
    =====
    <ul>Object->__call(setTabSet,Array)

    FileIFrameField->setTabSet(TabSet)
    line 128 of TabSet.php

    TabSet->push(FileIFrameField)
    line 128 of FieldSet.php

    FieldSet->addFieldsToTab(Root.Content,Array)
    line 14 of FilePage.php

    FilePage->getCMSFields(CMSMain)
    line 441 of CMSMain.php

    CMSMain->getEditForm(60)
    line 1039 of LeftAndMain.php

    LeftAndMain->EditForm()
    line 389 of LeftAndMain.php

    LeftAndMain->getitem(SS_HTTPRequest)
    line 193 of Controller.php

    Controller->handleAction(SS_HTTPRequest)
    line 143 of RequestHandler.php

    RequestHandler->handleRequest(SS_HTTPRequest)
    line 147 of Controller.php

    Controller->handleRequest(SS_HTTPRequest)
    line 282 of Director.php

    Director::handleRequest(SS_HTTPRequest,Session)
    line 125 of Director.php

    Director::direct(/admin/getitem)
    line 127 of main.php

    </ul>

    If you can take a look it would be great

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: Menu item link to download document Link to this post

    try ImageField instead of FileIFrameField

    that code wasn't tested - there should be something in the basic tutorial about adding a picture (of an author to an article page)

    2109 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.