Permalink
Browse files

FEATURE: Implemented a view action for GridField, and a record viewin…

…g GridFieldConfig.
  • Loading branch information...
1 parent dc1ccfe commit 05cde925084b82ff009b63e4e56ca83e96795080 @ajshort ajshort committed May 11, 2012
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -144,6 +144,20 @@ public function __construct($itemsPerPage=null) {
}
/**
+ * 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());
+ }
+
+}
+
+/**
*
*/
class GridFieldConfig_RecordEditor extends GridFieldConfig {
View
@@ -225,7 +225,30 @@ public function __construct($gridField, $component, $record, $popupController, $
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);
@@ -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
@@ -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;
}
}
}
@@ -0,0 +1 @@
+<a class="action action-detail view-link" href="$Link">View</a>
@@ -47,6 +47,26 @@ function testAddForm() {
$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 @@ function Form() {
$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());

0 comments on commit 05cde92

Please sign in to comment.