Updated widget system to support forms

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@50479 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Sam Minnee 2008-03-03 08:48:52 +00:00
parent 59d8a1ce8e
commit 848631558e
3 changed files with 46 additions and 15 deletions

View File

@ -147,20 +147,10 @@ class Controller extends ViewableData {
Form::set_current_action($funcName); Form::set_current_action($funcName);
} }
// Get the appropraite ocntroller: sometimes we want to get a form from another controller $formOwner = $this->getFormOwner();
if(isset($this->requestParams['formController'])) {
$formController = Director::getControllerForURL($this->requestParams['formController']);
while(is_a($formController, 'NestedController')) {
$formController = $formController->getNestedController();
}
} else {
$formController = $this;
}
// Create the form object // Create the form object
$form = $formController; $form = $formOwner;
$formObjParts = explode('.', $this->requestParams['executeForm']); $formObjParts = explode('.', $this->requestParams['executeForm']);
foreach($formObjParts as $formMethod){ foreach($formObjParts as $formMethod){
@ -204,7 +194,7 @@ class Controller extends ViewableData {
} }
}else{ }else{
user_error("No form (" . Session::get('CMSMain.currentPage') . ") returned by $formController->class->$_REQUEST[executeForm]", E_USER_WARNING); user_error("No form (" . Session::get('CMSMain.currentPage') . ") returned by $formOwner->class->$_REQUEST[executeForm]", E_USER_WARNING);
} }
if(isset($_GET['debug_profile'])) Profiler::unmark("Controller", "populate form"); if(isset($_GET['debug_profile'])) Profiler::unmark("Controller", "populate form");
@ -236,6 +226,9 @@ class Controller extends ViewableData {
if(isset($_GET['debug_profile'])) Profiler::mark("$this->class::$funcName (controller action)"); if(isset($_GET['debug_profile'])) Profiler::mark("$this->class::$funcName (controller action)");
$result = $this->$funcName($this->requestParams, $form); $result = $this->$funcName($this->requestParams, $form);
if(isset($_GET['debug_profile'])) Profiler::unmark("$this->class::$funcName (controller action)"); if(isset($_GET['debug_profile'])) Profiler::unmark("$this->class::$funcName (controller action)");
} else if(isset($formOwner) && $formOwner->hasMethod($funcName)) {
$result = $formOwner->$funcName($this->requestParams, $form);
// Otherwise, try a handler method on the form object // Otherwise, try a handler method on the form object
} else { } else {
@ -290,6 +283,25 @@ class Controller extends ViewableData {
$this->popCurrent(); $this->popCurrent();
return $this->response; return $this->response;
} }
/**
* Return the object that is going to own a form that's being processed, and handle its execution.
* Note that the result needn't be an actual controller object.
*/
function getFormOwner() {
// Get the appropraite ocntroller: sometimes we want to get a form from another controller
if(isset($this->requestParams['formController'])) {
$formController = Director::getControllerForURL($this->requestParams['formController']);
while(is_a($formController, 'NestedController')) {
$formController = $formController->getNestedController();
}
return $formController;
} else {
return $this;
}
}
/** /**
* This is the default action handler used if a method doesn't exist. * This is the default action handler used if a method doesn't exist.

View File

@ -94,7 +94,14 @@ class Widget extends DataObject {
$this->Name = $this->class.$this->ID; $this->Name = $this->class.$this->ID;
$this->write(); $this->write();
} }
function FormObjectLink($formName) {
if(is_numeric($this->ID)) {
return "WidgetFormProxy/index/$this->ID?executeForm=$formName";
} else {
user_error("Attempted to create a form on a widget that hasn't been saved to the database.", E_USER_WARNING);
}
}
} }
?> ?>

View File

@ -0,0 +1,12 @@
<?php
class WidgetFormProxy extends Controller {
function getFormOwner() {
$widget = DataObject::get_by_id("Widget", $this->urlParams['ID']);
// Put this in once widget->canView is implemented
//if($widget->canView())
return $widget;
}
}