mlantahler:Bugfix: Small bugfix to prevent the usage of uninitialized $funcName. Happens if the form is submitted by pressing <enter> instead of pressing the button (at least in IE7). (merged from branches/gsoc)

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@41779 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2007-09-14 18:02:03 +00:00
parent d47e6cf67c
commit 4ae8c85d46

View File

@ -5,7 +5,7 @@
* Controllers are the cornerstone of all site functionality in Sapphire. The {@link Director} * Controllers are the cornerstone of all site functionality in Sapphire. The {@link Director}
* selects a controller to pass control to, and then calls {@link run()}. This method will execute * selects a controller to pass control to, and then calls {@link run()}. This method will execute
* the appropriate action - either by calling the action method, or displaying the action's template. * the appropriate action - either by calling the action method, or displaying the action's template.
* *
* See {@link getTemplate()} for information on how the template is chosen. * See {@link getTemplate()} for information on how the template is chosen.
*/ */
class Controller extends ViewableData { class Controller extends ViewableData {
@ -44,7 +44,7 @@ class Controller extends ViewableData {
function getURLParams() { function getURLParams() {
return $this->urlParams; return $this->urlParams;
} }
/** /**
* Execute the appropriate action handler. If none is given, use defaultAction to display * Execute the appropriate action handler. If none is given, use defaultAction to display
* a template. The default action will be appropriate in most cases where displaying data * a template. The default action will be appropriate in most cases where displaying data
@ -76,6 +76,7 @@ class Controller extends ViewableData {
} }
// Look at the action variables for forms // Look at the action variables for forms
$funcName = null;
foreach($this->requestParams as $paramName => $paramVal) { foreach($this->requestParams as $paramName => $paramVal) {
if(substr($paramName,0,7) == 'action_') { if(substr($paramName,0,7) == 'action_') {
// Cleanup action_, _x and _y from image fields // Cleanup action_, _x and _y from image fields
@ -97,7 +98,7 @@ class Controller extends ViewableData {
while(is_a($formController, 'NestedController')) { while(is_a($formController, 'NestedController')) {
$formController = $formController->getNestedController(); $formController = $formController->getNestedController();
} }
} else { } else {
$formController = $this; $formController = $this;
} }
@ -112,10 +113,10 @@ class Controller extends ViewableData {
if(isset($_GET['debug_profile'])) Profiler::unmark("Calling $formMethod", "on $form->class"); if(isset($_GET['debug_profile'])) Profiler::unmark("Calling $formMethod", "on $form->class");
if(!$form) break; //user_error("Form method '" . $this->requestParams['executeForm'] . "' returns null in controller class '$this->class' ($_SERVER[REQUEST_URI])", E_USER_ERROR); if(!$form) break; //user_error("Form method '" . $this->requestParams['executeForm'] . "' returns null in controller class '$this->class' ($_SERVER[REQUEST_URI])", E_USER_ERROR);
} }
// Populate the form // Populate the form
if(isset($_GET['debug_profile'])) Profiler::mark("Controller", "populate form"); if(isset($_GET['debug_profile'])) Profiler::mark("Controller", "populate form");
if($form){ if($form){
$form->loadDataFrom($this->requestParams, true); $form->loadDataFrom($this->requestParams, true);
// disregard validation if a single field is called // disregard validation if a single field is called
@ -155,25 +156,26 @@ class Controller extends ViewableData {
user_error("No action button has been clicked in this form executon, and no default has been allowed", E_USER_ERROR); user_error("No action button has been clicked in this form executon, and no default has been allowed", E_USER_ERROR);
} }
// First, try a handler method on the controller // First, try a handler method on the controller
if($this->hasMethod($funcName) || !$form) { if($this->hasMethod($funcName) || !$form) {
if(isset($_GET['debug_controller'])){ if(isset($_GET['debug_controller'])){
Debug::show("Found function $funcName on the controller"); Debug::show("Found function $funcName on the controller");
} }
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)");
// Otherwise, try a handler method on the form object // Otherwise, try a handler method on the form object
} else { } else {
if(isset($_GET['debug_controller'])) { if(isset($_GET['debug_controller'])) {
Debug::show("Found function $funcName on the form object"); Debug::show("Found function $funcName on the form object");
} }
if(isset($_GET['debug_profile'])) Profiler::mark("$form->class::$funcName (form action)"); if(isset($_GET['debug_profile'])) Profiler::mark("$form->class::$funcName (form action)");
$result = $form->$funcName($this->requestParams, $form); $result = $form->$funcName($this->requestParams, $form);
if(isset($_GET['debug_profile'])) Profiler::unmark("$form->class::$funcName (form action)"); if(isset($_GET['debug_profile'])) Profiler::unmark("$form->class::$funcName (form action)");
} }
// Normal action // Normal action
@ -187,22 +189,22 @@ class Controller extends ViewableData {
$result = $this->$funcName($this->urlParams); $result = $this->$funcName($this->urlParams);
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 { } else {
if(isset($_GET['debug_controller'])) Debug::show("Running default action for $funcName on the $this->class controller" ); if(isset($_GET['debug_controller'])) Debug::show("Running default action for $funcName on the $this->class controller" );
if(isset($_GET['debug_profile'])) Profiler::mark("Controller::defaultAction($funcName)"); if(isset($_GET['debug_profile'])) Profiler::mark("Controller::defaultAction($funcName)");
$result = $this->defaultAction($funcName, $this->urlParams); $result = $this->defaultAction($funcName, $this->urlParams);
if(isset($_GET['debug_profile'])) Profiler::unmark("Controller::defaultAction($funcName)"); if(isset($_GET['debug_profile'])) Profiler::unmark("Controller::defaultAction($funcName)");
} }
} }
// If your controller function returns an array, then add that data to the // If your controller function returns an array, then add that data to the
// default template // default template
if(is_array($result)) { if(is_array($result)) {
$extended = $this->customise($result); $extended = $this->customise($result);
$viewer = $this->getViewer($funcName); $viewer = $this->getViewer($funcName);
$result = $viewer->process($extended); $result = $viewer->process($extended);
} }
@ -222,11 +224,11 @@ class Controller extends ViewableData {
function defaultAction($action) { function defaultAction($action) {
return $this->getViewer($action)->process($this); return $this->getViewer($action)->process($this);
} }
function getAction() { function getAction() {
return $this->action; return $this->action;
} }
/** /**
* Return an SSViewer object to process the data * Return an SSViewer object to process the data
* @return SSViewer The viewer identified being the default handler for this Controller/Action combination * @return SSViewer The viewer identified being the default handler for this Controller/Action combination
@ -247,7 +249,7 @@ class Controller extends ViewableData {
if($action && $action != "index") $templates[] = $templateName . '_' . $action; if($action && $action != "index") $templates[] = $templateName . '_' . $action;
$templates[] = $templateName; $templates[] = $templateName;
$parentClass = get_parent_class($parentClass); $parentClass = get_parent_class($parentClass);
} }
$templates = array_unique($templates); $templates = array_unique($templates);
@ -329,9 +331,9 @@ class Controller extends ViewableData {
return true; return true;
} }
} }
//----------------------------------------------------------------------------------- //-----------------------------------------------------------------------------------
/** /**
* returns a date object for use within a template * returns a date object for use within a template
* Usage: $Now.Year - Returns 2006 * Usage: $Now.Year - Returns 2006
@ -342,25 +344,25 @@ class Controller extends ViewableData {
$d->setVal(date("Y-m-d h:i:s")); $d->setVal(date("Y-m-d h:i:s"));
return $d; return $d;
} }
/** /**
* Returns a link to any other page * Returns a link to any other page
*/ */
function LinkTo($a, $b) { function LinkTo($a, $b) {
return Director::baseURL() . $a . '/' . $b; return Director::baseURL() . $a . '/' . $b;
} }
function AbsoluteLink() { function AbsoluteLink() {
return Director::absoluteURL($this->Link()); return Director::absoluteURL($this->Link());
} }
/** /**
* Returns the currently logged in user * Returns the currently logged in user
*/ */
function CurrentMember() { function CurrentMember() {
return Member::currentUser(); return Member::currentUser();
} }
/** /**
* Returns true if the visitor has been here before * Returns true if the visitor has been here before
* @return boolean * @return boolean
@ -368,7 +370,7 @@ class Controller extends ViewableData {
function PastVisitor() { function PastVisitor() {
return Cookie::get("PastVisitor") ? true : false; return Cookie::get("PastVisitor") ? true : false;
} }
/** /**
* Return true if the visitor has signed up for a login account before * Return true if the visitor has signed up for a login account before
* @return boolean * @return boolean