Skip to content

Commit

Permalink
ENHANCEMENT Renamed DataGrid to GridField
Browse files Browse the repository at this point in the history
  • Loading branch information
Stig Lindqvist authored and Hamish Friedlander committed Oct 28, 2011
1 parent a2c7175 commit b229c17
Show file tree
Hide file tree
Showing 19 changed files with 471 additions and 368 deletions.
2 changes: 1 addition & 1 deletion .gitignore
@@ -1,2 +1,2 @@
.sass-cache .sass-cache
.DS_Store .DS_Store
17 changes: 0 additions & 17 deletions css/DataGrid.css

This file was deleted.

17 changes: 17 additions & 0 deletions css/GridField.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

129 changes: 0 additions & 129 deletions forms/Datagrid.php

This file was deleted.

140 changes: 140 additions & 0 deletions forms/GridField.php
@@ -0,0 +1,140 @@
<?php
/**
* Displays a {@link SS_List} in a grid format.
*
* @package sapphire
* @subpackage forms
*/
class GridField extends FormField {

/**
* @var SS_List
*/
protected $dataSource = null;

/**
* @var string
*/
protected $presenterClassName = "GridFieldPresenter";

/**
* @var GridFieldPresenter
*/
protected $presenter = null;

/**
* @var string - the classname of the DataObject that the GridField will display
*/
protected $modelClassName = '';

/**
* Creates a new GridField field
*
* @param string $name
* @param string $title
* @param SS_List $datasource
* @param Form $form
* @param string $dataPresenterClassName
*/
public function __construct($name, $title = null, SS_List $datasource = null, Form $form = null, $dataPresenterClassName = 'GridFieldPresenter') {
parent::__construct($name, $title, null, $form);

if ($datasource) {
$this->setDatasource($datasource);
}

$this->setPresenter($dataPresenterClassName);
}

/**
* @param string $modelClassName
*/
public function setModelClass($modelClassName) {
$this->modelClassName = $modelClassName;

return $this;
}

/**
* @throws Exception
* @return string
*/
public function getModelClass() {
if ($this->modelClassName) {
return $this->modelClassName;
}
if ($this->datasource->dataClass) {
return $this->datasource->dataClass;
}

throw new Exception(get_class($this).' does not have a modelClassName');
}

/**
* @param string|GridFieldPresenter
*
* @throws Exception
*/
public function setPresenter($presenter) {
if(!$presenter){
throw new Exception('setPresenter() for GridField must be set with a class');
}

if(is_object($presenter)) {
$this->presenter = $presenter;
$this->presenter->setGridField($this);

return;
}

if(!class_exists($presenter)){
throw new Exception('DataPresenter for GridField must be set with an existing class, '.$presenter.' does not exists.');
}

if($presenter !='GridFieldPresenter' && !ClassInfo::is_subclass_of($presenter, 'GridFieldPresenter')) {
throw new Exception(sprintf(
'DataPresenter "%s" must subclass GridFieldPresenter', $presenter
));
}

$this->presenter = new $presenter;
$this->presenter->setGridField($this);

return $this;
}

/**
* @return GridFieldPresenter
*/
public function getPresenter(){
return $this->presenter;
}

/**
* Set the datasource
*
* @param SS_List $datasource
*/
public function setDataSource(SS_List $datasource) {
$this->datasource = $datasource;

return $this;
}

/**
* Get the datasource
*
* @return SS_List
*/
public function getDataSource() {
return $this->datasource;
}

/**
* @return string - html for the form
*/
function FieldHolder() {
return $this->getPresenter()->render();
}
}

0 comments on commit b229c17

Please sign in to comment.