<?php
class AddNotePage extends Page {
public static
$db = array(
);
public static
$has_one = array(
);
}
class AddNotePage_Controller extends Page_Controller {
public function AddForm() {
if($noteIDraw = Director::URLParam('ID')){
$noteID = Convert::raw2xml($noteIDraw);
Session::set('NoteID', $noteID);
}
if($noteID = Session::get('NoteID')){
$newRecord = DataObject::get_by_id('Note', $noteID);
$buttonName = "Update";
}else{
$newRecord = new Note();
$buttonName = "New Note";
}
if($newRecord->canCreate()){
if($newRecord->hasMethod('getCMSAddFormFields')) {
$fields = $newRecord->getCMSAddFormFields();
} else {
$fields = $newRecord->getCMSFields();
}
$validator = ($newRecord->hasMethod('getCMSValidator')) ? $newRecord->getCMSValidator() : null;
$actions = new FieldSet (
new FormAction("doCreate", $buttonName)
);
$form = new Form($this, "AddForm", $fields, $actions, $validator);
$form->loadDataFrom($newRecord);
return $form;
}
}
public function doCreate($data, $form, $request) {
if($NoteID = Session::get('NoteID')){
$newRecord = DataObject::get_by_id('Note', $NoteID);
Session::clear('NoteID');
}else{
// die();
$newRecord = new Note();
}
// We write before saveInto, since this will let us save has-many and many-many relationships :-)
$newRecord->write();
$form->saveInto($newRecord);
$newRecord->write();
// Director::redirectBack();
Director::redirect($this->Link().'edit/'.$newRecord->ID);
}
public function newNote(){
Session::clear('NoteID');
Director::redirect($this->Link());
}
}
?>