Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Feb 8th, 2010 | Syntax: PHP | Size: 1.70 KB | Hits: 112 | Expires: Never
Copy text to clipboard
  1. <?php
  2.  
  3. class AddNotePage extends Page {
  4.        
  5.         public static $db = array(
  6.         );
  7.        
  8.         public static $has_one = array(
  9.         );
  10.        
  11. }
  12.  
  13. class AddNotePage_Controller extends Page_Controller {
  14.        
  15.         public function AddForm() {
  16.  
  17.                 if($noteIDraw = Director::URLParam('ID')){
  18.                         $noteID = Convert::raw2xml($noteIDraw);
  19.                         Session::set('NoteID', $noteID);
  20.                 }
  21.                
  22.                 if($noteID = Session::get('NoteID')){
  23.                         $newRecord = DataObject::get_by_id('Note', $noteID);
  24.                         $buttonName = "Update";
  25.                 }else{
  26.                         $newRecord = new Note();
  27.                         $buttonName = "New Note";
  28.                 }
  29.  
  30.  
  31.                 if($newRecord->canCreate()){
  32.                         if($newRecord->hasMethod('getCMSAddFormFields')) {
  33.                                 $fields = $newRecord->getCMSAddFormFields();
  34.                         } else {
  35.                                 $fields = $newRecord->getCMSFields();
  36.                         }
  37.                        
  38.                         $validator = ($newRecord->hasMethod('getCMSValidator')) ? $newRecord->getCMSValidator() : null;
  39.                        
  40.                         $actions = new FieldSet (
  41.                                 new FormAction("doCreate", $buttonName)
  42.                         );
  43.                        
  44.                         $form = new Form($this, "AddForm", $fields, $actions, $validator);
  45.                         $form->loadDataFrom($newRecord);
  46.                        
  47.                         return $form;
  48.                 }
  49.         }
  50.        
  51.         public function doCreate($data, $form, $request) {
  52.                
  53.                 if($NoteID = Session::get('NoteID')){
  54.                         $newRecord = DataObject::get_by_id('Note', $NoteID);
  55.                         Session::clear('NoteID');
  56.                 }else{
  57. //                      die();
  58.                         $newRecord = new Note();
  59.                 }
  60.                
  61.  
  62.                 // We write before saveInto, since this will let us save has-many and many-many relationships :-)
  63.                 $newRecord->write();
  64.                 $form->saveInto($newRecord);
  65.                 $newRecord->write();
  66.  
  67. //              Director::redirectBack();      
  68.                 Director::redirect($this->Link().'edit/'.$newRecord->ID);
  69.         }
  70.        
  71.         public function newNote(){
  72.                 Session::clear('NoteID');
  73.                 Director::redirect($this->Link());
  74.         }
  75. }
  76.  
  77. ?>