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

Solved! Download section


Go to End


15 Posts   2286 Views

Avatar
borriej

Community Member, 267 Posts

25 November 2010 at 9:22am

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

Avatar
UncleCheese

Forum Moderator, 4102 Posts

25 November 2010 at 10:30am

1) $File.Extension

2)

public function onBeforeWrite() {
parent::onBeforeWrite();
if(!$this->Title && $this->FileID) {
$this->Title = $this->File()->Filename;
}
if(!$this->Comment) {
$this->Comment = "Some comment";
}
}

3) $File.Size

4) <% control YourImageObject %>$SetWidth(130), $CroppedImage(50,50), etc..<% end_control %>

---------------
Silverstripe tips, tutorials, screencasts, and more. http://www.leftandmain.com

Avatar
borriej

Community Member, 267 Posts

25 November 2010 at 10:55am

Edited: 25/11/2010 10:58am

Dude! thx it was quite easy.

But the setwidth isn't working.

								<% control File %>
									<a href="$URL" target="_blank">
										<% control YourImageObject %>
											$SetWidth(130)
										<% end_control %> 
									</a>
								<% end_control %>

Any ID?

(oh btw the thumbnail isnt showing in the browser ;) )

Avatar
borriej

Community Member, 267 Posts

25 November 2010 at 11:25am

Oh one more question:

When using your script with 'Filename' i get the path+file name.

For instance:

assets/Public_Downloads/Backgrounds/filename.jpg

and i would like to only have 'filename.jpg'

Avatar
UncleCheese

Forum Moderator, 4102 Posts

25 November 2010 at 6:46pm

Do you really have a relation called "YourImageObject" ?? I hope you didn't take that literally.

---------------
Silverstripe tips, tutorials, screencasts, and more. http://www.leftandmain.com

Avatar
borriej

Community Member, 267 Posts

25 November 2010 at 7:12pm

Ok, srry for asking, but what should it be?

Jusr control file?

Or something like control download?
Or control image?

Dont see the logic yet.

Avatar
UncleCheese

Forum Moderator, 4102 Posts

25 November 2010 at 8:29pm

Whatever the name of your image relationship is. If you have:

$has_one = array (
'SomeImage' => 'Image'
);

then <% control SomeImage %>

Avatar
borriej

Community Member, 267 Posts

25 November 2010 at 9:40pm

Thats just the point, i dont have has_one image! Im using file.

Go to Top