7913 Posts in 1355 Topics by 930 members
DataObjectManager Module
SilverStripe Forums » DataObjectManager Module » Image upload from with Thumbnail Image.
Discuss the DataObjectManager module, and the related ImageGallery module.
Moderators: martimiz, UncleCheese, Howard, Sean, Ryan M., biapar, Willr, Ingo, swaiba, simon_w
|
Page:
1
|
Go to End | |
| Author | Topic: | 793 Views |
-
Image upload from with Thumbnail Image.

28 May 2011 at 2:20pm
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;
}
} -
Re: Image upload from with Thumbnail Image.

31 May 2011 at 3:14pm Last edited: 31 May 2011 3:14pm
The reason you're not getting thumbnails is because you've casted the file relation as "File" rather than "Image", so you're not getting any of the functions contained in the Image class.
But there are some bigger problems going on with your datamodel. Your GetFiles() function is needlessly complex. Get rid of that all together.
Add a has_many to your ArticlePage:
static $has_many = array (
'FileAttachments' => 'FileAttach'
);And in your FileAttach class, you've put a foreign key in your DB array. That's no good. Use the ORM!!
FileAttach.php
$has_one = array (
'Attachment' => 'Image',
'Article' => 'ArticlePage'
);Then on your template, you just need to do:
<% control FileAttachments %>
<% end_control %>
Life is so much easier when you use the tools SS gives you!
---------------
Silverstripe tips, tutorials, screencasts, and more. http://www.leftandmain.com
| 793 Views | ||
|
Page:
1
|
Go to Top |

