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.

Form Questions

Change the status of a user


Reply

5 Posts   565 Views

Avatar
cumquat

18 January 2013 at 12:15am Community Member, 198 Posts

Ok i apologise first, i'm having a really really bad week and my brain is fried. This maybe a simple request but i just cant see it any-more.
I have a table of users displayed on the front end, just lists their name the company name and their status.
The status can be either 1 or 2 and on the same row as their details i want to put a button that if their status is 1 when it's clicked it changes the status to 2 if it's clicked again it changes it to 1.

That's it and I cant think of the best way to do it.... is this gonna be a form or is there another way of doing this, which is best?

Hope you can help

Mick

Avatar
cumquat

18 January 2013 at 12:47am Community Member, 198 Posts

Ok i have moved forward, maybe...

I believe this is the code i need but for the life of me i cant work out how to call this function from a button click.

public function Activate() {
   $memberid = $this->owner->ID;
   $member = Member::get()->byID($memberid);
      if ($member) {
         $member->Status = "2";
         $member->write();
      }
   
   }

Avatar
cumquat

18 January 2013 at 3:52am (Last edited: 18 January 2013 9:33pm), Community Member, 198 Posts

Ok well 1 step forward 5 steps back...
Below is a picture of what i'm trying to achieve.

I'm using a form so i can call the Activate function and if i hard code the value of the hidden field to be a member ID it works.

public function ActivateForm() {
    $fields = new FieldList(
            new HiddenField('ID' , 'aID', $ID )
         );
      $actions = new FieldList(
            new FormAction('Activate', 'Activate')
         );
         $form = new Form($this, 'ActivateForm', $fields, $actions);
         return $form;
   }
   
   public function Activate() {
      $theID = $_POST["ID"];
   
      $member = DataObject::get_by_id("Member", $theID);
      
      if ($member) {
         $member->Status = "2";
         $member->write();
      }
   Controller::curr()->redirectBack();
   }


The trouble is as this form is called within a control loop i have to use the $Up.ActivateForm to display the form but this therefore that puts the form outside of the Member loop.
How do i get the member ID value i can pass it the ActivateForm function?

Mick

Attached Files
Avatar
IOTI

22 January 2013 at 6:34am Community Member, 189 Posts

I can't remember if this works or not with SS3 new template system but you could try passing in the member id as an argument.

<% loop Member %>
$Up.ActivateForm($ID)
<% end_loop %>

and in your ActivateForm() function

public function ActivateForm($memberID){
if($memberID){
$ID = $memberID;
}
..... rest of your code
}

Not sure if that will work though.

A better solution would be to make this ajax driven. It would create a much friendlier user experience but does add some complexity for you. [url]http://ss2doc-v2.ernie.silverstripe.com/old/recipes:ajax_basics[/url].

Avatar
cumquat

23 January 2013 at 10:19pm Community Member, 198 Posts

Hi ya,

Thanks for the response, unfortunately the passing an argument didn't seem to work, the form just didn't show up. Will look at the ajax stuff although i don't really know too much about that.

Thanks

Mick