FEATURE "Add" for gridfield

This commit is contained in:
Andrew O'Neil 2012-02-20 14:50:53 +13:00
parent 38bce79fc6
commit 0acaf39664
3 changed files with 17 additions and 5 deletions

View File

@ -56,10 +56,16 @@ class GridFieldPopupForms implements GridField_URLHandler {
* @return GridFieldPopupForm_ItemRequest
*/
public function handleItem($gridField, $request) {
$record = $gridField->getList()->byId($request->param("ID"));
if(is_numeric($request->param('ID'))) {
$record = $gridField->getList()->byId($request->param("ID"));
} else {
$record = Object::create($gridField->getModelClass());
}
$handler = new GridFieldPopupForm_ItemRequest($gridField, $this, $record, $this->popupController, $this->popupFormName);
$handler->setTemplate($this->template);
return $handler;
return $handler->handleRequest($request, $gridField);
}
/**
@ -137,7 +143,7 @@ class GridFieldPopupForm_ItemRequest extends RequestHandler {
}
public function Link($action = null) {
return Controller::join_links($this->gridField->Link('item'), $this->record->ID, $action);
return Controller::join_links($this->gridField->Link('item'), $this->record->ID ? $this->record->ID : 'new', $action);
}
function edit($request) {
@ -186,9 +192,13 @@ class GridFieldPopupForm_ItemRequest extends RequestHandler {
}
function doSave($data, $form) {
$new_record = $this->record->ID == 0;
try {
$form->saveInto($this->record);
$this->record->write();
if($new_record)
$this->gridField->getList()->add($this->record);
} catch(ValidationException $e) {
$form->sessionMessage($e->getResult()->message(), 'bad');
return Director::redirectBack();

View File

@ -3,7 +3,9 @@
class GridFieldTitle implements GridField_HTMLProvider {
function getHTMLFragments($gridField) {
return array(
'header' => $gridField->renderWith('GridFieldTitle')
'header' => $gridField->customise(array(
'NewLink' => Controller::join_links($gridField->Link('item'), 'new')
))->renderWith('GridFieldTitle')
);
}
}

View File

@ -1,3 +1,3 @@
<tr class="title">
<th colspan="$ColumnCount">$Title</th>
<th colspan="$ColumnCount">$Title <a href="$NewLink">new</a></th>
</tr>