Jump to:

23453 Posts in 18904 Topics by 2877 members

General Questions

SilverStripe Forums » General Questions » Help with making my own function in Page_Controller

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
Go to End
Author Topic: 1526 Views
  • Rawbit
    Avatar
    Community Member
    52 Posts

    Help with making my own function in Page_Controller Link to this post

    I am using the DataObjectManager to manage some files per-page. Thus far I have done this:

    <% control DownloadItems %>
                            <li><a href="$Attachment.URL">$Title</a></li>
                         <% end_control %>

    Now what I need to do is create a function for this but I am stuck. I want to create the function so I can build one single url that contains these links that I will send to an external site for processing. I.e. the end result might be:
    <li><a href="http://example.com/process.php?downloads=http://mysilverstripewebsite.com/assets/Uploads/file1.pdf+http://mysilverstripewebsite.com/assets/Uploads/file2.pdf+http://mysilverstripewebsite.com/assets/Uploads/file3.pdf>Download</a></li>;

    I have been trying to build this link on a per-page level in Page_Controller.php here's what I have so far:

       function DownloadList()
       {
          $thisFiles = DataObject::get("DownloadItem", "PageID = 2"); // Pages at the root level only
          $output = "";
          return $thisFiles;
       }

    Two immediate problems. One, is that the '2' should be the id of the page they are on. Two, when I call $DownloadList in Page.ss it returns this:

    <li onclick="location.href = this.getElementsByTagName('a')[0].href"><a href="">File1</a></li>
    <li onclick="location.href = this.getElementsByTagName('a')[0].href"><a href="">File2</a></li>

    I don't know where all that <li> code is coming from but it seems this very close. It's just missing the link to the file, and of course the ability for me to customize how the output is returned.

    Please help!

  • Rawbit
    Avatar
    Community Member
    52 Posts

    Re: Help with making my own function in Page_Controller Link to this post

    I feel like i'm slowly making progress and getting closer:

    My full code now:

       function DownloadList()
       {
          $thisFiles = DataObject::get("DownloadItem", "PageID = 2");
          $output = "";
          $output = $this->DownloadBuilder($thisFiles);
          return $output;

       }
       
       private function DownloadBuilder($files) {
          $output = "";
          if(count($files))
          {
             $output = '
             <ul id="download-list">';
             foreach($files as $file)
             {
                $output .= '
                <li><a href="'.$file->URLSegment.'" title="Go to the '.Convert::raw2xml($file->Title).' file">'.Convert::raw2xml($file->Title).'</a>';
                $output .= '
                </li>';
             }
             $output .= '
             </ul>';
          }
          return $output;
       }

    I'm basing this off of:
    http://doc.silverstripe.com/doku.php?id=tutorial:site-map

    I really just need the '2' in $thisFiles = DataObject::get("DownloadItem", "PageID = 2") to become the id of the page [don't know how to do this]. Lastly, I need to get the url of the file. in Page.ss [as seen in post 1] I used to do $Attachment.URL

    ideas?

  • Rawbit
    Avatar
    Community Member
    52 Posts

    Re: Help with making my own function in Page_Controller Link to this post

    Once again simon_w on irc saves the day!

    SOLVED:

    function DownloadList()
    {
    return $this->DownloadBuilder($this->DownloadItems());
    }

    private function DownloadBuilder($downloadItems) {
    $output = "";
    if($downloadItems)
    {
    $output = '
    <ul id="download-list">';
    foreach($downloadItems as $downloadItem)
    {
    //get download url for this file.
    $file = $downloadItem->Attachment();

    $output .= '
    <li><a href="'.$file->Filename.'" title="Go to the '.Convert::raw2xml($downloadItem->Title).' downloadItem">'.Convert::raw2xml($downloadItem->Title).'</a>';
    $output .= '
    </li>';
    }
    $output .= '
    </ul>';
    }
    return $output;
    }

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