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.

Archive /

Our old forums are still available as a read-only archive.

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

Multiform - advice needed for email attachments and redirection to success message


Go to End


4 Posts   3511 Views

Avatar
streetdaddy

32 Posts

6 November 2008 at 2:57am

I've setup a multiple step form process using the fantastic Multiform module. I've managed to work most of it out via the tutorial, but I'm a bit stuck with a few of the final steps.

So right now the user can go right through to the end of process, then 'Send order' for the final step. At this point I've got it so it will construct an HTML email with the data, but there's a few bits I'm not sure how to do:

1. Redirect the user to a page with a Success message
2. Attach a file to the email that has been uploaded via a FileField

Here is the code for the MultiForm subclass:

(FYI the form is online at http://dcsd.milestillinger.com/orders, but code may have changed by the time you look. With any luck, it'll be working...)

<?php
 
class BoardOrderMultiForm extends MultiForm {

    protected static $start_step = 'BoardOrderAcceptTermsStep';   
    
    public function finish($data, $form) {
        parent::finish($data, $form);
        
        $body = "<h1>Online Order</h1>";
        
        $steps = DataObject::get('MultiFormStep', "SessionID = {$this->session->ID}");
        if($steps) {
            foreach($steps as $step) {
                
                $data = $step->loadData();
                
                if($step->class == 'BoardOrderAcceptTermsStep') {
                    $data = $step->loadData();
                    if($data) {
                    
                    }
                }

                if($step->class == 'BoardOrderShaping1Step') {
                    $data = $step->loadData();
                    if($data) {
                        $basic_type = $data['basic_type'];
                        $body .= "<h2>Shaping 1</h2>";
                        $body .= "<table style=\"width:500px;\"><tr>";
                        $body .= "<th>Basic type</th><td>" . $data['basic_type'] . "</td>";                        
                        $body .= "</tr><tr>";
                        $body .= "<th>Length</th><td>" . $data['length'] . "</td>";
                        $body .= "</tr><tr>";
                        $body .= "<th>Width</th><td>" . $data['width'] . "</td>";
                        $body .= "</tr><tr>";
                        $body .= "<th>Thickness</th><td>" . $data['thickness'] . "</td>";
                        $body .= "</tr></table>";
                    }
                }

                if($step->class == 'BoardOrderShaping2Step') {
                    $data = $step->loadData();
                    if($data) {
                        $body .= "<h2>Shaping 2</h2>";
                        $body .= "<table><tr>";
                        $body .= "<th>Rails type</th><td>" . $data['rails_type'] . "</td>";                    
                        $body .= "</tr><tr>";
                        $body .= "<th>Bottom shape</th><td>" . $data['bottom_shape'] . "</td>";                    
                        $body .= "</tr><tr>";
                        $body .= "<th>Bottom shape (other)</th><td>" . $data['bottom_shape_other'] . "</td>";                    
                        $body .= "</tr><tr>";
                        $body .= "<th>Tail shape</th><td>" . $data['tail_shape'] . "</td>";                    
                        $body .= "</tr><tr>";
                        $body .= "<th>Tail shape (other)</th><td>" . $data['tail_shape_other'] . "</td>";                    
                        $body .= "</tr><tr>";
                        $body .= "</dl>";
                    }
                }
                
                if($step->class == 'BoardOrderFinal') {
                    $data = $step->loadData();
                    if($data) {
                    
                    }
                }
         
                Debug::show($step->loadData()); // Shows the step data (unserialized by loadData)
            }
        }
        
        // Get email address from page or send to admin
        $sendto = ($this->ContactEmail!='' ? $this->ContactEmail : Email::getAdminEmail());

        // Send email to Item Contact
        $email = new Email('blah@gmail.com', 'blah@gmail.com', "Website Order: " . $basic_type, $body);
        $email->send();
        
        //Director::redirect("orders");
        
        //$response = array("Success" => "Success");

        // Send email to User who enquired
        //$email = new Email('blah@gmail.com', $data['email'], 'Receipt of website enquiry from Chapman Surfboards: '.$data['Title'], "Thankyou for your enquiry.\n\n".$body);
        //$email->sendPlain();
        
        //$this->customise($response)->renderWith(array('Page', 'Page'));

    }
   
}
 
?>

And here's the output of the Debug once the order has been sent:

Debug (BoardOrderMultiForm->finish() in line 68 of BoardOrderMultiForm.php)

    * terms_accepted =

      1

Debug (BoardOrderMultiForm->finish() in line 68 of BoardOrderMultiForm.php)

    * basic_type =

      shortboard

    * length =

      6'1"

    * width =

      18 0"

    * thickness =

      2 1/2"

Debug (BoardOrderMultiForm->finish() in line 68 of BoardOrderMultiForm.php)

    * rails_type =

      mid

    * bottom_shape =

      single_double

    * bottom_shape_other =

    * tail_shape =

      Round tail

    * tail_shape_other =

Debug (BoardOrderMultiForm->finish() in line 68 of BoardOrderMultiForm.php)

    * glassing =

      Strong (6-6 x 4)

    * fins =

      M3

    * fins_other =

    * finish =

      Sanded Polish

    * decals_colour =

      Blue & Black

    * spray_info =

    * MAX_FILE_SIZE =

      31457280

    * spray =
          o name =

            phil-roberts.jpg

          o type =

            image/jpeg

          o tmp_name =

            /tmp/php2yJINC

          o error =

            0

          o size =

            1262144

Debug (BoardOrderMultiForm->finish() in line 68 of BoardOrderMultiForm.php)

    * extra_shaping =

      Single wing

    * channels =

      4

    * extra_info =

      asdfasdf

Debug (BoardOrderMultiForm->finish() in line 68 of BoardOrderMultiForm.php)

    * rocker =

      Average

    * nose_rocker =

    * tail_rocker =

    * nose_width =

      Leave to shaper

    * tail_width =

      Leave to shaper

Debug (BoardOrderMultiForm->finish() in line 68 of BoardOrderMultiForm.php)

    * name =

      Testing by Miles

    * email =

      blah@mailinator.com

    * home_phone =

      234234

    * mobile =

      234234

    * street_address =

      asdfasdf

    * suburb =

      sdfasdf

    * postcode =

      23422

    * country =

      AU

    * height =

    * weight =

    * pickup_date =
          o date =

            17

          o month =

            10

          o year =

            2008

Debug (BoardOrderMultiForm->finish() in line 68 of BoardOrderMultiForm.php)

Avatar
streetdaddy

32 Posts

6 November 2008 at 2:59am

forum killed the indents, so I put it here: http://pastie.org/307918

Avatar
streetdaddy

32 Posts

6 November 2008 at 3:32am

I've gotten a little bit closer to the attachement with

 $email->attachFile($spray_file);

$spray_file is the tmp_name value from the FileField:

spray =

    * name =
      phil-roberts.jpg

    * type =
      image/jpeg

    * tmp_name =
      /tmp/php2yJINC

    * error =
      0

    * size =
      1262144

But if I look on the server, I can't find /tmp/php2yJINC, or phil-roberts.jpg in /tmp or /var/tmp. Its a shared host using hsphere, could the tmp file be buried somewhere else?

The email comes with an attachment 'noname' with size 0.

Avatar
Ingo

Forum Moderator, 801 Posts

12 November 2008 at 11:47am

Have a look at: http://de2.php.net/features.file-upload

"The file will be deleted from the temporary directory at the end of the request if it has not been moved away or renamed."

This means that you'll have to move the file *before* finishing the step, by overloading MultiFormStep->saveData() and the Upload() class. If you can already create a valid record after the specific step, use MultiFormStep->saveInto(). With both solutions you should be aware that the files are stored permanently regardless if the user actually finishes the form, so you'll need to take care of any record/file cleanup yourself (via a cron-job).