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

Bulk uploader on the front end


Go to End


29 Posts   8802 Views

Avatar
Howard

Community Member, 215 Posts

24 May 2009 at 2:36pm

Hi guys,

I have a page using the ImageDataObjectManager. This works really well and now I need to extend it to having front end uploading of images.

I envision something similar to the old bulk uploader module using swfuploader and stuff but I don't know how to create dataobjects with the uploaded images and associate them to that page, I also wonder what the best way is to add a title and description.

Ideally i would like it if the same popup that appears in the CMS stepping you through each step could appear on the front end when they click a button.

What are your thoughts on implementing something like this?

Avatar
UncleCheese

Forum Moderator, 4102 Posts

24 May 2009 at 3:16pm

Yeah, I do this all the time. Just create a form with a SWFUploadField. There's a tutorial in the SWFUploadField docs.

Avatar
Howard

Community Member, 215 Posts

1 June 2009 at 1:34am

Edited: 01/06/2009 1:36am

Hi UncleCheese,
<br >
<br >I have a had a go at doing this myself buy appeared to have failed :( - I've pasted my non-working code here http://php.pastebin.com/m4a747a6c and wonder if you would possibly have time to look over it. I kinda got messed up so not sure what it's finally trying to do - the error is a javascript alert saying "There was a error - file was rejected by server"
<br >
<br >The first function should handle the swf upload files, save them to a folder 'Images' and resize them to 10*10.
<br >
<br >The second function will hopefully attach the uploaded files as data objects to the page from which they were uploaded so that they can be displayed in the ImageGallery.
<br >
<br >Thanks for any advice

Avatar
UncleCheese

Forum Moderator, 4102 Posts

1 June 2009 at 4:37am

Crap, that code is dated. Uploads are handled differently in 2.3. Yours would work great in 2.2! :-)

Here's a working example of a resume upload form I'm using on a site right now:

class EmploymentPage_Controller extends Page_Controller
{
	public function ResumeForm()
	{
		return new Form(
			$this,
			"ResumeForm",
			new FieldSet(
				new TextField('Name','<span>*</span> Name'),
				new TextField('Address'),
				new TextField('City'),
				new TextField('State'),
				new NumericField('Zip'),
				new EmailField('Email','<span>*</span> Email'),
				new TextField('Phone','<span>*</span> Phone'),
				new TextareaField('Message','<span>*</span> Please use the space below to let us know why you are interested in employment','6','40'),
				new SWFUploadField(
					"ResumeForm",
					"Resume",
					"<span>*</span> Please attach your most current resume and cover letter (PDF or DOC only).",
					array (
						'file_types_list' => '*.doc;*.pdf',
						'file_queue_limit' => '1',
						'browse_button_text' => 'Choose file...',
						'upload_url' => $this->Link('handleswfupload'),
						'required' => 'true'
					)
				)
			),
			new FieldSet(
				new FormAction('doResumeSubmit','Submit Application')
			),
			new RequiredFields('Name','Email','Phone','Message')
		);
	}
	
	public function handleswfupload()
	{
		if (isset($_FILES["swfupload_file"]) && is_uploaded_file($_FILES["swfupload_file"]["tmp_name"])) {
			$file = new File();
			$u = new Upload();
			$u->loadIntoFile($_FILES['swfupload_file'], $file, "Resumes");
			$file->write();			
			echo $file->ID;
		} 
		else {
			echo ' '; // return something or SWFUpload won't fire uploadSuccess
		}
		
	}
	
	public function doResumeSubmit($data,$form)
	{
		$data = $form->getData();
		$to = "myemail@address.com";
		$subject = "New Employment Application";
		$from = $data['Email'] ? $data['Email'] : "webmaster@mysite.org";
		
		$email = new Email();
		$email->ss_template = "EmploymentEmail";
		$email->populateTemplate($data);
		$email->subject = $subject;
		$email->to = $to;
		$email->from = $from;
		if($_POST['uploaded_files']) {
			$file = DataObject::get_by_id("File",$_POST['uploaded_files'][0]);
			$email->attachFile(Director::baseFolder() . "/" . $file->Filename, basename($file->Filename));
		}
		$email->send();
		
		Director::redirect($this->Link('success'));
	}
	
}


Avatar
dcmagic

Community Member, 2 Posts

10 June 2009 at 4:28pm

UncleCheese,
This works great, but I'm having a issue. I posted your code exactly as it is displayed above. Everything shows up except the upload button. In my template all I do is show resume form.

Any ideas on what would prevent the upload button from being displayed?

Avatar
UncleCheese

Forum Moderator, 4102 Posts

11 June 2009 at 1:22am

Do you have 'requred' => true set on that field? I believe the effect of that is that it hides the upload button until you select a file. If that's not the case, check for javascript errors.

Avatar
batata

Community Member, 10 Posts

11 June 2009 at 2:33am

HI,

I have simillar problem, I would like to add some files to a page via frontend. I have a working form with swfupload field (files are uploaded but not attached to a page) and I am able to create new dataobject however without files attached to it. Page has has_many relation with filedataobject manager and I can attach files onlyvia cms. Here is my code:

Front end form

http://php.pastebin.com/m434a8a91

Page where files should be attached

http://php.pastebin.com/m6d3c71fc

DataObject code:

http://php.pastebin.com/m4a308325

thanks in advance

MrBookT

Avatar
UncleCheese

Forum Moderator, 4102 Posts

11 June 2009 at 2:59am

What you're trying to do is a little more complicated. It's hard to give you any guidance without seeing the interface, but I think what you want is to create the DataObject in the handleswfupload() function, create the File object, load the $_FILES data into the File object, then link the File object and the DataObject. You can also at that point link the DataObject to whatever page it is on. If the page is dynamic, you'll have to pass the current page ID to the handleswfupload() function using SWFUploadConfig::addPostParams() function.

Go to Top