Source 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(
'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 files'));
return $fields;
}
public function onBeforeWrite() {
parent::onBeforeWrite();
if(!$this->Title && $this->FileID) {
$this->Title = $this->File()->Name;
}
if(!$this->Comment) {
$this->Comment = "None";
}
}
}