Jump to:

7938 Posts in 1540 Topics by 945 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » Bulk uploader on the front end

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1 2 3 4
Go to End
Author Topic: 4609 Views
  • Howard
    Avatar
    Community Member
    215 Posts

    Bulk uploader on the front end Link to this post

    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?

  • UncleCheese
    Avatar
    4085 Posts

    Re: Bulk uploader on the front end Link to this post

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

  • Howard
    Avatar
    Community Member
    215 Posts

    Re: Bulk uploader on the front end Link to this post

    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

  • UncleCheese
    Avatar
    4085 Posts

    Re: Bulk uploader on the front end Link to this post

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

  • dcmagic
    Avatar
    Community Member
    2 Posts

    Re: Bulk uploader on the front end Link to this post

    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?

  • UncleCheese
    Avatar
    4085 Posts

    Re: Bulk uploader on the front end Link to this post

    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.

  • batata
    Avatar
    Community Member
    10 Posts

    Re: Bulk uploader on the front end Link to this post

    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

  • UncleCheese
    Avatar
    4085 Posts

    Re: Bulk uploader on the front end Link to this post

    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.

    4609 Views
Page: 1 2 3 4
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.