Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

Moderators: martimiz, UncleCheese, Sean, Ed, biapar, Willr, Ingo, swaiba

Image upload from with Thumbnail Image.


Go to End


2 Posts   1700 Views

Avatar
rog

Community Member, 8 Posts

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;
	}
}

Avatar
UncleCheese

Forum Moderator, 4102 Posts

31 May 2011 at 3:14pm

Edited: 31/05/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