FEATURE: Implemented a view action for GridField, and a record viewing GridFieldConfig.

This commit is contained in:
Andrew Short 2012-05-11 17:44:39 +10:00
parent dc1ccfe251
commit 05cde92508
7 changed files with 105 additions and 4 deletions

View File

@ -38,7 +38,9 @@ multiple images. */
.cms table.ss-gridfield-table tbody td button { border: none; background: none; margin: 0 0 0 2px; padding: 0; width: auto; text-shadow: none; }
.cms table.ss-gridfield-table tbody td button.ui-state-hover { background: none; -moz-box-shadow: none; -webkit-box-shadow: none; box-shadow: none; }
.cms table.ss-gridfield-table tbody td button.ui-state-active { border: none; -moz-box-shadow: none; -webkit-box-shadow: none; box-shadow: none; }
.cms table.ss-gridfield-table tbody td a.edit-link { display: inline-block; width: 16px; height: 20px; text-indent: 9999em; overflow: hidden; vertical-align: middle; background: url(../images/icons/document--pencil.png) no-repeat 0 1px; }
.cms table.ss-gridfield-table tbody td a.view-link, .cms table.ss-gridfield-table tbody td a.edit-link { display: inline-block; width: 16px; height: 20px; text-indent: 9999em; overflow: hidden; vertical-align: middle; }
.cms table.ss-gridfield-table tbody td a.view-link { background: url(../admin/images/btn-icon/magnifier.png) no-repeat 0 1px; }
.cms table.ss-gridfield-table tbody td a.edit-link { background: url(../admin/images/btn-icon/document--pencil.png) no-repeat 0 1px; }
.cms table.ss-gridfield-table tfoot { color: #1d2224; }
.cms table.ss-gridfield-table tfoot tr td { background: #95a5ab; padding: .7em; border-bottom: 1px solid rgba(0, 0, 0, 0.1); }
.cms table.ss-gridfield-table tr.title th { position: relative; background: #7f9198; border-top: 1px solid rgba(0, 0, 0, 0.1); padding: 5px; min-height: 40px; background-image: url('data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4gPHN2ZyB2ZXJzaW9uPSIxLjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+PGRlZnM+PGxpbmVhckdyYWRpZW50IGlkPSJncmFkIiBncmFkaWVudFVuaXRzPSJ1c2VyU3BhY2VPblVzZSIgeDE9IjUwJSIgeTE9IjAlIiB4Mj0iNTAlIiB5Mj0iMTAwJSI+PHN0b3Agb2Zmc2V0PSIwJSIgc3RvcC1jb2xvcj0iI2IxYzBjNSIvPjxzdG9wIG9mZnNldD0iMTAwJSIgc3RvcC1jb2xvcj0iIzdmOTE5OCIvPjwvbGluZWFyR3JhZGllbnQ+PC9kZWZzPjxyZWN0IHg9IjAiIHk9IjAiIHdpZHRoPSIxMDAlIiBoZWlnaHQ9IjEwMCUiIGZpbGw9InVybCgjZ3JhZCkiIC8+PC9zdmc+IA=='); background-size: 100%; background-image: -webkit-gradient(linear, 50% 0%, 50% 100%, color-stop(0%, #b1c0c5), color-stop(100%, #7f9198)); background-image: -webkit-linear-gradient(#b1c0c5, #7f9198); background-image: -moz-linear-gradient(#b1c0c5, #7f9198); background-image: -o-linear-gradient(#b1c0c5, #7f9198); background-image: -ms-linear-gradient(#b1c0c5, #7f9198); background-image: linear-gradient(#b1c0c5, #7f9198); text-shadow: rgba(0, 0, 0, 0.3) 0px -1px 0; }

14
forms/gridfield/GridFieldConfig.php Executable file → Normal file
View File

@ -143,6 +143,20 @@ class GridFieldConfig_Base extends GridFieldConfig {
}
}
/**
* Allows viewing readonly details of individual records.
*/
class GridFieldConfig_RecordViewer extends GridFieldConfig_Base {
public function __construct($itemsPerPage = null) {
parent::__construct($itemsPerPage);
$this->addComponent(new GridFieldViewButton());
$this->addComponent(new GridFieldDetailForm());
}
}
/**
*
*/

25
forms/gridfield/GridFieldDetailForm.php Executable file → Normal file
View File

@ -225,7 +225,30 @@ class GridFieldDetailForm_ItemRequest extends RequestHandler {
public function Link($action = null) {
return Controller::join_links($this->gridField->Link('item'), $this->record->ID ? $this->record->ID : 'new', $action);
}
public function view($request) {
if(!$this->record->canView()) {
$this->httpError(403);
}
$controller = $this->getToplevelController();
$form = $this->ItemEditForm($this->gridField, $request);
$form->makeReadonly();
$data = new ArrayData(array(
'Backlink' => $controller->Link(),
'ItemEditForm' => $form
));
$return = $data->renderWith($this->template);
if($request->isAjax()) {
return $return;
} else {
return $controller->customise(array('Content' => $return));
}
}
function edit($request) {
$controller = $this->getToplevelController();
$form = $this->ItemEditForm($this->gridField, $request);

View File

@ -0,0 +1,35 @@
<?php
/**
* A button that allows a user to view readonly details of a record. This is
* disabled by default and intended for use in readonly grid fields.
*
* @package framework
*/
class GridFieldViewButton implements GridField_ColumnProvider {
public function augmentColumns($field, &$cols) {
if(!in_array('Actions', $cols)) $cols[] = 'Actions';
}
public function getColumnsHandled($field) {
return array('Actions');
}
public function getColumnContent($field, $record, $col) {
if($record->canView()) {
$data = new ArrayData(array(
'Link' => Controller::join_links($field->Link('item'), $record->ID, 'view')
));
return $data->renderWith('GridFieldViewButton');
}
}
public function getColumnAttributes($field, $record, $col) {
return array('class' => 'col-buttons');
}
public function getColumnMetadata($gridField, $col) {
return array('title' => null);
}
}

View File

@ -219,14 +219,19 @@ $gf_grid_x: 16px;
@include box-shadow-none;
}
}
a.edit-link {
a.view-link, a.edit-link {
display:inline-block;
width:$gf_grid_x;
height:20px; //min height to fit the edit icon
text-indent:9999em;
overflow: hidden;
vertical-align: middle;
background: url(../images/icons/document--pencil.png) no-repeat 0 1px;
}
a.view-link {
background: url(../admin/images/btn-icon/magnifier.png) no-repeat 0 1px;
}
a.edit-link {
background: url(../admin/images/btn-icon/document--pencil.png) no-repeat 0 1px;
}
}
}

View File

@ -0,0 +1 @@
<a class="action action-detail view-link" href="$Link">View</a>

View File

@ -47,6 +47,26 @@ class GridFieldDetailFormTest extends FunctionalTest {
$this->assertEquals($count + 1, $group->People()->Count());
}
public function testViewForm() {
$this->logInWithPermission('ADMIN');
$response = $this->get('GridFieldDetailFormTest_Controller');
$parser = new CSSContentParser($response->getBody());
$viewLink = $parser->getBySelector('.ss-gridfield-items .first .view-link');
$viewLink = (string) $viewLink[0]['href'];
$response = $this->get($viewLink);
$parser = new CSSContentParser($response->getBody());
$firstName = $parser->getBySelector('#Form_ItemEditForm_FirstName');
$surname = $parser->getBySelector('#Form_ItemEditForm_Surname');
$this->assertFalse($response->isError());
$this->assertEquals('Joe', (string) $firstName[0]);
$this->assertEquals('Bloggs', (string) $surname[0]);
}
function testEditForm() {
$this->logInWithPermission('ADMIN');
$group = DataList::create('GridFieldDetailFormTest_PeopleGroup')
@ -237,6 +257,7 @@ class GridFieldDetailFormTest_Controller extends Controller implements TestOnly
$field = new GridField('testfield', 'testfield', $group->People());
$field->getConfig()->addComponent(new GridFieldToolbarHeader());
$field->getConfig()->addComponent(new GridFieldAddNewButton('toolbar-header-right'));
$field->getConfig()->addComponent(new GridFieldViewButton());
$field->getConfig()->addComponent(new GridFieldEditButton());
$field->getConfig()->addComponent($gridFieldForm = new GridFieldDetailForm($this, 'Form'));
$field->getConfig()->addComponent(new GridFieldEditButton());