Permalink
Browse files

ENHANCEMENT Making setting optional in MemberTableField. Field instan…

…ces without will list all members unfiltered, and remove members from the database rather than the group relation.

ENHANCEMENT Allow disabling of 'inline add' formfields in a MemberTableField through setPermissions(array('inlineadd'))

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/cms/trunk@98825 467b73ca-7a2a-4603-9d3b-597d59a354a9
  • Loading branch information...
1 parent 37a9146 commit 1ba1c563a858730588489324105ce63a18ce04eb @chillu chillu committed Feb 11, 2010
Showing with 200 additions and 14 deletions.
  1. +65 −7 code/MemberTableField.php
  2. +7 −7 templates/Includes/MemberTableField.ss
  3. +101 −0 tests/MemberTableFieldTest.php
  4. +27 −0 tests/MemberTableFieldTest.yml
@@ -30,13 +30,24 @@ class MemberTableField extends ComplexTableField {
public $popupClass = 'MemberTableField_Popup';
+ public $itemClass = 'MemberTableField_Item';
+
static $data_class = 'Member';
/**
* Set the page size for this table.
* @var int
*/
public static $page_size = 20;
+
+ protected $permissions = array(
+ "add",
+ "edit",
+ "show",
+ "delete",
+ 'inlineadd'
+ //"export",
+ );
/**
* Constructor method for MemberTableField.
@@ -47,7 +58,7 @@ class MemberTableField extends ComplexTableField {
* @param DataObjectSet $members Optional set of Members to set as the source items for this field
* @param boolean $hidePassword Hide the password field or not in the summary?
*/
- function __construct($controller, $name, $group, $members = null, $hidePassword = true) {
+ function __construct($controller, $name, $group = null, $members = null, $hidePassword = true) {
$sourceClass = self::$data_class;
$SNG_member = singleton($sourceClass);
$fieldList = $SNG_member->summaryFields();
@@ -64,7 +75,7 @@ function __construct($controller, $name, $group, $members = null, $hidePassword
} elseif(is_numeric($group)) {
$this->group = DataObject::get_by_id('Group', $group);
}
- } else if(isset($_REQUEST['ctf']) && is_numeric($_REQUEST['ctf'][$this->Name()]["ID"])) {
+ } else if(isset($_REQUEST['ctf'][$this->Name()]["ID"]) && is_numeric($_REQUEST['ctf'][$this->Name()]["ID"])) {
$this->group = DataObject::get_by_id('Group', $_REQUEST['ctf'][$this->Name()]["ID"]);
}
@@ -76,7 +87,7 @@ function __construct($controller, $name, $group, $members = null, $hidePassword
// @todo shouldn't this use $this->group? It's unclear exactly
// what group it should be customising the custom Member set with.
- if($members) {
+ if($members && $group) {
$this->setCustomSourceItems($this->memberListWithGroupID($members, $group));
}
@@ -241,6 +252,13 @@ function setGroup($group) {
$this->group = $group;
}
+ /**
+ * @return Group
+ */
+ function getGroup() {
+ return $this->group;
+ }
+
function setController($controller) {
$this->controller = $controller;
}
@@ -333,13 +351,21 @@ function sourceItems() {
$start = isset($_REQUEST['ctf'][$this->Name()]['start']) ? $_REQUEST['ctf'][$this->Name()]['start'] : 0;
$this->sourceItems = false;
+
if($this->group) {
$this->sourceItems = $this->group->Members(
$this->pageSize, // limit
$start, // offset
$this->sourceFilter,
$this->sourceSort
);
+ } else {
+ $this->sourceItems = DataObject::get(self::$data_class,
+ $this->sourceFilter,
+ $this->sourceSort,
+ null,
+ array('limit' => $this->pageSize, 'start' => $start)
+ );
}
// Because we are not used $this->upagedSourceItems any more, and the DataObjectSet is usually the source
// that a large member set runs out of memory. we disable it here.
@@ -381,6 +407,33 @@ function forTemplate() {
}
+class MemberTableField_Item extends ComplexTableField_Item {
+
+ function Actions() {
+ $actions = parent::Actions();
+
+ foreach($actions as $action) {
+ if($action->Name == 'delete') {
+ if($this->parent->getGroup()) {
+ $action->TitleText = _t('MemberTableField.DeleteTitleText',
+ 'Delete from this group',
+ PR_MEDIUM,
+ 'Delete button hover text'
+ );
+ } else {
+ $action->TitleText = _t('MemberTableField.DeleteTitleTextDatabase',
+ 'Delete from database and all groups',
+ PR_MEDIUM,
+ 'Delete button hover text'
+ );
+ }
+ }
+ }
+
+ return $actions;
+ }
+}
+
class MemberTableField_ItemRequest extends ComplexTableField_ItemRequest {
/**
* Deleting an item from a member table field should just remove that member from the group
@@ -390,10 +443,15 @@ function delete() {
return false;
}
- $groupID = $this->ctf->sourceID();
- $this->dataObj()->Groups()->remove($groupID);
+ // if a group limitation is set on the table, remove relation.
+ // otherwise remove the record from the database
+ if($this->ctf->getGroup()) {
+ $groupID = $this->ctf->sourceID();
+ $this->dataObj()->Groups()->remove($groupID);
+ } else {
+ $this->dataObj()->delete();
+ }
}
-
}
-?>
+?>
@@ -17,12 +17,13 @@
</tr>
</thead>
<tfoot>
- <% if can(add) %>
- <tr class="addtogrouprow">
- <% if Markable %><td width="18">&nbsp;</dh><% end_if %>
- $AddRecordForm.CellFields
- <td class="actions" colspan="3">$AddRecordForm.CellActions</td>
- </tr>
+ <% if Can(inlineadd) %>
+ <tr class="addtogrouprow">
+ <% if Markable %><td width="18">&nbsp;</dh><% end_if %>
+ $AddRecordForm.CellFields
+ <td class="actions" colspan="3">$AddRecordForm.CellActions</td>
+ </tr>
+ <% end_if %>
<tr style="display: none;">
<% if Markable %><td width="18">&nbsp;</td><% end_if %>
<td colspan="$ItemCount">
@@ -32,7 +33,6 @@
<% if Can(edit) %><td width="18">&nbsp;</td><% end_if %>
<% if Can(delete) %><td width="18">&nbsp;</td><% end_if %>
</tr>
- <% end_if %>
</tfoot>
<tbody>
<% if Items %>
@@ -0,0 +1,101 @@
+<?php
+class MemberTableFieldTest extends SapphireTest {
+ static $fixture_file = 'cms/tests/MemberTableFieldTest.yml';
+
+ function testLimitsToMembersInGroup() {
+ $member1 = $this->objFromFixture('Member', 'member1');
+ $member2 = $this->objFromFixture('Member', 'member2');
+ $member3 = $this->objFromFixture('Member', 'member3');
+ $group1 = $this->objFromFixture('Group', 'group1');
+
+ $tf = new MemberTableField(
+ $this,
+ "Members",
+ $group1
+ );
+ $members = $tf->sourceItems();
+
+ $this->assertContains($member1->ID, $members->column('ID'),
+ 'Members in the associated group are listed'
+ );
+ $this->assertContains($member2->ID, $members->column('ID'),
+ 'Members in children groups are listed as well'
+ );
+ $this->assertNotContains($member3->ID, $members->column('ID'),
+ 'Members in other groups are filtered out'
+ );
+ }
+
+ function testShowsAllMembersWithoutGroupParameter() {
+ $member1 = $this->objFromFixture('Member', 'member1');
+ $member2 = $this->objFromFixture('Member', 'member2');
+ $member3 = $this->objFromFixture('Member', 'member3');
+ $group1 = $this->objFromFixture('Group', 'group1');
+
+ $tf = new MemberTableField(
+ $this,
+ "Members"
+ // no group assignment
+ );
+ $members = $tf->sourceItems();
+
+ $this->assertContains($member1->ID, $members->column('ID'),
+ 'Members in the associated group are listed'
+ );
+ $this->assertContains($member2->ID, $members->column('ID'),
+ 'Members in children groups are listed as well'
+ );
+ $this->assertContains($member3->ID, $members->column('ID'),
+ 'Members in other groups are listed'
+ );
+ }
+
+ function testDeleteWithGroupOnlyDeletesRelation() {
+ $member1 = $this->objFromFixture('Member', 'member1');
+ $group1 = $this->objFromFixture('Group', 'group1');
+
+ $tf = new MemberTableField(
+ $this,
+ "Members",
+ $group1
+ );
+ $tfItem = new MemberTableField_ItemRequest($tf, $member1->ID);
+ $tfItem->delete();
+
+ $group1->flushCache();
+
+ $this->assertNotContains($member1->ID, $group1->Members()->column('ID'),
+ 'Member relation to group is removed'
+ );
+ $this->assertType(
+ 'DataObject',
+ DataObject::get_by_id('Member', $member1->ID),
+ 'Member record still exists'
+ );
+ }
+
+ function testDeleteWithoutGroupDeletesFromDatabase() {
+ $member1 = $this->objFromFixture('Member', 'member1');
+ $member1ID = $member1->ID;
+ $group1 = $this->objFromFixture('Group', 'group1');
+
+ $tf = new MemberTableField(
+ $this,
+ "Members"
+ // no group assignment
+ );
+ $tfItem = new MemberTableField_ItemRequest($tf, $member1->ID);
+ $tfItem->delete();
+
+ $group1->flushCache();
+
+ $this->assertNotContains($member1->ID, $group1->Members()->column('ID'),
+ 'Member relation to group is removed'
+ );
+ DataObject::flush_and_destroy_cache();
+ $this->assertFalse(
+ DataObject::get_by_id('Member', $member1ID),
+ 'Member record is removed from database'
+ );
+ }
+}
@@ -0,0 +1,27 @@
+Group:
+ admin:
+ Title: Administrators
+ group1:
+ Title: Group1
+ group1_child:
+ Title: Group1 Child
+ Parent: =>Group.group1
+ group2:
+ Title: Group2
+Member:
+ admin:
+ Email: admin@example.com
+ Groups: =>Group.admin
+ member1:
+ Email: member1@test.com
+ Groups: =>Group.group1
+ member2:
+ Email: member2@test.com
+ Groups: =>Group.group1_child
+ member3:
+ Email: member3@test.com
+ Groups: =>Group.group2
+Permission:
+ admin:
+ Code: ADMIN
+ GroupID: =>Group.admin

0 comments on commit 1ba1c56

Please sign in to comment.