2011-12-06 01:56:24 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2012-03-05 12:27:25 +01:00
|
|
|
* Encapsulates a collection of components following the {@link GridFieldComponent} interface.
|
|
|
|
* While the {@link GridField} itself has some configuration in the form of setters,
|
|
|
|
* most of the details are dealt with through components.
|
|
|
|
*
|
|
|
|
* For example, you would add a {@link GridFieldPaginator} component to enable
|
|
|
|
* pagination on the listed records, and configure it through {@link GridFieldPaginator->setItemsPerPage()}.
|
|
|
|
*
|
|
|
|
* In order to reduce the amount of custom code required, the framework provides
|
|
|
|
* some default configurations for common use cases:
|
|
|
|
* - {@link GridFieldConfig_Base} (added by default to GridField)
|
|
|
|
* - {@link GridFieldConfig_RecordEditor}
|
|
|
|
* - {@link GridFieldConfig_RelationEditor}
|
2011-12-06 01:56:24 +01:00
|
|
|
*/
|
|
|
|
class GridFieldConfig {
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @return GridFieldConfig
|
|
|
|
*/
|
|
|
|
public static function create(){
|
|
|
|
return new GridFieldConfig();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @var ArrayList
|
|
|
|
*/
|
|
|
|
protected $components = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function __construct() {
|
2012-01-30 15:47:22 +01:00
|
|
|
$this->components = new ArrayList();
|
2011-12-06 01:56:24 +01:00
|
|
|
}
|
|
|
|
|
2012-02-21 22:06:41 +01:00
|
|
|
/**
|
|
|
|
* @param GridFieldComponent $component
|
|
|
|
*/
|
2011-12-06 01:56:24 +01:00
|
|
|
public function addComponent(GridFieldComponent $component) {
|
|
|
|
$this->getComponents()->push($component);
|
|
|
|
return $this;
|
|
|
|
}
|
2012-02-21 22:06:41 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param GridFieldComponent One or more components
|
|
|
|
*/
|
|
|
|
public function addComponents() {
|
|
|
|
$components = func_get_args();
|
|
|
|
foreach($components as $component) $this->addComponent($component);
|
|
|
|
return $this;
|
2012-03-08 08:00:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param GridFieldComponent $component
|
|
|
|
* @return GridFieldConfig $this
|
|
|
|
*/
|
|
|
|
public function removeComponent(GridFieldComponent $component) {
|
|
|
|
$this->getComponents()->remove($component);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param String Class name or interface
|
|
|
|
* @return GridFieldConfig $this
|
|
|
|
*/
|
|
|
|
public function removeComponentsByType($type) {
|
|
|
|
$components = $this->getComponentsByType($type);
|
|
|
|
foreach($components as $component) {
|
|
|
|
$this->removeComponent($component);
|
|
|
|
}
|
|
|
|
return $this;
|
2012-02-21 22:06:41 +01:00
|
|
|
}
|
2011-12-06 01:56:24 +01:00
|
|
|
|
|
|
|
/**
|
2012-01-30 15:47:22 +01:00
|
|
|
* @return ArrayList Of GridFieldComponent
|
2011-12-06 01:56:24 +01:00
|
|
|
*/
|
|
|
|
public function getComponents() {
|
|
|
|
if(!$this->components) {
|
|
|
|
$this->components = new ArrayList();
|
|
|
|
}
|
|
|
|
return $this->components;
|
|
|
|
}
|
2012-01-30 15:47:22 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns all components extending a certain class, or implementing a certain interface.
|
|
|
|
*
|
|
|
|
* @param String Class name or interface
|
|
|
|
* @return ArrayList Of GridFieldComponent
|
|
|
|
*/
|
|
|
|
public function getComponentsByType($type) {
|
|
|
|
$components = new ArrayList();
|
|
|
|
foreach($this->components as $component) {
|
|
|
|
if($component instanceof $type) $components->push($component);
|
|
|
|
}
|
|
|
|
return $components;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the first available component with the given class or interface.
|
|
|
|
*
|
|
|
|
* @param String ClassName
|
|
|
|
* @return GridFieldComponent
|
|
|
|
*/
|
|
|
|
public function getComponentByType($type) {
|
|
|
|
foreach($this->components as $component) {
|
|
|
|
if($component instanceof $type) return $component;
|
|
|
|
}
|
|
|
|
}
|
2012-01-24 00:36:25 +01:00
|
|
|
}
|
|
|
|
|
2012-03-05 12:27:25 +01:00
|
|
|
/**
|
|
|
|
* A simple readonly, paginated view of records,
|
|
|
|
* with sortable and searchable headers.
|
|
|
|
*/
|
2012-01-24 00:36:25 +01:00
|
|
|
class GridFieldConfig_Base extends GridFieldConfig {
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param int $itemsPerPage - How many items per page should show up per page
|
|
|
|
* @return GridFieldConfig_Base
|
|
|
|
*/
|
2012-03-05 13:30:07 +01:00
|
|
|
public static function create($itemsPerPage=null){
|
2012-03-02 03:59:39 +01:00
|
|
|
return new GridFieldConfig_Base($itemsPerPage);
|
2011-12-06 01:56:24 +01:00
|
|
|
}
|
2012-01-24 00:36:25 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param int $itemsPerPage - How many items per page should show up
|
|
|
|
*/
|
2012-03-05 13:30:07 +01:00
|
|
|
public function __construct($itemsPerPage=null) {
|
2012-02-09 04:02:47 +01:00
|
|
|
$this->addComponent(new GridFieldTitle());
|
2012-01-24 00:36:25 +01:00
|
|
|
$this->addComponent(new GridFieldSortableHeader());
|
2012-01-30 15:51:07 +01:00
|
|
|
$this->addComponent(new GridFieldFilter());
|
2012-01-24 00:36:25 +01:00
|
|
|
$this->addComponent(new GridFieldDefaultColumns());
|
|
|
|
$this->addComponent(new GridFieldPaginator($itemsPerPage));
|
2011-12-06 01:56:24 +01:00
|
|
|
}
|
2012-01-24 00:36:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
2012-03-05 12:14:24 +01:00
|
|
|
*/
|
|
|
|
class GridFieldConfig_RecordEditor extends GridFieldConfig {
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param int $itemsPerPage - How many items per page should show up
|
|
|
|
* @return GridFieldConfig_RecordEditor
|
|
|
|
*/
|
2012-03-05 13:30:07 +01:00
|
|
|
public static function create($itemsPerPage=null){
|
2012-03-05 12:14:24 +01:00
|
|
|
return new GridFieldConfig_RecordEditor($itemsPerPage);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param int $itemsPerPage - How many items per page should show up
|
|
|
|
*/
|
2012-03-05 13:30:07 +01:00
|
|
|
public function __construct($itemsPerPage=null) {
|
|
|
|
$this->addComponent(new GridFieldTitle());
|
2012-03-05 12:14:24 +01:00
|
|
|
$this->addComponent(new GridFieldSortableHeader());
|
|
|
|
$this->addComponent(new GridFieldFilter());
|
|
|
|
$this->addComponent(new GridFieldDefaultColumns());
|
2012-03-06 12:57:16 +01:00
|
|
|
$this->addComponent(new GridFieldEditAction());
|
|
|
|
$this->addComponent(new GridFieldDeleteAction());
|
2012-03-05 12:14:24 +01:00
|
|
|
$this->addComponent(new GridFieldPaginator($itemsPerPage));
|
|
|
|
$this->addComponent(new GridFieldPopupForms());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Similar to {@link GridFieldConfig_RecordEditor}, but adds features
|
|
|
|
* to work on has-many or many-many relationships.
|
|
|
|
* Allows to search for existing records to add to the relationship,
|
|
|
|
* detach listed records from the relationship (rather than removing them from the database),
|
|
|
|
* and automatically add newly created records to it.
|
2012-01-24 00:36:25 +01:00
|
|
|
*
|
2012-03-05 12:14:24 +01:00
|
|
|
* To further configure the field, use {@link getComponentByType()},
|
|
|
|
* for example to change the field to search.
|
|
|
|
* <code>
|
|
|
|
* GridFieldConfig_RelationEditor::create()
|
|
|
|
* ->getComponentByType('GridFieldRelationAdd')->setSearchFields('MyField');
|
|
|
|
* </code>
|
2012-01-24 00:36:25 +01:00
|
|
|
*/
|
2012-03-05 12:14:24 +01:00
|
|
|
class GridFieldConfig_RelationEditor extends GridFieldConfig {
|
2012-01-24 00:36:25 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param int $itemsPerPage - How many items per page should show up
|
2012-03-05 12:14:24 +01:00
|
|
|
* @return GridFieldConfig_RelationEditor
|
2012-01-24 00:36:25 +01:00
|
|
|
*/
|
2012-03-05 13:30:07 +01:00
|
|
|
public static function create($itemsPerPage=null){
|
|
|
|
return new GridFieldConfig_RelationEditor($itemsPerPage);
|
2011-12-06 01:56:24 +01:00
|
|
|
}
|
2012-01-24 00:36:25 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param int $itemsPerPage - How many items per page should show up
|
|
|
|
*/
|
2012-03-05 13:30:07 +01:00
|
|
|
public function __construct($itemsPerPage=null) {
|
|
|
|
$this->addComponent(new GridFieldTitle());
|
2012-03-05 12:14:24 +01:00
|
|
|
$this->addComponent(new GridFieldRelationAdd());
|
2012-01-24 00:36:25 +01:00
|
|
|
$this->addComponent(new GridFieldSortableHeader());
|
2012-03-02 04:03:23 +01:00
|
|
|
$this->addComponent(new GridFieldFilter());
|
2012-01-24 00:36:25 +01:00
|
|
|
$this->addComponent(new GridFieldDefaultColumns());
|
2012-03-06 12:57:16 +01:00
|
|
|
$this->addComponent(new GridFieldEditAction());
|
2012-01-24 00:36:25 +01:00
|
|
|
$this->addComponent(new GridFieldRelationDelete());
|
|
|
|
$this->addComponent(new GridFieldPaginator($itemsPerPage));
|
2012-02-27 18:32:13 +01:00
|
|
|
$this->addComponent(new GridFieldPopupForms());
|
2011-12-06 01:56:24 +01:00
|
|
|
}
|
|
|
|
}
|