here is my code, for assign a member to a group, using registration group RegisterPage.php
Group ID is important - create a group (assign admin roles only if necessary) - than put the ID of the Group into this code
now is remaining to ad a check box for subscription http://doc.silverstripe.com/doku.php?id=member_groupset#componentset
<?php
/**
* Page containing a registration form.
* Uses Member::getMemberFormFields() to know what to ask of a user.
*/
class RegisterPage extends Page {
static $db = array(
"ThanksTitle" => "HTMLVarchar",
"ThanksContent" => "HTMLText",
);
function getCMSFields($cms) {
$fields = parent::getCMSFields($cms);
$fields->addFieldsToTab("Root.Content.Thanks", array(
new TextField("ThanksTitle", "Title"),
new HTMLEditorField("ThanksContent", "Content"),
));
return $fields;
}
}
class RegisterPage_Controller extends Page_Controller {
/**
* Return the edit form for the current user
*/
private $defaultGroupID = 2;
function Form() {
// Get the fields from a new member - seems like a good default :-)
$member = new Member();
$fields = $member->getMemberFormFields();
$actions = new FieldSet(
new FormAction('register', 'Register')
);
$form = new Form($this, 'Form', $fields, $actions);
return $form;
}
/**
* Save the profile details
*/
function register($data, $form) {
// Create a new member and save the form into it
$member = new Member();
$form->saveInto($member);
// Write to the databsae
$member->write();
// defaultGroupID
if($group = DataObject::get_one('Group', "ID = $this->defaultGroupID")) {
$member->Groups()->add($group);
// Redirect to a page thanking people for registering
Director::redirect($this->Link() . 'thanks');
}else{
// Redirect to a failure page
Director::redirect('registration-failed/');
}
}
function thanks() {
return array(
'Title' => $this->ThanksTitle,
'Content' => $this->ThanksContent,
'Form' => ' ',
);
}
}