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.

Customising the CMS /

FormAction in CMS


Reply


21 Posts   6520 Views

Avatar
Carbon Crayon

Community Member, 598 Posts

3 January 2009 at 8:32am

Wow finally got this working, thanks UncleCheese! :D

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>

Avatar
UncleCheese

Forum Moderator, 4096 Posts

3 January 2009 at 8:41am

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.

Avatar
Carbon Crayon

Community Member, 598 Posts

3 January 2009 at 8:57am

"I can't believe this worked!"

hehe beautiful isnt it :D

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?

Avatar
UncleCheese

Forum Moderator, 4096 Posts

3 January 2009 at 9:24am

/sapphire/core/model/Image.php

Avatar
Carbon Crayon

Community Member, 598 Posts

3 January 2009 at 2:38pm

Edited: 03/01/2009 2:40pm

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?

Avatar
fridolin24

Community Member, 5 Posts

29 January 2009 at 3:08am

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.

Avatar
_Vince

Community Member, 162 Posts

9 February 2009 at 1:07pm

Couldn't you just say

Director::redirectBack();

?

Avatar
Carbon Crayon

Community Member, 598 Posts

9 February 2009 at 2:09pm

_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 :P 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?