Jump to:

10849 Posts in 2516 Topics by 1792 members

All other Modules

SilverStripe Forums » All other Modules » [solved] Multiform email only sending data from last step

Discuss all other Modules here.

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

Page: 1
Go to End
Author Topic: 1005 Views
  • neilos
    Avatar
    Community Member
    19 Posts

    [solved] Multiform email only sending data from last step Link to this post

    Hi all,

    I have been working on this for some days but can't figure it out on my own.

    Using the multiform module, I have 5 steps and the end results are to be emailed in one big email. The trouble I am having is all the data from setps 1,2,3 and 4 are being stored in the databse but its not included in the email, only the data from the last step is.

    Here is the code;

    <?php

    class APMultiForm extends MultiForm {

    public static $start_step = 'APPersonalDetailsFormStep';

    public function finish($data, $form) {
    parent::finish($data, $form);
    $steps = DataObject::get('MultiFormStep', "SessionID = {$this->session->ID}");
       
       
          $From = "website";
          $To = "email";
          $Subject = "Subject";    
          $email = new Email($From, $To, $Subject);
          $email->setTemplate('AuPairEmail');
          $email->populateTemplate($data);
          $email->send();
          Director::redirect(Director::baseURL(). $this->URLSegment . "terms-and-conditions");
    }

    }

    If I change any of the steps to be the last step, then the data from that step is emailed fine.

  • Willr
    Avatar
    Forum Moderator
    5464 Posts

    Re: [solved] Multiform email only sending data from last step Link to this post

    From reading the documentation bundled with multiform (https://github.com/silverstripe/silverstripe-multiform) you may have to pull the data from each step manually. See the code snippet on that page...

    $steps = DataObject::get('MultiFormStep', "SessionID = {$this->session->ID}");
    if($steps) {
    foreach($steps as $step) {
    $data = $step->loadData();
    }
    }

  • neilos
    Avatar
    Community Member
    19 Posts

    Re: [solved] Multiform email only sending data from last step Link to this post

    Thanks for looking at this,

    I have followed that tutorial to get the basic form working and that snippet has been included.
    But I still cant get all the data into the email.
    Here is the dump from the debug;

    Debug (APMultiForm->finish() in line 14 of APMultiForm.php)

    * FirstName =

    q

    * Surname =

    * Address =

    ...

    Debug (APMultiForm->finish() in line 14 of APMultiForm.php)

    * ChildName =

    q

    * ChildAge =

    * ChildActivity =

    * Baby =

    0

    ...

    Debug (APMultiForm->finish() in line 14 of APMultiForm.php)

    * HouseType =

    Detached

    * Bedroom =

    * Bathroom =

    * CinemaDistance =

    q

    ...
    Debug (APMultiForm->finish() in line 14 of APMultiForm.php)

    * DateStart =

    q

    * Duration =

    * APNat =

    * action_finish =

    Submit


    The data is being pulled from the steps, but I can't figure out how to get all steps data included in the email.

    Is it someting to do with

    $email->populateTemplate($data);


    should it be something like

    $email->populateTemplate($step->loadData());


    I have tried that and it wasn't the solution.

    Any ideas?

  • neilos
    Avatar
    Community Member
    19 Posts

    Re: [solved] Multiform email only sending data from last step Link to this post

    I have found a solution, it might not be elegant but it achives what i need.

    <?php

    class APMultiForm extends MultiForm {

    public static $start_step = 'APStepOne';

    public function finish($data, $form) {
    parent::finish($data, $form);
    $steps = DataObject::get('MultiFormStep', "SessionID = {$this->session->ID}");
       
       
        if($steps) {
    foreach($steps as $step) {
           if($step->class == 'APStepOne') {
             $dataone = $step->loadData(); }
             if($step->class == 'APStepTwo') {
             $datatwo = $step->loadData(); }
             if($step->class == 'APStepThree') {
             $datathree = $step->loadData(); }
             if($step->class == 'APStepFour') {
             $datafour = $step->loadData(); }
             if($step->class == 'APStepFive') {
             $datafive = $step->loadData(); }
             //Debug::show($step->loadData());
    }
    }
       
          $From = "xxx@xxx.com";
          $To = "xxx@xxx.com";
          $Subject = "xxxsubjectxxx";    
          $email = new Email($From, $To, $Subject);
          $email->setTemplate('xxxtemplatexxx');
          $email->populateTemplate($dataone);
          $email->populateTemplate($datatwo);
          $email->populateTemplate($datathree);
          $email->populateTemplate($datafour);
          $email->populateTemplate($datafive);
          $email->send();
          Director::redirect(Director::baseURL(). $this->URLSegment . "terms-and-conditions");
    }

    }

  • Willr
    Avatar
    Forum Moderator
    5464 Posts

    Re: [solved] Multiform email only sending data from last step Link to this post

    Why so many arrays? You could have 1 array then use array_merge to join the two (first thing that came to my head)

    $output = array();
    foreach($steps as $step) {
    $output = array_merge($output, $step->loadData());
    }

    ..
    $email->populateTemplate($output);

    I'm surprised that there isn't a method on MultiForm to do this automatically? Have you looked at the code?

    1005 Views
Page: 1
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.