Jump to:

5542 Posts in 1739 Topics by 1225 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Three Step Form in CMS (Customising LeftAndMain)

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

Page: 1
Go to End
Author Topic: 1162 Views
  • swaiba
    Avatar
    Forum Moderator
    1792 Posts

    Three Step Form in CMS (Customising LeftAndMain) Link to this post

    Hi,

    I'd really appreciate some help with this... I have had a terrible time getting a rather simple concept to work... I wanted to create a custom LeftAndMain : on 'left' two buttons to display a form - on 'right' blank, then the created form. When the form is submitted the it goes to a confirmation screen (similar to the form, but required some extra data), then upon confirmation it sends some emails.

    At the moment my solution works, but it is not based around silverstripe principles I am sure!

    One HUGE problem with my solution is that the form returned via ajax and placed into the right div with "document.getElementById('formTarget').innerHTML=result" means there is no javascript validation for the form! I have handled that myself in the submit function, place the data into session and redirect back... I'd really like this to work without me having to keep all this validation code for required fields.

    Apart from this I am sure I have done it all in a bad way... so please post a reply to help me get this cleaned up!

    mysite/javascript/send_email.js

    function doAJAX(strType) {
       try{
          jQuery.ajax({
             type: "POST",
             url: "SendEamilLeftAndMain/get_from",
             data: 'type='+strType,
             success: function(result){
                //alert('success')
                document.getElementById('formTarget').innerHTML=result
             },
             failure : function(){
                alert('epic fail')
             }
          });
       }
       catch(ex)
       {
          alert(ex)
       }
    }

    mysite\code\SendEamilLeftAndMain.php

    class SendEamilLeftAndMain extends LeftAndMain
    {

       static $url_segment = 'SendEmail';
       static $url_rule = '$Action/$ID';
       static $menu_title = 'Send Email';
       static $menu_priority = -2;

       public function init()
       {
          parent::init();

          Requirements::javascript("mysite/javascript/send_email.js");
       }

       static $allowed_actions = array(
          'get_from',
          'DoEmailSend',
          'DoEmailConfirm',
       );

       public function get_from()
       {
          Session::clear('SentEmailData');
          Session::clear('FormInfo.SentEmailData');

          $form = $this->SendEmailForm($_POST['type']);
          $response = new SS_HTTPResponse($form->forTemplate());

          return $response;
       }

       public function SendEmailForm($srtType='AllMembersByType')
       {
          $doSentEmail = new SentEmail();
          $fields = $doSentEmail->getCMSFields();

          $fs = new FieldSet();
          $ts = $fields->findOrMakeTab('Root.Main');
          $fields = $ts->Fields();

          //*snip*
          //form cutomisation here

          $actions = new FieldSet(
             new FormAction('DoEmailConfirm', 'Submit')
          );

          $form = new Form($this, 'DoEmailConfirm', $fields, $actions);

          $data = Session::Get('SentEmailData');
          if ($data)
          {
             $vars = $data->postVars();
             $form->loadDataFrom($vars);
          }
          $form->setupFormErrors();

          return $form;
       }

       public function ConfirmEmailForm()
       {
          $data = Session::Get('SentEmailData');
          $vars = $data->postVars();

          $formSentEmail = $this->SendEmailForm();

          $formSentEmail->loadDataFrom($vars);
          $fields = $formSentEmail->Fields();

          //*snip*
          //form cutomisation here

          $actions = new FieldSet(
             new FormAction('DoEmailSend', 'Submit')
          );

          $formSentEmail->setActions($actions);

          return $formSentEmail;
       }

       function DoEmailConfirm($data)
       {
          $bRet = true;

          $strURL = Director::baseURL().'admin/'.$this->stat('url_segment');

          $form = new Form($this, 'DoEmailConfirm',new FieldSet(),new FieldSet());

          $vars = $data->postVars();
          if (empty($vars['Name']))
          {
             $form->addErrorMessage('Name','Please enter the name','required');
             $bRet = false;
          }

          //*snip*
          //more validations occur here...

          if (empty($vars['EventID']))
          {
             $form->addErrorMessage('EventID','Please select an event','required');
             $bRet = false;
          }

          if (!$bRet)
          {
             Session::Set('SentEmailData',$data);
             return Director::redirect($strURL. "/?error=1");
          }

          Session::Set('SentEmailData',$data);

          Director::redirect($strURL. "/?confirm=1");
       }

       function DoEmailSend($data)
       {
          $email = new Email('my@email.com', 'my@email.com', 'test');
          $email->body = str_replace("\r",'<BR>',$data['Body']);
          $email->plaintext_body = $data['Body'];
          //$email->send();

          $strURL = Director::baseURL().'admin/'.$this->stat('url_segment');

          Director::redirect($strURL. "/?success=1");
       }

       function PostedSuccess()
       {
          if (isset($_GET['success']))
           return 1;

          return 0;
       }

       function PostedError()
       {
          if (isset($_GET['error']))
           return 1;

          return 0;
       }

       function PostedtoConfirm()
       {
          if (isset($_GET['confirm']))
           return $_GET['confirm'];

          return 0;
       }

    }

    mysite\templates\Layout\SendEamilLeftAndMain_left.ss

    Send message to all members about event...
    <form>
       <fieldset>
          <p class="Actions">
             <input class=action type=button value="Members" onclick="doAJAX('AllMembersByType');">
          </p>
       </fieldset>
    </form>

    Send message to event attendees...
    <form>
       <fieldset>
          <p class="Actions">
             <input class=action type=button value="Attendees" onclick="doAJAX('EventAttendees');">
          </p>
       </fieldset>
    </form>

    mysite\templates\Layout\SendEamilLeftAndMain_right.ss

    <div id="formTarget" style="width:100%;height:100%;overflow:auto;">

       <% if PostedSuccess = 1 %>
          sent
       <% else %>
          <% if PostedtoConfirm = 1 %>
             $ConfirmEmailForm
          <% else %>
             <% if PostedError = 1 %>
                $SendEmailForm
             <% else %>
                Please choose from the options on the left
             <% end_if %>
          <% end_if %>
       <% end_if %>
    </div>

    1162 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.