Jump to:

5513 Posts in 1732 Topics by 1218 members

Customising the CMS

SilverStripe Forums » Customising the CMS » FormAction in CMS

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

Page: 1 2 3
Go to End
Author Topic: 6093 Views
  • Aram
    Avatar
    Community Member
    598 Posts

    Re: FormAction in CMS Link to this post

    Wow finally got this working, thanks UncleCheese!

    yes that getCMSActions works perfectly, and had I not got the other way working I would probably use that, the only thing is it's a bit close to the save and publish button and as the EndSeason function deletes all the fixtures from the current season and is only going to be used once a year I would rather hide it away in another tab.

    so in order to get it to pass the ID in the iframe URL i had to create a new SeasonField.php class which returned the iframe HTML just like the other iframe fields. I copied the code from the ImageField.php as you suggested and made it an extention of FormField instead of FileField and got it to return the correct URl as for some reason if i put {$this->ID} into the LiteralField call it just passed the literal URL of 'EndSeasonController/iframe/{$this->ID}'.

    Anyway both methods work. Thanks again, this was a real tough one for me and I wouldnt have managed it without your help

    below is my code for anyone that is interested:

    mysite/code/TeamPage.php

    function getCMSFields() {
          $fields = parent::getCMSFields();
          $fields->addFieldToTab('Root.Content.Seasons', new SeasonField('EndSeason', 'Finsih the current Season'), 'Content');
    return $fields;   
       }

    mysite/code/SeasonField.php

    class SeasonField extends FormField {

       public function Field($id = null) {
          
          $data = $this->form->getRecord();
          
          if($id && is_numeric($id)) {
             $parentID = $id;
          } elseif($data) {
             $parentID = $data->ID;
          } else {
             $parentID = null;
          }

             $iframe = "<iframe name=\"{$this->name}_iframe\" src=\"EndSeasonController/iframe/$parentID/\" style=\"height: 82px; width: 425px; border: none;\" frameborder=\"0\"></iframe>";

             return $iframe;

       }

    }


    mysite/code/EndSeasoncontroller.php

    class EndSeasonController extends Controller
    {
    static $allowed_actions = array ('iframe');
          
    function EndSeasonForm()
    {
       $params = Director::urlParams();
    return new Form(
    $this,
    "EndSeasonForm",
    new FieldSet( new HiddenField('ID', 'Page ID',$params['ID'])),
    new FieldSet( new FormAction('EndSeason', 'End the Season'))
    );
    }
       
       function Link(){
          
       }

    function EndSeason($data, $form){

    //My end season procedure

    }   
       
          
    }

    themes/mySite/templates/EndSeasonController_iframe.ss

    <!DOCTYPE [insert full doctype]>
    <body>
    $EndSeasonForm
    </body>
    </html>

  • UncleCheese
    Avatar
    4085 Posts

    Re: FormAction in CMS Link to this post

    Wow. I can't believe this worked! Good for you. The Link() function is useful, as you'll see in ImageField_Uploader, for having the controller "remember" where to go, e.g., keep it within the iframe action.

  • Aram
    Avatar
    Community Member
    598 Posts

    Re: FormAction in CMS Link to this post

    "I can't believe this worked!"

    hehe beautiful isnt it

    Honestly I can't believe it works either because I still don't think I even fully understand how I did it!

    I can't for the life of me find the ImageField_Uploader, where should I be looking?

  • UncleCheese
    Avatar
    4085 Posts

    Re: FormAction in CMS Link to this post

    /sapphire/core/model/Image.php

  • Aram
    Avatar
    Community Member
    598 Posts

    Re: FormAction in CMS Link to this post

    OK, one last question about this ;)

    I would like the new Season object to show up in the HasManyComplexTableField below after the action has run without having to refresh the page manually.

    I tried adding this to my EndSeasonController_iframe.ss:

    <script type="text/javascript">   

             window.onload = function(){
       
              self.parent.location.reload(true);
       
             };
    </script>

    but it just refreshes the page infinately, I guess because it is reloading the page which is reloading the iframe which then loads the page again etc. etc.

    is there a way to refresh just the HasManyComplexTableField? or another way?

  • fridolin24
    Avatar
    Community Member
    5 Posts

    Re: FormAction in CMS Link to this post

    Hello,

    I wanted to reload my page after using a FormAction. So I added the folowing code to my Controller:

    class LoadDataController extends Controller {
    static $allowed_action = array('iframe');

    function LoadDataForm() {
          $params = Director::urlParams();
          $form = new Form( $this,"LoadDataForm", new FieldSet(new HiddenField('ID', 'Page ID', $params['ID'])),new FieldSet( new FormAction('LoadData', 'Daten laden')));
    return $form;
    }
    function Link() {}

    function LoadData($data, $form) {
       $id = $data["ID"];
       $baustelle = DataObject::get_by_id("BaustellePage",$id);
       if($baustelle) {
          $baustelle->Objekt = "Objekt2"; // sets property on object
          $baustelle->Anschrift = "Anschrift2";
          $baustelle->Bauschein = "Bauschein2";
          //$baustelle->Bauschein = "Test3";
          //$baustelle->write(true,false,true,true); // writes row to database
          $baustelle->write();
       }
       ?>
       <html>
       <head>
          <script type="text/javascript">
           function refresh() {
              self.parent.location.reload(true);
              }
          </script>
       </head>
       <body onload="refresh()" >
          <p>Daten werden geladen ...</p>
       </body>
       </html>
       <?
    }
    }
    ?>

    This reloads my page.

  • _Vince
    Avatar
    Community Member
    158 Posts

    Re: FormAction in CMS Link to this post

    Couldn't you just say

    Director::redirectBack();

    ?

  • Aram
    Avatar
    Community Member
    598 Posts

    Re: FormAction in CMS Link to this post

    _Vince: The form is in an Iframe so calling Director::redirectBack(); (which I do) just refreshes the Iframe contents, not the parent page which has the table field on it.

    fridolin24: This does refresh the page.........over and over and over infinately Unfortunatly because the form is in an Iframe when you refresh the page like this is also refreshes the iframe which then refreshes the page and so on. unless I missed some code in there?

    6093 Views
Page: 1 2 3
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.