Hi,
I'm trying to build a form to allow users to attach a file to page. This more of less works but I can't get it to create the thumbnail image afterwards.
Can someone please tell why?
ArticlePage.php
class ArticlePage extends Page {
static $icon = "themes/tutorial/images/treeicons/news";
static $defaults = array(
'ProvideComments' => true
);
static $db = array(
'Date' => 'Date',
'Author' => 'Text'
);
static $has_one = array(
);
function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.Content.Main', $dateField = new DateField('Date','Article Date (for example: 20/12/2010)'), 'Content');
$dateField->setConfig('showcalendar', true);
$dateField->setConfig('dateformat', 'dd/MM/YYYY');
$fields->addFieldToTab('Root.Content.Main', new TextField('Author','Author Name'), 'Content');
return $fields;
}
}
class ArticlePage_Controller extends Page_Controller {
public function ApplyForm() {
$form = new Form (
$this,
"ApplyForm",
new FieldSet (
new TextField('ViewTitle', 'Title'),
new TextareaField('ViewDescription', 'Description'),
new HiddenField('ArticleTitle', _t('FileAttach.ARTICLETITLE'), $this->Title ),
new HiddenField('ArticleID', _t('FileAttach.ARTICLEID'), $this->ID ),
$file = new FileUploadField('Attachment', _t('FileAttach.ATTACHMENT','Upload a File'))
),
new FieldSet (
new FormAction('doApply', _t('FileAttach.ATTACHMENT','Add File'))
),
new RequiredFields('ViewTitle')
);
$file->setFileTypes(array(
'pdf',
'jpg'
));
return $form;
}
public function doApply($data, $form) {
$form->saveInto($application = new FileAttach());
$application->write();
return array (
'Application' => $application
);
}
function GetFiles() {
$id = $this->ID;
$obj = "FileAttach";
$filter = "ArticleID = $id";
$sort = "";
$join = "LEFT JOIN File ON (File.ID = FileAttach.AttachmentID)";
$limit = "";
$data = DataObject::get($obj, $filter, $sort, $join, $limit);
return $data;
}
}
ArticlePage.ss
<% include Menu2 %>
<div id="Content" class="typography">
<% include Breadcrumbs %>
<h1>$Title</h1>
$Content
<div class="newsDetails">
PAGE ID $ID Posted on $Date.Nice by $Author
$ApplyForm
<div>
<% control GetFiles %>
<div>$ID $ViewTitle $ViewDescription</div>
$Attachment.CMSThumbnail
<% end_control %>
</div>
</div>
</div>
FileAttach.php
class FileAttach extends DataObject {
static $db = array (
'ViewTitle' => 'Text',
'ViewDescription' => 'Text',
'ArticleTitle' => 'Text',
'ArticleID' => 'Varchar',
);
static $has_one = array (
'Attachment' => 'File'
);
public function getCMSFields_forPopup() {
return new FieldSet(
new TextField('ViewTitle', 'Title'),
new TextareaField('ViewDescription', 'Description'),
new ImageField('File', 'File')
);
}
public function getCMSFields() {
$f = parent::getCMSFields();
$manager = new ImageDataObjectManager(
$this, // Controller
'Attachment', // Source name
'File', // Source class
'ApplyForm', // File name on DataObject
array(
'Title' => 'Title',
'Caption' => 'Caption'
), // Headings
'getCMSFields_forPopup' // Detail fields
// Filter clause
// Sort clause
// Join clause
);
$f->addFieldToTab("Root.Content.Gallery",$manager);
return $f;
}
}