Permalink
Browse files

FEATURE "Add" for gridfield

  • Loading branch information...
1 parent 38bce79 commit 0acaf396644de44e3b95f2e522b18b04f201a72f @andrewandante andrewandante committed Feb 20, 2012
@@ -56,10 +56,16 @@ public function __construct($popupController, $popupFormName) {
* @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 @@ public function __construct($gridField, $component, $record, $popupController, $
}
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 @@ function ItemEditForm() {
}
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();
@@ -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')
);
}
}
@@ -1,3 +1,3 @@
<tr class="title">
- <th colspan="$ColumnCount">$Title</th>
+ <th colspan="$ColumnCount">$Title <a href="$NewLink">new</a></th>
</tr>

0 comments on commit 0acaf39

Please sign in to comment.