Jump to:

23358 Posts in 17874 Topics by 2862 members

General Questions

SilverStripe Forums » General Questions » Manipulating the member table

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1 2 3
Go to End
Author Topic: 2239 Views
  • Harley
    Avatar
    Community Member
    153 Posts

    Manipulating the member table Link to this post

    Hi,

    I've got a client page that creates a security group on the fly as new client pages are created. What I've done for now is added in a default member just to check that I can add members to each group. Ideally what I want is to have a tab called members with a popup that I can easily add or delete members with.

    I'm getting myself lost here though, how can I add a popup that automatically retreives the corresponding fields in the member database table? What is the best way of doing this? I've posted my code below to show how far I've got:

    <?php

    class ClientPage extends ClientGroup {

       static $has_one = array(
          'Photo' => 'Image',
       );
       
       static $has_many = array(
          'members' => 'member'
       );

       function getCMSFields() {
    $fields = parent::getCMSFields();

          $fields->addFieldToTab('Root.Content.Client picture', new ImageField('Photo'));
          
          $validator = new RequiredFields('Page name');
          
          return $fields;
    }

       static $has_written = false;

       function onBeforeWrite(){
          if($this->Title != "New ClientPage" && !DataObject::get_one('Group', "Title = '$this->Title'")){
             if(!self::$has_written){ // hack to ensure new security group isn't written multiple times
                   $group = new Group();
                   $group->Title = $this->Title;
                   $group->write();
                   Permission::grant($group->ID, "EDIT_SITE");
                   
                   $member = new Member();
                   $member->FirstName = "Test name"; // for testing purpose to check member is being created
                   $member->write();
                   $group->Members()->add($member);
                }
             self::$has_written = true;
          }
          parent::onBeforeWrite();
       }
       
    }

    class ClientPage_Controller extends Page_Controller{
    }
    ?>

    Thanks

  • Harley
    Avatar
    Community Member
    153 Posts

    Re: Manipulating the member table Link to this post

    bump

  • dalesaurus
    Avatar
    Community Member
    283 Posts

    Re: Manipulating the member table Link to this post

    In the end you're trying to manipulate the Group_Members relation table. But to do things with a Page as you propose above you could use a simple form with a Dropdown for each Delete and Add.

    For the Add form:
    You'd have to work out a query to get all members NOT IN the current Group, put them into an array with their MemberID => Name, then put that in a DropdownField. On submit just grab the MemberID in the incoming $data and do the same add to group you're using in the onBeforeWrite.

    The Delete would be fundamentally the same with a bit more work on the query.

  • Harley
    Avatar
    Community Member
    153 Posts

    Re: Manipulating the member table Link to this post

    Hi Dalesaurus, thanks for getting back.

    I think you may have misunderstood me a bit though, I don't want to add in existing members, I need to add brand new members.

    I have managed to hard write one with this part of the script just for testing purposes

       function onBeforeWrite(){
          if($this->Title != "New ClientPage" && !DataObject::get_one('Group', "Title = '$this->Title'")){
             if(!self::$has_written){ // hack to ensure new security group isn't written multiple times
                   $group = new Group();
                   $group->Title = $this->Title;
                   $group->write();
                   Permission::grant($group->ID, "EDIT_SITE");
                   
                   $member = new Member();
                   $member->FirstName = "First name";
                   $member->Surname = "Surname";
                   $member->Nickname = "Nick name";
                   $member->Email = "email@theinternet.com";
                   $member->Password = "password";
                   $member->write();
                   $group->Members()->add($member);
                }
             self::$has_written = true;
          }
          parent::onBeforeWrite();
       }

    I just need a form that allows me to add them now

    Hope that makes sense

    Thanks

  • dalesaurus
    Avatar
    Community Member
    283 Posts

    Re: Manipulating the member table Link to this post

    But... you're already adding them, is it not working?

    You do need to move the ->write() call below the Member Groups()->add() call otherwise it won't be saved.

  • Harley
    Avatar
    Community Member
    153 Posts

    Re: Manipulating the member table Link to this post

    Thanks for the advice on moving the write(), that works better now.

    I've since made progress, I almost have what I want, the members table is being populated but the members are not being put under the group as required.

    Maybe there is something I am not doing but I just can't see it, maybe someone more experienced with creating groups can point out to me where I'm going wrong.

    Below is my updated code:

    ClientPage:

    <?php

    class ClientPage extends ClientGroup {

       static $has_one = array(
          'Photo' => 'Image'
       );
       
       static $many_many = array(
          'ClientMembers' => 'Member'
       );
       
       static $allowed_children = array( 'ClientMember' );   
       
       function getCMSFields(){
    $fields = parent::getCMSFields();

          $fields->addFieldToTab('Root.Content.Client picture', new ImageField('Photo'));
          
          $MembersTableField = new ComplexTableField(
             $this,
             'ClientMembers',
             'Member',
          array(
             'FirstName' => 'First name',
             'Surname' => 'Surname',
             'Nickname' => 'Nickname',
             'Email' => 'Email',
             'Password' => 'Password'
          ),
             'getCMSFields_forPopup'
          );
          
          $MembersTableField->setParentClass('ClientPage');
          
          $fields->addFieldToTab('Root.Content.Add members', $MembersTableField);
          
          $MembersTableField->setAddTitle('a member');

          $validator = new RequiredFields('PageName');
          
          return $fields;
    }

       static $has_written = false;

       function onBeforeWrite(){
          if($this->Title != "New ClientPage" && !DataObject::get_one('Group', "Title = '$this->Title'")){
             if(!self::$has_written){ // hack to ensure new security group isn't written multiple times
                   $group = new Group();
                   $group->Code = 'EDIT_SITE';
                   $group->Title = $this->Title;
                   $group->write();
                   Permission::grant($group->ID, "EDIT_SITE");
                   $member = new Member();
                   $member->FirstName = 'FirstName';
                   $member->Surname = 'Surname';
                   $member->Nickname = 'Nickname';
                   $member->Email = 'Email';
                   $member->Password = 'Password';
                   $member->Groups()->add($group);
                   $member->write();
                   }
             self::$has_written = true;
          }
          parent::onBeforeWrite();
       }

    }

    class ClientPage_Controller extends Page_Controller{
    }
    ?>

    ClientMember:

    <?php

    class ClientMember extends DataObjectDecorator {

       static $has_one = array(
          'ClientPage' => 'ClientPage'
       );

       static $db = array(
          'FirstName' => 'Text',
          'Surname' => 'Text',
          'Nickname' => 'Text',
          'Email' => 'Text',
          'Password' => 'Text'
       );
       
       function getCMSFields_forPopup() {
          $fields = new FieldSet();
          $fields->push( new TextField( 'FirstName', 'First name' ) );
          $fields->push( new TextField( 'Surname', 'Surname' ) );
          $fields->push( new TextField( 'Nickname', 'Nickname' ) );
          $fields->push( new EmailField( 'Email', 'Email' ) );
          $fields->push( new TextField( 'Password', 'Password' ) );
          $validator = new RequiredFields('FirstName', 'Surname', 'Nickname', 'Email', 'Password');
          return $fields;
       }
    }

    ?>

    Thanks

  • dalesaurus
    Avatar
    Community Member
    283 Posts

    Re: Manipulating the member table Link to this post

    Try adding another $group->write(); after the Member add:

    $member->Groups()->add($group);
    $member->write();
    $group->write();

  • Harley
    Avatar
    Community Member
    153 Posts

    Re: Manipulating the member table Link to this post

    Hmm... Good suggestion but didn't seem to work. Another thing that I have noticed is that when I try to delete a member from a client page it is coming up with "Server error". Also my complexTableField is populated with all members, I guess when I figure out the main issue of asigning my members to a group that will be solved by default.

    Thanks

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