mirror of
https://github.com/symbiote/silverstripe-gridfieldextensions.git
synced 2024-10-22 17:05:39 +02:00
Add GridFieldAddNewMultiClass component.
This component allows the user to select from a list of classes when creating a new record.
This commit is contained in:
parent
8e8590bd06
commit
d7a9d470f9
@ -4,6 +4,7 @@ SilverStripe Grid Field Extensions Module
|
||||
This module provides a number of useful grid field components:
|
||||
|
||||
* `GridFieldAddExistingSearchButton` - a more advanced search form for adding items.
|
||||
* `GridFieldAddNewMultiClass` - lets the user select from a list of classes to create a new record from.
|
||||
* `GridFieldOrderableRows` - drag and drop re-ordering of rows.
|
||||
|
||||
Maintainer Contacts
|
||||
|
162
code/GridFieldAddNewMultiClass.php
Normal file
162
code/GridFieldAddNewMultiClass.php
Normal file
@ -0,0 +1,162 @@
|
||||
<?php
|
||||
/**
|
||||
* A component which lets the user select from a list of classes to create a new record form.
|
||||
*
|
||||
* By default the list of classes that are createable is the grid field's model class, and any
|
||||
* subclasses. This can be customised using {@link setClasses()}.
|
||||
*/
|
||||
class GridFieldAddNewMultiClass implements GridField_HTMLProvider, GridField_URLHandler {
|
||||
|
||||
private $fragment;
|
||||
|
||||
private $title;
|
||||
|
||||
private $classes;
|
||||
|
||||
/**
|
||||
* @param string $fragment the fragment to render the button in
|
||||
*/
|
||||
public function __construct($fragment = 'before') {
|
||||
$this->setFragment($fragment);
|
||||
$this->setTitle(_t('GridFieldExtensions.ADD', 'Add'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the fragment name this button is rendered into.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getFragment() {
|
||||
return $this->fragment;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the fragment name this button is rendered into.
|
||||
*
|
||||
* @param string $fragment
|
||||
*/
|
||||
public function setFragment($fragment) {
|
||||
$this->fragment = $fragment;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the button title text.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getTitle() {
|
||||
return $this->title;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the button title text.
|
||||
*
|
||||
* @param string $title
|
||||
*/
|
||||
public function setTitle($title) {
|
||||
$this->title = $title;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the classes that can be created using this button, defaulting to the model class and
|
||||
* its subclasses.
|
||||
*
|
||||
* @param GridField $grid
|
||||
* @return array a map of class name to title
|
||||
*/
|
||||
protected function getClasses($grid) {
|
||||
$result = array();
|
||||
|
||||
if(is_null($this->classes)) {
|
||||
$classes = array_values(ClassInfo::subclassesFor($grid->getModelClass()));
|
||||
} else {
|
||||
$classes = $this->classes;
|
||||
}
|
||||
|
||||
foreach($classes as $class => $title) {
|
||||
if(!is_string($class)) {
|
||||
$class = $title;
|
||||
$title = singleton($class)->i18n_singular_name();
|
||||
}
|
||||
|
||||
if(!singleton($class)->canCreate()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$result[$class] = $title;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the classes that can be created using this button.
|
||||
*
|
||||
* @param array $classes a set of class names, optionally mapped to titles
|
||||
*/
|
||||
public function setClasses(array $classes) {
|
||||
$this->classes = $classes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles adding a new instance of a selected class.
|
||||
*
|
||||
* @param GridField $grid
|
||||
* @param SS_HTTPRequest $request
|
||||
*/
|
||||
public function handleAdd($grid, $request) {
|
||||
$class = $request->param('ClassName');
|
||||
$classes = $this->getClasses($grid);
|
||||
$component = $grid->getConfig()->getComponentByType('GridFieldDetailForm');
|
||||
|
||||
if(!$component) {
|
||||
throw new Exception('The add new multi class component requires the detail form component.');
|
||||
}
|
||||
|
||||
if(!$class || !array_key_exists($class, $classes)) {
|
||||
throw new SS_HTTPResponse_Exception(400);
|
||||
}
|
||||
|
||||
$handler = new GridFieldAddNewMultiClassHandler(
|
||||
$grid, $component, new $class(), $grid->getForm()->getController(), 'add-multi-class'
|
||||
);
|
||||
$handler->setTemplate($component->getTemplate());
|
||||
|
||||
return $handler;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getHTMLFragments($grid) {
|
||||
$classes = $this->getClasses($grid);
|
||||
|
||||
if(!count($classes)) {
|
||||
return array();
|
||||
}
|
||||
|
||||
$field = new DropdownField(sprintf('%s[ClassName]', __CLASS__), '', $classes);
|
||||
$field->setEmptyString(_t('GridFieldExtensions.SELECTTYPETOCREATE', '(Select type to create)'));
|
||||
$field->addExtraClass('no-change-track');
|
||||
|
||||
$data = new ArrayData(array(
|
||||
'Title' => $this->getTitle(),
|
||||
'Link' => Controller::join_links($grid->Link(), 'add-multi-class'),
|
||||
'ClassField' => $field
|
||||
));
|
||||
|
||||
return array(
|
||||
$this->getFragment() => $data->renderWith(__CLASS__)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getURLHandlers($grid) {
|
||||
return array(
|
||||
'add-multi-class/$ClassName!' => 'handleAdd'
|
||||
);
|
||||
}
|
||||
|
||||
}
|
17
code/GridFieldAddNewMultiClassHandler.php
Normal file
17
code/GridFieldAddNewMultiClassHandler.php
Normal file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
/**
|
||||
* A custom grid field request handler that allows interacting with form fields when adding records.
|
||||
*/
|
||||
class GridFieldAddNewMultiClassHandler extends GridFieldDetailForm_ItemRequest {
|
||||
|
||||
public function Link($action = null) {
|
||||
if($this->record->ID) {
|
||||
return parent::Link($action);
|
||||
} else {
|
||||
return Controller::join_links(
|
||||
$this->gridField->Link(), 'add-multi-class', get_class($this->record)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -55,6 +55,21 @@
|
||||
padding: 6px;
|
||||
}
|
||||
|
||||
/**
|
||||
* GridFieldAddNewMultiClass
|
||||
*/
|
||||
|
||||
.ss-gridfield-add-new-multi-class {
|
||||
margin-bottom: 8px !important;
|
||||
}
|
||||
|
||||
.ss-gridfield-add-new-multi-class .field {
|
||||
border: none;
|
||||
box-shadow: none;
|
||||
float: left;
|
||||
margin: 0 4px 0 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* GridFieldOrderableRows
|
||||
*/
|
||||
|
@ -72,6 +72,41 @@
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* GridFieldAddNewMultiClass
|
||||
*/
|
||||
|
||||
$(".ss-gridfield-add-new-multi-class .ss-ui-button").entwine({
|
||||
onclick: function() {
|
||||
var link = this.prop("href");
|
||||
var cls = this.parents(".ss-gridfield-add-new-multi-class").find("select").val();
|
||||
|
||||
if(cls && cls.length) {
|
||||
this.getGridField().showDetailView(link + "/" + cls);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
||||
$(".ss-gridfield-add-new-multi-class select").entwine({
|
||||
onadd: function() {
|
||||
this.update();
|
||||
},
|
||||
onchange: function() {
|
||||
this.update();
|
||||
},
|
||||
update: function() {
|
||||
var btn = this.parents(".ss-gridfield-add-new-multi-class").find(".ss-ui-button");
|
||||
|
||||
if(this.val() && this.val().length) {
|
||||
btn.button("enable");
|
||||
} else {
|
||||
btn.button("disable");
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* GridFieldOrderableRows
|
||||
*/
|
||||
|
7
templates/GridFieldAddNewMultiClass.ss
Normal file
7
templates/GridFieldAddNewMultiClass.ss
Normal file
@ -0,0 +1,7 @@
|
||||
<div class="ss-gridfield-add-new-multi-class">
|
||||
$ClassField.FieldHolder
|
||||
|
||||
<a href="$Link" class="ss-ui-action-constructive ss-ui-button" data-icon="add">
|
||||
$Title
|
||||
</a>
|
||||
</div>
|
Loading…
Reference in New Issue
Block a user