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