2013-02-08 16:19:30 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Builds on the {@link GridFieldEditableColumns} component to allow creating new records.
|
|
|
|
*/
|
|
|
|
class GridFieldAddNewInlineButton implements GridField_HTMLProvider, GridField_SaveHandler {
|
|
|
|
|
|
|
|
private $fragment;
|
|
|
|
|
|
|
|
private $title;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $fragment the fragment to render the button in
|
|
|
|
*/
|
|
|
|
public function __construct($fragment = 'buttons-before-left') {
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getHTMLFragments($grid) {
|
|
|
|
$class = $grid->getModelClass();
|
|
|
|
$fragment = $this->getFragment();
|
|
|
|
|
|
|
|
if(!$editable = $grid->getConfig()->getComponentByType('GridFieldEditableColumns')) {
|
|
|
|
throw new Exception('Inline adding requires the editable columns component');
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!singleton($class)->canCreate()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Requirements::javascript(THIRDPARTY_DIR . '/javascript-templates/tmpl.js');
|
|
|
|
GridFieldExtensions::include_requirements();
|
|
|
|
|
|
|
|
$data = new ArrayData(array(
|
|
|
|
'Title' => $this->getTitle(),
|
|
|
|
));
|
|
|
|
|
|
|
|
return array(
|
|
|
|
$fragment => $data->renderWith(__CLASS__),
|
|
|
|
'after' => $this->getRowTemplate($grid, $editable)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getRowTemplate(GridField $grid, GridFieldEditableColumns $editable) {
|
|
|
|
$class = $grid->getModelClass();
|
|
|
|
|
|
|
|
$columns = new ArrayList();
|
|
|
|
$handled = array_keys($editable->getDisplayFields($grid));
|
|
|
|
|
|
|
|
$record = new $class();
|
|
|
|
$fields = $editable->getFields($grid, $record);
|
|
|
|
|
|
|
|
foreach($grid->getColumns() as $column) {
|
|
|
|
if(in_array($column, $handled)) {
|
|
|
|
$field = $fields->dataFieldByName($column);
|
|
|
|
$field->setName(sprintf(
|
|
|
|
'%s[%s][{%%=o.num%%}][%s]', $grid->getName(), __CLASS__, $field->getName()
|
|
|
|
));
|
|
|
|
|
|
|
|
$content = $field->Field();
|
|
|
|
} else {
|
|
|
|
$content = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$attrs = '';
|
|
|
|
|
|
|
|
foreach($grid->getColumnAttributes($record, $column) as $attr => $val) {
|
|
|
|
$attrs .= sprintf(' %s="%s"', $attr, Convert::raw2att($val));
|
|
|
|
}
|
|
|
|
|
|
|
|
$columns->push(new ArrayData(array(
|
|
|
|
'Content' => $content,
|
|
|
|
'Attributes' => $attrs,
|
|
|
|
'IsActions' => $column == 'Actions'
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $columns->renderWith('GridFieldAddNewInlineRow');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function handleSave(GridField $grid, DataObjectInterface $record) {
|
2013-02-10 06:59:06 +01:00
|
|
|
$list = $grid->getList();
|
2013-02-08 16:19:30 +01:00
|
|
|
$value = $grid->Value();
|
|
|
|
|
|
|
|
if(!isset($value[__CLASS__]) || !is_array($value[__CLASS__])) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$class = $grid->getModelClass();
|
|
|
|
$editable = $grid->getConfig()->getComponentByType('GridFieldEditableColumns');
|
|
|
|
$form = $editable->getForm($grid, $record);
|
|
|
|
|
|
|
|
if(!singleton($class)->canCreate()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach($value[__CLASS__] as $fields) {
|
2013-02-10 06:59:06 +01:00
|
|
|
$item = new $class();
|
|
|
|
$extra = array();
|
|
|
|
|
|
|
|
if($list instanceof ManyManyList) {
|
2013-03-03 09:21:42 +01:00
|
|
|
$extra = array_intersect_key($fields, (array) $list->getExtraFields());
|
2013-02-10 06:59:06 +01:00
|
|
|
}
|
2013-02-08 16:19:30 +01:00
|
|
|
|
|
|
|
$form->loadDataFrom($fields, Form::MERGE_CLEAR_MISSING);
|
|
|
|
$form->saveInto($item);
|
|
|
|
|
2013-02-10 06:59:06 +01:00
|
|
|
$item->write();
|
|
|
|
$list->add($item, $extra);
|
2013-02-08 16:19:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|