7912 Posts in 1355 Topics by 930 members
DataObjectManager Module
SilverStripe Forums » DataObjectManager Module » Solved! Download section
Discuss the DataObjectManager module, and the related ImageGallery module.
Moderators: martimiz, UncleCheese, Howard, Sean, Ryan M., biapar, Willr, Ingo, swaiba, simon_w
| Go to End | Next > | |
| Author | Topic: | 1020 Views |
-
Solved! Download section

25 November 2010 at 9:22am
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/extentionsHow 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 -
Re: Solved! Download section

25 November 2010 at 10:30am
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 -
Re: Solved! Download section

25 November 2010 at 10:55am Last edited: 25 November 2010 10:58am
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 ;) )
-
Re: Solved! Download section

25 November 2010 at 11:25am
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'
-
Re: Solved! Download section

25 November 2010 at 6:46pm
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 -
Re: Solved! Download section

25 November 2010 at 7:12pm
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.
-
Re: Solved! Download section

25 November 2010 at 8:29pm
Whatever the name of your image relationship is. If you have:
$has_one = array (
'SomeImage' => 'Image'
);then <% control SomeImage %>
-
Re: Solved! Download section

25 November 2010 at 9:40pm
Thats just the point, i dont have has_one image! Im using file.
| 1020 Views | ||
| Go to Top | Next > |
