APICHANGE: moved Group::addToGroupByName to $member->addToGroupByCode. (from r106217)

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@112528 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2010-10-15 03:00:48 +00:00
parent 197cdb0f64
commit 39b056024f
4 changed files with 59 additions and 20 deletions

View File

@ -180,25 +180,12 @@ class Group extends DataObject {
} }
/** /**
* Add a member to a group. This will create the group if the given * @deprecated 2.5
* group code doesn't work.
*
* @param DataObject $member
* @param string $groupcode
*/ */
static function addToGroupByName($member, $groupcode) { public static function addToGroupByName($member, $groupcode) {
$group = DataObject::get_one('Group', "\"Code\" = '" . Convert::raw2sql($groupcode). "'"); user_error('Group::addToGroupByName is deprecated. Please use $member->addToGroupByCode($groupcode)', E_USER_NOTICE);
if($group) {
$member->Groups()->add($group); return $member->addToGroupByCode($groupcode);
}
else {
$group = new Group();
$group->Code = $groupcode;
$group->Title = $groupcode;
$group->write();
$member->Groups()->add($group);
}
} }
/** /**

View File

@ -719,6 +719,31 @@ class Member extends DataObject {
return false; return false;
} }
/**
* Adds the member to a group. This will create the group if the given
* group code does not return a valid group object.
*
* @param string $groupcode
* @param string Title of the group
*/
public function addToGroupByCode($groupcode, $title = "") {
$group = DataObject::get_one('Group', "\"Code\" = '" . Convert::raw2sql($groupcode). "'");
if($group) {
$this->Groups()->add($group);
}
else {
if(!$title) $title = $groupcode;
$group = new Group();
$group->Code = $groupcode;
$group->Title = $title;
$group->write();
$this->Groups()->add($group);
}
}
/** /**
* Returns true if this user is an administrator. * Returns true if this user is an administrator.
* Administrators have access to everything. * Administrators have access to everything.

View File

@ -289,6 +289,28 @@ class MemberTest extends FunctionalTest {
); );
} }
function testAddToGroupByCode() {
$grouplessMember = $this->objFromFixture('Member', 'grouplessmember');
$memberlessGroup = $this->objFromFixture('Group','memberlessgroup');
$this->assertFalse($grouplessMember->Groups()->exists());
$this->assertFalse($memberlessGroup->Members()->exists());
$grouplessMember->addToGroupByCode('memberless');
$this->assertEquals($memberlessGroup->Members()->Count(), 1);
$this->assertEquals($grouplessMember->Groups()->Count(), 1);
$grouplessMember->addToGroupByCode('somegroupthatwouldneverexist', 'New Group');
$this->assertEquals($grouplessMember->Groups()->Count(), 2);
$group = DataObject::get_one('Group', "\"Code\" = 'somegroupthatwouldneverexist'");
$this->assertNotNull($group);
$this->assertEquals($group->Code, 'somegroupthatwouldneverexist');
$this->assertEquals($group->Title, 'New Group');
}
function testInGroup() { function testInGroup() {
$staffmember = $this->objFromFixture('Member', 'staffmember'); $staffmember = $this->objFromFixture('Member', 'staffmember');
$managementmember = $this->objFromFixture('Member', 'managementmember'); $managementmember = $this->objFromFixture('Member', 'managementmember');

View File

@ -20,7 +20,10 @@ Group:
ceogroup: ceogroup:
Title: ceogroup Title: ceogroup
Code: ceogroup Code: ceogroup
Parent: =>Group.managementgroup Parent: =>Group.managementgroup
memberlessgroup:
Title: Memberless Group
code: memberless
Member: Member:
test: test:
FirstName: Test FirstName: Test
@ -51,4 +54,6 @@ Member:
Groups: =>Group.accountinggroup Groups: =>Group.accountinggroup
ceomember: ceomember:
Email: ceomember@test.com Email: ceomember@test.com
Groups: =>Group.ceogroup Groups: =>Group.ceogroup
grouplessmember:
FirstName: Groupless Member