Merge pull request #382 from chillu/trac/7217-group-default-on-member

Group default on member (#7217)
This commit is contained in:
Sean Harvey 2012-05-01 21:43:05 -07:00
commit 6483cdd204
3 changed files with 91 additions and 6 deletions

View File

@ -28,6 +28,16 @@ class GridFieldDetailForm implements GridField_URLHandler {
*/ */
protected $validator; protected $validator;
/**
* @var String
*/
protected $itemRequestClass;
/**
* @var function With two parameters: $form and $component
*/
protected $itemEditFormCallback;
function getURLHandlers($gridField) { function getURLHandlers($gridField) {
return array( return array(
'item/$ID' => 'handleItem', 'item/$ID' => 'handleItem',
@ -64,11 +74,7 @@ class GridFieldDetailForm implements GridField_URLHandler {
$record = Object::create($gridField->getModelClass()); $record = Object::create($gridField->getModelClass());
} }
if(ClassInfo::exists(get_class($this) . "_ItemRequest")) { $class = $this->getItemRequestClass();
$class = get_class($this) . "_ItemRequest";
} else {
$class = 'GridFieldDetailForm_ItemRequest';
}
$handler = Object::create($class, $gridField, $this, $record, $controller, $this->name); $handler = Object::create($class, $gridField, $this, $record, $controller, $this->name);
$handler->setTemplate($this->template); $handler->setTemplate($this->template);
@ -120,6 +126,41 @@ class GridFieldDetailForm implements GridField_URLHandler {
public function getValidator() { public function getValidator() {
return $this->validator; return $this->validator;
} }
/**
* @param String
*/
public function setItemRequestClass($class) {
$this->itemRequestClass = $class;
return $this;
}
/**
* @return String
*/
public function getItemRequestClass() {
if($this->itemRequestClass) {
return $this->itemRequestClass;
} else if(ClassInfo::exists(get_class($this) . "_ItemRequest")) {
return get_class($this) . "_ItemRequest";
} else {
return 'GridFieldItemRequest_ItemRequest';
}
}
/**
* @param Closure $cb Make changes on the edit form after constructing it.
*/
public function setItemEditFormCallback(Closure $cb) {
$this->itemEditFormCallback = $cb;
}
/**
* @return Closure
*/
public function getItemEditFormCallback() {
return $this->itemEditFormCallback;
}
} }
class GridFieldDetailForm_ItemRequest extends RequestHandler { class GridFieldDetailForm_ItemRequest extends RequestHandler {
@ -269,6 +310,10 @@ class GridFieldDetailForm_ItemRequest extends RequestHandler {
// e.g. page/edit/show/6/ vs. page/edit/EditForm/field/MyGridField/.... // e.g. page/edit/show/6/ vs. page/edit/EditForm/field/MyGridField/....
$form->Backlink = $toplevelController->Link(); $form->Backlink = $toplevelController->Link();
} }
$cb = $this->component->getItemEditFormCallback();
if($cb) $cb($form, $this);
return $form; return $form;
} }

View File

@ -94,12 +94,23 @@ class Group extends DataObject {
$permissionsField->setHiddenPermissions(SecurityAdmin::$hidden_permissions); $permissionsField->setHiddenPermissions(SecurityAdmin::$hidden_permissions);
if($this->ID) { if($this->ID) {
$group = $this;
$config = new GridFieldConfig_RelationEditor(); $config = new GridFieldConfig_RelationEditor();
$config->addComponents(new GridFieldExportButton('before')); $config->addComponents(new GridFieldExportButton('before'));
$config->addComponents(new GridFieldPrintButton('before')); $config->addComponents(new GridFieldPrintButton('before'));
$config->getComponentByType('GridFieldAddExistingAutocompleter') $config->getComponentByType('GridFieldAddExistingAutocompleter')
->setResultsFormat('$Title ($Email)')->setSearchFields(array('FirstName', 'Surname', 'Email')); ->setResultsFormat('$Title ($Email)')->setSearchFields(array('FirstName', 'Surname', 'Email'));
$config->getComponentByType('GridFieldDetailForm')->setValidator(new Member_Validator()); $config->getComponentByType('GridFieldDetailForm')
->setValidator(new Member_Validator())
->setItemEditFormCallback(function($form, $component) use($group) {
// If new records are created in a group context,
// set this group by default.
$record = $form->getRecord();
if($record && !$record->ID) {
$groupsField = $form->Fields()->dataFieldByName('DirectGroups');
if($groupsField) $groupsField->setValue($group->ID);
}
});
$memberList = GridField::create('Members',false, $this->Members(), $config)->addExtraClass('members_grid'); $memberList = GridField::create('Members',false, $this->Members(), $config)->addExtraClass('members_grid');
// @todo Implement permission checking on GridField // @todo Implement permission checking on GridField
//$memberList->setPermissions(array('edit', 'delete', 'export', 'add', 'inlineadd')); //$memberList->setPermissions(array('edit', 'delete', 'export', 'add', 'inlineadd'));

View File

@ -126,6 +126,32 @@ class GridFieldDetailFormTest extends FunctionalTest {
// Fourth level form would be a Category detail view // Fourth level form would be a Category detail view
} }
function testCustomItemRequestClass() {
$component = new GridFieldDetailForm();
$this->assertEquals('GridFieldDetailForm_ItemRequest', $component->getItemRequestClass());
$component->setItemRequestClass('GridFieldDetailFormTest_ItemRequest');
$this->assertEquals('GridFieldDetailFormTest_ItemRequest', $component->getItemRequestClass());
}
function testItemEditFormCallback() {
$category = new GridFieldDetailFormTest_Category();
$component = new GridFieldDetailForm();
$component->setItemEditFormCallback(function($form, $component) {
$form->Fields()->push(new HiddenField('Callback'));
});
// Note: A lot of scaffolding to execute the tested logic,
// due to the coupling of form creation with request handling (and its context)
$request = new GridFieldDetailForm_ItemRequest(
GridField::create('Categories', 'Categories'),
$component,
$category,
new Controller(),
'Form'
);
$form = $request->ItemEditForm();
$this->assertNotNull($form->Fields()->fieldByName('Callback'));
}
} }
class GridFieldDetailFormTest_Person extends DataObject implements TestOnly { class GridFieldDetailFormTest_Person extends DataObject implements TestOnly {
@ -230,3 +256,6 @@ class GridFieldDetailFormTest_GroupController extends Controller implements Test
return new Form($this, 'Form', new FieldList($field), new FieldList()); return new Form($this, 'Form', new FieldList($field), new FieldList());
} }
} }
class GridFieldDetailFormTest_ItemRequest extends GridFieldDetailForm_ItemRequest implements TestOnly {
}