2011-12-06 01:56:24 +01:00
|
|
|
<?php
|
2016-06-15 06:03:16 +02:00
|
|
|
|
|
|
|
use SilverStripe\ORM\ArrayList;
|
2011-12-06 01:56:24 +01:00
|
|
|
/**
|
2014-08-15 08:53:05 +02: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
|
2013-05-20 12:18:07 +02:00
|
|
|
* dealt with through components.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-03-05 12:27:25 +01:00
|
|
|
* For example, you would add a {@link GridFieldPaginator} component to enable
|
2014-08-15 08:53:05 +02:00
|
|
|
* pagination on the listed records, and configure it through
|
2013-05-20 12:18:07 +02:00
|
|
|
* {@link GridFieldPaginator->setItemsPerPage()}.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
|
|
|
* In order to reduce the amount of custom code required, the framework
|
2013-05-20 12:18:07 +02:00
|
|
|
* provides some default configurations for common use cases:
|
|
|
|
*
|
2012-03-05 12:27:25 +01:00
|
|
|
* - {@link GridFieldConfig_Base} (added by default to GridField)
|
|
|
|
* - {@link GridFieldConfig_RecordEditor}
|
|
|
|
* - {@link GridFieldConfig_RelationEditor}
|
2013-05-20 12:18:07 +02:00
|
|
|
*
|
2013-11-29 05:12:47 +01:00
|
|
|
* @package forms
|
2013-05-20 12:18:07 +02:00
|
|
|
* @subpackage fields-gridfield
|
2011-12-06 01:56:24 +01:00
|
|
|
*/
|
2013-08-08 07:12:40 +02:00
|
|
|
class GridFieldConfig extends Object {
|
2013-05-20 12:18:07 +02:00
|
|
|
|
2011-12-06 01:56:24 +01:00
|
|
|
/**
|
2012-08-19 16:01:09 +02:00
|
|
|
* @var ArrayList
|
2011-12-06 01:56:24 +01:00
|
|
|
*/
|
2012-08-19 16:01:09 +02:00
|
|
|
protected $components = null;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
|
|
|
2011-12-06 01:56:24 +01:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-12-06 01:56:24 +01:00
|
|
|
*/
|
|
|
|
public function __construct() {
|
2013-08-08 07:12:40 +02:00
|
|
|
parent::__construct();
|
2012-01-30 15:47:22 +01:00
|
|
|
$this->components = new ArrayList();
|
2011-12-06 01:56:24 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-02-21 22:06:41 +01:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* @param GridFieldComponent $component
|
2012-05-18 03:36:52 +02:00
|
|
|
* @param string $insertBefore The class of the component to insert this one before
|
2012-02-21 22:06:41 +01:00
|
|
|
*/
|
2012-05-18 03:36:52 +02:00
|
|
|
public function addComponent(GridFieldComponent $component, $insertBefore = null) {
|
|
|
|
if($insertBefore) {
|
|
|
|
$existingItems = $this->getComponents();
|
|
|
|
$this->components = new ArrayList;
|
|
|
|
$inserted = false;
|
|
|
|
foreach($existingItems as $existingItem) {
|
|
|
|
if(!$inserted && $existingItem instanceof $insertBefore) {
|
|
|
|
$this->components->push($component);
|
|
|
|
$inserted = true;
|
|
|
|
}
|
|
|
|
$this->components->push($existingItem);
|
|
|
|
}
|
|
|
|
if(!$inserted) $this->components->push($component);
|
|
|
|
} else {
|
|
|
|
$this->getComponents()->push($component);
|
|
|
|
}
|
2011-12-06 01:56:24 +01:00
|
|
|
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
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-03-08 08:00:22 +01:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* @param GridFieldComponent $component
|
2012-03-08 08:00:22 +01:00
|
|
|
* @return GridFieldConfig $this
|
|
|
|
*/
|
|
|
|
public function removeComponent(GridFieldComponent $component) {
|
|
|
|
$this->getComponents()->remove($component);
|
2014-08-15 08:53:05 +02:00
|
|
|
return $this;
|
2012-03-08 08:00:22 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-03-08 08:00:22 +01:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
}
|
2014-08-15 08:53:05 +02: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.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-01-30 15:47:22 +01:00
|
|
|
* @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.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-01-30 15:47:22 +01:00
|
|
|
* @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
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* A simple readonly, paginated view of records, with sortable and searchable
|
2013-05-20 12:18:07 +02:00
|
|
|
* headers.
|
|
|
|
*
|
2013-11-29 05:12:47 +01:00
|
|
|
* @package forms
|
2013-05-20 12:18:07 +02:00
|
|
|
* @subpackage fields-gridfield
|
2012-03-05 12:27:25 +01:00
|
|
|
*/
|
2012-01-24 00:36:25 +01:00
|
|
|
class GridFieldConfig_Base extends GridFieldConfig {
|
2013-05-20 12:18:07 +02: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) {
|
2013-08-08 07:12:40 +02:00
|
|
|
parent::__construct();
|
2012-03-09 00:54:02 +01:00
|
|
|
$this->addComponent(new GridFieldToolbarHeader());
|
2012-03-09 02:32:16 +01:00
|
|
|
$this->addComponent($sort = new GridFieldSortableHeader());
|
|
|
|
$this->addComponent($filter = new GridFieldFilterHeader());
|
2012-03-09 02:07:40 +01:00
|
|
|
$this->addComponent(new GridFieldDataColumns());
|
2012-09-25 05:55:59 +02:00
|
|
|
$this->addComponent(new GridFieldPageCount('toolbar-header-right'));
|
2012-03-09 02:32:16 +01:00
|
|
|
$this->addComponent($pagination = new GridFieldPaginator($itemsPerPage));
|
|
|
|
|
2012-03-10 02:44:12 +01:00
|
|
|
$sort->setThrowExceptionOnBadDataType(false);
|
|
|
|
$filter->setThrowExceptionOnBadDataType(false);
|
|
|
|
$pagination->setThrowExceptionOnBadDataType(false);
|
2013-08-08 07:12:40 +02:00
|
|
|
|
|
|
|
$this->extend('updateConfig');
|
2011-12-06 01:56:24 +01:00
|
|
|
}
|
2012-01-24 00:36:25 +01:00
|
|
|
}
|
|
|
|
|
2012-05-11 09:44:39 +02:00
|
|
|
/**
|
|
|
|
* Allows viewing readonly details of individual records.
|
2013-05-20 12:18:07 +02:00
|
|
|
*
|
2013-11-29 05:12:47 +01:00
|
|
|
* @package forms
|
2013-05-20 12:18:07 +02:00
|
|
|
* @subpackage fields-gridfield
|
2012-05-11 09:44:39 +02:00
|
|
|
*/
|
|
|
|
class GridFieldConfig_RecordViewer extends GridFieldConfig_Base {
|
|
|
|
|
|
|
|
public function __construct($itemsPerPage = null) {
|
|
|
|
parent::__construct($itemsPerPage);
|
|
|
|
|
|
|
|
$this->addComponent(new GridFieldViewButton());
|
|
|
|
$this->addComponent(new GridFieldDetailForm());
|
2013-08-08 07:12:40 +02:00
|
|
|
|
|
|
|
$this->extend('updateConfig');
|
2012-05-11 09:44:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-01-24 00:36:25 +01:00
|
|
|
/**
|
2013-11-29 05:12:47 +01:00
|
|
|
* Allows editing of records contained within the GridField, instead of only allowing the ability to view records in
|
|
|
|
* the GridField.
|
|
|
|
*
|
|
|
|
* @package forms
|
2013-05-20 12:18:07 +02:00
|
|
|
* @subpackage fields-gridfield
|
2012-03-05 12:14:24 +01:00
|
|
|
*/
|
|
|
|
class GridFieldConfig_RecordEditor extends GridFieldConfig {
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param int $itemsPerPage - How many items per page should show up
|
|
|
|
*/
|
2012-03-05 13:30:07 +01:00
|
|
|
public function __construct($itemsPerPage=null) {
|
2013-08-08 07:12:40 +02:00
|
|
|
parent::__construct();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-21 04:41:46 +02:00
|
|
|
$this->addComponent(new GridFieldButtonRow('before'));
|
|
|
|
$this->addComponent(new GridFieldAddNewButton('buttons-before-left'));
|
|
|
|
$this->addComponent(new GridFieldToolbarHeader());
|
2012-03-09 02:32:16 +01:00
|
|
|
$this->addComponent($sort = new GridFieldSortableHeader());
|
|
|
|
$this->addComponent($filter = new GridFieldFilterHeader());
|
2012-03-09 02:07:40 +01:00
|
|
|
$this->addComponent(new GridFieldDataColumns());
|
2012-03-09 00:54:02 +01:00
|
|
|
$this->addComponent(new GridFieldEditButton());
|
2012-03-06 12:57:16 +01:00
|
|
|
$this->addComponent(new GridFieldDeleteAction());
|
2012-09-25 05:55:59 +02:00
|
|
|
$this->addComponent(new GridFieldPageCount('toolbar-header-right'));
|
2012-03-09 02:32:16 +01:00
|
|
|
$this->addComponent($pagination = new GridFieldPaginator($itemsPerPage));
|
2012-03-09 00:54:02 +01:00
|
|
|
$this->addComponent(new GridFieldDetailForm());
|
2012-03-09 02:32:16 +01:00
|
|
|
|
2012-03-10 02:44:12 +01:00
|
|
|
$sort->setThrowExceptionOnBadDataType(false);
|
|
|
|
$filter->setThrowExceptionOnBadDataType(false);
|
|
|
|
$pagination->setThrowExceptionOnBadDataType(false);
|
2013-08-08 07:12:40 +02:00
|
|
|
|
|
|
|
$this->extend('updateConfig');
|
2012-03-05 12:14:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* Similar to {@link GridFieldConfig_RecordEditor}, but adds features to work
|
|
|
|
* on has-many or many-many relationships.
|
2013-05-20 12:18:07 +02:00
|
|
|
*
|
2014-08-15 08:53:05 +02:00
|
|
|
* Allows to search for existing records to add to the relationship, detach
|
|
|
|
* listed records from the relationship (rather than removing them from the
|
2013-05-20 12:18:07 +02:00
|
|
|
* database), and automatically add newly created records to it.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
|
|
|
* To further configure the field, use {@link getComponentByType()}, for
|
2013-05-20 12:18:07 +02:00
|
|
|
* example to change the field to search.
|
|
|
|
*
|
2012-03-05 12:14:24 +01:00
|
|
|
* <code>
|
|
|
|
* GridFieldConfig_RelationEditor::create()
|
2013-05-20 12:18:07 +02:00
|
|
|
* ->getComponentByType('GridFieldAddExistingAutocompleter')
|
|
|
|
* ->setSearchFields('MyField');
|
2012-03-05 12:14:24 +01:00
|
|
|
* </code>
|
2013-05-20 12:18:07 +02:00
|
|
|
*
|
2013-11-29 05:12:47 +01:00
|
|
|
* @package forms
|
2013-05-20 12:18:07 +02:00
|
|
|
* @subpackage fields-gridfield
|
2012-01-24 00:36:25 +01:00
|
|
|
*/
|
2012-03-05 12:14:24 +01:00
|
|
|
class GridFieldConfig_RelationEditor extends GridFieldConfig {
|
2014-08-15 08:53:05 +02: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) {
|
2013-08-08 07:12:40 +02:00
|
|
|
parent::__construct();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-21 04:41:46 +02:00
|
|
|
$this->addComponent(new GridFieldButtonRow('before'));
|
|
|
|
$this->addComponent(new GridFieldAddNewButton('buttons-before-left'));
|
2013-10-22 17:44:28 +02:00
|
|
|
$this->addComponent(new GridFieldAddExistingAutocompleter('buttons-before-right'));
|
2012-03-09 00:54:02 +01:00
|
|
|
$this->addComponent(new GridFieldToolbarHeader());
|
2012-03-09 02:32:16 +01:00
|
|
|
$this->addComponent($sort = new GridFieldSortableHeader());
|
|
|
|
$this->addComponent($filter = new GridFieldFilterHeader());
|
2012-03-09 02:07:40 +01:00
|
|
|
$this->addComponent(new GridFieldDataColumns());
|
2012-03-09 00:54:02 +01:00
|
|
|
$this->addComponent(new GridFieldEditButton());
|
2012-10-15 18:41:11 +02:00
|
|
|
$this->addComponent(new GridFieldDeleteAction(true));
|
2012-09-25 05:55:59 +02:00
|
|
|
$this->addComponent(new GridFieldPageCount('toolbar-header-right'));
|
2012-03-09 02:32:16 +01:00
|
|
|
$this->addComponent($pagination = new GridFieldPaginator($itemsPerPage));
|
2012-03-09 00:54:02 +01:00
|
|
|
$this->addComponent(new GridFieldDetailForm());
|
2012-03-09 02:32:16 +01:00
|
|
|
|
2012-03-10 02:44:12 +01:00
|
|
|
$sort->setThrowExceptionOnBadDataType(false);
|
|
|
|
$filter->setThrowExceptionOnBadDataType(false);
|
|
|
|
$pagination->setThrowExceptionOnBadDataType(false);
|
2013-08-08 07:12:40 +02:00
|
|
|
|
|
|
|
$this->extend('updateConfig');
|
2011-12-06 01:56:24 +01:00
|
|
|
}
|
|
|
|
}
|