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.

General Questions /

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

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

Help with making my own function in Page_Controller


Go to End


3 Posts   2249 Views

Avatar
Rawbit

Community Member, 52 Posts

31 March 2009 at 3:38am

Edited: 31/03/2009 6:28am

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!

Avatar
Rawbit

Community Member, 52 Posts

31 March 2009 at 6:39am

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?

Avatar
Rawbit

Community Member, 52 Posts

31 March 2009 at 7:44am

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;
}