Source of file AbcGridFieldConfig.php
Size: 8,039 Bytes - Last Modified: 2022-01-13T10:01:10+00:00
/var/www/docs.ssmods.com/process/src/src/abc/code/Forms/GridField/AbcGridFieldConfig.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 | <?php /** * 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} */ class AbcGridFieldConfig extends GridFieldConfig{ /** * * @return GridFieldConfig */ public static function create(){ return new self; } /** * * @var ArrayList */ protected $components = null; /** * */ public function __construct() { $this->components = new ArrayList(); } /** * @param GridFieldComponent $component * @param string $insertBefore The class of the component to insert this one before */ 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); } return $this; } /** * @param GridFieldComponent One or more components */ public function addComponents() { $components = func_get_args(); foreach($components as $component) $this->addComponent($component); return $this; } /** * @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; } /** * @return ArrayList Of GridFieldComponent */ public function getComponents() { if(!$this->components) { $this->components = new ArrayList(); } return $this->components; } /** * 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; } } } /** * A simple readonly, paginated view of records, * with sortable and searchable headers. */ class AbcGridFieldConfig_Base extends GridFieldConfig_Base { /** * * @param int $itemsPerPage - How many items per page should show up per page * @return GridFieldConfig_Base */ public static function create($itemsPerPage=null){ return new self($itemsPerPage); } /** * * @param int $itemsPerPage - How many items per page should show up */ public function __construct($itemsPerPage=null) { $this->addComponent(new GridFieldToolbarHeader()); $this->addComponent($sort = new GridFieldSortableHeader()); $this->addComponent($filter = new GridFieldFilterHeader()); $this->addComponent(new GridFieldDataColumns()); $this->addComponent($pagination = new GridFieldPaginator($itemsPerPage)); $sort->setThrowExceptionOnBadDataType(false); $filter->setThrowExceptionOnBadDataType(false); $pagination->setThrowExceptionOnBadDataType(false); } } /** * Allows viewing readonly details of individual records. */ class AbcGridFieldConfig_RecordViewer extends GridFieldConfig_RecordViewer { public function __construct($itemsPerPage = null) { parent::__construct($itemsPerPage); $this->addComponent(new GridFieldViewButton()); $this->addComponent(new AbcGridFieldDetailForm()); } } /** * */ class AbcGridFieldConfig_RecordEditor extends GridFieldConfig_RecordEditor { /** * * @param int $itemsPerPage - How many items per page should show up * @return GridFieldConfig_RecordEditor */ public static function create($itemsPerPage=null){ return new self($itemsPerPage); } /** * * @param int $itemsPerPage - How many items per page should show up */ public function __construct($itemsPerPage=null) { $this->addComponent(new GridFieldButtonRow('before')); $this->addComponent(new GridFieldAddNewButton('buttons-before-left')); $this->addComponent(new GridFieldToolbarHeader()); $this->addComponent($sort = new GridFieldSortableHeader()); $this->addComponent($filter = new GridFieldFilterHeader()); $this->addComponent(new GridFieldDataColumns()); $this->addComponent(new GridFieldEditButton()); $this->addComponent(new GridFieldDeleteAction()); $this->addComponent($pagination = new GridFieldPaginator($itemsPerPage)); $this->addComponent(new AbcGridFieldDetailForm()); $sort->setThrowExceptionOnBadDataType(false); $filter->setThrowExceptionOnBadDataType(false); $pagination->setThrowExceptionOnBadDataType(false); } } /** * 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. * * To further configure the field, use {@link getComponentByType()}, * for example to change the field to search. * <code> * GridFieldConfig_RelationEditor::create() * ->getComponentByType('GridFieldAddExistingAutocompleter')->setSearchFields('MyField'); * </code> */ class AbcGridFieldConfig_RelationEditor extends GridFieldConfig_RelationEditor { /** * * @param int $itemsPerPage - How many items per page should show up * @return GridFieldConfig_RelationEditor */ public static function create($itemsPerPage=null){ return new self($itemsPerPage); } /** * * @param int $itemsPerPage - How many items per page should show up */ public function __construct($itemsPerPage=null) { $this->addComponent(new GridFieldButtonRow('before')); $this->addComponent(new GridFieldAddNewButton('buttons-before-left')); $this->addComponent(new GridFieldAddExistingAutocompleter('buttons-before-left')); $this->addComponent(new GridFieldToolbarHeader()); $this->addComponent($sort = new GridFieldSortableHeader()); $this->addComponent($filter = new GridFieldFilterHeader()); $this->addComponent(new GridFieldDataColumns()); $this->addComponent(new GridFieldEditButton()); $this->addComponent(new GridFieldDeleteAction()); $this->addComponent($pagination = new GridFieldPaginator($itemsPerPage)); $this->addComponent(new AbcGridFieldDetailForm()); $sort->setThrowExceptionOnBadDataType(false); $filter->setThrowExceptionOnBadDataType(false); $pagination->setThrowExceptionOnBadDataType(false); } } |