arse/includes/form/ctrl.inc.php
Emmanuel BENOîT 29a026e71a Improved URL rewriting support
When this code was written, it did not include an internal URL mapper
and each page was loaded by a PHP script. The internal URL was a recent,
mostly unfinished addition.

Base URL is now supported:
 * for views, when they implement the BaseURLAware interface (a base
class that does what most views will do with that is provided -
BaseURLAwareView),
 * in the menu,
 * in form actions,
 * in boxes (for buttons, and for the contents if the inner view
implements BaseURLAware).
2012-02-05 17:42:53 +01:00

79 lines
1.4 KiB
PHP

<?php
class Ctrl_Form
extends Controller
{
protected $form;
public function __construct( Form $form )
{
$this->form = $form;
}
protected function getValues( )
{
$success = true;
foreach ( $this->form->fields( ) as $field ) {
if ( $field === null ) {
continue;
}
try {
$value = $this->getParameter( $field->name( ) , $this->form->method( ) );
} catch ( ParameterException $e ) {
$value = null;
}
$field->setFormValue( $value );
$vResult = $field->validate( );
$success = $success && $vResult;
}
return $success;
}
protected function applyFormControllers( $page )
{
foreach ( $this->form->controllers( ) as $controller ) {
$result = $controller->handle( $page );
if ( $result === null ) {
continue;
}
return $result;
}
return null;
}
public function handle( Page $page )
{
try {
$this->getParameter( $this->form->name( ) . '-submit' );
} catch ( ParameterException $e ) {
return $this->form->view( );
}
if ( ! $this->getValues( ) ) {
return $this->form->view( );
}
$cResult = $this->applyFormControllers( $page );
if ( $cResult === null ) {
return $this->form->view( );
}
if ( $cResult ) {
$url = $this->form->successURL( );
} else {
$url = $this->form->cancelURL( );
}
if ( $url{0} != '/' ) {
$url = "/$url";
}
return $page->getBaseURL( ) . $url;
}
}