2012-02-05 12:56:54 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class View_Form
|
2012-02-05 17:42:53 +01:00
|
|
|
extends BaseURLAwareView
|
2012-02-05 12:56:54 +01:00
|
|
|
{
|
|
|
|
protected $form;
|
2012-02-07 09:25:10 +01:00
|
|
|
protected $fieldTypes;
|
2012-02-05 12:56:54 +01:00
|
|
|
|
|
|
|
public function __construct( Form $form )
|
|
|
|
{
|
|
|
|
$this->form = $form;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected function renderHiddenFields( $prefix )
|
|
|
|
{
|
|
|
|
$result = array( );
|
|
|
|
foreach ( $this->form->fields( ) as $field ) {
|
|
|
|
if ( $field === null || $field->type( ) !== 'hidden' ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
array_push( $result , HTML::make( 'input' )
|
|
|
|
->setAttribute( 'type' , 'hidden' )
|
|
|
|
->setAttribute( 'name' , $field->name( ) )
|
|
|
|
->setAttribute( 'value' , HTML::from( $field->value( ) ) )
|
|
|
|
->setAttribute( 'id' , $prefix . $field->name( ) ) );
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2012-02-07 09:25:10 +01:00
|
|
|
protected function renderField( $field , $prefix )
|
2012-02-05 12:56:54 +01:00
|
|
|
{
|
2012-02-07 09:25:10 +01:00
|
|
|
$type = $field->type( );
|
|
|
|
if ( array_key_exists( $type , $this->fieldTypes ) ) {
|
|
|
|
return $this->fieldTypes[ $type ]->render( $field , $prefix );
|
2012-02-05 12:56:54 +01:00
|
|
|
}
|
2012-02-07 09:25:10 +01:00
|
|
|
throw new Exception( "field " . $field->name() . " has unknown type " . $field->type() );
|
2012-02-05 12:56:54 +01:00
|
|
|
}
|
|
|
|
|
2012-02-07 09:25:10 +01:00
|
|
|
private function loadFieldTypes( )
|
2012-02-05 12:56:54 +01:00
|
|
|
{
|
2012-02-07 09:25:10 +01:00
|
|
|
$types = Loader::Find( 'FieldView' );
|
|
|
|
$loaded = array( );
|
2012-02-05 12:56:54 +01:00
|
|
|
|
2012-02-07 09:25:10 +01:00
|
|
|
foreach ( $types as $type ) {
|
|
|
|
$instance = Loader::Create( $type );
|
|
|
|
$loaded[ $instance->getFieldType( ) ] = $instance;
|
2012-02-05 12:56:54 +01:00
|
|
|
}
|
2012-02-07 09:25:10 +01:00
|
|
|
|
|
|
|
$this->fieldTypes = $loaded;
|
2012-02-05 12:56:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected function renderVisibleFields( $target , $prefix )
|
|
|
|
{
|
2012-02-07 09:25:10 +01:00
|
|
|
$this->loadFieldTypes( );
|
2012-02-05 12:56:54 +01:00
|
|
|
foreach ( $this->form->fields( ) as $field ) {
|
|
|
|
if ( $field === null ) {
|
|
|
|
$target->appendElement( HTML::make( 'hr' ) );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ( $field->type( ) === 'hidden' ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$fPrefix = $prefix . $field->name( ) . '-';
|
|
|
|
if ( $field->type( ) === 'html' ) {
|
|
|
|
$target->appendElement( HTML::make( 'dd' )
|
|
|
|
->setAttribute( 'id' , $fPrefix )
|
|
|
|
->setAttribute( 'class' , 'html-section' )
|
|
|
|
->append( $field->value( ) ) );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$fClass = 'field' . ( $field->mandatory( ) ? ' mandatory' : '' );
|
|
|
|
$target->appendElement( HTML::make( 'dt' )
|
|
|
|
->setAttribute( 'class' , $fClass )
|
|
|
|
->setAttribute( 'id' , $fPrefix . 'label' )
|
|
|
|
->appendElement( HTML::make( 'label' )
|
|
|
|
->setAttribute( 'for' , $fPrefix . 'field' )
|
|
|
|
->appendText( $field->description( ) ) ) );
|
|
|
|
|
|
|
|
$errors = $field->errors( );
|
|
|
|
if ( !empty( $errors ) ) {
|
|
|
|
foreach ( $errors as $error ) {
|
|
|
|
$target->appendElement( HTML::make( 'dd' )
|
|
|
|
->setAttribute( 'class' , 'form-error' )
|
|
|
|
->appendText( $error ) );
|
|
|
|
}
|
|
|
|
$fClass .= ' erroneous';
|
|
|
|
}
|
|
|
|
|
|
|
|
$target->appendElement( HTML::make( 'dd' )
|
|
|
|
->setAttribute( 'id' , $fPrefix . 'container' )
|
|
|
|
->setAttribute( 'class' , $fClass )
|
|
|
|
->append( $this->renderField( $field , $fPrefix ) ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function render( )
|
|
|
|
{
|
|
|
|
$name = $this->form->name();
|
|
|
|
$prefix = $name . '-';
|
|
|
|
|
2012-02-05 17:42:53 +01:00
|
|
|
$action = $this->form->action( );
|
|
|
|
if ( $action{0} != '?' ) {
|
|
|
|
if ( $action{0} != '/' ) {
|
|
|
|
$action = "/$action";
|
|
|
|
}
|
|
|
|
$action = $this->base . $action;
|
|
|
|
}
|
|
|
|
|
2012-02-05 12:56:54 +01:00
|
|
|
$form = HTML::make( 'form' )
|
|
|
|
->setAttribute( 'name' , $name )
|
|
|
|
->setAttribute( 'id' , $prefix . 'form' )
|
2012-02-05 17:42:53 +01:00
|
|
|
->setAttribute( 'action' , $action )
|
2012-02-05 12:56:54 +01:00
|
|
|
->setAttribute( 'method' , $this->form->method( ) )
|
|
|
|
->append( $this->renderHiddenFields( $prefix ) )
|
|
|
|
->append( $visibleArea = HTML::make( 'dl' ) );
|
|
|
|
|
|
|
|
$this->renderVisibleFields( $visibleArea , $prefix );
|
|
|
|
$visibleArea->appendElement( HTML::make( 'dt' )
|
|
|
|
->setAttribute( 'class' , 'submit-button' )
|
|
|
|
->setAttribute( 'id' , $prefix . 'submit-container' )
|
|
|
|
->appendElement( HTML::make( 'input' )
|
|
|
|
->setAttribute( 'type' , 'submit' )
|
|
|
|
->setAttribute( 'name' , $prefix . 'submit' )
|
|
|
|
->setAttribute( 'id' , $prefix . 'submit' )
|
|
|
|
->setAttribute( 'value' , $this->form->buttonTitle( ) ) ) );
|
|
|
|
|
|
|
|
return $form;
|
|
|
|
}
|
|
|
|
}
|