From 0acaf396644de44e3b95f2e522b18b04f201a72f Mon Sep 17 00:00:00 2001 From: Andrew O'Neil Date: Mon, 20 Feb 2012 14:50:53 +1300 Subject: [PATCH] FEATURE "Add" for gridfield --- forms/gridfield/GridFieldPopupForms.php | 16 +++++++++++++--- forms/gridfield/GridFieldTitle.php | 4 +++- templates/Includes/GridFieldTitle.ss | 2 +- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/forms/gridfield/GridFieldPopupForms.php b/forms/gridfield/GridFieldPopupForms.php index dc3425141..edcf9dc34 100755 --- a/forms/gridfield/GridFieldPopupForms.php +++ b/forms/gridfield/GridFieldPopupForms.php @@ -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(); diff --git a/forms/gridfield/GridFieldTitle.php b/forms/gridfield/GridFieldTitle.php index 1adc78b55..afb4480d6 100644 --- a/forms/gridfield/GridFieldTitle.php +++ b/forms/gridfield/GridFieldTitle.php @@ -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') ); } } diff --git a/templates/Includes/GridFieldTitle.ss b/templates/Includes/GridFieldTitle.ss index 984a94493..7dd7a06f3 100644 --- a/templates/Includes/GridFieldTitle.ss +++ b/templates/Includes/GridFieldTitle.ss @@ -1,3 +1,3 @@ - $Title + $Title new \ No newline at end of file